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
|
struct AstNode;
struct Scope;
struct DeclInfo;
enum ParseFileError {
ParseFile_None,
ParseFile_WrongExtension,
ParseFile_InvalidFile,
ParseFile_EmptyFile,
ParseFile_Permission,
ParseFile_NotFound,
ParseFile_InvalidToken,
ParseFile_Count,
};
typedef Array<AstNode *> AstNodeArray;
struct AstFile {
u32 id;
gbArena arena;
Tokenizer tokenizer;
Array<Token> tokens;
isize curr_token_index;
Token curr_token;
Token prev_token; // previous non-comment
// >= 0: In Expression
// < 0: In Control Clause
// NOTE(bill): Used to prevent type literals in control clauses
isize expr_level;
AstNodeArray decls;
b32 is_global_scope;
AstNode * curr_proc;
isize scope_level;
Scope * scope; // NOTE(bill): Created in checker
DeclInfo *decl_info; // NOTE(bill): Created in checker
// TODO(bill): Error recovery
// NOTE(bill): Error recovery
#define PARSER_MAX_FIX_COUNT 6
isize fix_count;
TokenPos fix_prev_pos;
};
struct ImportedFile {
String path;
String rel_path;
TokenPos pos; // #import
};
struct Parser {
String init_fullpath;
Array<AstFile> files;
Array<ImportedFile> imports;
gbAtomic32 import_index;
Array<String> system_libraries;
isize total_token_count;
gbMutex mutex;
};
enum ProcTag : u64 {
ProcTag_bounds_check = GB_BIT(0),
ProcTag_no_bounds_check = GB_BIT(1),
ProcTag_foreign = GB_BIT(10),
ProcTag_link_name = GB_BIT(11),
ProcTag_inline = GB_BIT(12),
ProcTag_no_inline = GB_BIT(13),
ProcTag_dll_import = GB_BIT(14),
ProcTag_dll_export = GB_BIT(15),
ProcTag_stdcall = GB_BIT(16),
ProcTag_fastcall = GB_BIT(17),
// ProcTag_cdecl = GB_BIT(18),
};
enum VarDeclTag {
VarDeclTag_thread_local = GB_BIT(0),
};
enum StmtStateFlag : u32 {
StmtStateFlag_bounds_check = GB_BIT(0),
StmtStateFlag_no_bounds_check = GB_BIT(1),
};
enum CallExprKind {
CallExpr_Prefix, // call(...)
CallExpr_Postfix, // a'call
CallExpr_Infix, // a ''call b
};
AstNodeArray make_ast_node_array(AstFile *f) {
AstNodeArray a;
array_init(&a, gb_arena_allocator(&f->arena));
return a;
}
#define AST_NODE_KINDS \
AST_NODE_KIND(Invalid, "invalid node", struct{}) \
AST_NODE_KIND(BasicLit, "basic literal", Token) \
AST_NODE_KIND(Ident, "identifier", Token) \
AST_NODE_KIND(Ellipsis, "ellipsis", struct { \
Token token; \
AstNode *expr; \
}) \
AST_NODE_KIND(ProcLit, "procedure literal", struct { \
AstNode *type; \
AstNode *body; \
u64 tags; \
}) \
AST_NODE_KIND(CompoundLit, "compound literal", struct { \
AstNode *type; \
AstNodeArray elems; \
Token open, close; \
}) \
AST_NODE_KIND(_ExprBegin, "", struct{}) \
AST_NODE_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
AST_NODE_KIND(TagExpr, "tag expression", struct { Token token, name; AstNode *expr; }) \
AST_NODE_KIND(RunExpr, "run expression", struct { Token token, name; AstNode *expr; }) \
AST_NODE_KIND(UnaryExpr, "unary expression", struct { Token op; AstNode *expr; }) \
AST_NODE_KIND(BinaryExpr, "binary expression", struct { Token op; AstNode *left, *right; } ) \
AST_NODE_KIND(ParenExpr, "parentheses expression", struct { AstNode *expr; Token open, close; }) \
AST_NODE_KIND(SelectorExpr, "selector expression", struct { Token token; AstNode *expr, *selector; }) \
AST_NODE_KIND(IndexExpr, "index expression", struct { AstNode *expr, *index; Token open, close; }) \
AST_NODE_KIND(DerefExpr, "dereference expression", struct { Token op; AstNode *expr; }) \
AST_NODE_KIND(DemaybeExpr, "demaybe expression", struct { Token op; AstNode *expr; }) \
AST_NODE_KIND(CallExpr, "call expression", struct { \
AstNode *proc; \
AstNodeArray args; \
Token open, close; \
Token ellipsis; \
CallExprKind kind; \
}) \
AST_NODE_KIND(SliceExpr, "slice expression", struct { \
AstNode *expr; \
Token open, close; \
AstNode *low, *high, *max; \
b32 triple_indexed; \
}) \
AST_NODE_KIND(FieldValue, "field value", struct { Token eq; AstNode *field, *value; }) \
AST_NODE_KIND(_ExprEnd, "", struct{}) \
AST_NODE_KIND(_StmtBegin, "", struct{}) \
AST_NODE_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
AST_NODE_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
AST_NODE_KIND(ExprStmt, "expression statement", struct { AstNode *expr; } ) \
AST_NODE_KIND(IncDecStmt, "increment/decrement statement", struct { Token op; AstNode *expr; }) \
AST_NODE_KIND(TagStmt, "tag statement", struct { \
Token token; \
Token name; \
AstNode *stmt; \
}) \
AST_NODE_KIND(AssignStmt, "assign statement", struct { \
Token op; \
AstNodeArray lhs, rhs; \
}) \
AST_NODE_KIND(_ComplexStmtBegin, "", struct{}) \
AST_NODE_KIND(BlockStmt, "block statement", struct { \
AstNodeArray stmts; \
Token open, close; \
}) \
AST_NODE_KIND(IfStmt, "if statement", struct { \
Token token; \
AstNode *init; \
AstNode *cond; \
AstNode *body; \
AstNode *else_stmt; \
}) \
AST_NODE_KIND(ReturnStmt, "return statement", struct { \
Token token; \
AstNodeArray results; \
}) \
AST_NODE_KIND(ForStmt, "for statement", struct { \
Token token; \
AstNode *init, *cond, *post; \
AstNode *body; \
}) \
AST_NODE_KIND(CaseClause, "case clause", struct { \
Token token; \
AstNodeArray list, stmts; \
}) \
AST_NODE_KIND(MatchStmt, "match statement", struct { \
Token token; \
AstNode *init, *tag; \
AstNode *body; \
}) \
AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \
Token token; \
AstNode *tag, *var; \
AstNode *body; \
}) \
AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
AST_NODE_KIND(BranchStmt, "branch statement", struct { Token token; }) \
AST_NODE_KIND(UsingStmt, "using statement", struct { Token token; AstNode *node; }) \
AST_NODE_KIND(AsmOperand, "assembly operand", struct { \
Token string; \
AstNode *operand; \
}) \
AST_NODE_KIND(AsmStmt, "assembly statement", struct { \
Token token; \
b32 is_volatile; \
Token open, close; \
Token code_string; \
AstNode *output_list; \
AstNode *input_list; \
AstNode *clobber_list; \
isize output_count, input_count, clobber_count; \
}) \
AST_NODE_KIND(PushAllocator, "push_allocator statement", struct { \
Token token; \
AstNode *expr; \
AstNode *body; \
}) \
AST_NODE_KIND(PushContext, "push_context statement", struct { \
Token token; \
AstNode *expr; \
AstNode *body; \
}) \
\
AST_NODE_KIND(_ComplexStmtEnd, "", struct{}) \
AST_NODE_KIND(_StmtEnd, "", struct{}) \
AST_NODE_KIND(_DeclBegin, "", struct{}) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(VarDecl, "variable declaration", struct { \
u64 tags; \
b32 is_using; \
AstNodeArray names; \
AstNode *type; \
AstNodeArray values; \
}) \
AST_NODE_KIND(ConstDecl, "constant declaration", struct { \
u64 tags; \
AstNodeArray names; \
AstNode *type; \
AstNodeArray values; \
}) \
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
AstNode *name; \
AstNode *type; \
AstNode *body; \
u64 tags; \
String foreign_name; \
String link_name; \
}) \
AST_NODE_KIND(TypeDecl, "type declaration", struct { Token token; AstNode *name, *type; }) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token, relpath; \
String fullpath; \
Token import_name; \
b32 is_load; \
}) \
AST_NODE_KIND(ForeignSystemLibrary, "foreign system library", struct { Token token, filepath; }) \
AST_NODE_KIND(_DeclEnd, "", struct{}) \
AST_NODE_KIND(_TypeBegin, "", struct{}) \
AST_NODE_KIND(Parameter, "parameter", struct { \
AstNodeArray names; \
AstNode *type; \
b32 is_using; \
}) \
AST_NODE_KIND(ProcType, "procedure type", struct { \
Token token; \
AstNodeArray params; \
AstNodeArray results; \
}) \
AST_NODE_KIND(PointerType, "pointer type", struct { \
Token token; \
AstNode *type; \
}) \
AST_NODE_KIND(MaybeType, "maybe type", struct { \
Token token; \
AstNode *type; \
}) \
AST_NODE_KIND(ArrayType, "array type", struct { \
Token token; \
AstNode *count; \
AstNode *elem; \
}) \
AST_NODE_KIND(VectorType, "vector type", struct { \
Token token; \
AstNode *count; \
AstNode *elem; \
}) \
AST_NODE_KIND(StructType, "struct type", struct { \
Token token; \
AstNodeArray decls; \
isize decl_count; \
b32 is_packed; \
b32 is_ordered; \
}) \
AST_NODE_KIND(UnionType, "union type", struct { \
Token token; \
AstNodeArray decls; \
isize decl_count; \
}) \
AST_NODE_KIND(RawUnionType, "raw union type", struct { \
Token token; \
AstNodeArray decls; \
isize decl_count; \
}) \
AST_NODE_KIND(EnumType, "enum type", struct { \
Token token; \
AstNode *base_type; \
AstNodeArray fields; \
}) \
AST_NODE_KIND(_TypeEnd, "", struct{}) \
AST_NODE_KIND(Count, "", struct{})
enum AstNodeKind {
#define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
AST_NODE_KINDS
#undef AST_NODE_KIND
};
String const ast_node_strings[] = {
#define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
AST_NODE_KINDS
#undef AST_NODE_KIND
};
#define AST_NODE_KIND(_kind_name_, name, ...) typedef __VA_ARGS__ GB_JOIN2(AstNode, _kind_name_);
AST_NODE_KINDS
#undef AST_NODE_KIND
struct AstNode {
AstNodeKind kind;
// AstNode *prev, *next; // NOTE(bill): allow for Linked list
u32 stmt_state_flags;
union {
#define AST_NODE_KIND(_kind_name_, name, ...) GB_JOIN2(AstNode, _kind_name_) _kind_name_;
AST_NODE_KINDS
#undef AST_NODE_KIND
};
};
#define ast_node(n_, Kind_, node_) GB_JOIN2(AstNode, Kind_) *n_ = &(node_)->Kind_; GB_ASSERT((node_)->kind == GB_JOIN2(AstNode_, Kind_))
#define case_ast_node(n_, Kind_, node_) case GB_JOIN2(AstNode_, Kind_): { ast_node(n_, Kind_, node_);
#define case_end } break;
gb_inline b32 is_ast_node_expr(AstNode *node) {
return gb_is_between(node->kind, AstNode__ExprBegin+1, AstNode__ExprEnd-1);
}
gb_inline b32 is_ast_node_stmt(AstNode *node) {
return gb_is_between(node->kind, AstNode__StmtBegin+1, AstNode__StmtEnd-1);
}
gb_inline b32 is_ast_node_complex_stmt(AstNode *node) {
return gb_is_between(node->kind, AstNode__ComplexStmtBegin+1, AstNode__ComplexStmtEnd-1);
}
gb_inline b32 is_ast_node_decl(AstNode *node) {
return gb_is_between(node->kind, AstNode__DeclBegin+1, AstNode__DeclEnd-1);
}
gb_inline b32 is_ast_node_type(AstNode *node) {
return gb_is_between(node->kind, AstNode__TypeBegin+1, AstNode__TypeEnd-1);
}
Token ast_node_token(AstNode *node) {
switch (node->kind) {
case AstNode_BasicLit:
return node->BasicLit;
case AstNode_Ident:
return node->Ident;
case AstNode_ProcLit:
return ast_node_token(node->ProcLit.type);
case AstNode_CompoundLit:
if (node->CompoundLit.type != NULL) {
return ast_node_token(node->CompoundLit.type);
}
return node->CompoundLit.open;
case AstNode_TagExpr:
return node->TagExpr.token;
case AstNode_RunExpr:
return node->RunExpr.token;
case AstNode_BadExpr:
return node->BadExpr.begin;
case AstNode_UnaryExpr:
return node->UnaryExpr.op;
case AstNode_BinaryExpr:
return ast_node_token(node->BinaryExpr.left);
case AstNode_ParenExpr:
return node->ParenExpr.open;
case AstNode_CallExpr:
return ast_node_token(node->CallExpr.proc);
case AstNode_SelectorExpr:
return ast_node_token(node->SelectorExpr.selector);
case AstNode_IndexExpr:
return node->IndexExpr.open;
case AstNode_SliceExpr:
return node->SliceExpr.open;
case AstNode_Ellipsis:
return node->Ellipsis.token;
case AstNode_FieldValue:
return node->FieldValue.eq;
case AstNode_DerefExpr:
return node->DerefExpr.op;
case AstNode_DemaybeExpr:
return node->DemaybeExpr.op;
case AstNode_BadStmt:
return node->BadStmt.begin;
case AstNode_EmptyStmt:
return node->EmptyStmt.token;
case AstNode_ExprStmt:
return ast_node_token(node->ExprStmt.expr);
case AstNode_TagStmt:
return node->TagStmt.token;
case AstNode_IncDecStmt:
return node->IncDecStmt.op;
case AstNode_AssignStmt:
return node->AssignStmt.op;
case AstNode_BlockStmt:
return node->BlockStmt.open;
case AstNode_IfStmt:
return node->IfStmt.token;
case AstNode_ReturnStmt:
return node->ReturnStmt.token;
case AstNode_ForStmt:
return node->ForStmt.token;
case AstNode_MatchStmt:
return node->MatchStmt.token;
case AstNode_CaseClause:
return node->CaseClause.token;
case AstNode_DeferStmt:
return node->DeferStmt.token;
case AstNode_BranchStmt:
return node->BranchStmt.token;
case AstNode_UsingStmt:
return node->UsingStmt.token;
case AstNode_AsmStmt:
return node->AsmStmt.token;
case AstNode_PushAllocator:
return node->PushAllocator.token;
case AstNode_PushContext:
return node->PushContext.token;
case AstNode_BadDecl:
return node->BadDecl.begin;
case AstNode_VarDecl:
return ast_node_token(node->VarDecl.names[0]);
case AstNode_ConstDecl:
return ast_node_token(node->ConstDecl.names[0]);
case AstNode_ProcDecl:
return node->ProcDecl.name->Ident;
case AstNode_TypeDecl:
return node->TypeDecl.token;
case AstNode_ImportDecl:
return node->ImportDecl.token;
case AstNode_ForeignSystemLibrary:
return node->ForeignSystemLibrary.token;
case AstNode_Parameter: {
if (node->Parameter.names.count > 0) {
return ast_node_token(node->Parameter.names[0]);
} else {
return ast_node_token(node->Parameter.type);
}
}
case AstNode_ProcType:
return node->ProcType.token;
case AstNode_PointerType:
return node->PointerType.token;
case AstNode_MaybeType:
return node->MaybeType.token;
case AstNode_ArrayType:
return node->ArrayType.token;
case AstNode_VectorType:
return node->VectorType.token;
case AstNode_StructType:
return node->StructType.token;
case AstNode_UnionType:
return node->UnionType.token;
case AstNode_RawUnionType:
return node->RawUnionType.token;
case AstNode_EnumType:
return node->EnumType.token;
}
return empty_token;
}
HashKey hash_token(Token t) {
return hash_string(t.string);
}
// NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
AstNode *make_node(AstFile *f, AstNodeKind kind) {
gbArena *arena = &f->arena;
if (gb_arena_size_remaining(arena, GB_DEFAULT_MEMORY_ALIGNMENT) <= gb_size_of(AstNode)) {
// NOTE(bill): If a syntax error is so bad, just quit!
gb_exit(1);
}
AstNode *node = gb_alloc_item(gb_arena_allocator(arena), AstNode);
node->kind = kind;
return node;
}
AstNode *make_bad_expr(AstFile *f, Token begin, Token end) {
AstNode *result = make_node(f, AstNode_BadExpr);
result->BadExpr.begin = begin;
result->BadExpr.end = end;
return result;
}
AstNode *make_tag_expr(AstFile *f, Token token, Token name, AstNode *expr) {
AstNode *result = make_node(f, AstNode_TagExpr);
result->TagExpr.token = token;
result->TagExpr.name = name;
result->TagExpr.expr = expr;
return result;
}
AstNode *make_run_expr(AstFile *f, Token token, Token name, AstNode *expr) {
AstNode *result = make_node(f, AstNode_RunExpr);
result->RunExpr.token = token;
result->RunExpr.name = name;
result->RunExpr.expr = expr;
return result;
}
AstNode *make_tag_stmt(AstFile *f, Token token, Token name, AstNode *stmt) {
AstNode *result = make_node(f, AstNode_TagStmt);
result->TagStmt.token = token;
result->TagStmt.name = name;
result->TagStmt.stmt = stmt;
return result;
}
AstNode *make_unary_expr(AstFile *f, Token op, AstNode *expr) {
AstNode *result = make_node(f, AstNode_UnaryExpr);
result->UnaryExpr.op = op;
result->UnaryExpr.expr = expr;
return result;
}
AstNode *make_binary_expr(AstFile *f, Token op, AstNode *left, AstNode *right) {
AstNode *result = make_node(f, AstNode_BinaryExpr);
if (left == NULL) {
syntax_error(op, "No lhs expression for binary expression `%.*s`", LIT(op.string));
left = make_bad_expr(f, op, op);
}
if (right == NULL) {
syntax_error(op, "No rhs expression for binary expression `%.*s`", LIT(op.string));
right = make_bad_expr(f, op, op);
}
result->BinaryExpr.op = op;
result->BinaryExpr.left = left;
result->BinaryExpr.right = right;
return result;
}
AstNode *make_paren_expr(AstFile *f, AstNode *expr, Token open, Token close) {
AstNode *result = make_node(f, AstNode_ParenExpr);
result->ParenExpr.expr = expr;
result->ParenExpr.open = open;
result->ParenExpr.close = close;
return result;
}
AstNode *make_call_expr(AstFile *f, AstNode *proc, AstNodeArray args, Token open, Token close, Token ellipsis) {
AstNode *result = make_node(f, AstNode_CallExpr);
result->CallExpr.proc = proc;
result->CallExpr.args = args;
result->CallExpr.open = open;
result->CallExpr.close = close;
result->CallExpr.ellipsis = ellipsis;
return result;
}
AstNode *make_selector_expr(AstFile *f, Token token, AstNode *expr, AstNode *selector) {
AstNode *result = make_node(f, AstNode_SelectorExpr);
result->SelectorExpr.expr = expr;
result->SelectorExpr.selector = selector;
return result;
}
AstNode *make_index_expr(AstFile *f, AstNode *expr, AstNode *index, Token open, Token close) {
AstNode *result = make_node(f, AstNode_IndexExpr);
result->IndexExpr.expr = expr;
result->IndexExpr.index = index;
result->IndexExpr.open = open;
result->IndexExpr.close = close;
return result;
}
AstNode *make_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, AstNode *low, AstNode *high, AstNode *max, b32 triple_indexed) {
AstNode *result = make_node(f, AstNode_SliceExpr);
result->SliceExpr.expr = expr;
result->SliceExpr.open = open;
result->SliceExpr.close = close;
result->SliceExpr.low = low;
result->SliceExpr.high = high;
result->SliceExpr.max = max;
result->SliceExpr.triple_indexed = triple_indexed;
return result;
}
AstNode *make_deref_expr(AstFile *f, AstNode *expr, Token op) {
AstNode *result = make_node(f, AstNode_DerefExpr);
result->DerefExpr.expr = expr;
result->DerefExpr.op = op;
return result;
}
AstNode *make_demaybe_expr(AstFile *f, AstNode *expr, Token op) {
AstNode *result = make_node(f, AstNode_DemaybeExpr);
result->DemaybeExpr.expr = expr;
result->DemaybeExpr.op = op;
return result;
}
AstNode *make_basic_lit(AstFile *f, Token basic_lit) {
AstNode *result = make_node(f, AstNode_BasicLit);
result->BasicLit = basic_lit;
return result;
}
AstNode *make_ident(AstFile *f, Token token) {
AstNode *result = make_node(f, AstNode_Ident);
result->Ident = token;
return result;
}
AstNode *make_ellipsis(AstFile *f, Token token, AstNode *expr) {
AstNode *result = make_node(f, AstNode_Ellipsis);
result->Ellipsis.token = token;
result->Ellipsis.expr = expr;
return result;
}
AstNode *make_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 tags) {
AstNode *result = make_node(f, AstNode_ProcLit);
result->ProcLit.type = type;
result->ProcLit.body = body;
result->ProcLit.tags = tags;
return result;
}
AstNode *make_field_value(AstFile *f, AstNode *field, AstNode *value, Token eq) {
AstNode *result = make_node(f, AstNode_FieldValue);
result->FieldValue.field = field;
result->FieldValue.value = value;
result->FieldValue.eq = eq;
return result;
}
AstNode *make_compound_lit(AstFile *f, AstNode *type, AstNodeArray elems, Token open, Token close) {
AstNode *result = make_node(f, AstNode_CompoundLit);
result->CompoundLit.type = type;
result->CompoundLit.elems = elems;
result->CompoundLit.open = open;
result->CompoundLit.close = close;
return result;
}
AstNode *make_bad_stmt(AstFile *f, Token begin, Token end) {
AstNode *result = make_node(f, AstNode_BadStmt);
result->BadStmt.begin = begin;
result->BadStmt.end = end;
return result;
}
AstNode *make_empty_stmt(AstFile *f, Token token) {
AstNode *result = make_node(f, AstNode_EmptyStmt);
result->EmptyStmt.token = token;
return result;
}
AstNode *make_expr_stmt(AstFile *f, AstNode *expr) {
AstNode *result = make_node(f, AstNode_ExprStmt);
result->ExprStmt.expr = expr;
return result;
}
AstNode *make_inc_dec_stmt(AstFile *f, Token op, AstNode *expr) {
AstNode *result = make_node(f, AstNode_IncDecStmt);
result->IncDecStmt.op = op;
result->IncDecStmt.expr = expr;
return result;
}
AstNode *make_assign_stmt(AstFile *f, Token op, AstNodeArray lhs, AstNodeArray rhs) {
AstNode *result = make_node(f, AstNode_AssignStmt);
result->AssignStmt.op = op;
result->AssignStmt.lhs = lhs;
result->AssignStmt.rhs = rhs;
return result;
}
AstNode *make_block_stmt(AstFile *f, AstNodeArray stmts, Token open, Token close) {
AstNode *result = make_node(f, AstNode_BlockStmt);
result->BlockStmt.stmts = stmts;
result->BlockStmt.open = open;
result->BlockStmt.close = close;
return result;
}
AstNode *make_if_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *body, AstNode *else_stmt) {
AstNode *result = make_node(f, AstNode_IfStmt);
result->IfStmt.token = token;
result->IfStmt.init = init;
result->IfStmt.cond = cond;
result->IfStmt.body = body;
result->IfStmt.else_stmt = else_stmt;
return result;
}
AstNode *make_return_stmt(AstFile *f, Token token, AstNodeArray results) {
AstNode *result = make_node(f, AstNode_ReturnStmt);
result->ReturnStmt.token = token;
result->ReturnStmt.results = results;
return result;
}
AstNode *make_for_stmt(AstFile *f, Token token, AstNode *init, AstNode *cond, AstNode *post, AstNode *body) {
AstNode *result = make_node(f, AstNode_ForStmt);
result->ForStmt.token = token;
result->ForStmt.init = init;
result->ForStmt.cond = cond;
result->ForStmt.post = post;
result->ForStmt.body = body;
return result;
}
AstNode *make_match_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, AstNode *body) {
AstNode *result = make_node(f, AstNode_MatchStmt);
result->MatchStmt.token = token;
result->MatchStmt.init = init;
result->MatchStmt.tag = tag;
result->MatchStmt.body = body;
return result;
}
AstNode *make_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *var, AstNode *body) {
AstNode *result = make_node(f, AstNode_TypeMatchStmt);
result->TypeMatchStmt.token = token;
result->TypeMatchStmt.tag = tag;
result->TypeMatchStmt.var = var;
result->TypeMatchStmt.body = body;
return result;
}
AstNode *make_case_clause(AstFile *f, Token token, AstNodeArray list, AstNodeArray stmts) {
AstNode *result = make_node(f, AstNode_CaseClause);
result->CaseClause.token = token;
result->CaseClause.list = list;
result->CaseClause.stmts = stmts;
return result;
}
AstNode *make_defer_stmt(AstFile *f, Token token, AstNode *stmt) {
AstNode *result = make_node(f, AstNode_DeferStmt);
result->DeferStmt.token = token;
result->DeferStmt.stmt = stmt;
return result;
}
AstNode *make_branch_stmt(AstFile *f, Token token) {
AstNode *result = make_node(f, AstNode_BranchStmt);
result->BranchStmt.token = token;
return result;
}
AstNode *make_using_stmt(AstFile *f, Token token, AstNode *node) {
AstNode *result = make_node(f, AstNode_UsingStmt);
result->UsingStmt.token = token;
result->UsingStmt.node = node;
return result;
}
AstNode *make_asm_operand(AstFile *f, Token string, AstNode *operand) {
AstNode *result = make_node(f, AstNode_AsmOperand);
result->AsmOperand.string = string;
result->AsmOperand.operand = operand;
return result;
}
AstNode *make_asm_stmt(AstFile *f, Token token, b32 is_volatile, Token open, Token close, Token code_string,
AstNode *output_list, AstNode *input_list, AstNode *clobber_list,
isize output_count, isize input_count, isize clobber_count) {
AstNode *result = make_node(f, AstNode_AsmStmt);
result->AsmStmt.token = token;
result->AsmStmt.is_volatile = is_volatile;
result->AsmStmt.open = open;
result->AsmStmt.close = close;
result->AsmStmt.code_string = code_string;
result->AsmStmt.output_list = output_list;
result->AsmStmt.input_list = input_list;
result->AsmStmt.clobber_list = clobber_list;
result->AsmStmt.output_count = output_count;
result->AsmStmt.input_count = input_count;
result->AsmStmt.clobber_count = clobber_count;
return result;
}
AstNode *make_push_allocator(AstFile *f, Token token, AstNode *expr, AstNode *body) {
AstNode *result = make_node(f, AstNode_PushAllocator);
result->PushAllocator.token = token;
result->PushAllocator.expr = expr;
result->PushAllocator.body = body;
return result;
}
AstNode *make_push_context(AstFile *f, Token token, AstNode *expr, AstNode *body) {
AstNode *result = make_node(f, AstNode_PushContext);
result->PushContext.token = token;
result->PushContext.expr = expr;
result->PushContext.body = body;
return result;
}
AstNode *make_bad_decl(AstFile *f, Token begin, Token end) {
AstNode *result = make_node(f, AstNode_BadDecl);
result->BadDecl.begin = begin;
result->BadDecl.end = end;
return result;
}
AstNode *make_var_decl(AstFile *f, AstNodeArray names, AstNode *type, AstNodeArray values) {
AstNode *result = make_node(f, AstNode_VarDecl);
result->VarDecl.names = names;
result->VarDecl.type = type;
result->VarDecl.values = values;
return result;
}
AstNode *make_const_decl(AstFile *f, AstNodeArray names, AstNode *type, AstNodeArray values) {
AstNode *result = make_node(f, AstNode_ConstDecl);
result->ConstDecl.names = names;
result->ConstDecl.type = type;
result->ConstDecl.values = values;
return result;
}
AstNode *make_parameter(AstFile *f, AstNodeArray names, AstNode *type, b32 is_using) {
AstNode *result = make_node(f, AstNode_Parameter);
result->Parameter.names = names;
result->Parameter.type = type;
result->Parameter.is_using = is_using;
return result;
}
AstNode *make_proc_type(AstFile *f, Token token, AstNodeArray params, AstNodeArray results) {
AstNode *result = make_node(f, AstNode_ProcType);
result->ProcType.token = token;
result->ProcType.params = params;
result->ProcType.results = results;
return result;
}
AstNode *make_proc_decl(AstFile *f, AstNode *name, AstNode *proc_type, AstNode *body, u64 tags, String foreign_name, String link_name) {
AstNode *result = make_node(f, AstNode_ProcDecl);
result->ProcDecl.name = name;
result->ProcDecl.type = proc_type;
result->ProcDecl.body = body;
result->ProcDecl.tags = tags;
result->ProcDecl.foreign_name = foreign_name;
result->ProcDecl.link_name = link_name;
return result;
}
AstNode *make_pointer_type(AstFile *f, Token token, AstNode *type) {
AstNode *result = make_node(f, AstNode_PointerType);
result->PointerType.token = token;
result->PointerType.type = type;
return result;
}
AstNode *make_maybe_type(AstFile *f, Token token, AstNode *type) {
AstNode *result = make_node(f, AstNode_MaybeType);
result->MaybeType.token = token;
result->MaybeType.type = type;
return result;
}
AstNode *make_array_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
AstNode *result = make_node(f, AstNode_ArrayType);
result->ArrayType.token = token;
result->ArrayType.count = count;
result->ArrayType.elem = elem;
return result;
}
AstNode *make_vector_type(AstFile *f, Token token, AstNode *count, AstNode *elem) {
AstNode *result = make_node(f, AstNode_VectorType);
result->VectorType.token = token;
result->VectorType.count = count;
result->VectorType.elem = elem;
return result;
}
AstNode *make_struct_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count, b32 is_packed, b32 is_ordered) {
AstNode *result = make_node(f, AstNode_StructType);
result->StructType.token = token;
result->StructType.decls = decls;
result->StructType.decl_count = decl_count;
result->StructType.is_packed = is_packed;
result->StructType.is_ordered = is_ordered;
return result;
}
AstNode *make_union_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count) {
AstNode *result = make_node(f, AstNode_UnionType);
result->UnionType.token = token;
result->UnionType.decls = decls;
result->UnionType.decl_count = decl_count;
return result;
}
AstNode *make_raw_union_type(AstFile *f, Token token, AstNodeArray decls, isize decl_count) {
AstNode *result = make_node(f, AstNode_RawUnionType);
result->RawUnionType.token = token;
result->RawUnionType.decls = decls;
result->RawUnionType.decl_count = decl_count;
return result;
}
AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNodeArray fields) {
AstNode *result = make_node(f, AstNode_EnumType);
result->EnumType.token = token;
result->EnumType.base_type = base_type;
result->EnumType.fields = fields;
return result;
}
AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_TypeDecl);
result->TypeDecl.token = token;
result->TypeDecl.name = name;
result->TypeDecl.type = type;
return result;
}
AstNode *make_import_decl(AstFile *f, Token token, Token relpath, Token import_name, b32 is_load) {
AstNode *result = make_node(f, AstNode_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.is_load = is_load;
return result;
}
AstNode *make_foreign_system_library(AstFile *f, Token token, Token filepath) {
AstNode *result = make_node(f, AstNode_ForeignSystemLibrary);
result->ForeignSystemLibrary.token = token;
result->ForeignSystemLibrary.filepath = filepath;
return result;
}
b32 next_token(AstFile *f) {
if (f->curr_token_index+1 < f->tokens.count) {
if (f->curr_token.kind != Token_Comment) {
f->prev_token = f->curr_token;
}
f->curr_token_index++;
f->curr_token = f->tokens[f->curr_token_index];
if (f->curr_token.kind == Token_Comment) {
return next_token(f);
}
return true;
}
syntax_error(f->curr_token, "Token is EOF");
return false;
}
Token expect_token(AstFile *f, TokenKind kind) {
Token prev = f->curr_token;
if (prev.kind != kind) {
syntax_error(f->curr_token, "Expected `%.*s`, got `%.*s`",
LIT(token_strings[kind]),
LIT(token_strings[prev.kind]));
}
next_token(f);
return prev;
}
Token expect_token_after(AstFile *f, TokenKind kind, char *msg) {
Token prev = f->curr_token;
if (prev.kind != kind) {
syntax_error(f->curr_token, "Expected `%.*s` after %s, got `%.*s`",
LIT(token_strings[kind]),
msg,
LIT(token_strings[prev.kind]));
}
next_token(f);
return prev;
}
Token expect_operator(AstFile *f) {
Token prev = f->curr_token;
if (!gb_is_between(prev.kind, Token__OperatorBegin+1, Token__OperatorEnd-1)) {
syntax_error(f->curr_token, "Expected an operator, got `%.*s`",
LIT(token_strings[prev.kind]));
}
next_token(f);
return prev;
}
Token expect_keyword(AstFile *f) {
Token prev = f->curr_token;
if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) {
syntax_error(f->curr_token, "Expected a keyword, got `%.*s`",
LIT(token_strings[prev.kind]));
}
next_token(f);
return prev;
}
b32 allow_token(AstFile *f, TokenKind kind) {
Token prev = f->curr_token;
if (prev.kind == kind) {
next_token(f);
return true;
}
return false;
}
b32 is_blank_ident(String str) {
if (str.len == 1) {
return str.text[0] == '_';
}
return false;
}
// NOTE(bill): Go to next statement to prevent numerous error messages popping up
void fix_advance_to_next_stmt(AstFile *f) {
// TODO(bill): fix_advance_to_next_stmt
#if 1
for (;;) {
Token t = f->curr_token;
switch (t.kind) {
case Token_EOF:
case Token_Semicolon:
return;
case Token_if:
case Token_return:
case Token_for:
case Token_match:
case Token_defer:
case Token_asm:
case Token_using:
case Token_break:
case Token_continue:
case Token_fallthrough:
case Token_push_allocator:
case Token_push_context:
case Token_Hash:
{
if (token_pos_are_equal(t.pos, f->fix_prev_pos) &&
f->fix_count < PARSER_MAX_FIX_COUNT) {
f->fix_count++;
return;
}
if (token_pos_cmp(f->fix_prev_pos, t.pos) < 0) {
f->fix_prev_pos = t.pos;
f->fix_count = 0; // NOTE(bill): Reset
return;
}
// NOTE(bill): Reaching here means there is a parsing bug
} break;
}
next_token(f);
}
#endif
}
b32 expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
if (allow_token(f, Token_Semicolon)) {
return true;
}
if (f->curr_token.pos.line != f->prev_token.pos.line) {
return true;
}
switch (f->curr_token.kind) {
case Token_EOF:
case Token_CloseBrace:
return true;
}
syntax_error(f->curr_token,
"Expected `;` after %.*s, got `%.*s`",
LIT(ast_node_strings[s->kind]), LIT(token_strings[f->curr_token.kind]));
fix_advance_to_next_stmt(f);
return false;
}
AstNode * parse_expr(AstFile *f, b32 lhs);
AstNode * parse_proc_type(AstFile *f);
AstNodeArray parse_stmt_list(AstFile *f);
AstNode * parse_stmt(AstFile *f);
AstNode * parse_body(AstFile *f);
AstNode *parse_identifier(AstFile *f) {
Token token = f->curr_token;
if (token.kind == Token_Identifier) {
next_token(f);
} else {
token.string = make_string("_");
expect_token(f, Token_Identifier);
}
return make_ident(f, token);
}
AstNode *parse_tag_expr(AstFile *f, AstNode *expression) {
Token token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Identifier);
return make_tag_expr(f, token, name, expression);
}
AstNode *parse_tag_stmt(AstFile *f, AstNode *statement) {
Token token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Identifier);
return make_tag_stmt(f, token, name, statement);
}
AstNode *unparen_expr(AstNode *node) {
for (;;) {
if (node->kind != AstNode_ParenExpr)
return node;
node = node->ParenExpr.expr;
}
}
AstNode *parse_value(AstFile *f);
AstNodeArray parse_element_list(AstFile *f) {
AstNodeArray elems = make_ast_node_array(f);
while (f->curr_token.kind != Token_CloseBrace &&
f->curr_token.kind != Token_EOF) {
AstNode *elem = parse_value(f);
if (f->curr_token.kind == Token_Eq) {
Token eq = expect_token(f, Token_Eq);
AstNode *value = parse_value(f);
elem = make_field_value(f, elem, value, eq);
}
array_add(&elems, elem);
if (f->curr_token.kind != Token_Comma) {
break;
}
next_token(f);
}
return elems;
}
AstNode *parse_literal_value(AstFile *f, AstNode *type) {
AstNodeArray elems = {};
Token open = expect_token(f, Token_OpenBrace);
f->expr_level++;
if (f->curr_token.kind != Token_CloseBrace) {
elems = parse_element_list(f);
}
f->expr_level--;
Token close = expect_token(f, Token_CloseBrace);
return make_compound_lit(f, type, elems, open, close);
}
AstNode *parse_value(AstFile *f) {
if (f->curr_token.kind == Token_OpenBrace)
return parse_literal_value(f, NULL);
AstNode *value = parse_expr(f, false);
return value;
}
AstNode *parse_identifier_or_type(AstFile *f, u32 flags = 0);
void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) {
if (*tags & tag) {
syntax_error(ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name));
}
*tags |= tag;
}
b32 is_foreign_name_valid(String name) {
// TODO(bill): is_foreign_name_valid
if (name.len == 0)
return false;
isize offset = 0;
while (offset < name.len) {
Rune rune;
isize remaining = name.len - offset;
isize width = gb_utf8_decode(name.text+offset, remaining, &rune);
if (rune == GB_RUNE_INVALID && width == 1) {
return false;
} else if (rune == GB_RUNE_BOM && remaining > 0) {
return false;
}
if (offset == 0) {
switch (rune) {
case '-':
case '$':
case '.':
case '_':
break;
default:
if (!gb_char_is_alpha(cast(char)rune))
return false;
break;
}
} else {
switch (rune) {
case '-':
case '$':
case '.':
case '_':
break;
default:
if (!gb_char_is_alphanumeric(cast(char)rune)) {
return false;
}
break;
}
}
offset += width;
}
return true;
}
void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name, String *link_name) {
// TODO(bill): Add this to procedure literals too
GB_ASSERT(foreign_name != NULL);
GB_ASSERT(link_name != NULL);
while (f->curr_token.kind == Token_Hash) {
AstNode *tag_expr = parse_tag_expr(f, NULL);
ast_node(te, TagExpr, tag_expr);
String tag_name = te->name.string;
#define ELSE_IF_ADD_TAG(name) \
else if (tag_name == #name) { \
check_proc_add_tag(f, tag_expr, tags, ProcTag_##name, tag_name); \
}
if (tag_name == "foreign") {
check_proc_add_tag(f, tag_expr, tags, ProcTag_foreign, tag_name);
if (f->curr_token.kind == Token_String) {
*foreign_name = f->curr_token.string;
// TODO(bill): Check if valid string
if (!is_foreign_name_valid(*foreign_name)) {
syntax_error(ast_node_token(tag_expr), "Invalid alternative foreign procedure name: `%.*s`", LIT(*foreign_name));
}
next_token(f);
}
} else if (tag_name == "link_name") {
check_proc_add_tag(f, tag_expr, tags, ProcTag_link_name, tag_name);
if (f->curr_token.kind == Token_String) {
*link_name = f->curr_token.string;
// TODO(bill): Check if valid string
if (!is_foreign_name_valid(*link_name)) {
syntax_error(ast_node_token(tag_expr), "Invalid alternative link procedure name `%.*s`", LIT(*link_name));
}
next_token(f);
} else {
expect_token(f, Token_String);
}
}
ELSE_IF_ADD_TAG(bounds_check)
ELSE_IF_ADD_TAG(no_bounds_check)
ELSE_IF_ADD_TAG(inline)
ELSE_IF_ADD_TAG(no_inline)
ELSE_IF_ADD_TAG(dll_import)
ELSE_IF_ADD_TAG(dll_export)
ELSE_IF_ADD_TAG(stdcall)
ELSE_IF_ADD_TAG(fastcall)
// ELSE_IF_ADD_TAG(cdecl)
else {
syntax_error(ast_node_token(tag_expr), "Unknown procedure tag");
}
#undef ELSE_IF_ADD_TAG
}
if ((*tags & ProcTag_foreign) && (*tags & ProcTag_link_name)) {
syntax_error(f->curr_token, "You cannot apply both #foreign and #link_name to a procedure");
}
if ((*tags & ProcTag_inline) && (*tags & ProcTag_no_inline)) {
syntax_error(f->curr_token, "You cannot apply both #inline and #no_inline to a procedure");
}
if ((*tags & ProcTag_bounds_check) && (*tags & ProcTag_no_bounds_check)) {
syntax_error(f->curr_token, "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
}
if (((*tags & ProcTag_bounds_check) || (*tags & ProcTag_no_bounds_check)) && (*tags & ProcTag_foreign)) {
syntax_error(f->curr_token, "You cannot apply both #bounds_check or #no_bounds_check to a procedure without a body");
}
if ((*tags & ProcTag_stdcall) && (*tags & ProcTag_fastcall)) {
syntax_error(f->curr_token, "You cannot apply one calling convention to a procedure");
}
}
AstNode *parse_operand(AstFile *f, b32 lhs) {
AstNode *operand = NULL; // Operand
switch (f->curr_token.kind) {
case Token_Identifier:
operand = parse_identifier(f);
if (!lhs) {
// TODO(bill): Handle?
}
return operand;
case Token_Integer:
case Token_Float:
case Token_String:
case Token_Rune:
operand = make_basic_lit(f, f->curr_token);
next_token(f);
return operand;
case Token_OpenParen: {
Token open, close;
// NOTE(bill): Skip the Paren Expression
open = expect_token(f, Token_OpenParen);
f->expr_level++;
operand = parse_expr(f, false);
f->expr_level--;
close = expect_token(f, Token_CloseParen);
return make_paren_expr(f, operand, open, close);
}
case Token_Hash: {
Token token = expect_token(f, Token_Hash);
Token name = expect_token(f, Token_Identifier);
if (name.string == "rune") {
if (f->curr_token.kind == Token_String) {
Token *s = &f->curr_token;
if (gb_utf8_strnlen(s->string.text, s->string.len) != 1) {
syntax_error(*s, "Invalid rune literal %.*s", LIT(s->string));
}
s->kind = Token_Rune; // NOTE(bill): Change it
} else {
expect_token(f, Token_String);
}
operand = parse_operand(f, lhs);
} else if (name.string == "file") {
Token token = name;
token.kind = Token_String;
token.string = token.pos.file;
return make_basic_lit(f, token);
} else if (name.string == "line") {
Token token = name;
token.kind = Token_Integer;
char *str = gb_alloc_array(gb_arena_allocator(&f->arena), char, 20);
gb_i64_to_str(token.pos.line, str, 10);
token.string = make_string(str);
return make_basic_lit(f, token);
} else if (name.string == "run") {
AstNode *expr = parse_expr(f, false);
operand = make_run_expr(f, token, name, expr);
if (unparen_expr(expr)->kind != AstNode_CallExpr) {
error(ast_node_token(expr), "#run can only be applied to procedure calls");
operand = make_bad_expr(f, token, f->curr_token);
}
warning(token, "#run is not yet implemented");
} else {
operand = make_tag_expr(f, token, name, parse_expr(f, false));
}
return operand;
}
// Parse Procedure Type or Literal
case Token_proc: {
AstNode *curr_proc = f->curr_proc;
AstNode *type = parse_proc_type(f);
f->curr_proc = type;
defer (f->curr_proc = curr_proc);
u64 tags = 0;
String foreign_name = {};
String link_name = {};
parse_proc_tags(f, &tags, &foreign_name, &link_name);
if (tags & ProcTag_foreign) {
syntax_error(f->curr_token, "#foreign cannot be applied to procedure literals");
}
if (tags & ProcTag_link_name) {
syntax_error(f->curr_token, "#link_name cannot be applied to procedure literals");
}
if (f->curr_token.kind != Token_OpenBrace) {
return type;
} else {
AstNode *body;
f->expr_level++;
body = parse_body(f);
f->expr_level--;
return make_proc_lit(f, type, body, tags);
}
}
default: {
AstNode *type = parse_identifier_or_type(f);
if (type != NULL) {
// NOTE(bill): Sanity check as identifiers should be handled already
GB_ASSERT_MSG(type->kind != AstNode_Ident, "Type Cannot be identifier");
return type;
}
}
}
Token begin = f->curr_token;
syntax_error(begin, "Expected an operand");
fix_advance_to_next_stmt(f);
return make_bad_expr(f, begin, f->curr_token);
}
b32 is_literal_type(AstNode *node) {
switch (node->kind) {
case AstNode_BadExpr:
case AstNode_Ident:
case AstNode_SelectorExpr:
case AstNode_ArrayType:
case AstNode_VectorType:
case AstNode_StructType:
return true;
}
return false;
}
AstNode *parse_call_expr(AstFile *f, AstNode *operand) {
AstNodeArray args = make_ast_node_array(f);
Token open_paren, close_paren;
Token ellipsis = {};
f->expr_level++;
open_paren = expect_token(f, Token_OpenParen);
while (f->curr_token.kind != Token_CloseParen &&
f->curr_token.kind != Token_EOF &&
ellipsis.pos.line == 0) {
if (f->curr_token.kind == Token_Comma)
syntax_error(f->curr_token, "Expected an expression not a ,");
if (f->curr_token.kind == Token_Ellipsis) {
ellipsis = f->curr_token;
next_token(f);
}
AstNode *arg = parse_expr(f, false);
array_add(&args, arg);
if (f->curr_token.kind != Token_Comma) {
if (f->curr_token.kind == Token_CloseParen)
break;
}
next_token(f);
}
f->expr_level--;
close_paren = expect_token(f, Token_CloseParen);
return make_call_expr(f, operand, args, open_paren, close_paren, ellipsis);
}
AstNode *parse_atom_expr(AstFile *f, b32 lhs) {
AstNode *operand = parse_operand(f, lhs);
b32 loop = true;
while (loop) {
switch (f->curr_token.kind) {
case Token_Prime: {
Token op = expect_token(f, Token_Prime);
if (lhs) {
// TODO(bill): Handle this
}
AstNode *proc = parse_identifier(f);
Array<AstNode *> args;
array_init(&args, gb_arena_allocator(&f->arena), 1);
array_add(&args, operand);
operand = make_call_expr(f, proc, args, ast_node_token(operand), op, empty_token);
} break;
case Token_OpenParen: {
if (lhs) {
// TODO(bill): Handle this shit! Is this even allowed in this language?!
}
operand = parse_call_expr(f, operand);
} break;
case Token_Period: {
Token token = f->curr_token;
next_token(f);
if (lhs) {
// TODO(bill): handle this
}
switch (f->curr_token.kind) {
case Token_Identifier:
operand = make_selector_expr(f, token, operand, parse_identifier(f));
break;
default: {
syntax_error(f->curr_token, "Expected a selector");
next_token(f);
operand = make_selector_expr(f, f->curr_token, operand, NULL);
} break;
}
} break;
case Token_OpenBracket: {
if (lhs) {
// TODO(bill): Handle this
}
Token open, close;
AstNode *indices[3] = {};
f->expr_level++;
open = expect_token(f, Token_OpenBracket);
if (f->curr_token.kind != Token_Colon)
indices[0] = parse_expr(f, false);
isize colon_count = 0;
Token colons[2] = {};
while (f->curr_token.kind == Token_Colon && colon_count < 2) {
colons[colon_count++] = f->curr_token;
next_token(f);
if (f->curr_token.kind != Token_Colon &&
f->curr_token.kind != Token_CloseBracket &&
f->curr_token.kind != Token_EOF) {
indices[colon_count] = parse_expr(f, false);
}
}
f->expr_level--;
close = expect_token(f, Token_CloseBracket);
if (colon_count == 0) {
operand = make_index_expr(f, operand, indices[0], open, close);
} else {
b32 triple_indexed = false;
if (colon_count == 2) {
triple_indexed = true;
if (indices[1] == NULL) {
syntax_error(colons[0], "Second index is required in a triple indexed slice");
indices[1] = make_bad_expr(f, colons[0], colons[1]);
}
if (indices[2] == NULL) {
syntax_error(colons[1], "Third index is required in a triple indexed slice");
indices[2] = make_bad_expr(f, colons[1], close);
}
}
operand = make_slice_expr(f, operand, open, close, indices[0], indices[1], indices[2], triple_indexed);
}
} break;
case Token_Pointer: // Deference
operand = make_deref_expr(f, operand, expect_token(f, Token_Pointer));
break;
case Token_Maybe: // Demaybe
operand = make_demaybe_expr(f, operand, expect_token(f, Token_Maybe));
break;
case Token_OpenBrace: {
if (!lhs && is_literal_type(operand) && f->expr_level >= 0) {
if (f->curr_token.pos.line == f->prev_token.pos.line) {
// TODO(bill): This is a hack due to optional semicolons
// TODO(bill): It's probably much better to solve this by changing
// the syntax for struct literals and array literals
operand = parse_literal_value(f, operand);
} else {
loop = false;
}
} else {
loop = false;
}
} break;
default:
loop = false;
break;
}
lhs = false; // NOTE(bill): 'tis not lhs anymore
}
return operand;
}
AstNode *parse_type(AstFile *f);
AstNode *parse_unary_expr(AstFile *f, b32 lhs) {
switch (f->curr_token.kind) {
case Token_Pointer:
case Token_Maybe:
case Token_Add:
case Token_Sub:
case Token_Not:
case Token_Xor: {
AstNode *operand;
Token op = f->curr_token;
next_token(f);
operand = parse_unary_expr(f, lhs);
return make_unary_expr(f, op, operand);
} break;
}
return parse_atom_expr(f, lhs);
}
// NOTE(bill): result == priority
i32 token_precedence(Token t) {
switch (t.kind) {
case Token_CmpOr:
return 1;
case Token_CmpAnd:
return 2;
case Token_CmpEq:
case Token_NotEq:
case Token_Lt:
case Token_Gt:
case Token_LtEq:
case Token_GtEq:
return 3;
case Token_Add:
case Token_Sub:
case Token_Or:
case Token_Xor:
return 4;
case Token_Mul:
case Token_Quo:
case Token_Mod:
case Token_And:
case Token_AndNot:
case Token_Shl:
case Token_Shr:
return 5;
case Token_DoublePrime:
return 6;
case Token_as:
case Token_transmute:
case Token_down_cast:
case Token_union_cast:
return 7;
}
return 0;
}
AstNode *parse_binary_expr(AstFile *f, b32 lhs, i32 prec_in) {
AstNode *expression = parse_unary_expr(f, lhs);
for (i32 prec = token_precedence(f->curr_token); prec >= prec_in; prec--) {
for (;;) {
AstNode *right;
Token op = f->curr_token;
i32 op_prec = token_precedence(op);
if (op_prec != prec)
break;
expect_operator(f); // NOTE(bill): error checks too
if (lhs) {
// TODO(bill): error checking
lhs = false;
}
switch (op.kind) {
case Token_DoublePrime: {
// TODO(bill): Properly define semantic for in-fix and post-fix calls
AstNode *proc = parse_identifier(f);
/* if (f->curr_token.kind == Token_OpenParen) {
AstNode *call = parse_call_expr(f, proc);
array_add(&call->CallExpr.args, expression);
for (isize i = gb_array_count(call->CallExpr.args)-1; i > 0; i--) {
gb_swap(AstNode *, call->CallExpr.args[i], call->CallExpr.args[i-1]);
}
expression = call;
} else */{
right = parse_binary_expr(f, false, prec+1);
Array<AstNode *> args = {};
array_init(&args, gb_arena_allocator(&f->arena), 2);
array_add(&args, expression);
array_add(&args, right);
expression = make_call_expr(f, proc, args, op, ast_node_token(right), empty_token);
}
continue;
} break;
case Token_as:
case Token_transmute:
case Token_down_cast:
case Token_union_cast:
right = parse_type(f);
break;
default:
right = parse_binary_expr(f, false, prec+1);
if (!right) {
syntax_error(op, "Expected expression on the right hand side of the binary operator");
}
break;
}
expression = make_binary_expr(f, op, expression, right);
}
}
return expression;
}
AstNode *parse_expr(AstFile *f, b32 lhs) {
return parse_binary_expr(f, lhs, 0+1);
}
AstNodeArray parse_expr_list(AstFile *f, b32 lhs) {
AstNodeArray list = make_ast_node_array(f);
do {
AstNode *e = parse_expr(f, lhs);
array_add(&list, e);
if (f->curr_token.kind != Token_Comma ||
f->curr_token.kind == Token_EOF) {
break;
}
next_token(f);
} while (true);
return list;
}
AstNodeArray parse_lhs_expr_list(AstFile *f) {
return parse_expr_list(f, true);
}
AstNodeArray parse_rhs_expr_list(AstFile *f) {
return parse_expr_list(f, false);
}
AstNode *parse_decl(AstFile *f, AstNodeArray names);
AstNode *parse_simple_stmt(AstFile *f) {
isize lhs_count = 0, rhs_count = 0;
AstNodeArray lhs = parse_lhs_expr_list(f);
AstNode *statement = NULL;
Token token = f->curr_token;
switch (token.kind) {
case Token_Eq:
case Token_AddEq:
case Token_SubEq:
case Token_MulEq:
case Token_QuoEq:
case Token_ModEq:
case Token_AndEq:
case Token_OrEq:
case Token_XorEq:
case Token_ShlEq:
case Token_ShrEq:
case Token_AndNotEq:
case Token_CmpAndEq:
case Token_CmpOrEq:
{
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a simple statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
next_token(f);
AstNodeArray rhs = parse_rhs_expr_list(f);
if (rhs.count == 0) {
syntax_error(token, "No right-hand side in assignment statement.");
return make_bad_stmt(f, token, f->curr_token);
}
return make_assign_stmt(f, token, lhs, rhs);
} break;
case Token_Colon: // Declare
return parse_decl(f, lhs);
}
if (lhs_count > 1) {
syntax_error(token, "Expected 1 expression");
return make_bad_stmt(f, token, f->curr_token);
}
token = f->curr_token;
switch (token.kind) {
case Token_Increment:
case Token_Decrement:
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a simple statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
statement = make_inc_dec_stmt(f, token, lhs[0]);
next_token(f);
return statement;
}
return make_expr_stmt(f, lhs[0]);
}
AstNode *parse_block_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a block statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
AstNode *block_stmt = parse_body(f);
return block_stmt;
}
AstNode *convert_stmt_to_expr(AstFile *f, AstNode *statement, String kind) {
if (statement == NULL)
return NULL;
if (statement->kind == AstNode_ExprStmt)
return statement->ExprStmt.expr;
syntax_error(f->curr_token, "Expected `%.*s`, found a simple statement.", LIT(kind));
return make_bad_expr(f, f->curr_token, f->tokens[f->curr_token_index+1]);
}
AstNodeArray parse_identfier_list(AstFile *f) {
AstNodeArray list = make_ast_node_array(f);
do {
array_add(&list, parse_identifier(f));
if (f->curr_token.kind != Token_Comma ||
f->curr_token.kind == Token_EOF) {
break;
}
next_token(f);
} while (true);
return list;
}
AstNode *parse_type_attempt(AstFile *f) {
AstNode *type = parse_identifier_or_type(f);
if (type != NULL) {
// TODO(bill): Handle?
}
return type;
}
AstNode *parse_type(AstFile *f) {
AstNode *type = parse_type_attempt(f);
if (type == NULL) {
Token token = f->curr_token;
syntax_error(token, "Expected a type");
next_token(f);
return make_bad_expr(f, token, f->curr_token);
}
return type;
}
Token parse_procedure_signature(AstFile *f,
AstNodeArray *params, AstNodeArray *results);
AstNode *parse_proc_type(AstFile *f) {
AstNodeArray params = {};
AstNodeArray results = {};
Token proc_token = parse_procedure_signature(f, ¶ms, &results);
return make_proc_type(f, proc_token, params, results);
}
AstNodeArray parse_parameter_list(AstFile *f) {
AstNodeArray params = make_ast_node_array(f);
while (f->curr_token.kind == Token_Identifier ||
f->curr_token.kind == Token_using) {
b32 is_using = false;
if (allow_token(f, Token_using)) {
is_using = true;
}
AstNodeArray names = parse_lhs_expr_list(f);
if (names.count == 0) {
syntax_error(f->curr_token, "Empty parameter declaration");
}
if (names.count > 1 && is_using) {
syntax_error(f->curr_token, "Cannot apply `using` to more than one of the same type");
is_using = false;
}
expect_token_after(f, Token_Colon, "parameter list");
AstNode *type = NULL;
if (f->curr_token.kind == Token_Ellipsis) {
Token ellipsis = f->curr_token;
next_token(f);
type = parse_type_attempt(f);
if (type == NULL) {
syntax_error(f->curr_token, "variadic parameter is missing a type after `..`");
type = make_bad_expr(f, ellipsis, f->curr_token);
} else {
if (names.count > 1) {
syntax_error(f->curr_token, "mutliple variadic parameters, only `..`");
} else {
type = make_ellipsis(f, ellipsis, type);
}
}
} else {
type = parse_type_attempt(f);
}
if (type == NULL) {
syntax_error(f->curr_token, "Expected a type for this parameter declaration");
}
array_add(¶ms, make_parameter(f, names, type, is_using));
if (f->curr_token.kind != Token_Comma) {
break;
}
next_token(f);
}
return params;
}
AstNodeArray parse_struct_params(AstFile *f, isize *decl_count_, b32 using_allowed) {
AstNodeArray decls = make_ast_node_array(f);
isize decl_count = 0;
while (f->curr_token.kind == Token_Identifier ||
f->curr_token.kind == Token_using) {
b32 is_using = false;
if (allow_token(f, Token_using)) {
is_using = true;
}
AstNodeArray names = parse_lhs_expr_list(f);
if (names.count == 0) {
syntax_error(f->curr_token, "Empty field declaration");
}
if (!using_allowed && is_using) {
syntax_error(f->curr_token, "Cannot apply `using` to members of a union");
is_using = false;
}
if (names.count > 1 && is_using) {
syntax_error(f->curr_token, "Cannot apply `using` to more than one of the same type");
}
AstNode *decl = NULL;
if (f->curr_token.kind == Token_Colon) {
decl = parse_decl(f, names);
if (decl->kind == AstNode_ProcDecl) {
syntax_error(f->curr_token, "Procedure declarations are not allowed within a structure");
decl = make_bad_decl(f, ast_node_token(names[0]), f->curr_token);
}
} else {
syntax_error(f->curr_token, "Illegal structure field");
decl = make_bad_decl(f, ast_node_token(names[0]), f->curr_token);
}
expect_semicolon_after_stmt(f, decl);
if (decl != NULL && is_ast_node_decl(decl)) {
array_add(&decls, decl);
if (decl->kind == AstNode_VarDecl) {
decl->VarDecl.is_using = is_using && using_allowed;
if (decl->VarDecl.values.count > 0) {
syntax_error(f->curr_token, "Default variable assignments within a structure will be ignored (at the moment)");
}
} else {
decl_count += 1;
}
}
}
if (decl_count_) *decl_count_ = decl_count;
return decls;
}
AstNode *parse_identifier_or_type(AstFile *f, u32 flags) {
switch (f->curr_token.kind) {
case Token_Identifier: {
AstNode *e = parse_identifier(f);
while (f->curr_token.kind == Token_Period) {
Token token = f->curr_token;
next_token(f);
AstNode *sel = parse_identifier(f);
e = make_selector_expr(f, token, e, sel);
}
if (f->curr_token.kind == Token_OpenParen) {
// HACK NOTE(bill): For type_of_val(expr)
e = parse_call_expr(f, e);
}
return e;
}
case Token_Pointer: {
Token token = expect_token(f, Token_Pointer);
AstNode *elem = parse_type(f);
return make_pointer_type(f, token, elem);
}
case Token_Maybe: {
Token token = expect_token(f, Token_Maybe);
AstNode *elem = parse_type(f);
return make_maybe_type(f, token, elem);
}
case Token_OpenBracket: {
f->expr_level++;
Token token = expect_token(f, Token_OpenBracket);
AstNode *count_expr = NULL;
if (f->curr_token.kind == Token_Ellipsis) {
count_expr = make_ellipsis(f, f->curr_token, NULL);
next_token(f);
} else if (f->curr_token.kind != Token_CloseBracket) {
count_expr = parse_expr(f, false);
}
expect_token(f, Token_CloseBracket);
f->expr_level--;
AstNode *e = make_array_type(f, token, count_expr, parse_type(f));
return e;
}
case Token_OpenBrace: {
f->expr_level++;
Token token = expect_token(f, Token_OpenBrace);
AstNode *count_expr = parse_expr(f, false);
expect_token(f, Token_CloseBrace);
f->expr_level--;
return make_vector_type(f, token, count_expr, parse_type(f));
}
case Token_struct: {
Token token = expect_token(f, Token_struct);
b32 is_packed = false;
b32 is_ordered = false;
while (allow_token(f, Token_Hash)) {
Token tag = expect_token_after(f, Token_Identifier, "`#`");
if (tag.string == "packed") {
if (is_packed) {
syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string));
}
is_packed = true;
} else if (tag.string == "ordered") {
if (is_ordered) {
syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string));
}
is_ordered = true;
} else {
syntax_error(tag, "Invalid struct tag `#%.*s`", LIT(tag.string));
}
}
if (is_packed && is_ordered) {
syntax_error(token, "`#ordered` is not needed with `#packed` which implies ordering");
}
Token open = expect_token_after(f, Token_OpenBrace, "`struct`");
isize decl_count = 0;
AstNodeArray decls = parse_struct_params(f, &decl_count, true);
Token close = expect_token(f, Token_CloseBrace);
return make_struct_type(f, token, decls, decl_count, is_packed, is_ordered);
} break;
case Token_union: {
Token token = expect_token(f, Token_union);
Token open = expect_token_after(f, Token_OpenBrace, "`union`");
isize decl_count = 0;
AstNodeArray decls = parse_struct_params(f, &decl_count, false);
Token close = expect_token(f, Token_CloseBrace);
return make_union_type(f, token, decls, decl_count);
}
case Token_raw_union: {
Token token = expect_token_after(f, Token_OpenBrace, "`raw_union`");
Token open = expect_token(f, Token_OpenBrace);
isize decl_count = 0;
AstNodeArray decls = parse_struct_params(f, &decl_count, true);
Token close = expect_token(f, Token_CloseBrace);
return make_raw_union_type(f, token, decls, decl_count);
}
case Token_enum: {
Token token = expect_token(f, Token_enum);
AstNode *base_type = NULL;
Token open, close;
if (f->curr_token.kind != Token_OpenBrace) {
base_type = parse_type(f);
}
AstNodeArray fields = make_ast_node_array(f);
open = expect_token_after(f, Token_OpenBrace, "`enum`");
while (f->curr_token.kind != Token_CloseBrace &&
f->curr_token.kind != Token_EOF) {
AstNode *name = parse_identifier(f);
AstNode *value = NULL;
Token eq = empty_token;
if (f->curr_token.kind == Token_Eq) {
eq = expect_token(f, Token_Eq);
value = parse_value(f);
}
AstNode *field = make_field_value(f, name, value, eq);
array_add(&fields, field);
if (f->curr_token.kind != Token_Comma) {
break;
}
next_token(f);
}
close = expect_token(f, Token_CloseBrace);
return make_enum_type(f, token, base_type, fields);
}
case Token_proc: {
AstNode *curr_proc = f->curr_proc;
AstNode *type = parse_proc_type(f);
f->curr_proc = type;
f->curr_proc = curr_proc;
return type;
}
case Token_OpenParen: {
// NOTE(bill): Skip the paren expression
AstNode *type;
Token open, close;
open = expect_token(f, Token_OpenParen);
type = parse_type(f);
close = expect_token(f, Token_CloseParen);
return type;
// return make_paren_expr(f, type, open, close);
}
// TODO(bill): Why is this even allowed? Is this a parsing error?
case Token_Colon:
break;
case Token_Eq:
if (f->prev_token.kind == Token_Colon)
break;
// fallthrough
default:
syntax_error(f->curr_token,
"Expected a type or identifier after `%.*s`, got `%.*s`", LIT(f->prev_token.string), LIT(f->curr_token.string));
break;
}
return NULL;
}
AstNodeArray parse_results(AstFile *f) {
AstNodeArray results = make_ast_node_array(f);
if (allow_token(f, Token_ArrowRight)) {
if (f->curr_token.kind == Token_OpenParen) {
expect_token(f, Token_OpenParen);
while (f->curr_token.kind != Token_CloseParen &&
f->curr_token.kind != Token_EOF) {
array_add(&results, parse_type(f));
if (f->curr_token.kind != Token_Comma) {
break;
}
next_token(f);
}
expect_token(f, Token_CloseParen);
return results;
}
array_add(&results, parse_type(f));
return results;
}
return results;
}
Token parse_procedure_signature(AstFile *f,
AstNodeArray *params,
AstNodeArray *results) {
Token proc_token = expect_token(f, Token_proc);
expect_token(f, Token_OpenParen);
*params = parse_parameter_list(f);
expect_token_after(f, Token_CloseParen, "parameter list");
*results = parse_results(f);
return proc_token;
}
AstNode *parse_body(AstFile *f) {
AstNodeArray stmts = {};
Token open, close;
open = expect_token(f, Token_OpenBrace);
stmts = parse_stmt_list(f);
close = expect_token(f, Token_CloseBrace);
return make_block_stmt(f, stmts, open, close);
}
AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) {
AstNodeArray params = {};
AstNodeArray results = {};
parse_procedure_signature(f, ¶ms, &results);
AstNode *proc_type = make_proc_type(f, proc_token, params, results);
AstNode *body = NULL;
u64 tags = 0;
String foreign_name = {};
String link_name = {};
parse_proc_tags(f, &tags, &foreign_name, &link_name);
AstNode *curr_proc = f->curr_proc;
f->curr_proc = proc_type;
defer (f->curr_proc = curr_proc);
if (f->curr_token.kind == Token_OpenBrace) {
if ((tags & ProcTag_foreign) != 0) {
syntax_error(f->curr_token, "A procedure tagged as `#foreign` cannot have a body");
}
body = parse_body(f);
}
return make_proc_decl(f, name, proc_type, body, tags, foreign_name, link_name);
}
AstNode *parse_decl(AstFile *f, AstNodeArray names) {
AstNodeArray values = {};
AstNode *type = NULL;
for_array(i, names) {
AstNode *name = names[i];
if (name->kind == AstNode_Ident) {
String n = name->Ident.string;
// NOTE(bill): Check for reserved identifiers
if (n == "context") {
syntax_error(ast_node_token(name), "`context` is a reserved identifier");
break;
}
}
}
if (allow_token(f, Token_Colon)) {
if (!allow_token(f, Token_type)) {
type = parse_identifier_or_type(f);
}
} else if (f->curr_token.kind != Token_Eq && f->curr_token.kind != Token_Semicolon) {
syntax_error(f->curr_token, "Expected type separator `:` or `=`");
}
b32 is_mutable = true;
if (f->curr_token.kind == Token_Eq ||
f->curr_token.kind == Token_Colon) {
if (f->curr_token.kind == Token_Colon) {
is_mutable = false;
}
next_token(f);
if (f->curr_token.kind == Token_type ||
f->curr_token.kind == Token_struct ||
f->curr_token.kind == Token_enum ||
f->curr_token.kind == Token_union ||
f->curr_token.kind == Token_raw_union) {
Token token = f->curr_token;
if (token.kind == Token_type) {
next_token(f);
}
if (names.count != 1) {
syntax_error(ast_node_token(names[0]), "You can only declare one type at a time");
return make_bad_decl(f, names[0]->Ident, token);
}
if (type != NULL) {
syntax_error(f->prev_token, "Expected either `type` or nothing between : and :");
// NOTE(bill): Do not fail though
}
return make_type_decl(f, token, names[0], parse_type(f));
} else if (f->curr_token.kind == Token_proc &&
is_mutable == false) {
// NOTE(bill): Procedure declarations
Token proc_token = f->curr_token;
AstNode *name = names[0];
if (names.count != 1) {
syntax_error(proc_token, "You can only declare one procedure at a time");
return make_bad_decl(f, name->Ident, proc_token);
}
return parse_proc_decl(f, proc_token, name);
} else {
values = parse_rhs_expr_list(f);
if (values.count > names.count) {
syntax_error(f->curr_token, "Too many values on the right hand side of the declaration");
} else if (values.count < names.count && !is_mutable) {
syntax_error(f->curr_token, "All constant declarations must be defined");
} else if (values.count == 0) {
syntax_error(f->curr_token, "Expected an expression for this declaration");
}
}
}
if (is_mutable) {
if (type == NULL && values.count == 0) {
syntax_error(f->curr_token, "Missing variable type or initialization");
return make_bad_decl(f, f->curr_token, f->curr_token);
}
} else {
if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing constant value");
return make_bad_decl(f, f->curr_token, f->curr_token);
}
}
if (values.data == NULL) {
values = make_ast_node_array(f);
}
if (is_mutable) {
return make_var_decl(f, names, type, values);
}
return make_const_decl(f, names, type, values);
}
AstNode *parse_if_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use an if statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
Token token = expect_token(f, Token_if);
AstNode *init = NULL;
AstNode *cond = NULL;
AstNode *body = NULL;
AstNode *else_stmt = NULL;
isize prev_level = f->expr_level;
f->expr_level = -1;
if (allow_token(f, Token_Semicolon)) {
cond = parse_expr(f, false);
} else {
init = parse_simple_stmt(f);
if (allow_token(f, Token_Semicolon)) {
cond = parse_expr(f, false);
} else {
cond = convert_stmt_to_expr(f, init, make_string("boolean expression"));
init = NULL;
}
}
f->expr_level = prev_level;
if (cond == NULL) {
syntax_error(f->curr_token, "Expected condition for if statement");
}
body = parse_block_stmt(f);
if (allow_token(f, Token_else)) {
switch (f->curr_token.kind) {
case Token_if:
else_stmt = parse_if_stmt(f);
break;
case Token_OpenBrace:
else_stmt = parse_block_stmt(f);
break;
default:
syntax_error(f->curr_token, "Expected if statement block statement");
else_stmt = make_bad_stmt(f, f->curr_token, f->tokens[f->curr_token_index+1]);
break;
}
}
return make_if_stmt(f, token, init, cond, body, else_stmt);
}
AstNode *parse_return_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a return statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
Token token = expect_token(f, Token_return);
AstNodeArray results = make_ast_node_array(f);
if (f->curr_token.kind != Token_Semicolon && f->curr_token.kind != Token_CloseBrace &&
f->curr_token.pos.line == token.pos.line) {
results = parse_rhs_expr_list(f);
}
if (f->curr_token.kind != Token_CloseBrace) {
expect_semicolon_after_stmt(f, results[0]);
}
return make_return_stmt(f, token, results);
}
AstNode *parse_for_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a for statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
Token token = expect_token(f, Token_for);
AstNode *init = NULL;
AstNode *cond = NULL;
AstNode *end = NULL;
AstNode *body = NULL;
if (f->curr_token.kind != Token_OpenBrace) {
isize prev_level = f->expr_level;
f->expr_level = -1;
if (f->curr_token.kind != Token_Semicolon) {
cond = parse_simple_stmt(f);
if (is_ast_node_complex_stmt(cond)) {
syntax_error(f->curr_token,
"You are not allowed that type of statement in a for statement, it is too complex!");
}
}
if (allow_token(f, Token_Semicolon)) {
init = cond;
cond = NULL;
if (f->curr_token.kind != Token_Semicolon) {
cond = parse_simple_stmt(f);
}
expect_token(f, Token_Semicolon);
if (f->curr_token.kind != Token_OpenBrace) {
end = parse_simple_stmt(f);
}
}
f->expr_level = prev_level;
}
body = parse_block_stmt(f);
cond = convert_stmt_to_expr(f, cond, make_string("boolean expression"));
return make_for_stmt(f, token, init, cond, end, body);
}
AstNode *parse_case_clause(AstFile *f) {
Token token = f->curr_token;
AstNodeArray list = make_ast_node_array(f);
if (allow_token(f, Token_case)) {
list = parse_rhs_expr_list(f);
} else {
expect_token(f, Token_default);
}
expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
// expect_token(f, Token_ArrowRight); // TODO(bill): Is this the best syntax?
AstNodeArray stmts = parse_stmt_list(f);
return make_case_clause(f, token, list, stmts);
}
AstNode *parse_type_case_clause(AstFile *f) {
Token token = f->curr_token;
AstNodeArray clause = make_ast_node_array(f);
if (allow_token(f, Token_case)) {
array_add(&clause, parse_expr(f, false));
} else {
expect_token(f, Token_default);
}
expect_token(f, Token_Colon); // TODO(bill): Is this the best syntax?
// expect_token(f, Token_ArrowRight); // TODO(bill): Is this the best syntax?
AstNodeArray stmts = parse_stmt_list(f);
return make_case_clause(f, token, clause, stmts);
}
AstNode *parse_match_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a match statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
Token token = expect_token(f, Token_match);
AstNode *init = NULL;
AstNode *tag = NULL;
AstNode *body = NULL;
Token open, close;
if (allow_token(f, Token_type)) {
isize prev_level = f->expr_level;
f->expr_level = -1;
AstNode *var = parse_identifier(f);
expect_token(f, Token_Colon);
tag = parse_simple_stmt(f);
f->expr_level = prev_level;
open = expect_token(f, Token_OpenBrace);
AstNodeArray list = make_ast_node_array(f);
while (f->curr_token.kind == Token_case ||
f->curr_token.kind == Token_default) {
array_add(&list, parse_type_case_clause(f));
}
close = expect_token(f, Token_CloseBrace);
body = make_block_stmt(f, list, open, close);
tag = convert_stmt_to_expr(f, tag, make_string("type match expression"));
return make_type_match_stmt(f, token, tag, var, body);
} else {
if (f->curr_token.kind != Token_OpenBrace) {
isize prev_level = f->expr_level;
f->expr_level = -1;
if (f->curr_token.kind != Token_Semicolon) {
tag = parse_simple_stmt(f);
}
if (allow_token(f, Token_Semicolon)) {
init = tag;
tag = NULL;
if (f->curr_token.kind != Token_OpenBrace) {
tag = parse_simple_stmt(f);
}
}
f->expr_level = prev_level;
}
open = expect_token(f, Token_OpenBrace);
AstNodeArray list = make_ast_node_array(f);
while (f->curr_token.kind == Token_case ||
f->curr_token.kind == Token_default) {
array_add(&list, parse_case_clause(f));
}
close = expect_token(f, Token_CloseBrace);
body = make_block_stmt(f, list, open, close);
tag = convert_stmt_to_expr(f, tag, make_string("match expression"));
return make_match_stmt(f, token, init, tag, body);
}
}
AstNode *parse_defer_stmt(AstFile *f) {
if (f->curr_proc == NULL) {
syntax_error(f->curr_token, "You cannot use a defer statement in the file scope");
return make_bad_stmt(f, f->curr_token, f->curr_token);
}
Token token = expect_token(f, Token_defer);
AstNode *statement = parse_stmt(f);
switch (statement->kind) {
case AstNode_EmptyStmt:
syntax_error(token, "Empty statement after defer (e.g. `;`)");
break;
case AstNode_DeferStmt:
syntax_error(token, "You cannot defer a defer statement");
break;
case AstNode_ReturnStmt:
syntax_error(token, "You cannot a return statement");
break;
}
return make_defer_stmt(f, token, statement);
}
AstNode *parse_asm_stmt(AstFile *f) {
Token token = expect_token(f, Token_asm);
b32 is_volatile = false;
if (allow_token(f, Token_volatile)) {
is_volatile = true;
}
Token open, close, code_string;
open = expect_token(f, Token_OpenBrace);
code_string = expect_token(f, Token_String);
AstNode *output_list = NULL;
AstNode *input_list = NULL;
AstNode *clobber_list = NULL;
isize output_count = 0;
isize input_count = 0;
isize clobber_count = 0;
// TODO(bill): Finish asm statement and determine syntax
// if (f->curr_token.kind != Token_CloseBrace) {
// expect_token(f, Token_Colon);
// }
close = expect_token(f, Token_CloseBrace);
return make_asm_stmt(f, token, is_volatile, open, close, code_string,
output_list, input_list, clobber_list,
output_count, input_count, clobber_count);
}
AstNode *parse_stmt(AstFile *f) {
AstNode *s = NULL;
Token token = f->curr_token;
switch (token.kind) {
// Operands
case Token_Identifier:
case Token_Integer:
case Token_Float:
case Token_Rune:
case Token_String:
case Token_OpenParen:
case Token_proc:
// Unary Operators
case Token_Add:
case Token_Sub:
case Token_Xor:
case Token_Not:
s = parse_simple_stmt(f);
expect_semicolon_after_stmt(f, s);
return s;
// TODO(bill): other keywords
case Token_if: return parse_if_stmt(f);
case Token_return: return parse_return_stmt(f);
case Token_for: return parse_for_stmt(f);
case Token_match: return parse_match_stmt(f);
case Token_defer: return parse_defer_stmt(f);
case Token_asm: return parse_asm_stmt(f);
case Token_break:
case Token_continue:
case Token_fallthrough:
next_token(f);
s = make_branch_stmt(f, token);
expect_semicolon_after_stmt(f, s);
return s;
case Token_using: {
AstNode *node = NULL;
next_token(f);
node = parse_stmt(f);
b32 valid = false;
switch (node->kind) {
case AstNode_ExprStmt: {
AstNode *e = unparen_expr(node->ExprStmt.expr);
while (e->kind == AstNode_SelectorExpr) {
e = unparen_expr(e->SelectorExpr.selector);
}
if (e->kind == AstNode_Ident) {
valid = true;
}
} break;
case AstNode_VarDecl:
valid = true;
break;
}
if (!valid) {
syntax_error(token, "Illegal use of `using` statement.");
return make_bad_stmt(f, token, f->curr_token);
}
return make_using_stmt(f, token, node);
} break;
case Token_push_allocator: {
next_token(f);
isize prev_level = f->expr_level;
f->expr_level = -1;
AstNode *expr = parse_expr(f, false);
f->expr_level = prev_level;
AstNode *body = parse_block_stmt(f);
return make_push_allocator(f, token, expr, body);
} break;
case Token_push_context: {
next_token(f);
isize prev_level = f->expr_level;
f->expr_level = -1;
AstNode *expr = parse_expr(f, false);
f->expr_level = prev_level;
AstNode *body = parse_block_stmt(f);
return make_push_context(f, token, expr, body);
} break;
case Token_Hash: {
s = parse_tag_stmt(f, NULL);
String tag = s->TagStmt.name.string;
if (tag == "shared_global_scope") {
if (f->curr_proc == NULL) {
f->is_global_scope = true;
return make_empty_stmt(f, f->curr_token);
}
syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope");
return make_bad_decl(f, token, f->curr_token);
} else if (tag == "import") {
// TODO(bill): better error messages
Token import_name = {};
Token file_path = expect_token_after(f, Token_String, "#import");
if (allow_token(f, Token_as)) {
// NOTE(bill): Custom import name
if (f->curr_token.kind == Token_Period) {
import_name = f->curr_token;
import_name.kind = Token_Identifier;
next_token(f);
} else {
import_name = expect_token_after(f, Token_Identifier, "`as` for import declaration");
}
if (import_name.string == "_") {
syntax_error(token, "Illegal import name: `_`");
return make_bad_decl(f, token, f->curr_token);
}
}
if (f->curr_proc == NULL) {
return make_import_decl(f, s->TagStmt.token, file_path, import_name, false);
}
syntax_error(token, "You cannot use #import within a procedure. This must be done at the file scope");
return make_bad_decl(f, token, file_path);
} else if (tag == "load") {
// TODO(bill): better error messages
Token file_path = expect_token(f, Token_String);
Token import_name = file_path;
import_name.string = make_string(".");
if (f->curr_proc == NULL) {
return make_import_decl(f, s->TagStmt.token, file_path, import_name, true);
}
syntax_error(token, "You cannot use #load within a procedure. This must be done at the file scope");
return make_bad_decl(f, token, file_path);
} else if (tag == "foreign_system_library") {
Token file_path = expect_token(f, Token_String);
if (f->curr_proc == NULL) {
return make_foreign_system_library(f, s->TagStmt.token, file_path);
}
syntax_error(token, "You cannot use #foreign_system_library within a procedure. This must be done at the file scope");
return make_bad_decl(f, token, file_path);
} else if (tag == "thread_local") {
AstNode *var_decl = parse_simple_stmt(f);
if (var_decl->kind != AstNode_VarDecl) {
syntax_error(token, "#thread_local may only be applied to variable declarations");
return make_bad_decl(f, token, ast_node_token(var_decl));
}
if (f->curr_proc != NULL) {
syntax_error(token, "#thread_local is only allowed at the file scope");
return make_bad_decl(f, token, ast_node_token(var_decl));
}
var_decl->VarDecl.tags |= VarDeclTag_thread_local;
return var_decl;
} else if (tag == "bounds_check") {
s = parse_stmt(f);
s->stmt_state_flags |= StmtStateFlag_bounds_check;
if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) {
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
} else if (tag == "no_bounds_check") {
s = parse_stmt(f);
s->stmt_state_flags |= StmtStateFlag_no_bounds_check;
if ((s->stmt_state_flags & StmtStateFlag_bounds_check) != 0) {
syntax_error(token, "#bounds_check and #no_bounds_check cannot be applied together");
}
return s;
}
s->TagStmt.stmt = parse_stmt(f); // TODO(bill): Find out why this doesn't work as an argument
return s;
} break;
case Token_OpenBrace:
return parse_block_stmt(f);
case Token_Semicolon:
s = make_empty_stmt(f, token);
next_token(f);
return s;
}
syntax_error(token,
"Expected a statement, got `%.*s`",
LIT(token_strings[token.kind]));
fix_advance_to_next_stmt(f);
return make_bad_stmt(f, token, f->curr_token);
}
AstNodeArray parse_stmt_list(AstFile *f) {
AstNodeArray list = make_ast_node_array(f);
while (f->curr_token.kind != Token_case &&
f->curr_token.kind != Token_default &&
f->curr_token.kind != Token_CloseBrace &&
f->curr_token.kind != Token_EOF) {
AstNode *stmt = parse_stmt(f);
if (stmt && stmt->kind != AstNode_EmptyStmt) {
array_add(&list, stmt);
}
}
return list;
}
ParseFileError init_ast_file(AstFile *f, String fullpath) {
if (!string_has_extension(fullpath, make_string("odin"))) {
return ParseFile_WrongExtension;
}
TokenizerInitError err = init_tokenizer(&f->tokenizer, fullpath);
if (err == TokenizerInit_None) {
array_init(&f->tokens, gb_heap_allocator());
{
PROF_SCOPED("Tokenize file");
for (;;) {
Token token = tokenizer_get_token(&f->tokenizer);
if (token.kind == Token_Invalid) {
return ParseFile_InvalidToken;
}
array_add(&f->tokens, token);
if (token.kind == Token_EOF) {
break;
}
}
}
f->curr_token_index = 0;
f->prev_token = f->tokens[f->curr_token_index];
f->curr_token = f->tokens[f->curr_token_index];
// NOTE(bill): Is this big enough or too small?
isize arena_size = gb_size_of(AstNode);
arena_size *= 2*f->tokens.count;
gb_arena_init_from_allocator(&f->arena, gb_heap_allocator(), arena_size);
f->curr_proc = NULL;
return ParseFile_None;
}
switch (err) {
case TokenizerInit_NotExists:
return ParseFile_NotFound;
case TokenizerInit_Permission:
return ParseFile_Permission;
case TokenizerInit_Empty:
return ParseFile_EmptyFile;
}
return ParseFile_InvalidFile;
}
void destroy_ast_file(AstFile *f) {
gb_arena_free(&f->arena);
array_free(&f->tokens);
gb_free(gb_heap_allocator(), f->tokenizer.fullpath.text);
destroy_tokenizer(&f->tokenizer);
}
b32 init_parser(Parser *p) {
array_init(&p->files, gb_heap_allocator());
array_init(&p->imports, gb_heap_allocator());
array_init(&p->system_libraries, gb_heap_allocator());
gb_mutex_init(&p->mutex);
return true;
}
void destroy_parser(Parser *p) {
// TODO(bill): Fix memory leak
for_array(i, p->files) {
destroy_ast_file(&p->files[i]);
}
#if 1
for_array(i, p->imports) {
// gb_free(gb_heap_allocator(), p->imports[i].text);
}
#endif
array_free(&p->files);
array_free(&p->imports);
array_free(&p->system_libraries);
gb_mutex_destroy(&p->mutex);
}
// NOTE(bill): Returns true if it's added
b32 try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos) {
gb_mutex_lock(&p->mutex);
defer (gb_mutex_unlock(&p->mutex));
for_array(i, p->imports) {
String import = p->imports[i].path;
if (import == path) {
return false;
}
}
ImportedFile item;
item.path = path;
item.rel_path = rel_path;
item.pos = pos;
array_add(&p->imports, item);
return true;
}
String get_fullpath_relative(gbAllocator a, String base_dir, String path) {
isize str_len = base_dir.len+path.len;
u8 *str = gb_alloc_array(gb_heap_allocator(), u8, str_len+1);
defer (gb_free(gb_heap_allocator(), str));
isize i = 0;
gb_memcopy(str+i, base_dir.text, base_dir.len); i += base_dir.len;
gb_memcopy(str+i, path.text, path.len);
str[str_len] = '\0';
return path_to_fullpath(a, make_string(str, str_len));
}
String get_fullpath_core(gbAllocator a, String path) {
String module_dir = get_module_dir();
char core[] = "core/";
isize core_len = gb_size_of(core)-1;
isize str_len = module_dir.len + core_len + path.len;
u8 *str = gb_alloc_array(gb_heap_allocator(), u8, str_len+1);
defer (gb_free(gb_heap_allocator(), str));
gb_memcopy(str, module_dir.text, module_dir.len);
gb_memcopy(str+module_dir.len, core, core_len);
gb_memcopy(str+module_dir.len+core_len, path.text, path.len);
str[str_len] = '\0';
return path_to_fullpath(a, make_string(str, str_len));
}
// NOTE(bill): Returns true if it's added
b32 try_add_foreign_system_library_path(Parser *p, String import_file) {
gb_mutex_lock(&p->mutex);
defer (gb_mutex_unlock(&p->mutex));
for_array(i, p->system_libraries) {
String import = p->system_libraries[i];
if (import == import_file) {
return false;
}
}
array_add(&p->system_libraries, import_file);
return true;
}
gb_global Rune illegal_import_runes[] = {
'"', '\'', '`', ' ', '\t', '\r', '\n', '\v', '\f',
'\\', // NOTE(bill): Disallow windows style filepaths
'!', '$', '%', '^', '&', '*', '(', ')', '=', '+',
'[', ']', '{', '}',
';', ':', '#',
'|', ',', '<', '>', '?',
};
b32 is_import_path_valid(String path) {
if (path.len > 0) {
u8 *start = path.text;
u8 *end = path.text + path.len;
u8 *curr = start;
Rune r = -1;
while (curr < end) {
isize width = 1;
r = curr[0];
if (r >= 0x80) {
width = gb_utf8_decode(curr, end-curr, &r);
if (r == GB_RUNE_INVALID && width == 1)
return false;
else if (r == GB_RUNE_BOM && curr-start > 0)
return false;
}
for (isize i = 0; i < gb_count_of(illegal_import_runes); i++) {
if (r == illegal_import_runes[i])
return false;
}
curr += width;
}
return true;
}
return false;
}
String get_filepath_extension(String path) {
isize dot = 0;
b32 seen_slash = false;
for (isize i = path.len-1; i >= 0; i--) {
u8 c = path.text[i];
if (c == '/' || c == '\\') {
seen_slash = true;
}
if (c == '.') {
if (seen_slash) {
return make_string("");
}
dot = i;
break;
}
}
return make_string(path.text, dot);
}
void parse_file(Parser *p, AstFile *f) {
PROF_PROC();
String filepath = f->tokenizer.fullpath;
String base_dir = filepath;
for (isize i = filepath.len-1; i >= 0; i--) {
if (base_dir.text[i] == '\\' ||
base_dir.text[i] == '/') {
break;
}
base_dir.len--;
}
f->decls = parse_stmt_list(f);
for_array(i, f->decls) {
AstNode *node = f->decls[i];
if (!is_ast_node_decl(node) &&
node->kind != AstNode_BadStmt &&
node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check
syntax_error(ast_node_token(node), "Only declarations are allowed at file scope");
} else {
if (node->kind == AstNode_ImportDecl) {
auto *id = &node->ImportDecl;
String file_str = id->relpath.string;
if (!is_import_path_valid(file_str)) {
if (id->is_load) {
syntax_error(ast_node_token(node), "Invalid #load path: `%.*s`", LIT(file_str));
} else {
syntax_error(ast_node_token(node), "Invalid #import path: `%.*s`", LIT(file_str));
}
// NOTE(bill): It's a naughty name
f->decls[i] = make_bad_decl(f, id->token, id->token);
continue;
}
gbAllocator allocator = gb_heap_allocator(); // TODO(bill): Change this allocator
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path;
}
}
id->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
} else if (node->kind == AstNode_ForeignSystemLibrary) {
auto *id = &node->ForeignSystemLibrary;
String file_str = id->filepath.string;
if (!is_import_path_valid(file_str)) {
syntax_error(ast_node_token(node), "Invalid `foreign_system_library` path");
// NOTE(bill): It's a naughty name
f->decls[i] = make_bad_decl(f, id->token, id->token);
continue;
}
try_add_foreign_system_library_path(p, file_str);
}
}
}
}
ParseFileError parse_files(Parser *p, char *init_filename) {
char *fullpath_str = gb_path_get_full_name(gb_heap_allocator(), init_filename);
String init_fullpath = make_string(fullpath_str);
TokenPos init_pos = {};
ImportedFile init_imported_file = {init_fullpath, init_fullpath, init_pos};
array_add(&p->imports, init_imported_file);
p->init_fullpath = init_fullpath;
{
String s = get_fullpath_core(gb_heap_allocator(), make_string("_preload.odin"));
ImportedFile runtime_file = {s, s, init_pos};
array_add(&p->imports, runtime_file);
}
for_array(i, p->imports) {
ImportedFile imported_file = p->imports[i];
String import_path = imported_file.path;
String import_rel_path = imported_file.rel_path;
TokenPos pos = imported_file.pos;
AstFile file = {};
ParseFileError err = init_ast_file(&file, import_path);
if (err != ParseFile_None) {
if (pos.line != 0) {
gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column);
}
gb_printf_err("Failed to parse file: %.*s\n\t", LIT(import_rel_path));
defer (gb_printf_err("\n"));
switch (err) {
case ParseFile_WrongExtension:
gb_printf_err("Invalid file extension: File must have the extension `.odin`");
break;
case ParseFile_InvalidFile:
gb_printf_err("Invalid file");
break;
case ParseFile_EmptyFile:
gb_printf_err("File is empty");
break;
case ParseFile_Permission:
gb_printf_err("File permissions problem");
break;
case ParseFile_NotFound:
gb_printf_err("File cannot be found");
break;
case ParseFile_InvalidToken:
gb_printf_err("Invalid token found in file");
break;
}
return err;
}
parse_file(p, &file);
{
gb_mutex_lock(&p->mutex);
defer (gb_mutex_unlock(&p->mutex));
file.id = p->files.count;
array_add(&p->files, file);
}
}
for_array(i, p->files) {
p->total_token_count += p->files[i].tokens.count;
}
return ParseFile_None;
}
|