1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
|
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* vim9compile.c: :def and dealing with instructions
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
#define DEFINE_VIM9_GLOBALS
#include "vim9.h"
// values for ctx_skip
typedef enum {
SKIP_NOT, // condition is a constant, produce code
SKIP_YES, // condition is a constant, do NOT produce code
SKIP_UNKNOWN // condition is not a constant, produce code
} skip_T;
/*
* Chain of jump instructions where the end label needs to be set.
*/
typedef struct endlabel_S endlabel_T;
struct endlabel_S {
endlabel_T *el_next; // chain end_label locations
int el_end_label; // instruction idx where to set end
};
/*
* info specific for the scope of :if / elseif / else
*/
typedef struct {
int is_seen_else;
int is_had_return; // every block ends in :return
int is_if_label; // instruction idx at IF or ELSEIF
endlabel_T *is_end_label; // instructions to set end label
} ifscope_T;
/*
* info specific for the scope of :while
*/
typedef struct {
int ws_top_label; // instruction idx at WHILE
endlabel_T *ws_end_label; // instructions to set end
} whilescope_T;
/*
* info specific for the scope of :for
*/
typedef struct {
int fs_top_label; // instruction idx at FOR
endlabel_T *fs_end_label; // break instructions
} forscope_T;
/*
* info specific for the scope of :try
*/
typedef struct {
int ts_try_label; // instruction idx at TRY
endlabel_T *ts_end_label; // jump to :finally or :endtry
int ts_catch_label; // instruction idx of last CATCH
int ts_caught_all; // "catch" without argument encountered
} tryscope_T;
typedef enum {
NO_SCOPE,
IF_SCOPE,
WHILE_SCOPE,
FOR_SCOPE,
TRY_SCOPE,
BLOCK_SCOPE
} scopetype_T;
/*
* Info for one scope, pointed to by "ctx_scope".
*/
typedef struct scope_S scope_T;
struct scope_S {
scope_T *se_outer; // scope containing this one
scopetype_T se_type;
int se_local_count; // ctx_locals.ga_len before scope
skip_T se_skip_save; // ctx_skip before the block
union {
ifscope_T se_if;
whilescope_T se_while;
forscope_T se_for;
tryscope_T se_try;
} se_u;
};
/*
* Entry for "ctx_locals". Used for arguments and local variables.
*/
typedef struct {
char_u *lv_name;
type_T *lv_type;
int lv_idx; // index of the variable on the stack
int lv_from_outer; // when TRUE using ctx_outer scope
int lv_const; // when TRUE cannot be assigned to
int lv_arg; // when TRUE this is an argument
} lvar_T;
/*
* Context for compiling lines of Vim script.
* Stores info about the local variables and condition stack.
*/
struct cctx_S {
ufunc_T *ctx_ufunc; // current function
int ctx_lnum; // line number in current function
char_u *ctx_line_start; // start of current line or NULL
garray_T ctx_instr; // generated instructions
garray_T ctx_locals; // currently visible local variables
int ctx_locals_count; // total number of local variables
int ctx_closure_count; // number of closures created in the
// function
garray_T ctx_imports; // imported items
skip_T ctx_skip;
scope_T *ctx_scope; // current scope, NULL at toplevel
int ctx_had_return; // last seen statement was "return"
cctx_T *ctx_outer; // outer scope for lambda or nested
// function
int ctx_outer_used; // var in ctx_outer was used
garray_T ctx_type_stack; // type of each item on the stack
garray_T *ctx_type_list; // list of pointers to allocated types
};
static char e_var_notfound[] = N_("E1001: variable not found: %s");
static char e_syntax_at[] = N_("E1002: Syntax error at %s");
static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
static char e_cannot_use_void[] = N_("E1031: Cannot use void value");
static void delete_def_function_contents(dfunc_T *dfunc);
static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
/*
* Lookup variable "name" in the local scope and return it.
* Return NULL if not found.
*/
static lvar_T *
lookup_local(char_u *name, size_t len, cctx_T *cctx)
{
int idx;
lvar_T *lvar;
if (len == 0)
return NULL;
// Find local in current function scope.
for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
if (STRNCMP(name, lvar->lv_name, len) == 0
&& STRLEN(lvar->lv_name) == len)
{
lvar->lv_from_outer = FALSE;
return lvar;
}
}
// Find local in outer function scope.
if (cctx->ctx_outer != NULL)
{
lvar = lookup_local(name, len, cctx->ctx_outer);
if (lvar != NULL)
{
// TODO: are there situations we should not mark the outer scope as
// used?
cctx->ctx_outer_used = TRUE;
lvar->lv_from_outer = TRUE;
return lvar;
}
}
return NULL;
}
/*
* Lookup an argument in the current function and an enclosing function.
* Returns the argument index in "idxp"
* Returns the argument type in "type"
* Sets "gen_load_outer" to TRUE if found in outer scope.
* Returns OK when found, FAIL otherwise.
*/
static int
lookup_arg(
char_u *name,
size_t len,
int *idxp,
type_T **type,
int *gen_load_outer,
cctx_T *cctx)
{
int idx;
char_u *va_name;
if (len == 0)
return FAIL;
for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx)
{
char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
{
if (idxp != NULL)
{
// Arguments are located above the frame pointer. One further
// if there is a vararg argument
*idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
+ STACK_FRAME_SIZE)
+ (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
if (cctx->ctx_ufunc->uf_arg_types != NULL)
*type = cctx->ctx_ufunc->uf_arg_types[idx];
else
*type = &t_any;
}
return OK;
}
}
va_name = cctx->ctx_ufunc->uf_va_name;
if (va_name != NULL
&& STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
{
if (idxp != NULL)
{
// varargs is always the last argument
*idxp = -STACK_FRAME_SIZE - 1;
*type = cctx->ctx_ufunc->uf_va_type;
}
return OK;
}
if (cctx->ctx_outer != NULL)
{
// Lookup the name for an argument of the outer function.
if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
== OK)
{
*gen_load_outer = TRUE;
return OK;
}
}
return FAIL;
}
/*
* Lookup a variable in the current script.
* Returns OK or FAIL.
*/
static int
lookup_script(char_u *name, size_t len)
{
int cc;
hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
dictitem_T *di;
cc = name[len];
name[len] = NUL;
di = find_var_in_ht(ht, 0, name, TRUE);
name[len] = cc;
return di == NULL ? FAIL: OK;
}
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx".
* Return FAIL and give an error if it defined.
*/
int
check_defined(char_u *p, size_t len, cctx_T *cctx)
{
if (lookup_script(p, len) == OK
|| (cctx != NULL
&& (lookup_local(p, len, cctx) != NULL
|| find_imported(p, len, cctx) != NULL)))
{
semsg("E1073: imported name already defined: %s", p);
return FAIL;
}
return OK;
}
/*
* Allocate memory for a type_T and add the pointer to type_gap, so that it can
* be freed later.
*/
static type_T *
alloc_type(garray_T *type_gap)
{
type_T *type;
if (ga_grow(type_gap, 1) == FAIL)
return NULL;
type = ALLOC_CLEAR_ONE(type_T);
if (type != NULL)
{
((type_T **)type_gap->ga_data)[type_gap->ga_len] = type;
++type_gap->ga_len;
}
return type;
}
void
clear_type_list(garray_T *gap)
{
while (gap->ga_len > 0)
vim_free(((type_T **)gap->ga_data)[--gap->ga_len]);
ga_clear(gap);
}
static type_T *
get_list_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_ANY)
return &t_list_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_list_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_list_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_list_number;
if (member_type->tt_type == VAR_STRING)
return &t_list_string;
// Not a common type, create a new entry.
type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_LIST;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
static type_T *
get_dict_type(type_T *member_type, garray_T *type_gap)
{
type_T *type;
// recognize commonly used types
if (member_type->tt_type == VAR_ANY)
return &t_dict_any;
if (member_type->tt_type == VAR_VOID
|| member_type->tt_type == VAR_UNKNOWN)
return &t_dict_empty;
if (member_type->tt_type == VAR_BOOL)
return &t_dict_bool;
if (member_type->tt_type == VAR_NUMBER)
return &t_dict_number;
if (member_type->tt_type == VAR_STRING)
return &t_dict_string;
// Not a common type, create a new entry.
type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_DICT;
type->tt_member = member_type;
type->tt_argcount = 0;
type->tt_args = NULL;
return type;
}
/*
* Allocate a new type for a function.
*/
static type_T *
alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
type_T *type = alloc_type(type_gap);
if (type == NULL)
return &t_any;
type->tt_type = VAR_FUNC;
type->tt_member = ret_type;
type->tt_argcount = argcount;
type->tt_args = NULL;
return type;
}
/*
* Get a function type, based on the return type "ret_type".
* If "argcount" is -1 or 0 a predefined type can be used.
* If "argcount" > 0 always create a new type, so that arguments can be added.
*/
static type_T *
get_func_type(type_T *ret_type, int argcount, garray_T *type_gap)
{
// recognize commonly used types
if (argcount <= 0)
{
if (ret_type == &t_unknown)
{
// (argcount == 0) is not possible
return &t_func_unknown;
}
if (ret_type == &t_void)
{
if (argcount == 0)
return &t_func_0_void;
else
return &t_func_void;
}
if (ret_type == &t_any)
{
if (argcount == 0)
return &t_func_0_any;
else
return &t_func_any;
}
if (ret_type == &t_number)
{
if (argcount == 0)
return &t_func_0_number;
else
return &t_func_number;
}
if (ret_type == &t_string)
{
if (argcount == 0)
return &t_func_0_string;
else
return &t_func_string;
}
}
return alloc_func_type(ret_type, argcount, type_gap);
}
/*
* For a function type, reserve space for "argcount" argument types (including
* vararg).
*/
static int
func_type_add_arg_types(
type_T *functype,
int argcount,
garray_T *type_gap)
{
// To make it easy to free the space needed for the argument types, add the
// pointer to type_gap.
if (ga_grow(type_gap, 1) == FAIL)
return FAIL;
functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount);
if (functype->tt_args == NULL)
return FAIL;
((type_T **)type_gap->ga_data)[type_gap->ga_len] =
(void *)functype->tt_args;
++type_gap->ga_len;
return OK;
}
/*
* Return the type_T for a typval. Only for primitive types.
*/
type_T *
typval2type(typval_T *tv)
{
if (tv->v_type == VAR_NUMBER)
return &t_number;
if (tv->v_type == VAR_BOOL)
return &t_bool; // not used
if (tv->v_type == VAR_STRING)
return &t_string;
if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
return &t_list_string;
if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
return &t_dict_any;
return &t_any; // not used
}
static void
type_mismatch(type_T *expected, type_T *actual)
{
char *tofree1, *tofree2;
semsg(_("E1013: type mismatch, expected %s but got %s"),
type_name(expected, &tofree1), type_name(actual, &tofree2));
vim_free(tofree1);
vim_free(tofree2);
}
static void
arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
{
char *tofree1, *tofree2;
semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"),
argidx,
type_name(expected, &tofree1), type_name(actual, &tofree2));
vim_free(tofree1);
vim_free(tofree2);
}
/*
* Check if the expected and actual types match.
* Does not allow for assigning "any" to a specific type.
*/
int
check_type(type_T *expected, type_T *actual, int give_msg)
{
int ret = OK;
// When expected is "unknown" we accept any actual type.
// When expected is "any" we accept any actual type except "void".
if (expected->tt_type != VAR_UNKNOWN
&& !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID))
{
if (expected->tt_type != actual->tt_type)
{
if (give_msg)
type_mismatch(expected, actual);
return FAIL;
}
if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST)
{
// "unknown" is used for an empty list or dict
if (actual->tt_member != &t_unknown)
ret = check_type(expected->tt_member, actual->tt_member, FALSE);
}
else if (expected->tt_type == VAR_FUNC)
{
if (expected->tt_member != &t_unknown)
ret = check_type(expected->tt_member, actual->tt_member, FALSE);
if (ret == OK && expected->tt_argcount != -1
&& (actual->tt_argcount < expected->tt_min_argcount
|| actual->tt_argcount > expected->tt_argcount))
ret = FAIL;
}
if (ret == FAIL && give_msg)
type_mismatch(expected, actual);
}
return ret;
}
/////////////////////////////////////////////////////////////////////
// Following generate_ functions expect the caller to call ga_grow().
#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
/*
* Generate an instruction without arguments.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr(cctx_T *cctx, isntype_T isn_type)
{
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
RETURN_NULL_IF_SKIP(cctx);
if (ga_grow(instr, 1) == FAIL)
return NULL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len;
isn->isn_type = isn_type;
isn->isn_lnum = cctx->ctx_lnum + 1;
++instr->ga_len;
return isn;
}
/*
* Generate an instruction without arguments.
* "drop" will be removed from the stack.
* Returns a pointer to the new instruction, NULL if failed.
*/
static isn_T *
generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
{
garray_T *stack = &cctx->ctx_type_stack;
RETURN_NULL_IF_SKIP(cctx);
stack->ga_len -= drop;
return generate_instr(cctx, isn_type);
}
/*
* Generate instruction "isn_type" and put "type" on the type stack.
*/
static isn_T *
generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return NULL;
if (ga_grow(stack, 1) == FAIL)
return NULL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return isn;
}
/*
* If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
*/
static int
may_generate_2STRING(int offset, cctx_T *cctx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
if ((*type)->tt_type == VAR_STRING)
return OK;
*type = &t_string;
if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
return FAIL;
isn->isn_arg.number = offset;
return OK;
}
static int
check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
{
if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY)
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT
|| type2 == VAR_ANY)))
{
if (*op == '+')
emsg(_("E1051: wrong argument type for +"));
else
semsg(_("E1036: %c requires number or float arguments"), *op);
return FAIL;
}
return OK;
}
/*
* Generate an instruction with two arguments. The instruction depends on the
* type of the arguments.
*/
static int
generate_two_op(cctx_T *cctx, char_u *op)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type1;
type_T *type2;
vartype_T vartype;
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack. If they are matching
// use a type-specific instruction. Otherwise fall back to runtime type
// checking.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2];
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
vartype = VAR_ANY;
if (type1->tt_type == type2->tt_type
&& (type1->tt_type == VAR_NUMBER
|| type1->tt_type == VAR_LIST
#ifdef FEAT_FLOAT
|| type1->tt_type == VAR_FLOAT
#endif
|| type1->tt_type == VAR_BLOB))
vartype = type1->tt_type;
switch (*op)
{
case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB
&& type1->tt_type != VAR_ANY
&& type2->tt_type != VAR_ANY
&& check_number_or_float(
type1->tt_type, type2->tt_type, op) == FAIL)
return FAIL;
isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR
: vartype == VAR_LIST ? ISN_ADDLIST
: vartype == VAR_BLOB ? ISN_ADDBLOB
#ifdef FEAT_FLOAT
: vartype == VAR_FLOAT ? ISN_OPFLOAT
#endif
: ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = EXPR_ADD;
break;
case '-':
case '*':
case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
op) == FAIL)
return FAIL;
if (vartype == VAR_NUMBER)
isn = generate_instr_drop(cctx, ISN_OPNR, 1);
#ifdef FEAT_FLOAT
else if (vartype == VAR_FLOAT)
isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
#endif
else
isn = generate_instr_drop(cctx, ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = *op == '*'
? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
break;
case '%': if ((type1->tt_type != VAR_ANY
&& type1->tt_type != VAR_NUMBER)
|| (type2->tt_type != VAR_ANY
&& type2->tt_type != VAR_NUMBER))
{
emsg(_("E1035: % requires number arguments"));
return FAIL;
}
isn = generate_instr_drop(cctx,
vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
if (isn != NULL)
isn->isn_arg.op.op_type = EXPR_REM;
break;
}
// correct type of result
if (vartype == VAR_ANY)
{
type_T *type = &t_any;
#ifdef FEAT_FLOAT
// float+number and number+float results in float
if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
&& (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
type = &t_float;
#endif
((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
}
return OK;
}
/*
* Get the instruction to use for comparing "type1" with "type2"
* Return ISN_DROP when failed.
*/
static isntype_T
get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2)
{
isntype_T isntype = ISN_DROP;
if (type1 == VAR_UNKNOWN)
type1 = VAR_ANY;
if (type2 == VAR_UNKNOWN)
type2 = VAR_ANY;
if (type1 == type2)
{
switch (type1)
{
case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
case VAR_NUMBER: isntype = ISN_COMPARENR; break;
case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
case VAR_STRING: isntype = ISN_COMPARESTRING; break;
case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
case VAR_LIST: isntype = ISN_COMPARELIST; break;
case VAR_DICT: isntype = ISN_COMPAREDICT; break;
case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
default: isntype = ISN_COMPAREANY; break;
}
}
else if (type1 == VAR_ANY || type2 == VAR_ANY
|| ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
&& (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
isntype = ISN_COMPAREANY;
if ((exptype == EXPR_IS || exptype == EXPR_ISNOT)
&& (isntype == ISN_COMPAREBOOL
|| isntype == ISN_COMPARESPECIAL
|| isntype == ISN_COMPARENR
|| isntype == ISN_COMPAREFLOAT))
{
semsg(_("E1037: Cannot use \"%s\" with %s"),
exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
return ISN_DROP;
}
if (isntype == ISN_DROP
|| ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
&& (type1 == VAR_BOOL || type1 == VAR_SPECIAL
|| type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
|| ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL
&& exptype != EXPR_IS && exptype != EXPR_ISNOT
&& (type1 == VAR_BLOB || type2 == VAR_BLOB
|| type1 == VAR_LIST || type2 == VAR_LIST))))
{
semsg(_("E1072: Cannot compare %s with %s"),
vartype_name(type1), vartype_name(type2));
return ISN_DROP;
}
return isntype;
}
int
check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
{
if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
return FAIL;
return OK;
}
/*
* Generate an ISN_COMPARE* instruction with a boolean result.
*/
static int
generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic)
{
isntype_T isntype;
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
vartype_T type1;
vartype_T type2;
RETURN_OK_IF_SKIP(cctx);
// Get the known type of the two items on the stack. If they are matching
// use a type-specific instruction. Otherwise fall back to runtime type
// checking.
type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type;
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type;
isntype = get_compare_isn(exptype, type1, type2);
if (isntype == ISN_DROP)
return FAIL;
if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.op.op_type = exptype;
isn->isn_arg.op.op_ic = ic;
// takes two arguments, puts one bool back
if (stack->ga_len >= 2)
{
--stack->ga_len;
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
}
return OK;
}
/*
* Generate an ISN_2BOOL instruction.
*/
static int
generate_2BOOL(cctx_T *cctx, int invert)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
return FAIL;
isn->isn_arg.number = invert;
// type becomes bool
((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool;
return OK;
}
static int
generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
return FAIL;
// TODO: whole type, e.g. for a function also arg and return types
isn->isn_arg.type.ct_type = vartype->tt_type;
isn->isn_arg.type.ct_off = offset;
// type becomes vartype
((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype;
return OK;
}
/*
* Check that
* - "actual" is "expected" type or
* - "actual" is a type that can be "expected" type: add a runtime check; or
* - return FAIL.
*/
static int
need_type(
type_T *actual,
type_T *expected,
int offset,
cctx_T *cctx,
int silent)
{
if (check_type(expected, actual, FALSE) == OK)
return OK;
if (actual->tt_type != VAR_ANY
&& actual->tt_type != VAR_UNKNOWN
&& !(actual->tt_type == VAR_FUNC
&& (actual->tt_member == &t_any || actual->tt_argcount < 0)))
{
if (!silent)
type_mismatch(expected, actual);
return FAIL;
}
generate_TYPECHECK(cctx, expected, offset);
return OK;
}
/*
* Generate an ISN_PUSHNR instruction.
*/
static int
generate_PUSHNR(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHBOOL instruction.
*/
static int
generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
/*
* Generate an ISN_PUSHSPEC instruction.
*/
static int
generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
return FAIL;
isn->isn_arg.number = number;
return OK;
}
#ifdef FEAT_FLOAT
/*
* Generate an ISN_PUSHF instruction.
*/
static int
generate_PUSHF(cctx_T *cctx, float_T fnumber)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
return FAIL;
isn->isn_arg.fnumber = fnumber;
return OK;
}
#endif
/*
* Generate an ISN_PUSHS instruction.
* Consumes "str".
*/
static int
generate_PUSHS(cctx_T *cctx, char_u *str)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
return FAIL;
isn->isn_arg.string = str;
return OK;
}
/*
* Generate an ISN_PUSHCHANNEL instruction.
* Consumes "channel".
*/
static int
generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.channel = channel;
return OK;
}
/*
* Generate an ISN_PUSHJOB instruction.
* Consumes "job".
*/
static int
generate_PUSHJOB(cctx_T *cctx, job_T *job)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
return FAIL;
isn->isn_arg.job = job;
return OK;
}
/*
* Generate an ISN_PUSHBLOB instruction.
* Consumes "blob".
*/
static int
generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
return FAIL;
isn->isn_arg.blob = blob;
return OK;
}
/*
* Generate an ISN_PUSHFUNC instruction with name "name".
* Consumes "name".
*/
static int
generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
return FAIL;
isn->isn_arg.string = name;
return OK;
}
/*
* Generate an ISN_GETITEM instruction with "index".
*/
static int
generate_GETITEM(cctx_T *cctx, int index)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
type_T *item_type = &t_any;
RETURN_OK_IF_SKIP(cctx);
if (type->tt_type == VAR_LIST)
item_type = type->tt_member;
else if (type->tt_type != VAR_ANY)
{
emsg(_(e_listreq));
return FAIL;
}
if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
return FAIL;
isn->isn_arg.number = index;
// add the item type to the type stack
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = item_type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_SLICE instruction with "count".
*/
static int
generate_SLICE(cctx_T *cctx, int count)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Generate an ISN_CHECKLEN instruction with "min_len".
*/
static int
generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
return FAIL;
isn->isn_arg.checklen.cl_min_len = min_len;
isn->isn_arg.checklen.cl_more_OK = more_OK;
return OK;
}
/*
* Generate an ISN_STORE instruction.
*/
static int
generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
*/
static int
generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
return FAIL;
isn->isn_arg.storenr.stnr_idx = idx;
isn->isn_arg.storenr.stnr_val = value;
return OK;
}
/*
* Generate an ISN_STOREOPT instruction
*/
static int
generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL)
return FAIL;
isn->isn_arg.storeopt.so_name = vim_strsave(name);
isn->isn_arg.storeopt.so_flags = opt_flags;
return OK;
}
/*
* Generate an ISN_LOAD or similar instruction.
*/
static int
generate_LOAD(
cctx_T *cctx,
isntype_T isn_type,
int idx,
char_u *name,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL)
return FAIL;
if (name != NULL)
isn->isn_arg.string = vim_strsave(name);
else
isn->isn_arg.number = idx;
return OK;
}
/*
* Generate an ISN_LOADV instruction for v:var.
*/
static int
generate_LOADV(
cctx_T *cctx,
char_u *name,
int error)
{
int di_flags;
int vidx = find_vim_var(name, &di_flags);
type_T *type;
RETURN_OK_IF_SKIP(cctx);
if (vidx < 0)
{
if (error)
semsg(_(e_var_notfound), name);
return FAIL;
}
type = typval2type(get_vim_var_tv(vidx));
return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
}
/*
* Generate an ISN_UNLET instruction.
*/
static int
generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, isn_type)) == NULL)
return FAIL;
isn->isn_arg.unlet.ul_name = vim_strsave(name);
isn->isn_arg.unlet.ul_forceit = forceit;
return OK;
}
/*
* Generate an ISN_LOADS instruction.
*/
static int
generate_OLDSCRIPT(
cctx_T *cctx,
isntype_T isn_type,
char_u *name,
int sid,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADS)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.loadstore.ls_name = vim_strsave(name);
isn->isn_arg.loadstore.ls_sid = sid;
return OK;
}
/*
* Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
*/
static int
generate_VIM9SCRIPT(
cctx_T *cctx,
isntype_T isn_type,
int sid,
int idx,
type_T *type)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if (isn_type == ISN_LOADSCRIPT)
isn = generate_instr_type(cctx, isn_type, type);
else
isn = generate_instr_drop(cctx, isn_type, 1);
if (isn == NULL)
return FAIL;
isn->isn_arg.script.script_sid = sid;
isn->isn_arg.script.script_idx = idx;
return OK;
}
/*
* Generate an ISN_NEWLIST instruction.
*/
static int
generate_NEWLIST(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// drop the value types
stack->ga_len -= count;
// Use the first value type for the list member type. Use "any" for an
// empty list.
if (count > 0)
member = ((type_T **)stack->ga_data)[stack->ga_len];
else
member = &t_void;
type = get_list_type(member, cctx->ctx_type_list);
// add the list type to the type stack
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_NEWDICT instruction.
*/
static int
generate_NEWDICT(cctx_T *cctx, int count)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type_T *member;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
return FAIL;
isn->isn_arg.number = count;
// drop the key and value types
stack->ga_len -= 2 * count;
// Use the first value type for the list member type. Use "void" for an
// empty dict.
if (count > 0)
member = ((type_T **)stack->ga_data)[stack->ga_len + 1];
else
member = &t_void;
type = get_dict_type(member, cctx->ctx_type_list);
// add the dict type to the type stack
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_FUNCREF instruction.
*/
static int
generate_FUNCREF(cctx_T *cctx, int dfunc_idx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
return FAIL;
isn->isn_arg.funcref.fr_func = dfunc_idx;
isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++;
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any;
// TODO: argument and return types
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_JUMP instruction.
*/
static int
generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
return FAIL;
isn->isn_arg.jump.jump_when = when;
isn->isn_arg.jump.jump_where = where;
if (when != JUMP_ALWAYS && stack->ga_len > 0)
--stack->ga_len;
return OK;
}
static int
generate_FOR(cctx_T *cctx, int loop_idx)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
return FAIL;
isn->isn_arg.forloop.for_idx = loop_idx;
if (ga_grow(stack, 1) == FAIL)
return FAIL;
// type doesn't matter, will be stored next
((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_BCALL instruction.
* "method_call" is TRUE for "value->method()"
* Return FAIL if the number of arguments is wrong.
*/
static int
generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
int argoff;
type_T *argtypes[MAX_FUNC_ARGS];
int i;
RETURN_OK_IF_SKIP(cctx);
argoff = check_internal_func(func_idx, argcount);
if (argoff < 0)
return FAIL;
if (method_call && argoff > 1)
{
if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
return FAIL;
isn->isn_arg.shuffle.shfl_item = argcount;
isn->isn_arg.shuffle.shfl_up = argoff - 1;
}
if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
return FAIL;
isn->isn_arg.bfunc.cbf_idx = func_idx;
isn->isn_arg.bfunc.cbf_argcount = argcount;
for (i = 0; i < argcount; ++i)
argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
stack->ga_len -= argcount; // drop the arguments
if (ga_grow(stack, 1) == FAIL)
return FAIL;
((type_T **)stack->ga_data)[stack->ga_len] =
internal_func_ret_type(func_idx, argcount, argtypes);
++stack->ga_len; // add return value
return OK;
}
/*
* Generate an ISN_DCALL or ISN_UCALL instruction.
* Return FAIL if the number of arguments is wrong.
*/
static int
generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
int regular_args = ufunc->uf_args.ga_len;
int argcount = pushed_argcount;
RETURN_OK_IF_SKIP(cctx);
if (argcount > regular_args && !has_varargs(ufunc))
{
semsg(_(e_toomanyarg), ufunc->uf_name);
return FAIL;
}
if (argcount < regular_args - ufunc->uf_def_args.ga_len)
{
semsg(_(e_toofewarg), ufunc->uf_name);
return FAIL;
}
if (ufunc->uf_def_status != UF_NOT_COMPILED)
{
int i;
for (i = 0; i < argcount; ++i)
{
type_T *expected;
type_T *actual;
if (i < regular_args)
{
if (ufunc->uf_arg_types == NULL)
continue;
expected = ufunc->uf_arg_types[i];
}
else
expected = ufunc->uf_va_type->tt_member;
actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i];
if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL)
{
arg_type_mismatch(expected, actual, i + 1);
return FAIL;
}
}
if (ufunc->uf_def_status == UF_TO_BE_COMPILED)
if (compile_def_function(ufunc, TRUE, NULL) == FAIL)
return FAIL;
}
if ((isn = generate_instr(cctx,
ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
: ISN_UCALL)) == NULL)
return FAIL;
if (ufunc->uf_def_status != UF_NOT_COMPILED)
{
isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
isn->isn_arg.dfunc.cdf_argcount = argcount;
}
else
{
// A user function may be deleted and redefined later, can't use the
// ufunc pointer, need to look it up again at runtime.
isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
isn->isn_arg.ufunc.cuf_argcount = argcount;
}
stack->ga_len -= argcount; // drop the arguments
if (ga_grow(stack, 1) == FAIL)
return FAIL;
// add return value
((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_UCALL instruction when the function isn't defined yet.
*/
static int
generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
return FAIL;
isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
isn->isn_arg.ufunc.cuf_argcount = argcount;
stack->ga_len -= argcount; // drop the arguments
if (ga_grow(stack, 1) == FAIL)
return FAIL;
// add return value
((type_T **)stack->ga_data)[stack->ga_len] = &t_any;
++stack->ga_len;
return OK;
}
/*
* Generate an ISN_PCALL instruction.
* "type" is the type of the FuncRef.
*/
static int
generate_PCALL(
cctx_T *cctx,
int argcount,
char_u *name,
type_T *type,
int at_top)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *ret_type;
RETURN_OK_IF_SKIP(cctx);
if (type->tt_type == VAR_ANY)
ret_type = &t_any;
else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
{
if (type->tt_argcount != -1)
{
int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
if (argcount < type->tt_min_argcount - varargs)
{
semsg(_(e_toofewarg), "[reference]");
return FAIL;
}
if (!varargs && argcount > type->tt_argcount)
{
semsg(_(e_toomanyarg), "[reference]");
return FAIL;
}
}
ret_type = type->tt_member;
}
else
{
semsg(_("E1085: Not a callable type: %s"), name);
return FAIL;
}
if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
return FAIL;
isn->isn_arg.pfunc.cpf_top = at_top;
isn->isn_arg.pfunc.cpf_argcount = argcount;
stack->ga_len -= argcount; // drop the arguments
// drop the funcref/partial, get back the return value
((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type;
// If partial is above the arguments it must be cleared and replaced with
// the return value.
if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
return FAIL;
return OK;
}
/*
* Generate an ISN_STRINGMEMBER instruction.
*/
static int
generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
{
isn_T *isn;
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strnsave(name, len);
// check for dict type
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (type->tt_type != VAR_DICT && type != &t_any)
{
emsg(_(e_dictreq));
return FAIL;
}
// change dict type to dict member type
if (type->tt_type == VAR_DICT)
((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member;
return OK;
}
/*
* Generate an ISN_ECHO instruction.
*/
static int
generate_ECHO(cctx_T *cctx, int with_white, int count)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
return FAIL;
isn->isn_arg.echo.echo_with_white = with_white;
isn->isn_arg.echo.echo_count = count;
return OK;
}
/*
* Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
*/
static int
generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
{
isn_T *isn;
if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
static int
generate_EXEC(cctx_T *cctx, char_u *line)
{
isn_T *isn;
RETURN_OK_IF_SKIP(cctx);
if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL)
return FAIL;
isn->isn_arg.string = vim_strsave(line);
return OK;
}
static int
generate_EXECCONCAT(cctx_T *cctx, int count)
{
isn_T *isn;
if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
return FAIL;
isn->isn_arg.number = count;
return OK;
}
/*
* Reserve space for a local variable.
* Return the variable or NULL if it failed.
*/
static lvar_T *
reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type)
{
lvar_T *lvar;
if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK)
{
emsg_namelen(_(e_used_as_arg), name, (int)len);
return NULL;
}
if (ga_grow(&cctx->ctx_locals, 1) == FAIL)
return NULL;
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
// Every local variable uses the next entry on the stack. We could re-use
// the last ones when leaving a scope, but then variables used in a closure
// might get overwritten. To keep things simple do not re-use stack
// entries. This is less efficient, but memory is cheap these days.
lvar->lv_idx = cctx->ctx_locals_count++;
lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
lvar->lv_const = isConst;
lvar->lv_type = type;
return lvar;
}
/*
* Remove local variables above "new_top".
*/
static void
unwind_locals(cctx_T *cctx, int new_top)
{
if (cctx->ctx_locals.ga_len > new_top)
{
int idx;
lvar_T *lvar;
for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx)
{
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
vim_free(lvar->lv_name);
}
}
cctx->ctx_locals.ga_len = new_top;
}
/*
* Free all local variables.
*/
static void
free_locals(cctx_T *cctx)
{
unwind_locals(cctx, 0);
ga_clear(&cctx->ctx_locals);
}
/*
* Skip over a type definition and return a pointer to just after it.
*/
char_u *
skip_type(char_u *start)
{
char_u *p = start;
while (ASCII_ISALNUM(*p) || *p == '_')
++p;
// Skip over "<type>"; this is permissive about white space.
if (*skipwhite(p) == '<')
{
p = skipwhite(p);
p = skip_type(skipwhite(p + 1));
p = skipwhite(p);
if (*p == '>')
++p;
}
else if (*p == '(' && STRNCMP("func", start, 4) == 0)
{
// handle func(args): type
++p;
while (*p != ')' && *p != NUL)
{
char_u *sp = p;
p = skip_type(p);
if (p == sp)
return p; // syntax error
if (*p == ',')
p = skipwhite(p + 1);
}
if (*p == ')')
{
if (p[1] == ':')
p = skip_type(skipwhite(p + 2));
else
p = skipwhite(p + 1);
}
}
return p;
}
/*
* Parse the member type: "<type>" and return "type" with the member set.
* Use "type_gap" if a new type needs to be added.
* Returns NULL in case of failure.
*/
static type_T *
parse_type_member(char_u **arg, type_T *type, garray_T *type_gap)
{
type_T *member_type;
int prev_called_emsg = called_emsg;
if (**arg != '<')
{
if (*skipwhite(*arg) == '<')
semsg(_(e_no_white_before), "<");
else
emsg(_("E1008: Missing <type>"));
return type;
}
*arg = skipwhite(*arg + 1);
member_type = parse_type(arg, type_gap);
*arg = skipwhite(*arg);
if (**arg != '>' && called_emsg == prev_called_emsg)
{
emsg(_("E1009: Missing > after type"));
return type;
}
++*arg;
if (type->tt_type == VAR_LIST)
return get_list_type(member_type, type_gap);
return get_dict_type(member_type, type_gap);
}
/*
* Parse a type at "arg" and advance over it.
* Return &t_any for failure.
*/
type_T *
parse_type(char_u **arg, garray_T *type_gap)
{
char_u *p = *arg;
size_t len;
// skip over the first word
while (ASCII_ISALNUM(*p) || *p == '_')
++p;
len = p - *arg;
switch (**arg)
{
case 'a':
if (len == 3 && STRNCMP(*arg, "any", len) == 0)
{
*arg += len;
return &t_any;
}
break;
case 'b':
if (len == 4 && STRNCMP(*arg, "bool", len) == 0)
{
*arg += len;
return &t_bool;
}
if (len == 4 && STRNCMP(*arg, "blob", len) == 0)
{
*arg += len;
return &t_blob;
}
break;
case 'c':
if (len == 7 && STRNCMP(*arg, "channel", len) == 0)
{
*arg += len;
return &t_channel;
}
break;
case 'd':
if (len == 4 && STRNCMP(*arg, "dict", len) == 0)
{
*arg += len;
return parse_type_member(arg, &t_dict_any, type_gap);
}
break;
case 'f':
if (len == 5 && STRNCMP(*arg, "float", len) == 0)
{
#ifdef FEAT_FLOAT
*arg += len;
return &t_float;
#else
emsg(_("E1076: This Vim is not compiled with float support"));
return &t_any;
#endif
}
if (len == 4 && STRNCMP(*arg, "func", len) == 0)
{
type_T *type;
type_T *ret_type = &t_unknown;
int argcount = -1;
int flags = 0;
int first_optional = -1;
type_T *arg_type[MAX_FUNC_ARGS + 1];
// func({type}, ...{type}): {type}
*arg += len;
if (**arg == '(')
{
// "func" may or may not return a value, "func()" does
// not return a value.
ret_type = &t_void;
p = ++*arg;
argcount = 0;
while (*p != NUL && *p != ')')
{
if (*p == '?')
{
if (first_optional == -1)
first_optional = argcount;
++p;
}
else if (first_optional != -1)
{
emsg(_("E1007: mandatory argument after optional argument"));
return &t_any;
}
else if (STRNCMP(p, "...", 3) == 0)
{
flags |= TTFLAG_VARARGS;
p += 3;
}
arg_type[argcount++] = parse_type(&p, type_gap);
// Nothing comes after "...{type}".
if (flags & TTFLAG_VARARGS)
break;
if (*p != ',' && *skipwhite(p) == ',')
{
semsg(_(e_no_white_before), ",");
return &t_any;
}
if (*p == ',')
{
++p;
if (!VIM_ISWHITE(*p))
{
semsg(_(e_white_after), ",");
return &t_any;
}
}
p = skipwhite(p);
if (argcount == MAX_FUNC_ARGS)
{
emsg(_("E740: Too many argument types"));
return &t_any;
}
}
p = skipwhite(p);
if (*p != ')')
{
emsg(_(e_missing_close));
return &t_any;
}
*arg = p + 1;
}
if (**arg == ':')
{
// parse return type
++*arg;
if (!VIM_ISWHITE(**arg))
semsg(_(e_white_after), ":");
*arg = skipwhite(*arg);
ret_type = parse_type(arg, type_gap);
}
if (flags == 0 && first_optional == -1 && argcount <= 0)
type = get_func_type(ret_type, argcount, type_gap);
else
{
type = alloc_func_type(ret_type, argcount, type_gap);
type->tt_flags = flags;
if (argcount > 0)
{
type->tt_argcount = argcount;
type->tt_min_argcount = first_optional == -1
? argcount : first_optional;
if (func_type_add_arg_types(type, argcount,
type_gap) == FAIL)
return &t_any;
mch_memmove(type->tt_args, arg_type,
sizeof(type_T *) * argcount);
}
}
return type;
}
break;
case 'j':
if (len == 3 && STRNCMP(*arg, "job", len) == 0)
{
*arg += len;
return &t_job;
}
break;
case 'l':
if (len == 4 && STRNCMP(*arg, "list", len) == 0)
{
*arg += len;
return parse_type_member(arg, &t_list_any, type_gap);
}
break;
case 'n':
if (len == 6 && STRNCMP(*arg, "number", len) == 0)
{
*arg += len;
return &t_number;
}
break;
case 's':
if (len == 6 && STRNCMP(*arg, "string", len) == 0)
{
*arg += len;
return &t_string;
}
break;
case 'v':
if (len == 4 && STRNCMP(*arg, "void", len) == 0)
{
*arg += len;
return &t_void;
}
break;
}
semsg(_("E1010: Type not recognized: %s"), *arg);
return &t_any;
}
/*
* Check if "type1" and "type2" are exactly the same.
*/
static int
equal_type(type_T *type1, type_T *type2)
{
int i;
if (type1->tt_type != type2->tt_type)
return FALSE;
switch (type1->tt_type)
{
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_VOID:
case VAR_SPECIAL:
case VAR_BOOL:
case VAR_NUMBER:
case VAR_FLOAT:
case VAR_STRING:
case VAR_BLOB:
case VAR_JOB:
case VAR_CHANNEL:
break; // not composite is always OK
case VAR_LIST:
case VAR_DICT:
return equal_type(type1->tt_member, type2->tt_member);
case VAR_FUNC:
case VAR_PARTIAL:
if (!equal_type(type1->tt_member, type2->tt_member)
|| type1->tt_argcount != type2->tt_argcount)
return FALSE;
if (type1->tt_argcount < 0
|| type1->tt_args == NULL || type2->tt_args == NULL)
return TRUE;
for (i = 0; i < type1->tt_argcount; ++i)
if (!equal_type(type1->tt_args[i], type2->tt_args[i]))
return FALSE;
return TRUE;
}
return TRUE;
}
/*
* Find the common type of "type1" and "type2" and put it in "dest".
* "type2" and "dest" may be the same.
*/
static void
common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap)
{
if (equal_type(type1, type2))
{
*dest = type1;
return;
}
if (type1->tt_type == type2->tt_type)
{
if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
{
type_T *common;
common_type(type1->tt_member, type2->tt_member, &common, type_gap);
if (type1->tt_type == VAR_LIST)
*dest = get_list_type(common, type_gap);
else
*dest = get_dict_type(common, type_gap);
return;
}
if (type1->tt_type == VAR_FUNC)
{
type_T *common;
common_type(type1->tt_member, type2->tt_member, &common, type_gap);
if (type1->tt_argcount == type2->tt_argcount
&& type1->tt_argcount >= 0)
{
int argcount = type1->tt_argcount;
int i;
*dest = alloc_func_type(common, argcount, type_gap);
if (type1->tt_args != NULL && type2->tt_args != NULL)
{
if (func_type_add_arg_types(*dest, argcount,
type_gap) == OK)
for (i = 0; i < argcount; ++i)
common_type(type1->tt_args[i], type2->tt_args[i],
&(*dest)->tt_args[i], type_gap);
}
}
else
*dest = alloc_func_type(common, -1, type_gap);
return;
}
}
*dest = &t_any;
}
char *
vartype_name(vartype_T type)
{
switch (type)
{
case VAR_UNKNOWN: break;
case VAR_ANY: return "any";
case VAR_VOID: return "void";
case VAR_SPECIAL: return "special";
case VAR_BOOL: return "bool";
case VAR_NUMBER: return "number";
case VAR_FLOAT: return "float";
case VAR_STRING: return "string";
case VAR_BLOB: return "blob";
case VAR_JOB: return "job";
case VAR_CHANNEL: return "channel";
case VAR_LIST: return "list";
case VAR_DICT: return "dict";
case VAR_FUNC:
case VAR_PARTIAL: return "func";
}
return "unknown";
}
/*
* Return the name of a type.
* The result may be in allocated memory, in which case "tofree" is set.
*/
char *
type_name(type_T *type, char **tofree)
{
char *name = vartype_name(type->tt_type);
*tofree = NULL;
if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
{
char *member_free;
char *member_name = type_name(type->tt_member, &member_free);
size_t len;
len = STRLEN(name) + STRLEN(member_name) + 3;
*tofree = alloc(len);
if (*tofree != NULL)
{
vim_snprintf(*tofree, len, "%s<%s>", name, member_name);
vim_free(member_free);
return *tofree;
}
}
if (type->tt_type == VAR_FUNC)
{
garray_T ga;
int i;
int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
ga_init2(&ga, 1, 100);
if (ga_grow(&ga, 20) == FAIL)
return "[unknown]";
*tofree = ga.ga_data;
STRCPY(ga.ga_data, "func(");
ga.ga_len += 5;
for (i = 0; i < type->tt_argcount; ++i)
{
char *arg_free;
char *arg_type;
int len;
if (type->tt_args == NULL)
arg_type = "[unknown]";
else
arg_type = type_name(type->tt_args[i], &arg_free);
if (i > 0)
{
STRCPY((char *)ga.ga_data + ga.ga_len, ", ");
ga.ga_len += 2;
}
len = (int)STRLEN(arg_type);
if (ga_grow(&ga, len + 8) == FAIL)
{
vim_free(arg_free);
return "[unknown]";
}
*tofree = ga.ga_data;
if (varargs && i == type->tt_argcount - 1)
{
STRCPY((char *)ga.ga_data + ga.ga_len, "...");
ga.ga_len += 3;
}
else if (i >= type->tt_min_argcount)
*((char *)ga.ga_data + ga.ga_len++) = '?';
STRCPY((char *)ga.ga_data + ga.ga_len, arg_type);
ga.ga_len += len;
vim_free(arg_free);
}
if (type->tt_member == &t_void)
STRCPY((char *)ga.ga_data + ga.ga_len, ")");
else
{
char *ret_free;
char *ret_name = type_name(type->tt_member, &ret_free);
int len;
len = (int)STRLEN(ret_name) + 4;
if (ga_grow(&ga, len) == FAIL)
{
vim_free(ret_free);
return "[unknown]";
}
*tofree = ga.ga_data;
STRCPY((char *)ga.ga_data + ga.ga_len, "): ");
STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name);
vim_free(ret_free);
}
return ga.ga_data;
}
return name;
}
/*
* Find "name" in script-local items of script "sid".
* Returns the index in "sn_var_vals" if found.
* If found but not in "sn_var_vals" returns -1.
* If not found returns -2.
*/
int
get_script_item_idx(int sid, char_u *name, int check_writable)
{
hashtab_T *ht;
dictitem_T *di;
scriptitem_T *si = SCRIPT_ITEM(sid);
int idx;
// First look the name up in the hashtable.
if (sid <= 0 || sid > script_items.ga_len)
return -1;
ht = &SCRIPT_VARS(sid);
di = find_var_in_ht(ht, 0, name, TRUE);
if (di == NULL)
return -2;
// Now find the svar_T index in sn_var_vals.
for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
if (sv->sv_tv == &di->di_tv)
{
if (check_writable && sv->sv_const)
semsg(_(e_readonlyvar), name);
return idx;
}
}
return -1;
}
/*
* Find "name" in imported items of the current script or in "cctx" if not
* NULL.
*/
imported_T *
find_imported(char_u *name, size_t len, cctx_T *cctx)
{
scriptitem_T *si;
int idx;
if (current_sctx.sc_sid <= 0)
return NULL;
si = SCRIPT_ITEM(current_sctx.sc_sid);
if (cctx != NULL)
for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data)
+ idx;
if (len == 0 ? STRCMP(name, import->imp_name) == 0
: STRLEN(import->imp_name) == len
&& STRNCMP(name, import->imp_name, len) == 0)
return import;
}
for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
if (len == 0 ? STRCMP(name, import->imp_name) == 0
: STRLEN(import->imp_name) == len
&& STRNCMP(name, import->imp_name, len) == 0)
return import;
}
return NULL;
}
/*
* Free all imported variables.
*/
static void
free_imported(cctx_T *cctx)
{
int idx;
for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx)
{
imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx;
vim_free(import->imp_name);
}
ga_clear(&cctx->ctx_imports);
}
/*
* Return TRUE if "p" points at a "#" but not at "#{".
*/
static int
vim9_comment_start(char_u *p)
{
return p[0] == '#' && p[1] != '{';
}
/*
* Return a pointer to the next line that isn't empty or only contains a
* comment. Skips over white space.
* Returns NULL if there is none.
*/
char_u *
peek_next_line_from_context(cctx_T *cctx)
{
int lnum = cctx->ctx_lnum;
while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
{
char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
char_u *p;
if (line == NULL)
break;
p = skipwhite(line);
if (*p != NUL && !vim9_comment_start(p))
return p;
}
return NULL;
}
/*
* Called when checking for a following operator at "arg". When the rest of
* the line is empty or only a comment, peek the next line. If there is a next
* line return a pointer to it and set "nextp".
* Otherwise skip over white space.
*/
static char_u *
may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
{
char_u *p = skipwhite(arg);
*nextp = NULL;
if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
{
*nextp = peek_next_line_from_context(cctx);
if (*nextp != NULL)
return *nextp;
}
return p;
}
/*
* Get the next line of the function from "cctx".
* Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
* Returns NULL when at the end.
*/
char_u *
next_line_from_context(cctx_T *cctx, int skip_comment)
{
char_u *line;
do
{
++cctx->ctx_lnum;
if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
{
line = NULL;
break;
}
line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
cctx->ctx_line_start = line;
SOURCING_LNUM = cctx->ctx_lnum + 1;
} while (line == NULL || *skipwhite(line) == NUL
|| (skip_comment && vim9_comment_start(skipwhite(line))));
return line;
}
/*
* If "*arg" is at the end of the line, advance to the next line.
* Also when "whitep" points to white space and "*arg" is on a "#".
* Return FAIL if beyond the last line, "*arg" is unmodified then.
*/
static int
may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
{
if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
{
char_u *next = next_line_from_context(cctx, TRUE);
if (next == NULL)
return FAIL;
*arg = skipwhite(next);
}
return OK;
}
/*
* Idem, and give an error when failed.
*/
static int
may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
{
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
emsg(_("E1097: line incomplete"));
return FAIL;
}
return OK;
}
// Structure passed between the compile_expr* functions to keep track of
// constants that have been parsed but for which no code was produced yet. If
// possible expressions on these constants are applied at compile time. If
// that is not possible, the code to push the constants needs to be generated
// before other instructions.
// Using 50 should be more than enough of 5 levels of ().
#define PPSIZE 50
typedef struct {
typval_T pp_tv[PPSIZE]; // stack of ppconst constants
int pp_used; // active entries in pp_tv[]
} ppconst_T;
static int compile_expr0(char_u **arg, cctx_T *cctx);
static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
/*
* Generate a PUSH instruction for "tv".
* "tv" will be consumed or cleared.
* Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
*/
static int
generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
{
if (tv != NULL)
{
switch (tv->v_type)
{
case VAR_UNKNOWN:
break;
case VAR_BOOL:
generate_PUSHBOOL(cctx, tv->vval.v_number);
break;
case VAR_SPECIAL:
generate_PUSHSPEC(cctx, tv->vval.v_number);
break;
case VAR_NUMBER:
generate_PUSHNR(cctx, tv->vval.v_number);
break;
#ifdef FEAT_FLOAT
case VAR_FLOAT:
generate_PUSHF(cctx, tv->vval.v_float);
break;
#endif
case VAR_BLOB:
generate_PUSHBLOB(cctx, tv->vval.v_blob);
tv->vval.v_blob = NULL;
break;
case VAR_STRING:
generate_PUSHS(cctx, tv->vval.v_string);
tv->vval.v_string = NULL;
break;
default:
iemsg("constant type not supported");
clear_tv(tv);
return FAIL;
}
tv->v_type = VAR_UNKNOWN;
}
return OK;
}
/*
* Generate code for any ppconst entries.
*/
static int
generate_ppconst(cctx_T *cctx, ppconst_T *ppconst)
{
int i;
int ret = OK;
int save_skip = cctx->ctx_skip;
cctx->ctx_skip = SKIP_NOT;
for (i = 0; i < ppconst->pp_used; ++i)
if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL)
ret = FAIL;
ppconst->pp_used = 0;
cctx->ctx_skip = save_skip;
return ret;
}
/*
* Clear ppconst constants. Used when failing.
*/
static void
clear_ppconst(ppconst_T *ppconst)
{
int i;
for (i = 0; i < ppconst->pp_used; ++i)
clear_tv(&ppconst->pp_tv[i]);
ppconst->pp_used = 0;
}
/*
* Generate an instruction to load script-local variable "name", without the
* leading "s:".
* Also finds imported variables.
*/
static int
compile_load_scriptvar(
cctx_T *cctx,
char_u *name, // variable NUL terminated
char_u *start, // start of variable
char_u **end, // end of variable
int error) // when TRUE may give error
{
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
imported_T *import;
if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9)
{
// variable is not in sn_var_vals: old style script.
return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid,
&t_any);
}
if (idx >= 0)
{
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
current_sctx.sc_sid, idx, sv->sv_type);
return OK;
}
import = find_imported(name, 0, cctx);
if (import != NULL)
{
if (import->imp_all)
{
char_u *p = skipwhite(*end);
char_u *exp_name;
int cc;
ufunc_T *ufunc;
type_T *type;
// Used "import * as Name", need to lookup the member.
if (*p != '.')
{
semsg(_("E1060: expected dot after name: %s"), start);
return FAIL;
}
++p;
if (VIM_ISWHITE(*p))
{
emsg(_("E1074: no white space allowed after dot"));
return FAIL;
}
// isolate one name
exp_name = p;
while (eval_isnamec(*p))
++p;
cc = *p;
*p = NUL;
idx = find_exported(import->imp_sid, exp_name, &ufunc, &type);
*p = cc;
p = skipwhite(p);
// TODO: what if it is a function?
if (idx < 0)
return FAIL;
*end = p;
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
import->imp_sid,
idx,
type);
}
else
{
// TODO: check this is a variable, not a function?
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
import->imp_sid,
import->imp_var_vals_idx,
import->imp_type);
}
return OK;
}
if (error)
semsg(_("E1050: Item not found: %s"), name);
return FAIL;
}
static int
generate_funcref(cctx_T *cctx, char_u *name)
{
ufunc_T *ufunc = find_func(name, FALSE, cctx);
if (ufunc == NULL)
return FAIL;
return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name),
ufunc->uf_func_type);
}
/*
* Compile a variable name into a load instruction.
* "end" points to just after the name.
* When "error" is FALSE do not give an error when not found.
*/
static int
compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
{
type_T *type;
char_u *name;
char_u *end = end_arg;
int res = FAIL;
int prev_called_emsg = called_emsg;
if (*(*arg + 1) == ':')
{
// load namespaced variable
if (end <= *arg + 2)
name = vim_strsave((char_u *)"[empty]");
else
name = vim_strnsave(*arg + 2, end - (*arg + 2));
if (name == NULL)
return FAIL;
if (**arg == 'v')
{
res = generate_LOADV(cctx, name, error);
}
else if (**arg == 'g')
{
// Global variables can be defined later, thus we don't check if it
// exists, give error at runtime.
res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any);
}
else if (**arg == 's')
{
res = compile_load_scriptvar(cctx, name, NULL, NULL, error);
}
else if (**arg == 'b')
{
// Buffer-local variables can be defined later, thus we don't check
// if it exists, give error at runtime.
res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any);
}
else if (**arg == 'w')
{
// Window-local variables can be defined later, thus we don't check
// if it exists, give error at runtime.
res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any);
}
else if (**arg == 't')
{
// Tabpage-local variables can be defined later, thus we don't
// check if it exists, give error at runtime.
res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any);
}
else
{
semsg("E1075: Namespace not supported: %s", *arg);
goto theend;
}
}
else
{
size_t len = end - *arg;
int idx;
int gen_load = FALSE;
int gen_load_outer = FALSE;
name = vim_strnsave(*arg, end - *arg);
if (name == NULL)
return FAIL;
if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK)
{
if (!gen_load_outer)
gen_load = TRUE;
}
else
{
lvar_T *lvar = lookup_local(*arg, len, cctx);
if (lvar != NULL)
{
type = lvar->lv_type;
idx = lvar->lv_idx;
if (lvar->lv_from_outer)
gen_load_outer = TRUE;
else
gen_load = TRUE;
}
else
{
// "var" can be script-local even without using "s:" if it
// already exists.
if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
== SCRIPT_VERSION_VIM9
|| lookup_script(*arg, len) == OK)
res = compile_load_scriptvar(cctx, name, *arg, &end,
FALSE);
// When the name starts with an uppercase letter or "x:" it
// can be a user defined function.
if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':'))
res = generate_funcref(cctx, name);
}
}
if (gen_load)
res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type);
if (gen_load_outer)
res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type);
}
*arg = end;
theend:
if (res == FAIL && error && called_emsg == prev_called_emsg)
semsg(_(e_var_notfound), name);
vim_free(name);
return res;
}
/*
* Compile the argument expressions.
* "arg" points to just after the "(" and is advanced to after the ")"
*/
static int
compile_arguments(char_u **arg, cctx_T *cctx, int *argcount)
{
char_u *p = *arg;
char_u *whitep = *arg;
for (;;)
{
if (may_get_next_line(whitep, &p, cctx) == FAIL)
goto failret;
if (*p == ')')
{
*arg = p + 1;
return OK;
}
if (compile_expr0(&p, cctx) == FAIL)
return FAIL;
++*argcount;
if (*p != ',' && *skipwhite(p) == ',')
{
semsg(_(e_no_white_before), ",");
p = skipwhite(p);
}
if (*p == ',')
{
++p;
if (*p != NUL && !VIM_ISWHITE(*p))
semsg(_(e_white_after), ",");
}
whitep = p;
p = skipwhite(p);
}
failret:
emsg(_(e_missing_close));
return FAIL;
}
/*
* Compile a function call: name(arg1, arg2)
* "arg" points to "name", "arg + varlen" to the "(".
* "argcount_init" is 1 for "value->method()"
* Instructions:
* EVAL arg1
* EVAL arg2
* BCALL / DCALL / UCALL
*/
static int
compile_call(
char_u **arg,
size_t varlen,
cctx_T *cctx,
ppconst_T *ppconst,
int argcount_init)
{
char_u *name = *arg;
char_u *p;
int argcount = argcount_init;
char_u namebuf[100];
char_u fname_buf[FLEN_FIXED + 1];
char_u *tofree = NULL;
int error = FCERR_NONE;
ufunc_T *ufunc;
int res = FAIL;
// we can evaluate "has('name')" at compile time
if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0)
{
char_u *s = skipwhite(*arg + varlen + 1);
typval_T argvars[2];
argvars[0].v_type = VAR_UNKNOWN;
if (*s == '"')
(void)eval_string(&s, &argvars[0], TRUE);
else if (*s == '\'')
(void)eval_lit_string(&s, &argvars[0], TRUE);
s = skipwhite(s);
if (*s == ')' && argvars[0].v_type == VAR_STRING)
{
typval_T *tv = &ppconst->pp_tv[ppconst->pp_used];
*arg = s + 1;
argvars[1].v_type = VAR_UNKNOWN;
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
f_has(argvars, tv);
clear_tv(&argvars[0]);
++ppconst->pp_used;
return OK;
}
clear_tv(&argvars[0]);
}
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
if (varlen >= sizeof(namebuf))
{
semsg(_("E1011: name too long: %s"), name);
return FAIL;
}
vim_strncpy(namebuf, *arg, varlen);
name = fname_trans_sid(namebuf, fname_buf, &tofree, &error);
*arg = skipwhite(*arg + varlen + 1);
if (compile_arguments(arg, cctx, &argcount) == FAIL)
goto theend;
if (ASCII_ISLOWER(*name) && name[1] != ':')
{
int idx;
// builtin function
idx = find_internal_func(name);
if (idx >= 0)
res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);
else
semsg(_(e_unknownfunc), namebuf);
goto theend;
}
// If we can find the function by name generate the right call.
ufunc = find_func(name, FALSE, cctx);
if (ufunc != NULL)
{
res = generate_CALL(cctx, ufunc, argcount);
goto theend;
}
// If the name is a variable, load it and use PCALL.
// Not for g:Func(), we don't know if it is a variable or not.
p = namebuf;
if (STRNCMP(namebuf, "g:", 2) != 0
&& compile_load(&p, namebuf + varlen, cctx, FALSE) == OK)
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
res = generate_PCALL(cctx, argcount, namebuf, type, FALSE);
goto theend;
}
// A global function may be defined only later. Need to figure out at
// runtime. Also handles a FuncRef at runtime.
if (STRNCMP(namebuf, "g:", 2) == 0)
res = generate_UCALL(cctx, name, argcount);
else
semsg(_(e_unknownfunc), namebuf);
theend:
vim_free(tofree);
return res;
}
// like NAMESPACE_CHAR but with 'a' and 'l'.
#define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw"
/*
* Find the end of a variable or function name. Unlike find_name_end() this
* does not recognize magic braces.
* When "namespace" is TRUE recognize "b:", "s:", etc.
* Return a pointer to just after the name. Equal to "arg" if there is no
* valid name.
*/
static char_u *
to_name_end(char_u *arg, int namespace)
{
char_u *p;
// Quick check for valid starting character.
if (!eval_isnamec1(*arg))
return arg;
for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
// Include a namespace such as "s:var" and "v:var". But "n:" is not
// and can be used in slice "[n:]".
if (*p == ':' && (p != arg + 1
|| !namespace
|| vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL))
break;
return p;
}
/*
* Like to_name_end() but also skip over a list or dict constant.
* This intentionally does not handle line continuation.
*/
char_u *
to_name_const_end(char_u *arg)
{
char_u *p = to_name_end(arg, TRUE);
typval_T rettv;
if (p == arg && *arg == '[')
{
// Can be "[1, 2, 3]->Func()".
if (eval_list(&p, &rettv, NULL, FALSE) == FAIL)
p = arg;
}
else if (p == arg && *arg == '#' && arg[1] == '{')
{
// Can be "#{a: 1}->Func()".
++p;
if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL)
p = arg;
}
else if (p == arg && *arg == '{')
{
int ret = get_lambda_tv(&p, &rettv, NULL);
// Can be "{x -> ret}()".
// Can be "{'a': 1}->Func()".
if (ret == NOTDONE)
ret = eval_dict(&p, &rettv, NULL, FALSE);
if (ret != OK)
p = arg;
}
return p;
}
/*
* parse a list: [expr, expr]
* "*arg" points to the '['.
*/
static int
compile_list(char_u **arg, cctx_T *cctx)
{
char_u *p = skipwhite(*arg + 1);
char_u *whitep = *arg + 1;
int count = 0;
for (;;)
{
if (may_get_next_line(whitep, &p, cctx) == FAIL)
{
semsg(_(e_list_end), *arg);
return FAIL;
}
if (*p == ']')
{
++p;
// Allow for following comment, after at least one space.
if (VIM_ISWHITE(*p) && *skipwhite(p) == '#')
p += STRLEN(p);
break;
}
if (compile_expr0(&p, cctx) == FAIL)
break;
++count;
if (*p == ',')
{
++p;
if (*p != ']' && !IS_WHITE_OR_NUL(*p))
{
semsg(_(e_white_after), ",");
return FAIL;
}
}
whitep = p;
p = skipwhite(p);
}
*arg = p;
generate_NEWLIST(cctx, count);
return OK;
}
/*
* parse a lambda: {arg, arg -> expr}
* "*arg" points to the '{'.
*/
static int
compile_lambda(char_u **arg, cctx_T *cctx)
{
typval_T rettv;
ufunc_T *ufunc;
evalarg_T evalarg;
CLEAR_FIELD(evalarg);
evalarg.eval_flags = EVAL_EVALUATE;
evalarg.eval_cctx = cctx;
// Get the funcref in "rettv".
if (get_lambda_tv(arg, &rettv, &evalarg) != OK)
return FAIL;
ufunc = rettv.vval.v_partial->pt_func;
++ufunc->uf_refcount;
clear_tv(&rettv);
ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
// The function will have one line: "return {expr}".
// Compile it into instructions.
compile_def_function(ufunc, TRUE, cctx);
clear_evalarg(&evalarg, NULL);
if (ufunc->uf_def_status == UF_COMPILED)
return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx);
func_ptr_unref(ufunc);
return FAIL;
}
/*
* Compile a lamda call: expr->{lambda}(args)
* "arg" points to the "{".
*/
static int
compile_lambda_call(char_u **arg, cctx_T *cctx)
{
ufunc_T *ufunc;
typval_T rettv;
int argcount = 1;
int ret = FAIL;
// Get the funcref in "rettv".
if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL)
return FAIL;
if (**arg != '(')
{
if (*skipwhite(*arg) == '(')
emsg(_(e_nowhitespace));
else
semsg(_(e_missing_paren), "lambda");
clear_tv(&rettv);
return FAIL;
}
ufunc = rettv.vval.v_partial->pt_func;
++ufunc->uf_refcount;
clear_tv(&rettv);
ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
// The function will have one line: "return {expr}".
// Compile it into instructions.
compile_def_function(ufunc, TRUE, cctx);
// compile the arguments
*arg = skipwhite(*arg + 1);
if (compile_arguments(arg, cctx, &argcount) == OK)
// call the compiled function
ret = generate_CALL(cctx, ufunc, argcount);
if (ret == FAIL)
func_ptr_unref(ufunc);
return ret;
}
/*
* parse a dict: {'key': val} or #{key: val}
* "*arg" points to the '{'.
*/
static int
compile_dict(char_u **arg, cctx_T *cctx, int literal)
{
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
int count = 0;
dict_T *d = dict_alloc();
dictitem_T *item;
char_u *whitep = *arg;
char_u *p;
if (d == NULL)
return FAIL;
*arg = skipwhite(*arg + 1);
for (;;)
{
char_u *key = NULL;
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (**arg == '}')
break;
if (literal)
{
char_u *end = to_name_end(*arg, !literal);
if (end == *arg)
{
semsg(_("E1014: Invalid key: %s"), *arg);
return FAIL;
}
key = vim_strnsave(*arg, end - *arg);
if (generate_PUSHS(cctx, key) == FAIL)
return FAIL;
*arg = end;
}
else
{
isn_T *isn;
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
if (isn->isn_type == ISN_PUSHS)
key = isn->isn_arg.string;
else
{
type_T *keytype = ((type_T **)stack->ga_data)
[stack->ga_len - 1];
if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL)
return FAIL;
}
}
// Check for duplicate keys, if using string keys.
if (key != NULL)
{
item = dict_find(d, key, -1);
if (item != NULL)
{
semsg(_(e_duplicate_key), key);
goto failret;
}
item = dictitem_alloc(key);
if (item != NULL)
{
item->di_tv.v_type = VAR_UNKNOWN;
item->di_tv.v_lock = 0;
if (dict_add(d, item) == FAIL)
dictitem_free(item);
}
}
*arg = skipwhite(*arg);
if (**arg != ':')
{
semsg(_(e_missing_dict_colon), *arg);
return FAIL;
}
whitep = *arg + 1;
*arg = skipwhite(*arg + 1);
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
++count;
whitep = *arg;
*arg = skipwhite(*arg);
if (may_get_next_line(whitep, arg, cctx) == FAIL)
{
*arg = NULL;
goto failret;
}
if (**arg == '}')
break;
if (**arg != ',')
{
semsg(_(e_missing_dict_comma), *arg);
goto failret;
}
whitep = *arg + 1;
*arg = skipwhite(*arg + 1);
}
*arg = *arg + 1;
// Allow for following comment, after at least one space.
p = skipwhite(*arg);
if (VIM_ISWHITE(**arg) && vim9_comment_start(p))
*arg += STRLEN(*arg);
dict_unref(d);
return generate_NEWDICT(cctx, count);
failret:
if (*arg == NULL)
semsg(_(e_missing_dict_end), _("[end of lines]"));
dict_unref(d);
return FAIL;
}
/*
* Compile "&option".
*/
static int
compile_get_option(char_u **arg, cctx_T *cctx)
{
typval_T rettv;
char_u *start = *arg;
int ret;
// parse the option and get the current value to get the type.
rettv.v_type = VAR_UNKNOWN;
ret = eval_option(arg, &rettv, TRUE);
if (ret == OK)
{
// include the '&' in the name, eval_option() expects it.
char_u *name = vim_strnsave(start, *arg - start);
type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string;
ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
vim_free(name);
}
clear_tv(&rettv);
return ret;
}
/*
* Compile "$VAR".
*/
static int
compile_get_env(char_u **arg, cctx_T *cctx)
{
char_u *start = *arg;
int len;
int ret;
char_u *name;
++*arg;
len = get_env_len(arg);
if (len == 0)
{
semsg(_(e_syntax_at), start - 1);
return FAIL;
}
// include the '$' in the name, eval_env_var() expects it.
name = vim_strnsave(start, len + 1);
ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string);
vim_free(name);
return ret;
}
/*
* Compile "@r".
*/
static int
compile_get_register(char_u **arg, cctx_T *cctx)
{
int ret;
++*arg;
if (**arg == NUL)
{
semsg(_(e_syntax_at), *arg - 1);
return FAIL;
}
if (!valid_yank_reg(**arg, TRUE))
{
emsg_invreg(**arg);
return FAIL;
}
ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string);
++*arg;
return ret;
}
/*
* Apply leading '!', '-' and '+' to constant "rettv".
*/
static int
apply_leader(typval_T *rettv, char_u *start, char_u *end)
{
char_u *p = end;
// this works from end to start
while (p > start)
{
--p;
if (*p == '-' || *p == '+')
{
// only '-' has an effect, for '+' we only check the type
#ifdef FEAT_FLOAT
if (rettv->v_type == VAR_FLOAT)
{
if (*p == '-')
rettv->vval.v_float = -rettv->vval.v_float;
}
else
#endif
{
varnumber_T val;
int error = FALSE;
// tv_get_number_chk() accepts a string, but we don't want that
// here
if (check_not_string(rettv) == FAIL)
return FAIL;
val = tv_get_number_chk(rettv, &error);
clear_tv(rettv);
if (error)
return FAIL;
if (*p == '-')
val = -val;
rettv->v_type = VAR_NUMBER;
rettv->vval.v_number = val;
}
}
else
{
int v = tv2bool(rettv);
// '!' is permissive in the type.
clear_tv(rettv);
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE;
}
}
return OK;
}
/*
* Recognize v: variables that are constants and set "rettv".
*/
static void
get_vim_constant(char_u **arg, typval_T *rettv)
{
if (STRNCMP(*arg, "v:true", 6) == 0)
{
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_TRUE;
*arg += 6;
}
else if (STRNCMP(*arg, "v:false", 7) == 0)
{
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_FALSE;
*arg += 7;
}
else if (STRNCMP(*arg, "v:null", 6) == 0)
{
rettv->v_type = VAR_SPECIAL;
rettv->vval.v_number = VVAL_NULL;
*arg += 6;
}
else if (STRNCMP(*arg, "v:none", 6) == 0)
{
rettv->v_type = VAR_SPECIAL;
rettv->vval.v_number = VVAL_NONE;
*arg += 6;
}
}
static exptype_T
get_compare_type(char_u *p, int *len, int *type_is)
{
exptype_T type = EXPR_UNKNOWN;
int i;
switch (p[0])
{
case '=': if (p[1] == '=')
type = EXPR_EQUAL;
else if (p[1] == '~')
type = EXPR_MATCH;
break;
case '!': if (p[1] == '=')
type = EXPR_NEQUAL;
else if (p[1] == '~')
type = EXPR_NOMATCH;
break;
case '>': if (p[1] != '=')
{
type = EXPR_GREATER;
*len = 1;
}
else
type = EXPR_GEQUAL;
break;
case '<': if (p[1] != '=')
{
type = EXPR_SMALLER;
*len = 1;
}
else
type = EXPR_SEQUAL;
break;
case 'i': if (p[1] == 's')
{
// "is" and "isnot"; but not a prefix of a name
if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
*len = 5;
i = p[*len];
if (!isalnum(i) && i != '_')
{
type = *len == 2 ? EXPR_IS : EXPR_ISNOT;
*type_is = TRUE;
}
}
break;
}
return type;
}
/*
* Compile code to apply '-', '+' and '!'.
*/
static int
compile_leader(cctx_T *cctx, char_u *start, char_u *end)
{
char_u *p = end;
// this works from end to start
while (p > start)
{
--p;
if (*p == '-' || *p == '+')
{
int negate = *p == '-';
isn_T *isn;
// TODO: check type
while (p > start && (p[-1] == '-' || p[-1] == '+'))
{
--p;
if (*p == '-')
negate = !negate;
}
// only '-' has an effect, for '+' we only check the type
if (negate)
isn = generate_instr(cctx, ISN_NEGATENR);
else
isn = generate_instr(cctx, ISN_CHECKNR);
if (isn == NULL)
return FAIL;
}
else
{
int invert = TRUE;
while (p > start && p[-1] == '!')
{
--p;
invert = !invert;
}
if (generate_2BOOL(cctx, invert) == FAIL)
return FAIL;
}
}
return OK;
}
/*
* Compile whatever comes after "name" or "name()".
* Advances "*arg" only when something was recognized.
*/
static int
compile_subscript(
char_u **arg,
cctx_T *cctx,
char_u **start_leader,
char_u *end_leader,
ppconst_T *ppconst)
{
for (;;)
{
char_u *p = skipwhite(*arg);
if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p)))
{
char_u *next = peek_next_line_from_context(cctx);
// If a following line starts with "->{" or "->X" advance to that
// line, so that a line break before "->" is allowed.
// Also if a following line starts with ".x".
if (next != NULL &&
((next[0] == '-' && next[1] == '>'
&& (next[2] == '{' || ASCII_ISALPHA(next[2])))
|| (next[0] == '.' && ASCII_ISALPHA(next[1]))))
{
next = next_line_from_context(cctx, TRUE);
if (next == NULL)
return FAIL;
*arg = next;
p = skipwhite(*arg);
}
}
if (*p == '(')
{
garray_T *stack = &cctx->ctx_type_stack;
type_T *type;
int argcount = 0;
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
// funcref(arg)
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
*arg = skipwhite(p + 1);
if (compile_arguments(arg, cctx, &argcount) == FAIL)
return FAIL;
if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL)
return FAIL;
}
else if (*p == '-' && p[1] == '>')
{
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
// something->method()
// Apply the '!', '-' and '+' first:
// -1.0->func() works like (-1.0)->func()
if (compile_leader(cctx, *start_leader, end_leader) == FAIL)
return FAIL;
*start_leader = end_leader; // don't apply again later
p += 2;
*arg = skipwhite(p);
if (may_get_next_line(p, arg, cctx) == FAIL)
return FAIL;
if (**arg == '{')
{
// lambda call: list->{lambda}
if (compile_lambda_call(arg, cctx) == FAIL)
return FAIL;
}
else
{
// method call: list->method()
p = *arg;
if (ASCII_ISALPHA(*p) && p[1] == ':')
p += 2;
for ( ; eval_isnamec1(*p); ++p)
;
if (*p != '(')
{
semsg(_(e_missing_paren), *arg);
return FAIL;
}
// TODO: base value may not be the first argument
if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL)
return FAIL;
}
}
else if (*p == '[')
{
garray_T *stack = &cctx->ctx_type_stack;
type_T **typep;
// list index: list[123]
// dict member: dict[key]
// TODO: blob index
// TODO: more arguments
// TODO: recognize list or dict at runtime
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
++p;
*arg = skipwhite(p);
if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL;
if (compile_expr0(arg, cctx) == FAIL)
return FAIL;
if (may_get_next_line_error(p, arg, cctx) == FAIL)
return FAIL;
if (**arg != ']')
{
emsg(_(e_missbrac));
return FAIL;
}
*arg = *arg + 1;
typep = ((type_T **)stack->ga_data) + stack->ga_len - 2;
if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any)
{
if ((*typep)->tt_type == VAR_LIST)
*typep = (*typep)->tt_member;
if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL)
return FAIL;
}
else if ((*typep)->tt_type == VAR_DICT)
{
*typep = (*typep)->tt_member;
if (may_generate_2STRING(-1, cctx) == FAIL)
return FAIL;
if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL)
return FAIL;
}
else
{
emsg(_(e_listdictblobreq));
return FAIL;
}
}
else if (*p == '.' && p[1] != '.')
{
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
*arg = p + 1;
if (may_get_next_line(*arg, arg, cctx) == FAIL)
return FAIL;
// dictionary member: dict.name
p = *arg;
if (eval_isnamec1(*p))
while (eval_isnamec(*p))
MB_PTR_ADV(p);
if (p == *arg)
{
semsg(_(e_syntax_at), *arg);
return FAIL;
}
if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL)
return FAIL;
*arg = p;
}
else
break;
}
// TODO - see handle_subscript():
// Turn "dict.Func" into a partial for "Func" bound to "dict".
// Don't do this when "Func" is already a partial that was bound
// explicitly (pt_auto is FALSE).
return OK;
}
/*
* Compile an expression at "*arg" and add instructions to "cctx->ctx_instr".
* "arg" is advanced until after the expression, skipping white space.
*
* If the value is a constant "ppconst->pp_ret" will be set.
* Before instructions are generated, any values in "ppconst" will generated.
*
* This is the compiling equivalent of eval1(), eval2(), etc.
*/
/*
* number number constant
* 0zFFFFFFFF Blob constant
* "string" string constant
* 'string' literal string constant
* &option-name option value
* @r register contents
* identifier variable value
* function() function call
* $VAR environment variable
* (expression) nested expression
* [expr, expr] List
* {key: val, key: val} Dictionary
* #{key: val, key: val} Dictionary with literal keys
*
* Also handle:
* ! in front logical NOT
* - in front unary minus
* + in front unary plus (ignored)
* trailing (arg) funcref/partial call
* trailing [] subscript in String or List
* trailing .name entry in Dictionary
* trailing ->name() method call
*/
static int
compile_expr7(
char_u **arg,
cctx_T *cctx,
ppconst_T *ppconst)
{
char_u *start_leader, *end_leader;
int ret = OK;
typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used];
int used_before = ppconst->pp_used;
/*
* Skip '!', '-' and '+' characters. They are handled later.
*/
start_leader = *arg;
while (**arg == '!' || **arg == '-' || **arg == '+')
*arg = skipwhite(*arg + 1);
end_leader = *arg;
rettv->v_type = VAR_UNKNOWN;
switch (**arg)
{
/*
* Number constant.
*/
case '0': // also for blob starting with 0z
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL)
return FAIL;
break;
/*
* String constant: "string".
*/
case '"': if (eval_string(arg, rettv, TRUE) == FAIL)
return FAIL;
break;
/*
* Literal string constant: 'str''ing'.
*/
case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL)
return FAIL;
break;
/*
* Constant Vim variable.
*/
case 'v': get_vim_constant(arg, rettv);
ret = NOTDONE;
break;
/*
* "true" constant
*/
case 't': if (STRNCMP(*arg, "true", 4) == 0
&& !eval_isnamec((*arg)[4]))
{
*arg += 4;
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_TRUE;
}
else
ret = NOTDONE;
break;
/*
* "false" constant
*/
case 'f': if (STRNCMP(*arg, "false", 5) == 0
&& !eval_isnamec((*arg)[5]))
{
*arg += 5;
rettv->v_type = VAR_BOOL;
rettv->vval.v_number = VVAL_FALSE;
}
else
ret = NOTDONE;
break;
/*
* List: [expr, expr]
*/
case '[': ret = compile_list(arg, cctx);
break;
/*
* Dictionary: #{key: val, key: val}
*/
case '#': if ((*arg)[1] == '{')
{
++*arg;
ret = compile_dict(arg, cctx, TRUE);
}
else
ret = NOTDONE;
break;
/*
* Lambda: {arg, arg -> expr}
* Dictionary: {'key': val, 'key': val}
*/
case '{': {
char_u *start = skipwhite(*arg + 1);
// Find out what comes after the arguments.
ret = get_function_args(&start, '-', NULL,
NULL, NULL, NULL, TRUE, NULL, NULL);
if (ret != FAIL && *start == '>')
ret = compile_lambda(arg, cctx);
else
ret = compile_dict(arg, cctx, FALSE);
}
break;
/*
* Option value: &name
*/
case '&': ret = compile_get_option(arg, cctx);
break;
/*
* Environment variable: $VAR.
*/
case '$': ret = compile_get_env(arg, cctx);
break;
/*
* Register contents: @r.
*/
case '@': ret = compile_get_register(arg, cctx);
break;
/*
* nested expression: (expression).
*/
case '(': *arg = skipwhite(*arg + 1);
// recursive!
if (ppconst->pp_used <= PPSIZE - 10)
{
ret = compile_expr1(arg, cctx, ppconst);
}
else
{
// Not enough space in ppconst, flush constants.
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
ret = compile_expr0(arg, cctx);
}
*arg = skipwhite(*arg);
if (**arg == ')')
++*arg;
else if (ret == OK)
{
emsg(_(e_missing_close));
ret = FAIL;
}
break;
default: ret = NOTDONE;
break;
}
if (ret == FAIL)
return FAIL;
if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used)
{
// apply the '!', '-' and '+' before the constant
if (apply_leader(rettv, start_leader, end_leader) == FAIL)
{
clear_tv(rettv);
return FAIL;
}
start_leader = end_leader; // don't apply again below
if (cctx->ctx_skip == SKIP_YES)
clear_tv(rettv);
else
// A constant expression can possibly be handled compile time,
// return the value instead of generating code.
++ppconst->pp_used;
}
else if (ret == NOTDONE)
{
char_u *p;
int r;
if (!eval_isnamec1(**arg))
{
semsg(_("E1015: Name expected: %s"), *arg);
return FAIL;
}
// "name" or "name()"
p = to_name_end(*arg, TRUE);
if (*p == '(')
{
r = compile_call(arg, p - *arg, cctx, ppconst, 0);
}
else
{
if (generate_ppconst(cctx, ppconst) == FAIL)
return FAIL;
r = compile_load(arg, p, cctx, TRUE);
}
if (r == FAIL)
return FAIL;
}
// Handle following "[]", ".member", etc.
// Then deal with prefixed '-', '+' and '!', if not done already.
if (compile_subscript(arg, cctx, &start_leader, end_leader,
ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used > 0)
{
// apply the '!', '-' and '+' before the constant
rettv = &ppconst->pp_tv[ppconst->pp_used - 1];
if (apply_leader(rettv, start_leader, end_leader) == FAIL)
return FAIL;
return OK;
}
if (compile_leader(cctx, start_leader, end_leader) == FAIL)
return FAIL;
return OK;
}
/*
* * number multiplication
* / number division
* % number modulo
*/
static int
compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *op;
char_u *next;
int ppconst_used = ppconst->pp_used;
// get the first expression
if (compile_expr7(arg, cctx, ppconst) == FAIL)
return FAIL;
/*
* Repeat computing, until no "*", "/" or "%" is following.
*/
for (;;)
{
op = may_peek_next_line(cctx, *arg, &next);
if (*op != '*' && *op != '/' && *op != '%')
break;
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
op = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
{
char_u buf[3];
vim_strncpy(buf, op, 1);
semsg(_(e_white_both), buf);
return FAIL;
}
*arg = skipwhite(op + 1);
if (may_get_next_line(op + 1, arg, cctx) == FAIL)
return FAIL;
// get the second expression
if (compile_expr7(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2
&& ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)
{
typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
varnumber_T res = 0;
// both are numbers: compute the result
switch (*op)
{
case '*': res = tv1->vval.v_number * tv2->vval.v_number;
break;
case '/': res = tv1->vval.v_number / tv2->vval.v_number;
break;
case '%': res = tv1->vval.v_number % tv2->vval.v_number;
break;
}
tv1->vval.v_number = res;
--ppconst->pp_used;
}
else
{
generate_ppconst(cctx, ppconst);
generate_two_op(cctx, op);
}
}
return OK;
}
/*
* + number addition
* - number subtraction
* .. string concatenation
*/
static int
compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *op;
char_u *next;
int oplen;
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr6(arg, cctx, ppconst) == FAIL)
return FAIL;
/*
* Repeat computing, until no "+", "-" or ".." is following.
*/
for (;;)
{
op = may_peek_next_line(cctx, *arg, &next);
if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.'))
break;
oplen = (*op == '.' ? 2 : 1);
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
op = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
{
char_u buf[3];
vim_strncpy(buf, op, oplen);
semsg(_(e_white_both), buf);
return FAIL;
}
*arg = skipwhite(op + oplen);
if (may_get_next_line(op + oplen, arg, cctx) == FAIL)
return FAIL;
// get the second expression
if (compile_expr6(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2
&& (*op == '.'
? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING)
: (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER
&& ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER)))
{
typval_T *tv1 = &ppconst->pp_tv[ppconst_used];
typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1];
// concat/subtract/add constant numbers
if (*op == '+')
tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number;
else if (*op == '-')
tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number;
else
{
// concatenate constant strings
char_u *s1 = tv1->vval.v_string;
char_u *s2 = tv2->vval.v_string;
size_t len1 = STRLEN(s1);
tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1));
if (tv1->vval.v_string == NULL)
{
clear_ppconst(ppconst);
return FAIL;
}
mch_memmove(tv1->vval.v_string, s1, len1);
STRCPY(tv1->vval.v_string + len1, s2);
vim_free(s1);
vim_free(s2);
}
--ppconst->pp_used;
}
else
{
generate_ppconst(cctx, ppconst);
if (*op == '.')
{
if (may_generate_2STRING(-2, cctx) == FAIL
|| may_generate_2STRING(-1, cctx) == FAIL)
return FAIL;
generate_instr_drop(cctx, ISN_CONCAT, 1);
}
else
generate_two_op(cctx, op);
}
}
return OK;
}
/*
* expr5a == expr5b
* expr5a =~ expr5b
* expr5a != expr5b
* expr5a !~ expr5b
* expr5a > expr5b
* expr5a >= expr5b
* expr5a < expr5b
* expr5a <= expr5b
* expr5a is expr5b
* expr5a isnot expr5b
*
* Produces instructions:
* EVAL expr5a Push result of "expr5a"
* EVAL expr5b Push result of "expr5b"
* COMPARE one of the compare instructions
*/
static int
compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
exptype_T type = EXPR_UNKNOWN;
char_u *p;
char_u *next;
int len = 2;
int type_is = FALSE;
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr5(arg, cctx, ppconst) == FAIL)
return FAIL;
p = may_peek_next_line(cctx, *arg, &next);
type = get_compare_type(p, &len, &type_is);
/*
* If there is a comparative operator, use it.
*/
if (type != EXPR_UNKNOWN)
{
int ic = FALSE; // Default: do not ignore case
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (type_is && (p[len] == '?' || p[len] == '#'))
{
semsg(_(e_invexpr2), *arg);
return FAIL;
}
// extra question mark appended: ignore case
if (p[len] == '?')
{
ic = TRUE;
++len;
}
// extra '#' appended: match case (ignored)
else if (p[len] == '#')
++len;
// nothing appended: match case
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
{
char_u buf[7];
vim_strncpy(buf, p, len);
semsg(_(e_white_both), buf);
return FAIL;
}
// get the second variable
*arg = skipwhite(p + len);
if (may_get_next_line(p + len, arg, cctx) == FAIL)
return FAIL;
if (compile_expr5(arg, cctx, ppconst) == FAIL)
return FAIL;
if (ppconst->pp_used == ppconst_used + 2)
{
typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2];
typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1];
int ret;
// Both sides are a constant, compute the result now.
// First check for a valid combination of types, this is more
// strict than typval_compare().
if (check_compare_types(type, tv1, tv2) == FAIL)
ret = FAIL;
else
{
ret = typval_compare(tv1, tv2, type, ic);
tv1->v_type = VAR_BOOL;
tv1->vval.v_number = tv1->vval.v_number
? VVAL_TRUE : VVAL_FALSE;
clear_tv(tv2);
--ppconst->pp_used;
}
return ret;
}
generate_ppconst(cctx, ppconst);
return generate_COMPARE(cctx, type, ic);
}
return OK;
}
static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst);
/*
* Compile || or &&.
*/
static int
compile_and_or(
char_u **arg,
cctx_T *cctx,
char *op,
ppconst_T *ppconst,
int ppconst_used UNUSED)
{
char_u *next;
char_u *p = may_peek_next_line(cctx, *arg, &next);
int opchar = *op;
if (p[0] == opchar && p[1] == opchar)
{
garray_T *instr = &cctx->ctx_instr;
garray_T end_ga;
/*
* Repeat until there is no following "||" or "&&"
*/
ga_init2(&end_ga, sizeof(int), 10);
while (p[0] == opchar && p[1] == opchar)
{
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2]))
{
semsg(_(e_white_both), op);
return FAIL;
}
// TODO: use ppconst if the value is a constant
generate_ppconst(cctx, ppconst);
if (ga_grow(&end_ga, 1) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
*(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len;
++end_ga.ga_len;
generate_JUMP(cctx, opchar == '|'
? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0);
// eval the next expression
*arg = skipwhite(p + 2);
if (may_get_next_line(p + 2, arg, cctx) == FAIL)
return FAIL;
if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst)
: compile_expr4(arg, cctx, ppconst)) == FAIL)
{
ga_clear(&end_ga);
return FAIL;
}
p = may_peek_next_line(cctx, *arg, &next);
}
generate_ppconst(cctx, ppconst);
// Fill in the end label in all jumps.
while (end_ga.ga_len > 0)
{
isn_T *isn;
--end_ga.ga_len;
isn = ((isn_T *)instr->ga_data)
+ *(((int *)end_ga.ga_data) + end_ga.ga_len);
isn->isn_arg.jump.jump_where = instr->ga_len;
}
ga_clear(&end_ga);
}
return OK;
}
/*
* expr4a && expr4a && expr4a logical AND
*
* Produces instructions:
* EVAL expr4a Push result of "expr4a"
* JUMP_AND_KEEP_IF_FALSE end
* EVAL expr4b Push result of "expr4b"
* JUMP_AND_KEEP_IF_FALSE end
* EVAL expr4c Push result of "expr4c"
* end:
*/
static int
compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
int ppconst_used = ppconst->pp_used;
// get the first variable
if (compile_expr4(arg, cctx, ppconst) == FAIL)
return FAIL;
// || and && work almost the same
return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used);
}
/*
* expr3a || expr3b || expr3c logical OR
*
* Produces instructions:
* EVAL expr3a Push result of "expr3a"
* JUMP_AND_KEEP_IF_TRUE end
* EVAL expr3b Push result of "expr3b"
* JUMP_AND_KEEP_IF_TRUE end
* EVAL expr3c Push result of "expr3c"
* end:
*/
static int
compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
int ppconst_used = ppconst->pp_used;
// eval the first expression
if (compile_expr3(arg, cctx, ppconst) == FAIL)
return FAIL;
// || and && work almost the same
return compile_and_or(arg, cctx, "||", ppconst, ppconst_used);
}
/*
* Toplevel expression: expr2 ? expr1a : expr1b
*
* Produces instructions:
* EVAL expr2 Push result of "expr"
* JUMP_IF_FALSE alt jump if false
* EVAL expr1a
* JUMP_ALWAYS end
* alt: EVAL expr1b
* end:
*/
static int
compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
{
char_u *p;
int ppconst_used = ppconst->pp_used;
char_u *next;
// Evaluate the first expression.
if (compile_expr2(arg, cctx, ppconst) == FAIL)
return FAIL;
p = may_peek_next_line(cctx, *arg, &next);
if (*p == '?')
{
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
int alt_idx = instr->ga_len;
int end_idx = 0;
isn_T *isn;
type_T *type1 = NULL;
type_T *type2;
int has_const_expr = FALSE;
int const_value = FALSE;
int save_skip = cctx->ctx_skip;
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
{
semsg(_(e_white_both), "?");
return FAIL;
}
if (ppconst->pp_used == ppconst_used + 1)
{
// the condition is a constant, we know whether the ? or the :
// expression is to be evaluated.
has_const_expr = TRUE;
const_value = tv2bool(&ppconst->pp_tv[ppconst_used]);
clear_tv(&ppconst->pp_tv[ppconst_used]);
--ppconst->pp_used;
cctx->ctx_skip = save_skip == SKIP_YES || !const_value
? SKIP_YES : SKIP_NOT;
}
else
{
generate_ppconst(cctx, ppconst);
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
// evaluate the second expression; any type is accepted
*arg = skipwhite(p + 1);
if (may_get_next_line(p + 1, arg, cctx) == FAIL)
return FAIL;
if (compile_expr1(arg, cctx, ppconst) == FAIL)
return FAIL;
if (!has_const_expr)
{
generate_ppconst(cctx, ppconst);
// remember the type and drop it
--stack->ga_len;
type1 = ((type_T **)stack->ga_data)[stack->ga_len];
end_idx = instr->ga_len;
generate_JUMP(cctx, JUMP_ALWAYS, 0);
// jump here from JUMP_IF_FALSE
isn = ((isn_T *)instr->ga_data) + alt_idx;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
// Check for the ":".
p = may_peek_next_line(cctx, *arg, &next);
if (*p != ':')
{
emsg(_(e_missing_colon));
return FAIL;
}
if (next != NULL)
{
*arg = next_line_from_context(cctx, TRUE);
p = skipwhite(*arg);
}
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1]))
{
semsg(_(e_white_both), ":");
return FAIL;
}
// evaluate the third expression
if (has_const_expr)
cctx->ctx_skip = save_skip == SKIP_YES || const_value
? SKIP_YES : SKIP_NOT;
*arg = skipwhite(p + 1);
if (may_get_next_line(p + 1, arg, cctx) == FAIL)
return FAIL;
if (compile_expr1(arg, cctx, ppconst) == FAIL)
return FAIL;
if (!has_const_expr)
{
generate_ppconst(cctx, ppconst);
// If the types differ, the result has a more generic type.
type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
common_type(type1, type2, &type2, cctx->ctx_type_list);
// jump here from JUMP_ALWAYS
isn = ((isn_T *)instr->ga_data) + end_idx;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
cctx->ctx_skip = save_skip;
}
return OK;
}
/*
* Toplevel expression.
*/
static int
compile_expr0(char_u **arg, cctx_T *cctx)
{
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
if (compile_expr1(arg, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return FAIL;
}
if (generate_ppconst(cctx, &ppconst) == FAIL)
return FAIL;
return OK;
}
/*
* compile "return [expr]"
*/
static char_u *
compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
{
char_u *p = arg;
garray_T *stack = &cctx->ctx_type_stack;
type_T *stack_type;
if (*p != NUL && *p != '|' && *p != '\n')
{
// compile return argument into instructions
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (set_return_type)
cctx->ctx_ufunc->uf_ret_type = stack_type;
else
{
if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
&& stack_type->tt_type != VAR_VOID
&& stack_type->tt_type != VAR_UNKNOWN)
{
emsg(_("E1096: Returning a value in a function without a return type"));
return NULL;
}
if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
cctx, FALSE) == FAIL)
return NULL;
}
}
else
{
// "set_return_type" cannot be TRUE, only used for a lambda which
// always has an argument.
if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
&& cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
{
emsg(_("E1003: Missing return value"));
return NULL;
}
// No argument, return zero.
generate_PUSHNR(cctx, 0);
}
if (generate_instr(cctx, ISN_RETURN) == NULL)
return NULL;
// "return val | endif" is possible
return skipwhite(p);
}
/*
* Get a line from the compilation context, compatible with exarg_T getline().
* Return a pointer to the line in allocated memory.
* Return NULL for end-of-file or some error.
*/
static char_u *
exarg_getline(
int c UNUSED,
void *cookie,
int indent UNUSED,
int do_concat UNUSED)
{
cctx_T *cctx = (cctx_T *)cookie;
if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len)
{
iemsg("Heredoc got to end");
return NULL;
}
++cctx->ctx_lnum;
return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)
[cctx->ctx_lnum]);
}
/*
* Compile a nested :def command.
*/
static char_u *
compile_nested_function(exarg_T *eap, cctx_T *cctx)
{
char_u *name_start = eap->arg;
char_u *name_end = to_name_end(eap->arg, FALSE);
char_u *name = get_lambda_name();
lvar_T *lvar;
ufunc_T *ufunc;
eap->arg = name_end;
eap->getline = exarg_getline;
eap->cookie = cctx;
eap->skip = cctx->ctx_skip == SKIP_YES;
eap->forceit = FALSE;
ufunc = def_function(eap, name);
if (ufunc == NULL)
return NULL;
if (ufunc->uf_def_status == UF_TO_BE_COMPILED
&& compile_def_function(ufunc, TRUE, cctx) == FAIL)
return NULL;
// Define a local variable for the function reference.
lvar = reserve_local(cctx, name_start, name_end - name_start,
TRUE, ufunc->uf_func_type);
if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL
|| generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL)
return NULL;
// TODO: warning for trailing text?
return (char_u *)"";
}
/*
* Return the length of an assignment operator, or zero if there isn't one.
*/
int
assignment_len(char_u *p, int *heredoc)
{
if (*p == '=')
{
if (p[1] == '<' && p[2] == '<')
{
*heredoc = TRUE;
return 3;
}
return 1;
}
if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
return 2;
if (STRNCMP(p, "..=", 3) == 0)
return 3;
return 0;
}
// words that cannot be used as a variable
static char *reserved[] = {
"true",
"false",
NULL
};
typedef enum {
dest_local,
dest_option,
dest_env,
dest_global,
dest_buffer,
dest_window,
dest_tab,
dest_vimvar,
dest_script,
dest_reg,
} assign_dest_T;
/*
* Generate the load instruction for "name".
*/
static void
generate_loadvar(
cctx_T *cctx,
assign_dest_T dest,
char_u *name,
lvar_T *lvar,
type_T *type)
{
switch (dest)
{
case dest_option:
// TODO: check the option exists
generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
break;
case dest_global:
generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
break;
case dest_buffer:
generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
break;
case dest_window:
generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
break;
case dest_tab:
generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
break;
case dest_script:
compile_load_scriptvar(cctx,
name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE);
break;
case dest_env:
// Include $ in the name here
generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
break;
case dest_reg:
generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
break;
case dest_vimvar:
generate_LOADV(cctx, name + 2, TRUE);
break;
case dest_local:
if (lvar->lv_from_outer)
generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx,
NULL, type);
else
generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
break;
}
}
void
vim9_declare_error(char_u *name)
{
char *scope = "";
switch (*name)
{
case 'g': scope = _("global"); break;
case 'b': scope = _("buffer"); break;
case 'w': scope = _("window"); break;
case 't': scope = _("tab"); break;
case 'v': scope = "v:"; break;
case '$': semsg(_(e_declare_env_var), name); return;
default: return;
}
semsg(_(e_declare_var), scope, name);
}
/*
* Compile declaration and assignment:
* "let var", "let var = expr", "const var = expr" and "var = expr"
* "arg" points to "var".
* Return NULL for an error.
* Return "arg" if it does not look like a variable list.
*/
static char_u *
compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
{
char_u *var_start;
char_u *p;
char_u *end = arg;
char_u *ret = NULL;
int var_count = 0;
int var_idx;
int semicolon = 0;
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
char_u *op;
int oplen = 0;
int heredoc = FALSE;
type_T *type = &t_any;
type_T *member_type = &t_any;
char_u *name = NULL;
char_u *sp;
int is_decl = cmdidx == CMD_let || cmdidx == CMD_const;
// Skip over the "var" or "[var, var]" to get to any "=".
p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE);
if (p == NULL)
return *arg == '[' ? arg : NULL;
if (var_count > 0 && is_decl)
{
emsg(_("E1092: Cannot use a list for a declaration"));
return NULL;
}
sp = p;
p = skipwhite(p);
op = p;
oplen = assignment_len(p, &heredoc);
if (var_count > 0 && oplen == 0)
// can be something like "[1, 2]->func()"
return arg;
if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
{
char_u buf[4];
vim_strncpy(buf, op, oplen);
semsg(_(e_white_both), buf);
return NULL;
}
if (heredoc)
{
list_T *l;
listitem_T *li;
// [let] varname =<< [trim] {end}
eap->getline = exarg_getline;
eap->cookie = cctx;
l = heredoc_get(eap, op + 3, FALSE);
// Push each line and the create the list.
FOR_ALL_LIST_ITEMS(l, li)
{
generate_PUSHS(cctx, li->li_tv.vval.v_string);
li->li_tv.vval.v_string = NULL;
}
generate_NEWLIST(cctx, l->lv_len);
type = &t_list_string;
member_type = &t_list_string;
list_free(l);
p += STRLEN(p);
end = p;
}
else if (var_count > 0)
{
// for "[var, var] = expr" evaluate the expression here, loop over the
// list of variables below.
p = skipwhite(op + oplen);
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
end = p;
if (cctx->ctx_skip != SKIP_YES)
{
type_T *stacktype;
stacktype = stack->ga_len == 0 ? &t_void
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (stacktype->tt_type == VAR_VOID)
{
emsg(_(e_cannot_use_void));
goto theend;
}
if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL)
goto theend;
// TODO: check the length of a constant list here
generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count,
semicolon);
}
}
/*
* Loop over variables in "[var, var] = expr".
* For "var = expr" and "let var: type" this is done only once.
*/
if (var_count > 0)
var_start = skipwhite(arg + 1); // skip over the "["
else
var_start = arg;
for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++)
{
char_u *var_end = skip_var_one(var_start, FALSE);
size_t varlen;
int new_local = FALSE;
int opt_type;
int opt_flags = 0;
assign_dest_T dest = dest_local;
int vimvaridx = -1;
lvar_T *lvar = NULL;
lvar_T arg_lvar;
int has_type = FALSE;
int has_index = FALSE;
int instr_count = -1;
p = (*var_start == '&' || *var_start == '$'
|| *var_start == '@') ? var_start + 1 : var_start;
p = to_name_end(p, TRUE);
// "a: type" is declaring variable "a" with a type, not "a:".
if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
--var_end;
if (is_decl && p == var_start + 2 && p[-1] == ':')
--p;
varlen = p - var_start;
vim_free(name);
name = vim_strnsave(var_start, varlen);
if (name == NULL)
return NULL;
if (!heredoc)
type = &t_any;
if (cctx->ctx_skip != SKIP_YES)
{
if (*var_start == '&')
{
int cc;
long numval;
dest = dest_option;
if (cmdidx == CMD_const)
{
emsg(_(e_const_option));
goto theend;
}
if (is_decl)
{
semsg(_("E1052: Cannot declare an option: %s"), var_start);
goto theend;
}
p = var_start;
p = find_option_end(&p, &opt_flags);
if (p == NULL)
{
// cannot happen?
emsg(_(e_letunexp));
goto theend;
}
cc = *p;
*p = NUL;
opt_type = get_option_value(var_start + 1, &numval,
NULL, opt_flags);
*p = cc;
if (opt_type == -3)
{
semsg(_(e_unknown_option), var_start);
goto theend;
}
if (opt_type == -2 || opt_type == 0)
type = &t_string;
else
type = &t_number; // both number and boolean option
}
else if (*var_start == '$')
{
dest = dest_env;
type = &t_string;
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else if (*var_start == '@')
{
if (!valid_yank_reg(var_start[1], TRUE))
{
emsg_invreg(var_start[1]);
goto theend;
}
dest = dest_reg;
type = &t_string;
if (is_decl)
{
semsg(_("E1066: Cannot declare a register: %s"), name);
goto theend;
}
}
else if (STRNCMP(var_start, "g:", 2) == 0)
{
dest = dest_global;
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else if (STRNCMP(var_start, "b:", 2) == 0)
{
dest = dest_buffer;
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else if (STRNCMP(var_start, "w:", 2) == 0)
{
dest = dest_window;
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else if (STRNCMP(var_start, "t:", 2) == 0)
{
dest = dest_tab;
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else if (STRNCMP(var_start, "v:", 2) == 0)
{
typval_T *vtv;
int di_flags;
vimvaridx = find_vim_var(name + 2, &di_flags);
if (vimvaridx < 0)
{
semsg(_(e_var_notfound), var_start);
goto theend;
}
// We use the current value of "sandbox" here, is that OK?
if (var_check_ro(di_flags, name, FALSE))
goto theend;
dest = dest_vimvar;
vtv = get_vim_var_tv(vimvaridx);
type = typval2type(vtv);
if (is_decl)
{
vim9_declare_error(name);
goto theend;
}
}
else
{
int idx;
for (idx = 0; reserved[idx] != NULL; ++idx)
if (STRCMP(reserved[idx], name) == 0)
{
semsg(_("E1034: Cannot use reserved name %s"), name);
goto theend;
}
lvar = lookup_local(var_start, varlen, cctx);
if (lvar == NULL)
{
CLEAR_FIELD(arg_lvar);
if (lookup_arg(var_start, varlen,
&arg_lvar.lv_idx, &arg_lvar.lv_type,
&arg_lvar.lv_from_outer, cctx) == OK)
{
if (is_decl)
{
semsg(_(e_used_as_arg), name);
goto theend;
}
lvar = &arg_lvar;
}
}
if (lvar != NULL)
{
if (is_decl)
{
semsg(_("E1017: Variable already declared: %s"), name);
goto theend;
}
else if (lvar->lv_const)
{
semsg(_("E1018: Cannot assign to a constant: %s"),
name);
goto theend;
}
}
else if (STRNCMP(var_start, "s:", 2) == 0
|| lookup_script(var_start, varlen) == OK
|| find_imported(var_start, varlen, cctx) != NULL)
{
dest = dest_script;
if (is_decl)
{
semsg(_("E1054: Variable already declared in the script: %s"),
name);
goto theend;
}
}
else if (name[1] == ':' && name[2] != NUL)
{
semsg(_("E1082: Cannot use a namespaced variable: %s"),
name);
goto theend;
}
else if (!is_decl)
{
semsg(_("E1089: unknown variable: %s"), name);
goto theend;
}
}
}
// handle "a:name" as a name, not index "name" on "a"
if (varlen > 1 || var_start[varlen] != ':')
p = var_end;
if (dest != dest_option)
{
if (is_decl && *p == ':')
{
// parse optional type: "let var: type = expr"
if (!VIM_ISWHITE(p[1]))
{
semsg(_(e_white_after), ":");
goto theend;
}
p = skipwhite(p + 1);
type = parse_type(&p, cctx->ctx_type_list);
has_type = TRUE;
}
else if (lvar != NULL)
type = lvar->lv_type;
}
if (oplen == 3 && !heredoc && dest != dest_global
&& type->tt_type != VAR_STRING
&& type->tt_type != VAR_ANY)
{
emsg(_("E1019: Can only concatenate to string"));
goto theend;
}
if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES)
{
if (oplen > 1 && !heredoc)
{
// +=, /=, etc. require an existing variable
semsg(_("E1020: cannot use an operator on a new variable: %s"),
name);
goto theend;
}
// new local variable
if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE))
goto theend;
lvar = reserve_local(cctx, var_start, varlen,
cmdidx == CMD_const, type);
if (lvar == NULL)
goto theend;
new_local = TRUE;
}
member_type = type;
if (var_end > var_start + varlen)
{
// Something follows after the variable: "var[idx]".
if (is_decl)
{
emsg(_("E1087: cannot use an index when declaring a variable"));
goto theend;
}
if (var_start[varlen] == '[')
{
has_index = TRUE;
if (type->tt_member == NULL)
{
semsg(_("E1088: cannot use an index on %s"), name);
goto theend;
}
member_type = type->tt_member;
}
else
{
semsg("Not supported yet: %s", var_start);
goto theend;
}
}
else if (lvar == &arg_lvar)
{
semsg(_("E1090: Cannot assign to argument %s"), name);
goto theend;
}
if (!heredoc)
{
if (cctx->ctx_skip == SKIP_YES)
{
if (oplen > 0 && var_count == 0)
{
// skip over the "=" and the expression
p = skipwhite(op + oplen);
compile_expr0(&p, cctx);
}
}
else if (oplen > 0)
{
type_T *stacktype;
// For "var = expr" evaluate the expression.
if (var_count == 0)
{
int r;
// for "+=", "*=", "..=" etc. first load the current value
if (*op != '=')
{
generate_loadvar(cctx, dest, name, lvar, type);
if (has_index)
{
// TODO: get member from list or dict
emsg("Index with operation not supported yet");
goto theend;
}
}
// Compile the expression. Temporarily hide the new local
// variable here, it is not available to this expression.
if (new_local)
--cctx->ctx_locals.ga_len;
instr_count = instr->ga_len;
p = skipwhite(op + oplen);
r = compile_expr0(&p, cctx);
if (new_local)
++cctx->ctx_locals.ga_len;
if (r == FAIL)
goto theend;
}
else if (semicolon && var_idx == var_count - 1)
{
// For "[var; var] = expr" get the rest of the list
if (generate_SLICE(cctx, var_count - 1) == FAIL)
goto theend;
}
else
{
// For "[var, var] = expr" get the "var_idx" item from the
// list.
if (generate_GETITEM(cctx, var_idx) == FAIL)
return FAIL;
}
stacktype = stack->ga_len == 0 ? &t_void
: ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (lvar != NULL && (is_decl || !has_type))
{
if (new_local && !has_type)
{
if (stacktype->tt_type == VAR_VOID)
{
emsg(_(e_cannot_use_void));
goto theend;
}
else
{
// An empty list or dict has a &t_void member,
// for a variable that implies &t_any.
if (stacktype == &t_list_empty)
lvar->lv_type = &t_list_any;
else if (stacktype == &t_dict_empty)
lvar->lv_type = &t_dict_any;
else
lvar->lv_type = stacktype;
}
}
else
{
type_T *use_type = lvar->lv_type;
if (has_index)
{
use_type = use_type->tt_member;
if (use_type == NULL)
use_type = &t_void;
}
if (need_type(stacktype, use_type, -1, cctx, FALSE)
== FAIL)
goto theend;
}
}
else if (*p != '=' && need_type(stacktype, member_type, -1,
cctx, FALSE) == FAIL)
goto theend;
}
else if (cmdidx == CMD_const)
{
emsg(_(e_const_req_value));
goto theend;
}
else if (!has_type || dest == dest_option)
{
emsg(_(e_type_req));
goto theend;
}
else
{
// variables are always initialized
if (ga_grow(instr, 1) == FAIL)
goto theend;
switch (member_type->tt_type)
{
case VAR_BOOL:
generate_PUSHBOOL(cctx, VVAL_FALSE);
break;
case VAR_FLOAT:
#ifdef FEAT_FLOAT
generate_PUSHF(cctx, 0.0);
#endif
break;
case VAR_STRING:
generate_PUSHS(cctx, NULL);
break;
case VAR_BLOB:
generate_PUSHBLOB(cctx, NULL);
break;
case VAR_FUNC:
generate_PUSHFUNC(cctx, NULL, &t_func_void);
break;
case VAR_LIST:
generate_NEWLIST(cctx, 0);
break;
case VAR_DICT:
generate_NEWDICT(cctx, 0);
break;
case VAR_JOB:
generate_PUSHJOB(cctx, NULL);
break;
case VAR_CHANNEL:
generate_PUSHCHANNEL(cctx, NULL);
break;
case VAR_NUMBER:
case VAR_UNKNOWN:
case VAR_ANY:
case VAR_PARTIAL:
case VAR_VOID:
case VAR_SPECIAL: // cannot happen
generate_PUSHNR(cctx, 0);
break;
}
}
if (var_count == 0)
end = p;
}
// no need to parse more when skipping
if (cctx->ctx_skip == SKIP_YES)
break;
if (oplen > 0 && *op != '=')
{
type_T *expected = &t_number;
type_T *stacktype;
// TODO: if type is known use float or any operation
if (*op == '.')
expected = &t_string;
stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL)
goto theend;
if (*op == '.')
generate_instr_drop(cctx, ISN_CONCAT, 1);
else
{
isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1);
if (isn == NULL)
goto theend;
switch (*op)
{
case '+': isn->isn_arg.op.op_type = EXPR_ADD; break;
case '-': isn->isn_arg.op.op_type = EXPR_SUB; break;
case '*': isn->isn_arg.op.op_type = EXPR_MULT; break;
case '/': isn->isn_arg.op.op_type = EXPR_DIV; break;
case '%': isn->isn_arg.op.op_type = EXPR_REM; break;
}
}
}
if (has_index)
{
int r;
// Compile the "idx" in "var[idx]".
if (new_local)
--cctx->ctx_locals.ga_len;
p = skipwhite(var_start + varlen + 1);
r = compile_expr0(&p, cctx);
if (new_local)
++cctx->ctx_locals.ga_len;
if (r == FAIL)
goto theend;
if (*skipwhite(p) != ']')
{
emsg(_(e_missbrac));
goto theend;
}
if (type->tt_type == VAR_DICT
&& may_generate_2STRING(-1, cctx) == FAIL)
goto theend;
if (type->tt_type == VAR_LIST
&& ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type
!= VAR_NUMBER)
{
emsg(_(e_number_exp));
goto theend;
}
// Load the dict or list. On the stack we then have:
// - value
// - index
// - variable
generate_loadvar(cctx, dest, name, lvar, type);
if (type->tt_type == VAR_LIST)
{
if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL)
return FAIL;
}
else if (type->tt_type == VAR_DICT)
{
if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL)
return FAIL;
}
else
{
emsg(_(e_listreq));
goto theend;
}
}
else
{
switch (dest)
{
case dest_option:
generate_STOREOPT(cctx, name + 1, opt_flags);
break;
case dest_global:
// include g: with the name, easier to execute that way
generate_STORE(cctx, ISN_STOREG, 0, name);
break;
case dest_buffer:
// include b: with the name, easier to execute that way
generate_STORE(cctx, ISN_STOREB, 0, name);
break;
case dest_window:
// include w: with the name, easier to execute that way
generate_STORE(cctx, ISN_STOREW, 0, name);
break;
case dest_tab:
// include t: with the name, easier to execute that way
generate_STORE(cctx, ISN_STORET, 0, name);
break;
case dest_env:
generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
break;
case dest_reg:
generate_STORE(cctx, ISN_STOREREG, name[1], NULL);
break;
case dest_vimvar:
generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
break;
case dest_script:
{
char_u *rawname = name + (name[1] == ':' ? 2 : 0);
imported_T *import = NULL;
int sid = current_sctx.sc_sid;
int idx;
if (name[1] != ':')
{
import = find_imported(name, 0, cctx);
if (import != NULL)
sid = import->imp_sid;
}
idx = get_script_item_idx(sid, rawname, TRUE);
// TODO: specific type
if (idx < 0)
{
char_u *name_s = name;
// Include s: in the name for store_var()
if (name[1] != ':')
{
int len = (int)STRLEN(name) + 3;
name_s = alloc(len);
if (name_s == NULL)
name_s = name;
else
vim_snprintf((char *)name_s, len,
"s:%s", name);
}
generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid,
&t_any);
if (name_s != name)
vim_free(name_s);
}
else
generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
sid, idx, &t_any);
}
break;
case dest_local:
if (lvar != NULL)
{
isn_T *isn = ((isn_T *)instr->ga_data)
+ instr->ga_len - 1;
// optimization: turn "var = 123" from ISN_PUSHNR +
// ISN_STORE into ISN_STORENR
if (!lvar->lv_from_outer
&& instr->ga_len == instr_count + 1
&& isn->isn_type == ISN_PUSHNR)
{
varnumber_T val = isn->isn_arg.number;
isn->isn_type = ISN_STORENR;
isn->isn_arg.storenr.stnr_idx = lvar->lv_idx;
isn->isn_arg.storenr.stnr_val = val;
if (stack->ga_len > 0)
--stack->ga_len;
}
else if (lvar->lv_from_outer)
generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx,
NULL);
else
generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
}
break;
}
}
if (var_idx + 1 < var_count)
var_start = skipwhite(var_end + 1);
}
// for "[var, var] = expr" drop the "expr" value
if (var_count > 0 && !semicolon)
{
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
goto theend;
}
ret = end;
theend:
vim_free(name);
return ret;
}
/*
* Check if "name" can be "unlet".
*/
int
check_vim9_unlet(char_u *name)
{
if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL)
{
semsg(_("E1081: Cannot unlet %s"), name);
return FAIL;
}
return OK;
}
/*
* Callback passed to ex_unletlock().
*/
static int
compile_unlet(
lval_T *lvp,
char_u *name_end,
exarg_T *eap,
int deep UNUSED,
void *coookie)
{
cctx_T *cctx = coookie;
if (lvp->ll_tv == NULL)
{
char_u *p = lvp->ll_name;
int cc = *name_end;
int ret = OK;
// Normal name. Only supports g:, w:, t: and b: namespaces.
*name_end = NUL;
if (*p == '$')
ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit);
else if (check_vim9_unlet(p) == FAIL)
ret = FAIL;
else
ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit);
*name_end = cc;
return ret;
}
// TODO: unlet {list}[idx]
// TODO: unlet {dict}[key]
emsg("Sorry, :unlet not fully implemented yet");
return FAIL;
}
/*
* compile "unlet var", "lock var" and "unlock var"
* "arg" points to "var".
*/
static char_u *
compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
{
char_u *p = arg;
if (eap->cmdidx != CMD_unlet)
{
emsg("Sorry, :lock and unlock not implemented yet");
return NULL;
}
if (*p == '!')
{
p = skipwhite(p + 1);
eap->forceit = TRUE;
}
ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx);
return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
}
/*
* Compile an :import command.
*/
static char_u *
compile_import(char_u *arg, cctx_T *cctx)
{
return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx);
}
/*
* generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry".
*/
static int
compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T);
if (endlabel == NULL)
return FAIL;
endlabel->el_next = *el;
*el = endlabel;
endlabel->el_end_label = instr->ga_len;
generate_JUMP(cctx, when, 0);
return OK;
}
static void
compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
while (*el != NULL)
{
endlabel_T *cur = (*el);
isn_T *isn;
isn = ((isn_T *)instr->ga_data) + cur->el_end_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
*el = cur->el_next;
vim_free(cur);
}
}
static void
compile_free_jump_to_end(endlabel_T **el)
{
while (*el != NULL)
{
endlabel_T *cur = (*el);
*el = cur->el_next;
vim_free(cur);
}
}
/*
* Create a new scope and set up the generic items.
*/
static scope_T *
new_scope(cctx_T *cctx, scopetype_T type)
{
scope_T *scope = ALLOC_CLEAR_ONE(scope_T);
if (scope == NULL)
return NULL;
scope->se_outer = cctx->ctx_scope;
cctx->ctx_scope = scope;
scope->se_type = type;
scope->se_local_count = cctx->ctx_locals.ga_len;
return scope;
}
/*
* Free the current scope and go back to the outer scope.
*/
static void
drop_scope(cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
if (scope == NULL)
{
iemsg("calling drop_scope() without a scope");
return;
}
cctx->ctx_scope = scope->se_outer;
switch (scope->se_type)
{
case IF_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break;
case FOR_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break;
case WHILE_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break;
case TRY_SCOPE:
compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break;
case NO_SCOPE:
case BLOCK_SCOPE:
break;
}
vim_free(scope);
}
/*
* compile "if expr"
*
* "if expr" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE end
* ... body ...
* end:
*
* "if expr | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*
* "if expr1 | elseif expr2 | else" Produces instructions:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE elseif
* ... body ...
* JUMP_ALWAYS end
* elseif:
* EVAL expr Push result of "expr"
* JUMP_IF_FALSE else
* ... body ...
* JUMP_ALWAYS end
* else:
* ... body ...
* end:
*/
static char_u *
compile_if(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count = instr->ga_len;
scope_T *scope;
skip_T skip_save = cctx->ctx_skip;
ppconst_T ppconst;
CLEAR_FIELD(ppconst);
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
if (cctx->ctx_skip == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
// The expression results in a constant.
cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
clear_ppconst(&ppconst);
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
}
scope = new_scope(cctx, IF_SCOPE);
if (scope == NULL)
return NULL;
scope->se_skip_save = skip_save;
// "is_had_return" will be reset if any block does not end in :return
scope->se_u.se_if.is_had_return = TRUE;
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
else
scope->se_u.se_if.is_if_label = -1;
return p;
}
static char_u *
compile_elseif(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
int instr_count = instr->ga_len;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
ppconst_T ppconst;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_elseif_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
// compile "expr"; if we know it evaluates to FALSE skip the block
CLEAR_FIELD(ppconst);
if (compile_expr1(&p, cctx, &ppconst) == FAIL)
{
clear_ppconst(&ppconst);
return NULL;
}
if (scope->se_skip_save == SKIP_YES)
clear_ppconst(&ppconst);
else if (instr->ga_len == instr_count && ppconst.pp_used == 1)
{
// The expression results in a constant.
// TODO: how about nesting?
cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES;
clear_ppconst(&ppconst);
scope->se_u.se_if.is_if_label = -1;
}
else
{
// Not a constant, generate instructions for the expression.
cctx->ctx_skip = SKIP_UNKNOWN;
if (generate_ppconst(cctx, &ppconst) == FAIL)
return NULL;
// "where" is set when ":elseif", "else" or ":endif" is found
scope->se_u.se_if.is_if_label = instr->ga_len;
generate_JUMP(cctx, JUMP_IF_FALSE, 0);
}
return p;
}
static char_u *
compile_else(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
scope_T *scope = cctx->ctx_scope;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_else_without_if));
return NULL;
}
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
scope->se_u.se_if.is_had_return = FALSE;
scope->se_u.se_if.is_seen_else = TRUE;
if (scope->se_skip_save != SKIP_YES)
{
// jump from previous block to the end, unless the else block is empty
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (!cctx->ctx_had_return
&& compile_jump_to_end(&scope->se_u.se_if.is_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
}
if (cctx->ctx_skip == SKIP_UNKNOWN)
{
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
scope->se_u.se_if.is_if_label = -1;
}
}
if (cctx->ctx_skip != SKIP_UNKNOWN)
cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES;
}
return p;
}
static char_u *
compile_endif(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
ifscope_T *ifscope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
if (scope == NULL || scope->se_type != IF_SCOPE)
{
emsg(_(e_endif_without_if));
return NULL;
}
ifscope = &scope->se_u.se_if;
unwind_locals(cctx, scope->se_local_count);
if (!cctx->ctx_had_return)
ifscope->is_had_return = FALSE;
if (scope->se_u.se_if.is_if_label >= 0)
{
// previous "if" or "elseif" jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&ifscope->is_end_label, cctx);
cctx->ctx_skip = scope->se_skip_save;
// If all the blocks end in :return and there is an :else then the
// had_return flag is set.
cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else;
drop_scope(cctx);
return arg;
}
/*
* compile "for var in expr"
*
* Produces instructions:
* PUSHNR -1
* STORE loop-idx Set index to -1
* EVAL expr Push result of "expr"
* top: FOR loop-idx, end Increment index, use list on bottom of stack
* - if beyond end, jump to "end"
* - otherwise get item from list and push it
* STORE var Store item in "var"
* ... body ...
* JUMP top Jump back to repeat
* end: DROP Drop the result of "expr"
*
*/
static char_u *
compile_for(char_u *arg, cctx_T *cctx)
{
char_u *p;
size_t varlen;
garray_T *instr = &cctx->ctx_instr;
garray_T *stack = &cctx->ctx_type_stack;
scope_T *scope;
lvar_T *loop_lvar; // loop iteration variable
lvar_T *var_lvar; // variable for "var"
type_T *vartype;
// TODO: list of variables: "for [key, value] in dict"
// parse "var"
for (p = arg; eval_isnamec1(*p); ++p)
;
varlen = p - arg;
var_lvar = lookup_local(arg, varlen, cctx);
if (var_lvar != NULL)
{
semsg(_("E1023: variable already defined: %s"), arg);
return NULL;
}
// consume "in"
p = skipwhite(p);
if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2]))
{
emsg(_(e_missing_in));
return NULL;
}
p = skipwhite(p + 2);
scope = new_scope(cctx, FOR_SCOPE);
if (scope == NULL)
return NULL;
// Reserve a variable to store the loop iteration counter.
loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number);
if (loop_lvar == NULL)
{
// out of memory
drop_scope(cctx);
return NULL;
}
// Reserve a variable to store "var"
var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);
if (var_lvar == NULL)
{
// out of memory or used as an argument
drop_scope(cctx);
return NULL;
}
generate_STORENR(cctx, loop_lvar->lv_idx, -1);
// compile "expr", it remains on the stack until "endfor"
arg = p;
if (compile_expr0(&arg, cctx) == FAIL)
{
drop_scope(cctx);
return NULL;
}
// Now that we know the type of "var", check that it is a list, now or at
// runtime.
vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL)
{
drop_scope(cctx);
return NULL;
}
if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY)
var_lvar->lv_type = vartype->tt_member;
// "for_end" is set when ":endfor" is found
scope->se_u.se_for.fs_top_label = instr->ga_len;
generate_FOR(cctx, loop_lvar->lv_idx);
generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL);
return arg;
}
/*
* compile "endfor"
*/
static char_u *
compile_endfor(char_u *arg, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
scope_T *scope = cctx->ctx_scope;
forscope_T *forscope;
isn_T *isn;
if (scope == NULL || scope->se_type != FOR_SCOPE)
{
emsg(_(e_for));
return NULL;
}
forscope = &scope->se_u.se_for;
cctx->ctx_scope = scope->se_outer;
unwind_locals(cctx, scope->se_local_count);
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label);
// Fill in the "end" label in the FOR statement so it can jump here
isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label;
isn->isn_arg.forloop.for_end = instr->ga_len;
// Fill in the "end" label any BREAK statements
compile_fill_jump_to_end(&forscope->fs_end_label, cctx);
// Below the ":for" scope drop the "expr" list from the stack.
if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
return NULL;
vim_free(scope);
return arg;
}
/*
* compile "while expr"
*
* Produces instructions:
* top: EVAL expr Push result of "expr"
* JUMP_IF_FALSE end jump if false
* ... body ...
* JUMP top Jump back to repeat
* end:
*
*/
static char_u *
compile_while(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
garray_T *instr = &cctx->ctx_instr;
scope_T *scope;
scope = new_scope(cctx, WHILE_SCOPE);
if (scope == NULL)
return NULL;
scope->se_u.se_while.ws_top_label = instr->ga_len;
// compile "expr"
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
// "while_end" is set when ":endwhile" is found
if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label,
JUMP_IF_FALSE, cctx) == FAIL)
return FAIL;
return p;
}
/*
* compile "endwhile"
*/
static char_u *
compile_endwhile(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
if (scope == NULL || scope->se_type != WHILE_SCOPE)
{
emsg(_(e_while));
return NULL;
}
cctx->ctx_scope = scope->se_outer;
unwind_locals(cctx, scope->se_local_count);
// At end of ":for" scope jump back to the FOR instruction.
generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label);
// Fill in the "end" label in the WHILE statement so it can jump here.
// And in any jumps for ":break"
compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx);
vim_free(scope);
return arg;
}
/*
* compile "continue"
*/
static char_u *
compile_continue(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
for (;;)
{
if (scope == NULL)
{
emsg(_(e_continue));
return NULL;
}
if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
break;
scope = scope->se_outer;
}
// Jump back to the FOR or WHILE instruction.
generate_JUMP(cctx, JUMP_ALWAYS,
scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label
: scope->se_u.se_while.ws_top_label);
return arg;
}
/*
* compile "break"
*/
static char_u *
compile_break(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
endlabel_T **el;
for (;;)
{
if (scope == NULL)
{
emsg(_(e_break));
return NULL;
}
if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
break;
scope = scope->se_outer;
}
// Jump to the end of the FOR or WHILE loop.
if (scope->se_type == FOR_SCOPE)
el = &scope->se_u.se_for.fs_end_label;
else
el = &scope->se_u.se_while.ws_end_label;
if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL)
return FAIL;
return arg;
}
/*
* compile "{" start of block
*/
static char_u *
compile_block(char_u *arg, cctx_T *cctx)
{
if (new_scope(cctx, BLOCK_SCOPE) == NULL)
return NULL;
return skipwhite(arg + 1);
}
/*
* compile end of block: drop one scope
*/
static void
compile_endblock(cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
cctx->ctx_scope = scope->se_outer;
unwind_locals(cctx, scope->se_local_count);
vim_free(scope);
}
/*
* compile "try"
* Creates a new scope for the try-endtry, pointing to the first catch and
* finally.
* Creates another scope for the "try" block itself.
* TRY instruction sets up exception handling at runtime.
*
* "try"
* TRY -> catch1, -> finally push trystack entry
* ... try block
* "throw {exception}"
* EVAL {exception}
* THROW create exception
* ... try block
* " catch {expr}"
* JUMP -> finally
* catch1: PUSH exeception
* EVAL {expr}
* MATCH
* JUMP nomatch -> catch2
* CATCH remove exception
* ... catch block
* " catch"
* JUMP -> finally
* catch2: CATCH remove exception
* ... catch block
* " finally"
* finally:
* ... finally block
* " endtry"
* ENDTRY pop trystack entry, may rethrow
*/
static char_u *
compile_try(char_u *arg, cctx_T *cctx)
{
garray_T *instr = &cctx->ctx_instr;
scope_T *try_scope;
scope_T *scope;
// scope that holds the jumps that go to catch/finally/endtry
try_scope = new_scope(cctx, TRY_SCOPE);
if (try_scope == NULL)
return NULL;
// "catch" is set when the first ":catch" is found.
// "finally" is set when ":finally" or ":endtry" is found
try_scope->se_u.se_try.ts_try_label = instr->ga_len;
if (generate_instr(cctx, ISN_TRY) == NULL)
return NULL;
// scope for the try block itself
scope = new_scope(cctx, BLOCK_SCOPE);
if (scope == NULL)
return NULL;
return arg;
}
/*
* compile "catch {expr}"
*/
static char_u *
compile_catch(char_u *arg, cctx_T *cctx UNUSED)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
char_u *p;
isn_T *isn;
// end block scope from :try or :catch
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
emsg(_(e_catch));
return NULL;
}
if (scope->se_u.se_try.ts_caught_all)
{
emsg(_("E1033: catch unreachable after catch-all"));
return NULL;
}
// Jump from end of previous block to :finally or :endtry
if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label,
JUMP_ALWAYS, cctx) == FAIL)
return NULL;
// End :try or :catch scope: set value in ISN_TRY instruction
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (isn->isn_arg.try.try_catch == 0)
isn->isn_arg.try.try_catch = instr->ga_len;
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Previous catch without match jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
p = skipwhite(arg);
if (ends_excmd2(arg, p))
{
scope->se_u.se_try.ts_caught_all = TRUE;
scope->se_u.se_try.ts_catch_label = 0;
}
else
{
char_u *end;
char_u *pat;
char_u *tofree = NULL;
int dropped = 0;
int len;
// Push v:exception, push {expr} and MATCH
generate_instr_type(cctx, ISN_PUSHEXC, &t_string);
end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped);
if (*end != *p)
{
semsg(_("E1067: Separator mismatch: %s"), p);
vim_free(tofree);
return FAIL;
}
if (tofree == NULL)
len = (int)(end - (p + 1));
else
len = (int)(end - tofree);
pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len);
vim_free(tofree);
p += len + 2 + dropped;
if (pat == NULL)
return FAIL;
if (generate_PUSHS(cctx, pat) == FAIL)
return FAIL;
if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL)
return NULL;
scope->se_u.se_try.ts_catch_label = instr->ga_len;
if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL)
return NULL;
}
if (generate_instr(cctx, ISN_CATCH) == NULL)
return NULL;
if (new_scope(cctx, BLOCK_SCOPE) == NULL)
return NULL;
return p;
}
static char_u *
compile_finally(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
// end block scope from :try or :catch
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
emsg(_(e_finally));
return NULL;
}
// End :catch or :finally scope: set value in ISN_TRY instruction
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (isn->isn_arg.try.try_finally != 0)
{
emsg(_(e_finally_dup));
return NULL;
}
// Fill in the "end" label in jumps at the end of the blocks.
compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
isn->isn_arg.try.try_finally = instr->ga_len;
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Previous catch without match jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
scope->se_u.se_try.ts_catch_label = 0;
}
// TODO: set index in ts_finally_label jumps
return arg;
}
static char_u *
compile_endtry(char_u *arg, cctx_T *cctx)
{
scope_T *scope = cctx->ctx_scope;
garray_T *instr = &cctx->ctx_instr;
isn_T *isn;
// end block scope from :catch or :finally
if (scope != NULL && scope->se_type == BLOCK_SCOPE)
compile_endblock(cctx);
scope = cctx->ctx_scope;
// Error if not in a :try scope
if (scope == NULL || scope->se_type != TRY_SCOPE)
{
if (scope == NULL)
emsg(_(e_no_endtry));
else if (scope->se_type == WHILE_SCOPE)
emsg(_(e_endwhile));
else if (scope->se_type == FOR_SCOPE)
emsg(_(e_endfor));
else
emsg(_(e_endif));
return NULL;
}
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label;
if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0)
{
emsg(_("E1032: missing :catch or :finally"));
return NULL;
}
// Fill in the "end" label in jumps at the end of the blocks, if not done
// by ":finally".
compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx);
// End :catch or :finally scope: set value in ISN_TRY instruction
if (isn->isn_arg.try.try_catch == 0)
isn->isn_arg.try.try_catch = instr->ga_len;
if (isn->isn_arg.try.try_finally == 0)
isn->isn_arg.try.try_finally = instr->ga_len;
if (scope->se_u.se_try.ts_catch_label != 0)
{
// Last catch without match jumps here
isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label;
isn->isn_arg.jump.jump_where = instr->ga_len;
}
compile_endblock(cctx);
if (generate_instr(cctx, ISN_ENDTRY) == NULL)
return NULL;
return arg;
}
/*
* compile "throw {expr}"
*/
static char_u *
compile_throw(char_u *arg, cctx_T *cctx UNUSED)
{
char_u *p = skipwhite(arg);
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (may_generate_2STRING(-1, cctx) == FAIL)
return NULL;
if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL)
return NULL;
return p;
}
/*
* compile "echo expr"
* compile "echomsg expr"
* compile "echoerr expr"
* compile "execute expr"
*/
static char_u *
compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx)
{
char_u *p = arg;
int count = 0;
for (;;)
{
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
++count;
p = skipwhite(p);
if (ends_excmd(*p))
break;
}
if (cmdidx == CMD_echo || cmdidx == CMD_echon)
generate_ECHO(cctx, cmdidx == CMD_echo, count);
else if (cmdidx == CMD_execute)
generate_MULT_EXPR(cctx, ISN_EXECUTE, count);
else if (cmdidx == CMD_echomsg)
generate_MULT_EXPR(cctx, ISN_ECHOMSG, count);
else
generate_MULT_EXPR(cctx, ISN_ECHOERR, count);
return p;
}
/*
* A command that is not compiled, execute with legacy code.
*/
static char_u *
compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx)
{
char_u *p;
int has_expr = FALSE;
char_u *nextcmd = (char_u *)"";
if (cctx->ctx_skip == SKIP_YES)
goto theend;
if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE)
{
long argt = excmd_get_argt(eap->cmdidx);
int usefilter = FALSE;
has_expr = argt & (EX_XFILE | EX_EXPAND);
// If the command can be followed by a bar, find the bar and truncate
// it, so that the following command can be compiled.
// The '|' is overwritten with a NUL, it is put back below.
if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read)
&& *eap->arg == '!')
// :w !filter or :r !filter or :r! filter
usefilter = TRUE;
if ((argt & EX_TRLBAR) && !usefilter)
{
separate_nextcmd(eap);
if (eap->nextcmd != NULL)
nextcmd = eap->nextcmd;
}
}
if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0)
{
// expand filename in "syntax include [@group] filename"
has_expr = TRUE;
eap->arg = skipwhite(eap->arg + 7);
if (*eap->arg == '@')
eap->arg = skiptowhite(eap->arg);
}
if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL)
{
int count = 0;
char_u *start = skipwhite(line);
// :cmd xxx`=expr1`yyy`=expr2`zzz
// PUSHS ":cmd xxx"
// eval expr1
// PUSHS "yyy"
// eval expr2
// PUSHS "zzz"
// EXECCONCAT 5
for (;;)
{
if (p > start)
{
generate_PUSHS(cctx, vim_strnsave(start, p - start));
++count;
}
p += 2;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
may_generate_2STRING(-1, cctx);
++count;
p = skipwhite(p);
if (*p != '`')
{
emsg(_("E1083: missing backtick"));
return NULL;
}
start = p + 1;
p = (char_u *)strstr((char *)start, "`=");
if (p == NULL)
{
if (*skipwhite(start) != NUL)
{
generate_PUSHS(cctx, vim_strsave(start));
++count;
}
break;
}
}
generate_EXECCONCAT(cctx, count);
}
else
generate_EXEC(cctx, line);
theend:
if (*nextcmd != NUL)
{
// the parser expects a pointer to the bar, put it back
--nextcmd;
*nextcmd = '|';
}
return nextcmd;
}
/*
* Add a function to the list of :def functions.
* This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
*/
static int
add_def_function(ufunc_T *ufunc)
{
dfunc_T *dfunc;
if (def_functions.ga_len == 0)
{
// The first position is not used, so that a zero uf_dfunc_idx means it
// wasn't set.
if (ga_grow(&def_functions, 1) == FAIL)
return FAIL;
++def_functions.ga_len;
}
// Add the function to "def_functions".
if (ga_grow(&def_functions, 1) == FAIL)
return FAIL;
dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
CLEAR_POINTER(dfunc);
dfunc->df_idx = def_functions.ga_len;
ufunc->uf_dfunc_idx = dfunc->df_idx;
dfunc->df_ufunc = ufunc;
++def_functions.ga_len;
return OK;
}
/*
* After ex_function() has collected all the function lines: parse and compile
* the lines into instructions.
* Adds the function to "def_functions".
* When "set_return_type" is set then set ufunc->uf_ret_type to the type of the
* return statement (used for lambda).
* "outer_cctx" is set for a nested function.
* This can be used recursively through compile_lambda(), which may reallocate
* "def_functions".
* Returns OK or FAIL.
*/
int
compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx)
{
char_u *line = NULL;
char_u *p;
char *errormsg = NULL; // error message
cctx_T cctx;
garray_T *instr;
int called_emsg_before = called_emsg;
int ret = FAIL;
sctx_T save_current_sctx = current_sctx;
int do_estack_push;
int emsg_before = called_emsg;
// When using a function that was compiled before: Free old instructions.
// Otherwise add a new entry in "def_functions".
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
delete_def_function_contents(dfunc);
}
else if (add_def_function(ufunc) == FAIL)
return FAIL;
ufunc->uf_def_status = UF_COMPILING;
CLEAR_FIELD(cctx);
cctx.ctx_ufunc = ufunc;
cctx.ctx_lnum = -1;
cctx.ctx_outer = outer_cctx;
ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50);
ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
cctx.ctx_type_list = &ufunc->uf_type_list;
ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
instr = &cctx.ctx_instr;
// Set the context to the function, it may be compiled when called from
// another script. Set the script version to the most modern one.
// The line number will be set in next_line_from_context().
current_sctx = ufunc->uf_script_ctx;
current_sctx.sc_version = SCRIPT_VERSION_VIM9;
// Make sure error messages are OK.
do_estack_push = !estack_top_is_ufunc(ufunc, 1);
if (do_estack_push)
estack_push_ufunc(ufunc, 1);
if (ufunc->uf_def_args.ga_len > 0)
{
int count = ufunc->uf_def_args.ga_len;
int first_def_arg = ufunc->uf_args.ga_len - count;
int i;
char_u *arg;
int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
// Produce instructions for the default values of optional arguments.
// Store the instruction index in uf_def_arg_idx[] so that we know
// where to start when the function is called, depending on the number
// of arguments.
ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1);
if (ufunc->uf_def_arg_idx == NULL)
goto erret;
for (i = 0; i < count; ++i)
{
garray_T *stack = &cctx.ctx_type_stack;
type_T *val_type;
int arg_idx = first_def_arg + i;
ufunc->uf_def_arg_idx[i] = instr->ga_len;
arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
if (compile_expr0(&arg, &cctx) == FAIL)
goto erret;
// If no type specified use the type of the default value.
// Otherwise check that the default value type matches the
// specified type.
val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
ufunc->uf_arg_types[arg_idx] = val_type;
else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE)
== FAIL)
{
arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type,
arg_idx + 1);
goto erret;
}
if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL)
goto erret;
}
ufunc->uf_def_arg_idx[count] = instr->ga_len;
}
/*
* Loop over all the lines of the function and generate instructions.
*/
for (;;)
{
exarg_T ea;
int starts_with_colon = FALSE;
char_u *cmd;
int save_msg_scroll = msg_scroll;
// Bail out on the first error to avoid a flood of errors and report
// the right line number when inside try/catch.
if (emsg_before != called_emsg)
goto erret;
if (line != NULL && *line == '|')
// the line continues after a '|'
++line;
else if (line != NULL && *skipwhite(line) != NUL
&& !(*line == '#' && (line == cctx.ctx_line_start
|| VIM_ISWHITE(line[-1]))))
{
semsg(_("E488: Trailing characters: %s"), line);
goto erret;
}
else
{
line = next_line_from_context(&cctx, FALSE);
if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len)
// beyond the last line
break;
}
emsg_before = called_emsg;
CLEAR_FIELD(ea);
ea.cmdlinep = &line;
ea.cmd = skipwhite(line);
// Some things can be recognized by the first character.
switch (*ea.cmd)
{
case '#':
// "#" starts a comment, but "#{" does not.
if (ea.cmd[1] != '{')
{
line = (char_u *)"";
continue;
}
break;
case '}':
{
// "}" ends a block scope
scopetype_T stype = cctx.ctx_scope == NULL
? NO_SCOPE : cctx.ctx_scope->se_type;
if (stype == BLOCK_SCOPE)
{
compile_endblock(&cctx);
line = ea.cmd;
}
else
{
emsg(_("E1025: using } outside of a block scope"));
goto erret;
}
if (line != NULL)
line = skipwhite(ea.cmd + 1);
continue;
}
case '{':
// "{" starts a block scope
// "{'a': 1}->func() is something else
if (ends_excmd(*skipwhite(ea.cmd + 1)))
{
line = compile_block(ea.cmd, &cctx);
continue;
}
break;
case ':':
starts_with_colon = TRUE;
break;
}
/*
* COMMAND MODIFIERS
*/
if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
{
if (errormsg != NULL)
goto erret;
// empty line or comment
line = (char_u *)"";
continue;
}
// TODO: use modifiers in the command
undo_cmdmod(&ea, save_msg_scroll);
CLEAR_FIELD(cmdmod);
// Skip ":call" to get to the function name.
if (checkforcmd(&ea.cmd, "call", 3))
ea.cmd = skipwhite(ea.cmd);
if (!starts_with_colon)
{
char_u *pskip;
// Assuming the command starts with a variable or function name,
// find what follows.
// Skip over "var.member", "var[idx]" and the like.
// Also "&opt = val", "$ENV = val" and "@r = val".
pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
? ea.cmd + 1 : ea.cmd;
p = to_name_end(pskip, TRUE);
if (p > ea.cmd && *p != NUL)
{
char_u *var_end;
int oplen;
int heredoc;
var_end = find_name_end(pskip, NULL, NULL,
FNE_CHECK_START | FNE_INCL_BR);
oplen = assignment_len(skipwhite(var_end), &heredoc);
if (oplen > 0)
{
size_t len = p - ea.cmd;
// Recognize an assignment if we recognize the variable
// name:
// "g:var = expr"
// "local = expr" where "local" is a local var.
// "script = expr" where "script" is a script-local var.
// "import = expr" where "import" is an imported var
// "&opt = expr"
// "$ENV = expr"
// "@r = expr"
if (*ea.cmd == '&'
|| *ea.cmd == '$'
|| *ea.cmd == '@'
|| ((len) > 2 && ea.cmd[1] == ':')
|| lookup_local(ea.cmd, len, &cctx) != NULL
|| lookup_arg(ea.cmd, len, NULL, NULL,
NULL, &cctx) == OK
|| lookup_script(ea.cmd, len) == OK
|| find_imported(ea.cmd, len, &cctx) != NULL)
{
line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
if (line == NULL || line == ea.cmd)
goto erret;
continue;
}
}
}
if (*ea.cmd == '[')
{
// [var, var] = expr
line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx);
if (line == NULL)
goto erret;
if (line != ea.cmd)
continue;
}
}
/*
* COMMAND after range
* 'text'->func() should not be confused with 'a mark
*/
cmd = ea.cmd;
if (*cmd != '\'')
{
ea.cmd = skip_range(ea.cmd, NULL);
if (ea.cmd > cmd && !starts_with_colon)
{
emsg(_(e_colon_required));
goto erret;
}
}
p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
: (void *(*)(char_u *, size_t, cctx_T *))lookup_local,
&cctx);
if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
{
if (cctx.ctx_skip == SKIP_YES)
{
line += STRLEN(line);
continue;
}
// Expression or function call.
if (ea.cmdidx != CMD_eval)
{
// CMD_let cannot happen, compile_assignment() above is used
iemsg("Command from find_ex_command() not handled");
goto erret;
}
}
p = skipwhite(p);
if (cctx.ctx_skip == SKIP_YES
&& ea.cmdidx != CMD_if
&& ea.cmdidx != CMD_elseif
&& ea.cmdidx != CMD_else
&& ea.cmdidx != CMD_endif)
{
line = (char_u *)"";
continue;
}
if (ea.cmdidx != CMD_elseif
&& ea.cmdidx != CMD_else
&& ea.cmdidx != CMD_endif
&& ea.cmdidx != CMD_endfor
&& ea.cmdidx != CMD_endwhile
&& ea.cmdidx != CMD_catch
&& ea.cmdidx != CMD_finally
&& ea.cmdidx != CMD_endtry)
{
if (cctx.ctx_had_return)
{
emsg(_("E1095: Unreachable code after :return"));
goto erret;
}
}
switch (ea.cmdidx)
{
case CMD_def:
ea.arg = p;
line = compile_nested_function(&ea, &cctx);
break;
case CMD_function:
emsg(_("E1086: Cannot use :function inside :def"));
goto erret;
case CMD_return:
line = compile_return(p, set_return_type, &cctx);
cctx.ctx_had_return = TRUE;
break;
case CMD_let:
case CMD_const:
line = compile_assignment(p, &ea, ea.cmdidx, &cctx);
if (line == p)
line = NULL;
break;
case CMD_unlet:
case CMD_unlockvar:
case CMD_lockvar:
line = compile_unletlock(p, &ea, &cctx);
break;
case CMD_import:
line = compile_import(p, &cctx);
break;
case CMD_if:
line = compile_if(p, &cctx);
break;
case CMD_elseif:
line = compile_elseif(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_else:
line = compile_else(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_endif:
line = compile_endif(p, &cctx);
break;
case CMD_while:
line = compile_while(p, &cctx);
break;
case CMD_endwhile:
line = compile_endwhile(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_for:
line = compile_for(p, &cctx);
break;
case CMD_endfor:
line = compile_endfor(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_continue:
line = compile_continue(p, &cctx);
break;
case CMD_break:
line = compile_break(p, &cctx);
break;
case CMD_try:
line = compile_try(p, &cctx);
break;
case CMD_catch:
line = compile_catch(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_finally:
line = compile_finally(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_endtry:
line = compile_endtry(p, &cctx);
cctx.ctx_had_return = FALSE;
break;
case CMD_throw:
line = compile_throw(p, &cctx);
break;
case CMD_eval:
if (compile_expr0(&p, &cctx) == FAIL)
goto erret;
// drop the return value
generate_instr_drop(&cctx, ISN_DROP, 1);
line = skipwhite(p);
break;
case CMD_echo:
case CMD_echon:
case CMD_execute:
case CMD_echomsg:
case CMD_echoerr:
line = compile_mult_expr(p, ea.cmdidx, &cctx);
break;
// TODO: other commands with an expression argument
case CMD_SIZE:
semsg(_("E476: Invalid command: %s"), ea.cmd);
goto erret;
default:
// Not recognized, execute with do_cmdline_cmd().
ea.arg = p;
line = compile_exec(line, &ea, &cctx);
break;
}
if (line == NULL)
goto erret;
line = skipwhite(line);
if (cctx.ctx_type_stack.ga_len < 0)
{
iemsg("Type stack underflow");
goto erret;
}
}
if (cctx.ctx_scope != NULL)
{
if (cctx.ctx_scope->se_type == IF_SCOPE)
emsg(_(e_endif));
else if (cctx.ctx_scope->se_type == WHILE_SCOPE)
emsg(_(e_endwhile));
else if (cctx.ctx_scope->se_type == FOR_SCOPE)
emsg(_(e_endfor));
else
emsg(_("E1026: Missing }"));
goto erret;
}
if (!cctx.ctx_had_return)
{
if (ufunc->uf_ret_type->tt_type != VAR_VOID)
{
emsg(_("E1027: Missing return statement"));
goto erret;
}
// Return zero if there is no return at the end.
generate_PUSHNR(&cctx, 0);
generate_instr(&cctx, ISN_RETURN);
}
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
dfunc->df_deleted = FALSE;
dfunc->df_instr = instr->ga_data;
dfunc->df_instr_count = instr->ga_len;
dfunc->df_varcount = cctx.ctx_locals_count;
dfunc->df_closure_count = cctx.ctx_closure_count;
if (cctx.ctx_outer_used)
ufunc->uf_flags |= FC_CLOSURE;
ufunc->uf_def_status = UF_COMPILED;
}
ret = OK;
erret:
if (ret == FAIL)
{
int idx;
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
for (idx = 0; idx < instr->ga_len; ++idx)
delete_instr(((isn_T *)instr->ga_data) + idx);
ga_clear(instr);
// if using the last entry in the table we might as well remove it
if (!dfunc->df_deleted
&& ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
--def_functions.ga_len;
ufunc->uf_def_status = UF_NOT_COMPILED;
while (cctx.ctx_scope != NULL)
drop_scope(&cctx);
// Don't execute this function body.
ga_clear_strings(&ufunc->uf_lines);
if (errormsg != NULL)
emsg(errormsg);
else if (called_emsg == called_emsg_before)
emsg(_("E1028: compile_def_function failed"));
}
current_sctx = save_current_sctx;
if (do_estack_push)
estack_pop();
free_imported(&cctx);
free_locals(&cctx);
ga_clear(&cctx.ctx_type_stack);
return ret;
}
void
set_function_type(ufunc_T *ufunc)
{
int varargs = ufunc->uf_va_name != NULL;
int argcount = ufunc->uf_args.ga_len;
// Create a type for the function, with the return type and any
// argument types.
// A vararg is included in uf_args.ga_len but not in uf_arg_types.
// The type is included in "tt_args".
if (argcount > 0 || varargs)
{
ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
argcount, &ufunc->uf_type_list);
// Add argument types to the function type.
if (func_type_add_arg_types(ufunc->uf_func_type,
argcount + varargs,
&ufunc->uf_type_list) == FAIL)
return;
ufunc->uf_func_type->tt_argcount = argcount + varargs;
ufunc->uf_func_type->tt_min_argcount =
argcount - ufunc->uf_def_args.ga_len;
if (ufunc->uf_arg_types == NULL)
{
int i;
// lambda does not have argument types.
for (i = 0; i < argcount; ++i)
ufunc->uf_func_type->tt_args[i] = &t_any;
}
else
mch_memmove(ufunc->uf_func_type->tt_args,
ufunc->uf_arg_types, sizeof(type_T *) * argcount);
if (varargs)
{
ufunc->uf_func_type->tt_args[argcount] =
ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type;
ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
}
}
else
// No arguments, can use a predefined type.
ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
argcount, &ufunc->uf_type_list);
}
/*
* Delete an instruction, free what it contains.
*/
void
delete_instr(isn_T *isn)
{
switch (isn->isn_type)
{
case ISN_EXEC:
case ISN_LOADENV:
case ISN_LOADG:
case ISN_LOADB:
case ISN_LOADW:
case ISN_LOADT:
case ISN_LOADOPT:
case ISN_STRINGMEMBER:
case ISN_PUSHEXC:
case ISN_PUSHS:
case ISN_STOREENV:
case ISN_STOREG:
case ISN_STOREB:
case ISN_STOREW:
case ISN_STORET:
case ISN_PUSHFUNC:
vim_free(isn->isn_arg.string);
break;
case ISN_LOADS:
case ISN_STORES:
vim_free(isn->isn_arg.loadstore.ls_name);
break;
case ISN_UNLET:
case ISN_UNLETENV:
vim_free(isn->isn_arg.unlet.ul_name);
break;
case ISN_STOREOPT:
vim_free(isn->isn_arg.storeopt.so_name);
break;
case ISN_PUSHBLOB: // push blob isn_arg.blob
blob_unref(isn->isn_arg.blob);
break;
case ISN_PUSHJOB:
#ifdef FEAT_JOB_CHANNEL
job_unref(isn->isn_arg.job);
#endif
break;
case ISN_PUSHCHANNEL:
#ifdef FEAT_JOB_CHANNEL
channel_unref(isn->isn_arg.channel);
#endif
break;
case ISN_UCALL:
vim_free(isn->isn_arg.ufunc.cuf_name);
break;
case ISN_FUNCREF:
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ isn->isn_arg.funcref.fr_func;
func_ptr_unref(dfunc->df_ufunc);
}
break;
case ISN_2BOOL:
case ISN_2STRING:
case ISN_ADDBLOB:
case ISN_ADDLIST:
case ISN_BCALL:
case ISN_CATCH:
case ISN_CHECKNR:
case ISN_CHECKTYPE:
case ISN_CHECKLEN:
case ISN_COMPAREANY:
case ISN_COMPAREBLOB:
case ISN_COMPAREBOOL:
case ISN_COMPAREDICT:
case ISN_COMPAREFLOAT:
case ISN_COMPAREFUNC:
case ISN_COMPARELIST:
case ISN_COMPARENR:
case ISN_COMPARESPECIAL:
case ISN_COMPARESTRING:
case ISN_CONCAT:
case ISN_DCALL:
case ISN_SHUFFLE:
case ISN_DROP:
case ISN_ECHO:
case ISN_ECHOERR:
case ISN_ECHOMSG:
case ISN_ENDTRY:
case ISN_EXECCONCAT:
case ISN_EXECUTE:
case ISN_FOR:
case ISN_INDEX:
case ISN_GETITEM:
case ISN_SLICE:
case ISN_MEMBER:
case ISN_JUMP:
case ISN_LOAD:
case ISN_LOADOUTER:
case ISN_LOADSCRIPT:
case ISN_LOADREG:
case ISN_LOADV:
case ISN_NEGATENR:
case ISN_NEWDICT:
case ISN_NEWLIST:
case ISN_OPNR:
case ISN_OPFLOAT:
case ISN_OPANY:
case ISN_PCALL:
case ISN_PCALL_END:
case ISN_PUSHF:
case ISN_PUSHNR:
case ISN_PUSHBOOL:
case ISN_PUSHSPEC:
case ISN_RETURN:
case ISN_STORE:
case ISN_STOREOUTER:
case ISN_STOREV:
case ISN_STORENR:
case ISN_STOREREG:
case ISN_STORESCRIPT:
case ISN_STOREDICT:
case ISN_STORELIST:
case ISN_THROW:
case ISN_TRY:
// nothing allocated
break;
}
}
/*
* Free all instructions for "dfunc".
*/
static void
delete_def_function_contents(dfunc_T *dfunc)
{
int idx;
ga_clear(&dfunc->df_def_args_isn);
if (dfunc->df_instr != NULL)
{
for (idx = 0; idx < dfunc->df_instr_count; ++idx)
delete_instr(dfunc->df_instr + idx);
VIM_CLEAR(dfunc->df_instr);
}
dfunc->df_deleted = TRUE;
}
/*
* When a user function is deleted, clear the contents of any associated def
* function. The position in def_functions can be re-used.
*/
void
clear_def_function(ufunc_T *ufunc)
{
if (ufunc->uf_dfunc_idx > 0)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
+ ufunc->uf_dfunc_idx;
delete_def_function_contents(dfunc);
ufunc->uf_def_status = UF_NOT_COMPILED;
}
}
#if defined(EXITFREE) || defined(PROTO)
/*
* Free all functions defined with ":def".
*/
void
free_def_functions(void)
{
int idx;
for (idx = 0; idx < def_functions.ga_len; ++idx)
{
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
delete_def_function_contents(dfunc);
}
ga_clear(&def_functions);
}
#endif
#endif // FEAT_EVAL
|