summaryrefslogtreecommitdiff
path: root/05/parse.b
blob: bc14b5801c2548dd29954eddcb3a7826ab3b056b (plain)
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
; is this token the start of a type?
function token_is_type
	argument token
	local c
	c = *1token
	if c == TOKEN_IDENTIFIER goto token_is_ident_type
	if c == KEYWORD_UNSIGNED goto return_1
	if c == KEYWORD_CHAR goto return_1
	if c == KEYWORD_SHORT goto return_1
	if c == KEYWORD_INT goto return_1
	if c == KEYWORD_LONG goto return_1
	if c == KEYWORD_FLOAT goto return_1
	if c == KEYWORD_DOUBLE goto return_1
	if c == KEYWORD_VOID goto return_1
	if c == KEYWORD_STRUCT goto return_1
	if c == KEYWORD_UNION goto return_1
	if c == KEYWORD_ENUM goto return_1
	goto return_0
	:token_is_ident_type
		token += 8
		c = *8token
		local b
		b = ident_list_lookup(typedefs, c)
		if b != 0 goto return_1
		goto return_0
	
function parse_tokens
	argument tokens
	local token
	local ident
	local type
	local p
	local b
	local base_type
	local base_type_end
	local name
	local prefix
	local prefix_end
	local suffix
	local suffix_end
	local is_extern
	
	token = tokens
	:parse_tokens_loop
		is_extern = 0
		if *1token == TOKEN_EOF goto parse_tokens_eof
		if *1token == KEYWORD_STATIC goto parse_static_toplevel_decl
		if *1token == KEYWORD_EXTERN goto parse_extern_toplevel_decl
		if *1token == KEYWORD_TYPEDEF goto parse_typedef
		
		b = token_is_type(token)
		if b != 0 goto parse_toplevel_decl
		
		die(.str_bad_statement)
		:str_bad_statement
			string Bad statement.
			byte 0
		:parse_static_toplevel_decl
			token += 16 ; we don't care that this is static
			goto parse_toplevel_decl
		:parse_extern_toplevel_decl
			token += 16
			is_extern = 1
			goto parse_toplevel_decl
		:parse_toplevel_decl
			base_type = token
			base_type_end = type_get_base_end(token)
			token = base_type_end
			:tl_decl_loop
				prefix = token
				prefix_end = type_get_prefix_end(prefix)
				if *1prefix_end != TOKEN_IDENTIFIER goto tl_decl_no_ident
				name = prefix_end + 8
				name = *8name
				suffix = prefix_end + 16
				suffix_end = type_get_suffix_end(prefix)
				type = types_bytes_used
				parse_type_declarators(prefix, prefix_end, suffix, suffix_end)
				parse_base_type(base_type, base_type_end)
				token = suffix_end
				if *1token == SYMBOL_LBRACE goto parse_function_definition
				if *1token == SYMBOL_SEMICOLON goto parse_tld_no_initializer
				if *1token == SYMBOL_COMMA goto parse_tld_no_initializer
				if *1token == SYMBOL_EQ goto parse_tld_initializer
				token_error(token, .str_unrecognized_stuff_after_declaration)
				:str_unrecognized_stuff_after_declaration
					string Declaration should be followed by one of: { , =
					byte 32
					byte 59 ; semicolon
					byte 0	
				:parse_tl_decl_cont
				if *1token == SYMBOL_SEMICOLON goto tl_decl_loop_done
				if *1token != SYMBOL_COMMA goto tld_bad_stuff_after_decl
				goto tl_decl_loop
			:tl_decl_loop_done
			token += 16 ; skip semicolon
			goto parse_tokens_loop
				 				 
			:tl_decl_no_ident
				token_error(prefix_end, .str_tl_decl_no_ident)
			:str_tl_decl_no_ident
				string No identifier in top-level declaration.
				byte 0
			:tld_bad_stuff_after_decl
				token_error(token, .str_tld_bad_stuff_after_decl)
			:str_tld_bad_stuff_after_decl
				string Declarations should be immediately followed by a comma or semicolon.
				byte 0
		:parse_tld_no_initializer
			p = types + type
			if *1p == TYPE_FUNCTION goto parse_tl_decl_cont ; ignore function declarations -- we do two passes anyways
			ident_list_add(global_variables, name, rwdata_end_addr)
			; just skip forward by the size of this variable -- it'll automatically be filled with 0s.
			rwdata_end_addr += type_sizeof(type)
			goto parse_tl_decl_cont
		:parse_tld_initializer
			die(.str_tldinNI) ; @TODO
			:str_tldinNI
				string tld initializer not implemented.
				byte 10
				byte 0
		:parse_function_definition
			p = types + type
			if *1p != TYPE_FUNCTION goto lbrace_after_declaration
			die(.str_fdNI) ; @TODO
			:str_fdNI
				string function definitions not implemented.
				byte 10
				byte 0
			:lbrace_after_declaration
				token_error(token, .str_lbrace_after_declaration)
			:str_lbrace_after_declaration
				string Opening { after declaration of non-function.
				byte 0
		:parse_typedef
			base_type = token + 16
			base_type_end = type_get_base_end(base_type)
			
			token = base_type_end
			
			:typedef_loop
				prefix = token
				prefix_end = type_get_prefix_end(prefix)
				if *1prefix_end != TOKEN_IDENTIFIER goto typedef_no_ident
				ident = prefix_end + 8
				ident = *8ident
				suffix = prefix_end + 16
				suffix_end = type_get_suffix_end(prefix)
				
				;putc('B)
				;putc(':)
				;print_tokens(base_type, base_type_end)
				;putc('P)
				;putc(':)
				;print_tokens(prefix, prefix_end)
				;putc('S)
				;putc(':)
				;print_tokens(suffix, suffix_end)
				
				type = types_bytes_used
				parse_type_declarators(prefix, prefix_end, suffix, suffix_end)
				parse_base_type(base_type)
				
				puts(.str_typedef)
				putc(32)
				print_type(type)
				putc(10)
				
				b = ident_list_lookup(typedefs, ident)
				if b != 0 goto typedef_redefinition
				
				ident_list_add(typedefs, ident, type)
				token = suffix_end
				if *1token == SYMBOL_SEMICOLON goto typedef_loop_end
				if *1token != SYMBOL_COMMA goto bad_typedef
				token += 16 ; skip comma
				goto typedef_loop
			:typedef_loop_end
			token += 16 ; skip semicolon
			goto parse_tokens_loop
		:typedef_no_ident
			token_error(token, .str_typedef_no_ident)
		:str_typedef_no_ident
			string No identifier in typedef declaration.
			byte 0
		:bad_typedef
			token_error(token, .str_bad_typedef)
		:str_bad_typedef
			string Bad typedef.
			byte 0
		:typedef_redefinition
			token_error(token, .str_typedef_redefinition)
		:str_typedef_redefinition
			string typedef redefinition.
			byte 0
	:parse_tokens_eof
	return


; *p_token should be pointing to a {, this will advance it to point to the matching }
function token_skip_to_matching_rbrace
	argument p_token
	local token
	local depth
	token = *8p_token
	depth = 0
	:skip_rbrace_loop
		if *1token == SYMBOL_LBRACE goto skip_rbrace_incdepth
		if *1token == SYMBOL_RBRACE goto skip_rbrace_decdepth
		if *1token == TOKEN_EOF goto skip_rbrace_eof
		:skip_rbrace_next
		token += 16
		goto skip_rbrace_loop
		:skip_rbrace_incdepth
			depth += 1
			goto skip_rbrace_next
		:skip_rbrace_decdepth
			depth -= 1
			if depth == 0 goto skip_rbrace_ret
			goto skip_rbrace_next
	:skip_rbrace_ret
	*8p_token = token
	return
	
	:skip_rbrace_eof
		token_error(*8p_token, .str_skip_rbrace_eof)
	:str_skip_rbrace_eof
		string Unmatched {
		byte 0

; *p_token should be pointing to a [, this will advance it to point to the matching ]
function token_skip_to_matching_rsquare
	argument p_token
	local token
	local depth
	token = *8p_token
	depth = 0
	:skip_square_loop
		if *1token == SYMBOL_LSQUARE goto skip_square_incdepth
		if *1token == SYMBOL_RSQUARE goto skip_square_decdepth
		if *1token == TOKEN_EOF goto skip_square_eof
		:skip_square_next
		token += 16
		goto skip_square_loop
		:skip_square_incdepth
			depth += 1
			goto skip_square_next
		:skip_square_decdepth
			depth -= 1
			if depth == 0 goto skip_square_ret
			goto skip_square_next
	:skip_square_ret
	*8p_token = token
	return
	
	:skip_square_eof
		token_error(*8p_token, .str_skip_square_eof)
	:str_skip_square_eof
		string Unmatched [
		byte 0


; *p_token should be on a ); this goes back to the corresponding (
;  THERE MUST ACTUALLY BE A MATCHING BRACKET, OTHERWISE THIS WILL DO BAD THINGS
function token_reverse_to_matching_lparen
	argument p_token
	local token
	local depth
	token = *8p_token
	depth = 0
	:reverse_paren_loop
		if *1token == SYMBOL_LPAREN goto reverse_paren_incdepth
		if *1token == SYMBOL_RPAREN goto reverse_paren_decdepth
		:reverse_paren_next
		token -= 16
		goto reverse_paren_loop
		:reverse_paren_incdepth
			depth += 1
			if depth == 0 goto reverse_paren_ret
			goto reverse_paren_next
		:reverse_paren_decdepth
			depth -= 1
			goto reverse_paren_next
	:reverse_paren_ret
	*8p_token = token
	return
	
; parse things like  `int x` or `int f(void, int, char *)`
; advances *p_token
; returns type ID, or 0, in which case you should look at parse_type_result
function parse_type
	; split types into base (B), prefix (P) and suffix (S)
	;     struct Thing (*things[5])(void), *something_else[3];
	;     BBBBBBBBBBBB PP      SSSSSSSSSS  P              SSS
	; Here, we call `struct Thing` the "base type".
	byte 0xcc

; return the end of the base for this type.
function type_get_base_end
	argument token
	local c
	c = *1token
	if c == KEYWORD_STRUCT goto skip_struct_union_enum
	if c == KEYWORD_UNION goto skip_struct_union_enum
	if c == KEYWORD_ENUM goto skip_struct_union_enum
	; skip the "base type"
	token += 16 ; importantly, this skips the typedef'd name if there is one (e.g. typedef int Foo; Foo x;)
	:skip_base_type_loop
		c = *1token
		if c == KEYWORD_UNSIGNED goto skip_base_type_loop_cont ;e.g. int unsigned x;
		if c == KEYWORD_CHAR goto skip_base_type_loop_cont ;e.g. unsigned char x;
		if c == KEYWORD_SHORT goto skip_base_type_loop_cont ;e.g. unsigned short x;
		if c == KEYWORD_INT goto skip_base_type_loop_cont ;e.g. unsigned int x;
		if c == KEYWORD_LONG goto skip_base_type_loop_cont ;e.g. unsigned long x;
		if c == KEYWORD_DOUBLE goto skip_base_type_loop_cont ;e.g. long double x;
		goto skip_base_type_loop_end
		:skip_base_type_loop_cont
			token += 16
			goto skip_base_type_loop
	
	:skip_base_type_loop_end
	return token
	
	:skip_struct_union_enum
		token += 16
		if *1token != TOKEN_IDENTIFIER goto skip_sue_no_name
			token += 16 ; struct *blah*
		:skip_sue_no_name
		if *1token != SYMBOL_LBRACE goto skip_base_type_loop_end ; e.g. struct Something x[5];
		; okay we have something like
		;   struct {
		;       int x, y;
		;   } test;
		token_skip_to_matching_rbrace(&token)
		token += 16
		goto skip_base_type_loop_end


; return the end of this type prefix
function type_get_prefix_end
	argument token
	local c
	:find_prefix_end_loop
		c = *1token
		if c == TOKEN_IDENTIFIER goto found_prefix_end
		if c == KEYWORD_UNSIGNED goto prefix_end_cont
		if c == KEYWORD_CHAR goto prefix_end_cont
		if c == KEYWORD_SHORT goto prefix_end_cont
		if c == KEYWORD_INT goto prefix_end_cont
		if c == KEYWORD_LONG goto prefix_end_cont
		if c == KEYWORD_FLOAT goto prefix_end_cont
		if c == KEYWORD_DOUBLE goto prefix_end_cont
		if c == SYMBOL_LPAREN goto prefix_end_cont
		if c == SYMBOL_TIMES goto prefix_end_cont
		if c == SYMBOL_LSQUARE goto found_prefix_end
		if c == SYMBOL_RPAREN goto found_prefix_end
		goto found_prefix_end
		
		:prefix_end_cont
			token += 16
			goto find_prefix_end_loop
	:found_prefix_end
	return token

; return the end of this type suffix
; NOTE: you must pass in the PREFIX.
; (In general, we can't find the end of the suffix without knowing the prefix.)
;    int (*x);
;            ^ suffix ends here
;    (int *)
;          ^ suffix ends here
function type_get_suffix_end
	argument prefix
	local depth
	local token
	local c
	
	; find end of suffix
	token = prefix
	depth = 0 ; parenthesis/square bracket depth
	:suffix_end_loop
		c = *1token
		if c == TOKEN_IDENTIFIER goto suffix_end_cont
		if c == SYMBOL_LSQUARE goto suffix_end_incdepth
		if c == SYMBOL_RSQUARE goto suffix_end_decdepth
		if c == SYMBOL_LPAREN goto suffix_end_incdepth
		if c == SYMBOL_RPAREN goto suffix_end_decdepth
		if c == SYMBOL_TIMES goto suffix_end_cont
		if depth == 0 goto suffix_end_found
		if c == TOKEN_EOF goto type_get_suffix_bad_type
		goto suffix_end_cont
		
		:suffix_end_incdepth
			depth += 1
			goto suffix_end_cont
		:suffix_end_decdepth
			depth -= 1
			if depth < 0 goto suffix_end_found
			goto suffix_end_cont
		:suffix_end_cont
			token += 16
			goto suffix_end_loop
	:suffix_end_found
	
	return token
	:type_get_suffix_bad_type
		token_error(prefix, .str_bad_type_suffix)
	:str_bad_type_suffix
		string Bad type suffix.
		byte 0


; writes to *(types + types_bytes_used), and updates types_bytes_used
function parse_type_declarators
	argument prefix
	argument prefix_end
	argument suffix
	argument suffix_end
	local p
	local expr
	local n
	local c
	local depth
	local out
	
	; main loop for parsing types
	:type_declarators_loop
		p = prefix_end - 16
		if *1suffix == SYMBOL_LSQUARE goto parse_array_type
		if *1suffix == SYMBOL_LPAREN goto parse_function_type
		if *1p == SYMBOL_TIMES goto parse_pointer_type
		if suffix == suffix_end goto type_declarators_loop_end
		if *1suffix == SYMBOL_RPAREN goto parse_type_remove_parentheses
		goto parse_typedecls_bad_type
		
		:parse_pointer_type
			out = types + types_bytes_used
			*1out = TYPE_POINTER
			types_bytes_used += 1
			prefix_end = p
			goto type_declarators_loop
		:parse_array_type
			out = types + types_bytes_used
			*1out = TYPE_ARRAY
			types_bytes_used += 1
			
			p = suffix
			token_skip_to_matching_rsquare(&p)
			suffix += 16 ; skip [
			if *1suffix == SYMBOL_RSQUARE goto array_no_size
			
			; little hack to avoid screwing up types like  double[sizeof(int)]
			;   temporarily pretend we're using a lot more of types
			local prev_types_bytes_used
			prev_types_bytes_used = types_bytes_used
			types_bytes_used += 4000
			
			expr = malloc(4000)
			
			parse_expression(suffix, p, expr)
			;print_expression(expr)
			;putc(10)
			evaluate_constant_expression(prefix, expr, &n)
			if n < 0 goto bad_array_size
			free(expr)
			
			types_bytes_used = prev_types_bytes_used
			
			out = types + types_bytes_used
			*8out = n
			types_bytes_used += 8
			
			suffix = p + 16
			goto type_declarators_loop
			:bad_array_size
				token_error(suffix, .str_bad_array_size)
			:str_bad_array_size
				string Very large or negative array size.
				byte 0
			:array_no_size
				; e.g. int x[] = {1,2,3};
				out = types + types_bytes_used
				*8out = 0
				types_bytes_used += 8
				suffix += 16
				goto type_declarators_loop
		:parse_function_type
			local param_base_type
			local param_prefix
			local param_prefix_end
			local param_suffix
			local param_suffix_end
			
			p = suffix + 16
			out = types + types_bytes_used
			*1out = TYPE_FUNCTION
			types_bytes_used += 1
			:function_type_loop
				param_base_type = p
				param_prefix = type_get_base_end(param_base_type)
				param_prefix_end = type_get_prefix_end(param_prefix)
				param_suffix = param_prefix_end
				if *1param_suffix != TOKEN_IDENTIFIER goto functype_no_ident
				param_suffix += 16
				:functype_no_ident
				param_suffix_end = type_get_suffix_end(param_prefix)
				parse_type_declarators(param_prefix, param_prefix_end, param_suffix, param_suffix_end)
				parse_base_type(param_base_type)
				p = param_suffix_end
				if *1p == SYMBOL_RPAREN goto function_type_loop_end
				if *1p != SYMBOL_COMMA goto parse_typedecls_bad_type
				p += 16
				goto function_type_loop
			:function_type_loop_end
			out = types + types_bytes_used
			*1out = 0
			types_bytes_used += 1
			suffix = p + 16
			goto type_declarators_loop
		:parse_type_remove_parentheses
			if *1p != SYMBOL_LPAREN goto parse_typedecls_bad_type
			prefix_end = p
			suffix += 16
			goto type_declarators_loop
	:type_declarators_loop_end
	return 0
	:parse_typedecls_bad_type
		token_error(prefix, .str_bad_type_declarators)
	:str_bad_type_declarators
		string Bad type declarators.
		byte 0
	
; writes to *(types + types_bytes_used), and updates types_bytes_used (no return value)
function parse_base_type
	argument base_type
	local out
	local flags
	local p
	local c
	local depth
	local is_struct
	is_struct = 0
	
	out = types + types_bytes_used
	
	c = *1base_type
	if c == TOKEN_IDENTIFIER goto base_type_typedef
	if c == KEYWORD_STRUCT goto base_type_struct
	if c == KEYWORD_UNION goto base_type_union
	if c == KEYWORD_ENUM goto base_type_enum
	if c == KEYWORD_FLOAT goto base_type_float
	if c == KEYWORD_VOID goto base_type_void
	
	; "normal" type like int, unsigned char, etc.
	; annoyingly, all of these are equivalent to `unsigned long`:
	;    unsigned long int
	;    long unsigned int
	;    int long unsigned
	;    etc.
	; so we represent these as  PARSETYPE_FLAG_UNSIGNED|PARSETYPE_FLAG_LONG|PARSETYPE_FLAG_INT.
#define PARSETYPE_FLAG_UNSIGNED 1
#define PARSETYPE_FLAG_CHAR 2
#define PARSETYPE_FLAG_SHORT 4
#define PARSETYPE_FLAG_INT 8
#define PARSETYPE_FLAG_LONG 16
#define PARSETYPE_FLAG_DOUBLE 32
	flags = 0
	p = base_type
	:base_type_normal_loop
		c = *1p
		p += 16
		if c == KEYWORD_CHAR goto base_type_flag_char
		if c == KEYWORD_SHORT goto base_type_flag_short
		if c == KEYWORD_INT goto base_type_flag_int
		if c == KEYWORD_LONG goto base_type_flag_long
		if c == KEYWORD_UNSIGNED goto base_type_flag_unsigned
		if c == KEYWORD_DOUBLE goto base_type_flag_double
		goto base_type_normal_loop_end
		:base_type_flag_char
			c = flags & PARSETYPE_FLAG_CHAR
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_CHAR
			goto base_type_normal_loop
		:base_type_flag_short
			c = flags & PARSETYPE_FLAG_SHORT
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_SHORT
			goto base_type_normal_loop
		:base_type_flag_int
			c = flags & PARSETYPE_FLAG_INT
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_INT
			goto base_type_normal_loop
		:base_type_flag_long
			c = flags & PARSETYPE_FLAG_LONG
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_LONG
			goto base_type_normal_loop
		:base_type_flag_unsigned
			c = flags & PARSETYPE_FLAG_UNSIGNED
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_UNSIGNED
			goto base_type_normal_loop
		:base_type_flag_double
			c = flags & PARSETYPE_FLAG_DOUBLE
			if c != 0 goto repeated_base_type
			flags |= PARSETYPE_FLAG_DOUBLE
			goto base_type_normal_loop
		:repeated_base_type
			token_error(p, .str_repeated_base_type)
		:str_repeated_base_type
			string Arithmetic type repeated (e.g. unsigned unsigned int).
			byte 0
	:base_type_normal_loop_end
	if flags == 8 goto base_type_int ; `int`
	if flags == 1 goto base_type_uint ; `unsigned`
	if flags == 9 goto base_type_uint ; `unsigned int` etc.
	if flags == 2 goto base_type_char ; `char`
	if flags == 3 goto base_type_uchar ; `unsigned char` etc.
	if flags == 4 goto base_type_short ; `short`
	if flags == 12 goto base_type_short `short int` etc.
	if flags == 5 goto base_type_ushort ; `unsigned short` etc.
	if flags == 13 goto base_type_ushort ; `unsigned short int` etc. 
	if flags == 16 goto base_type_long ; `long`
	if flags == 24 goto base_type_long ; `long int` etc.
	if flags == 17 goto base_type_ulong ; `unsigned long` etc.
	if flags == 25 goto base_type_ulong ; `unsigned long int` etc.
	if flags == 32 goto base_type_double ; `double`
	if flags == 48 goto base_type_double ; `long double` (we use the same type for double and long double)
	
	goto bad_base_type
	
	:base_type_char
		*1out = TYPE_CHAR
		out += 1
		goto base_type_done
	:base_type_uchar
		*1out = TYPE_UNSIGNED_CHAR
		out += 1
		goto base_type_done
	:base_type_short
		*1out = TYPE_SHORT
		out += 1
		goto base_type_done
	:base_type_ushort
		*1out = TYPE_UNSIGNED_SHORT
		out += 1
		goto base_type_done
	:base_type_int
		*1out = TYPE_INT
		out += 1
		goto base_type_done
	:base_type_uint
		*1out = TYPE_UNSIGNED_INT
		out += 1
		goto base_type_done
	:base_type_long
		*1out = TYPE_LONG
		out += 1
		goto base_type_done
	:base_type_ulong
		*1out = TYPE_UNSIGNED_LONG
		out += 1
		goto base_type_done
	:base_type_double
		*1out = TYPE_DOUBLE
		out += 1
		goto base_type_done
	
	:base_type_done
	types_bytes_used = out - types
	return 0
	
	:base_type_struct
		is_struct = 1
		; fallthrough
	:base_type_union
		local struct_name
		local struct
		struct_name = .empty_string
		p = base_type + 16
		if *1p != TOKEN_IDENTIFIER goto base_type_have_name
		p += 8
		struct_name = *8p
		p += 8
		:base_type_have_name
		c = ident_list_lookup(structures, struct_name)
		if *1p == SYMBOL_LBRACE goto base_type_struct_definition
		
		if c == 0 goto base_type_incomplete_struct
			; e.g. struct Foo x;  where struct Foo has been defined
			*1out = TYPE_STRUCT
			out += 1
			*8out = c
			out += 8
			goto base_type_done
		:base_type_incomplete_struct
			; e.g. struct Foo *x;  where struct Foo hasn't been defined
			*1out = TYPE_VOID
			out += 1
			goto base_type_done
		:base_type_struct_definition
			;  @NONSTANDARD: we don't handle bit-fields.
			
			local member_base_type
			local member_prefix
			local member_prefix_end
			local member_suffix
			local member_suffix_end
			local member_name
			local member_type
			local member_align
			local member_size
			
			if c != 0 goto struct_redefinition
			struct = ident_list_create(8000) ; note: maximum "* 127 members in a single structure or union" C89 § 2.2.4.1
			*1out = TYPE_STRUCT
			out += 1
			*8out = struct
			out += 8
			types_bytes_used = out - types
			p += 16 ; skip opening {
			
			local offset
			offset = 0
			
			if *1struct_name == 0 goto struct_unnamed
			ident_list_add(structures, struct_name, struct)
			:struct_unnamed
			
			:struct_defn_loop
				if *1p == SYMBOL_RBRACE goto struct_defn_loop_end
				member_base_type = p
				p = type_get_base_end(member_base_type)
				:struct_defn_decl_loop ; handle each element of  int x, y[5], *z;
					member_prefix = p
					member_prefix_end = type_get_prefix_end(member_prefix)
					if *1member_prefix_end != TOKEN_IDENTIFIER goto member_no_identifier
					member_name = member_prefix_end + 8
					member_name = *8member_name
					
					c = ident_list_lookup_check(struct, member_name, 0)
					if c == 1 goto duplicate_member
					
					member_suffix = member_prefix_end + 16
					member_suffix_end = type_get_suffix_end(member_prefix)
					member_type = types_bytes_used
					
					
					parse_type_declarators(member_prefix, member_prefix_end, member_suffix, member_suffix_end)
					parse_base_type(member_base_type)
					
					; make sure struct member is aligned
					member_align = type_alignof(member_type)
					; offset = ceil(offset / align) * align
					offset += member_align - 1
					offset /= member_align
					offset *= member_align
					
					if offset ] 0xffffffff goto struct_too_large
					;putnln(offset)
					; data = (type << 32) | offset
					c = member_type < 32
					c |= offset
					ident_list_add(struct, member_name, c)
					
					member_size = type_sizeof(member_type)
					offset += member_size * is_struct ; keep offset as 0 if this is a union
					p = member_suffix_end
					if *1p == SYMBOL_SEMICOLON goto struct_defn_decl_loop_end
					if *1p != SYMBOL_COMMA goto struct_bad_declaration
					p += 16 ; skip comma
					goto struct_defn_decl_loop
					:duplicate_member
						token_error(p, .str_duplicate_member)
					:str_duplicate_member
						string Duplicate member in struct/union.
						byte 0
				:struct_defn_decl_loop_end
				p += 16 ; skip semicolon
				goto struct_defn_loop
			:struct_defn_loop_end
			out = types + types_bytes_used
			goto base_type_done
		:struct_redefinition
			token_error(p, .str_struct_redefinition)
		:str_struct_redefinition
			string struct redefinition.
			byte 0
		:struct_bad_declaration
			token_error(p, .str_struct_bad_declaration)
		:str_struct_bad_declaration
			string Bad declaration in struct.
			byte 0
		:struct_too_large
			token_error(p, .str_struct_too_large)
		:str_struct_too_large
			string struct too large (maximum is 4GB).
			byte 0
		:member_no_identifier
			; e.g. struct { int; };
			token_error(p, .str_member_no_identifier)
		:str_member_no_identifier
			string No identifier in struct member.
			byte 0
	:base_type_enum
		local q
		local expr
		
		*1out = TYPE_INT ; treat any enum as int
		out += 1
		types_bytes_used = out - types
		
		p = base_type + 16
		if *1p == SYMBOL_LBRACE goto enum_definition
		if *1p != TOKEN_IDENTIFIER goto bad_base_type ; e.g. enum int x;
		p += 16
		if *1p == SYMBOL_LBRACE goto enum_definition
		goto base_type_done ; just using an enum type, not defining it.
		:enum_definition
		local name
		local value
		value = -1 ; consider initial previous value as -1, because -1 + 1 = 0
		p += 16 ; skip opening {
		:enum_defn_loop
			if *1p == SYMBOL_RBRACE goto enum_defn_loop_end
			if *1p != TOKEN_IDENTIFIER goto bad_enum_definition
			p += 8
			name = *8p
			p += 8
			if *1p == SYMBOL_COMMA goto enum_defn_no_equals
			if *1p == SYMBOL_RBRACE goto enum_defn_no_equals
			if *1p != SYMBOL_EQ goto bad_enum_definition ; e.g. enum { X ! };
				; value provided, e.g. X = 5,
				p += 16
				depth = 0 ; parenthesis depth
				q = p
				; find matching comma/right brace 
				;  -- yes, a comma can appear in an enumerator expression, e.g.
				;     enum { X = sizeof(struct{int x, y;}) };
				; or  enum { X = (enum {A,B})3 };
				
				; find associated comma or right-brace
				:enum_comma_loop
					if depth > 0 goto enum_comma_deep
					if *1q == SYMBOL_COMMA goto enum_comma_loop_end
					if *1q == SYMBOL_RBRACE goto enum_comma_loop_end
					:enum_comma_deep
					if *1q == TOKEN_EOF goto bad_base_type
					c = *1q
					q += 16
					if c == SYMBOL_LPAREN goto enum_comma_incdepth
					if c == SYMBOL_RPAREN goto enum_comma_decdepth
					goto enum_comma_loop
					:enum_comma_incdepth
						depth += 1
						goto enum_comma_loop
					:enum_comma_decdepth
						depth -= 1
						goto enum_comma_loop
				:enum_comma_loop_end
				expr = malloc(4000)
				parse_expression(p, q, expr)
				evaluate_constant_expression(p, expr, &value)
				free(expr)
				if value < -0x80000000 goto bad_enumerator
				if value > 0x7fffffff goto bad_enumerator
				ident_list_add(enumerators, name, value)
				p = q
				if *1p == SYMBOL_RBRACE goto enum_defn_loop_end
				p += 16 ; skip ,
				goto enum_defn_loop
				:bad_enumerator
					token_error(p, .str_bad_enumerator)
				:str_bad_enumerator
					string Enumerators too large for int.
					byte 0
			:enum_defn_no_equals
				; no value provided, e.g. X,
				; the value of this enumerator is one more than the value of the last one
				value += 1
				ident_list_add(enumerators, name, value)
				if *1p == SYMBOL_RBRACE goto enum_defn_loop_end
				p += 16 ; skip ,
				goto enum_defn_loop
		:enum_defn_loop_end
		out = types + types_bytes_used ; fix stuff in case there were any types in the enumerator expressions
		goto base_type_done
		:bad_enum_definition
			token_error(base_type, .str_bad_enum_defn)
		:str_bad_enum_defn
			string Bad enum definition.
			byte 0
	:base_type_float
		*1out = TYPE_FLOAT
		out += 1
		goto base_type_done
	:base_type_void
		*1out = TYPE_VOID
		out += 1
		goto base_type_done	
	:base_type_typedef
		p = base_type + 8
		c = ident_list_lookup(typedefs, *8p)
		if c == 0 goto bad_base_type
		local len
		len = type_length(c)
		c += types
		memcpy(out, c, len)
		out += len
		goto base_type_done
	
	:bad_base_type
		token_error(base_type, .str_bad_base_type)
	:str_bad_base_type
		string Bad base type.
		byte 0

; how many bytes does it take to encode this type?
function type_length
	argument type
	local p
	local n
	p = types + type
	if *1p <= TYPE_DOUBLE goto return_1
	if *1p != TYPE_POINTER goto type_length_not_pointer
		type += 1
		n = type_length(type)
		return n + 1
	:type_length_not_pointer
	if *1p != TYPE_ARRAY goto type_length_not_array
		type += 9
		n = type_length(type)
		return n + 9
	:type_length_not_array
	if *1p == TYPE_STRUCT goto return_9
	if *1p != TYPE_FUNCTION goto type_length_not_function
		local start
		start = type
		type += 1
		:type_length_function_loop
			p = types + type
			if *1p == 0 goto type_length_function_loop_end
			type += type_length(type)
			goto type_length_function_loop
		:type_length_function_loop_end
		type += 1
		type += type_length(type)
		return type - start
	:type_length_not_function
	fputs(2, .str_type_length_bad_type)
	exit(1)
	:str_type_length_bad_type
		string Bad type passed to type_length. This shouldn't happen.
		byte 10
		byte 0
	

; returns length of type	
function type_copy_ids
	argument dest
	argument src
	local n
	n = type_length(src)
	dest += types
	src += types
	memcpy(dest, src, n)
	return n

function type_create_pointer
	argument type
	local id
	local p
	id = types_bytes_used
	p = types + id
	*1p = TYPE_POINTER
	types_bytes_used += 1
	p = id + 1
	types_bytes_used += type_copy_ids(p, type)
	return id
	
function parse_expression
	argument tokens
	argument tokens_end
	argument out
	local in
	local a
	local b
	local c
	local p
	local n
	local type
	local best
	local best_precedence
	local depth
	local value
	local first_token
	:parse_expression_top
	
	;print_tokens(tokens, tokens_end)
	
	type = out + 4
	
	if tokens == tokens_end goto empty_expression
	p = tokens + 16
	if p == tokens_end goto single_token_expression
	if *1tokens != SYMBOL_LPAREN goto parse_expression_not_entirely_in_parens
	p = tokens_end - 16
	if *1p != SYMBOL_RPAREN goto parse_expression_not_entirely_in_parens
	
	depth = 1 ; bracket depth
	p = tokens + 16
	a = tokens_end - 16 ; stop point
	:expr_paren_check_loop
		if p >= a goto expr_paren_check_loop_end
		c = *1p
		p += 16
		if c == SYMBOL_LPAREN goto expr_paren_check_loop_incdepth
		if c == SYMBOL_RPAREN goto expr_paren_check_loop_decdepth
		goto expr_paren_check_loop
		:expr_paren_check_loop_incdepth
			depth += 1
			goto expr_paren_check_loop
		:expr_paren_check_loop_decdepth
			depth -= 1
			if depth == 0 goto parse_expression_not_entirely_in_parens
			goto expr_paren_check_loop
	:expr_paren_check_loop_end
	
	; if we made it this far, the expression is entirely in parenthesis, e.g. (x+2)
	tokens += 16
	tokens_end -= 16
	goto parse_expression_top
	
	:parse_expression_not_entirely_in_parens
	
	; look for the operator with the lowest precedence not in brackets
	depth = 0 ; paren/square bracket depth
	first_token = 1
	p = tokens
	best = 0
	best_precedence = 1000
	goto expr_find_operator_loop_first
	:expr_find_operator_loop
		first_token = 0
		:expr_find_operator_loop_first
		if p >= tokens_end goto expr_find_operator_loop_end
		n = p
		c = *1p
		p += 16
		if depth > 0 goto expr_findop_not_new_best
		if depth < 0 goto expr_too_many_closing_brackets
		a = operator_precedence(n, first_token)
		n = a
		if a == 0xe0 goto select_leftmost ; ensure that the leftmost unary operator is processed first
		b = operator_right_associative(c)
		if b != 0 goto select_leftmost ; ensure that the leftmost += / -= / etc. is processed first
		goto select_rightmost
		:select_leftmost
		n += 1
		; fallthrough
		:select_rightmost
		if n > best_precedence goto expr_findop_not_new_best
		; new best!
		best = p - 16
		;putc('O)
		;putc(':)
		;putn(*1best)
		;putc(32)
		;putc('P)
		;putc(':)
		;putnln(a)
		best_precedence = a
		:expr_findop_not_new_best
		if c == SYMBOL_LPAREN goto expr_findop_incdepth
		if c == SYMBOL_RPAREN goto expr_findop_decdepth
		if c == SYMBOL_LSQUARE goto expr_findop_incdepth
		if c == SYMBOL_RSQUARE goto expr_findop_decdepth
		goto expr_find_operator_loop
		
		:expr_findop_incdepth
			depth += 1
			goto expr_find_operator_loop
		:expr_findop_decdepth
			depth -= 1
			goto expr_find_operator_loop
	:expr_find_operator_loop_end
	
	
	if best == 0 goto unrecognized_expression
	
	n = best - tokens
	
	c = *1best
		
	if best == tokens goto parse_expr_unary
	
	; it's a binary expression.
	if c == SYMBOL_PLUS_PLUS goto parse_postincrement
	if c == SYMBOL_MINUS_MINUS goto parse_postdecrement
	if c == SYMBOL_QUESTION goto parse_conditional
	*1out = binop_symbol_to_expression_type(c)
	c = *1out
	out += 8
	if c == EXPRESSION_DOT goto parse_expr_member
	if c == EXPRESSION_ARROW goto parse_expr_member
	a = out + 4 ; type of first operand
	out = parse_expression(tokens, best, out) ; first operand
	p = best + 16
	b = out + 4 ; type of second operand
	if c != EXPRESSION_SUBSCRIPT goto binary_not_subscript
	tokens_end -= 16
	if *1tokens_end != SYMBOL_RSQUARE goto unrecognized_expression
	:binary_not_subscript
	
	out = parse_expression(p, tokens_end, out) ; second operand
	
	if c == EXPRESSION_LSHIFT goto type_shift
	if c == EXPRESSION_RSHIFT goto type_shift
	if c == EXPRESSION_SUBSCRIPT goto type_subscript
	if c == EXPRESSION_EQ goto type_int
	if c == EXPRESSION_NEQ goto type_int
	if c == EXPRESSION_LEQ goto type_int
	if c == EXPRESSION_GEQ goto type_int
	if c == EXPRESSION_LT goto type_int
	if c == EXPRESSION_GT goto type_int
	if c == EXPRESSION_COMMA goto type_binary_right
	if c == EXPRESSION_EQ goto type_binary_left
	if c == EXPRESSION_ASSIGN_ADD goto type_binary_left
	if c == EXPRESSION_ASSIGN_SUB goto type_binary_left
	if c == EXPRESSION_ASSIGN_MUL goto type_binary_left
	if c == EXPRESSION_ASSIGN_DIV goto type_binary_left
	if c == EXPRESSION_ASSIGN_REMAINDER goto type_binary_left
	if c == EXPRESSION_ASSIGN_AND goto type_binary_left_integer
	if c == EXPRESSION_ASSIGN_XOR goto type_binary_left_integer
	if c == EXPRESSION_ASSIGN_OR goto type_binary_left_integer
	if c == EXPRESSION_ASSIGN_LSHIFT goto type_binary_left_integer
	if c == EXPRESSION_ASSIGN_RSHIFT goto type_binary_left_integer
	if c == EXPRESSION_LOGICAL_OR goto type_int
	if c == EXPRESSION_LOGICAL_AND goto type_int
	if c == EXPRESSION_BITWISE_AND goto type_binary_usual_integer
	if c == EXPRESSION_BITWISE_XOR goto type_binary_usual_integer
	if c == EXPRESSION_BITWISE_OR goto type_binary_usual_integer
	if c == EXPRESSION_ADD goto type_plus
	if c == EXPRESSION_SUB goto type_minus
	if c == EXPRESSION_MUL goto type_binary_usual
	if c == EXPRESSION_DIV goto type_binary_usual
	if c == EXPRESSION_REMAINDER goto type_binary_usual_integer
	
	fputs(2, .str_binop_this_shouldnt_happen)
	exit(1)
	:str_binop_this_shouldnt_happen
		string Bad binop symbol (this shouldn't happen).
		byte 10
		byte 0	
	
	:type_plus
		p = types + *4a
		if *1p == TYPE_POINTER goto type_binary_left ; pointer plus integer
		p = types + *4b
		if *1p == TYPE_POINTER goto type_binary_right ; integer plus pointer
		goto type_binary_usual
	:type_minus
		p = types + *4a
		if *1p == TYPE_POINTER goto type_minus_left_ptr
		goto type_binary_usual
		:type_minus_left_ptr
		p = types + *4b
		if *1p == TYPE_POINTER goto type_long ; pointer difference
		goto type_binary_left ; pointer minus integer
	:type_subscript
		p = types + *4a
		if *1p == TYPE_POINTER goto type_subscript_pointer
		if *1p == TYPE_ARRAY goto type_subscript_array
		goto subscript_bad_type
		:type_subscript_pointer
			*4type = *4a + 1
			return out
		:type_subscript_array
			*4type = *4a + 9
			return out
		:subscript_bad_type
			token_error(tokens, .str_subscript_bad_type)
		:str_subscript_bad_type
			string Subscript of non-pointer type.
			byte 0
	; apply the "usual conversions"
	:type_binary_usual
		*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
		return out
	; like type_binary_usual, but the operands must be integers
	:type_binary_usual_integer
		*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
		p = types + *4type
		if *1p >= TYPE_FLOAT goto expr_binary_bad_types
		return out
	:type_binary_left_integer
		p = types + *4a
		if *1p >= TYPE_FLOAT goto expr_binary_bad_types
		p = types + *4b
		if *1p >= TYPE_FLOAT goto expr_binary_bad_types
		goto type_binary_left
	:type_binary_left
		*4type = *4a
		return out
	:type_binary_right
		*4type = *4b
		return out
	:type_shift
		p = types + *4a
		if *1p >= TYPE_FLOAT goto expr_binary_bad_types
		p = types + *4b
		if *1p >= TYPE_FLOAT goto expr_binary_bad_types
		*4type = type_promotion(*4a)
		return out
	; the type here is just int
	:type_int
		*4type = TYPE_INT
		return out
	:type_long
		*4type = TYPE_LONG
		return out
	:expr_binary_bad_types
		bad_types_to_operator(tokens, *4a, *4b)
	
	
	:parse_expr_unary
		if c == KEYWORD_SIZEOF goto parse_sizeof
		*1out = unary_op_to_expression_type(c)
		c = *1out
		if c == EXPRESSION_CAST goto parse_cast
		out += 8
		a = out + 4 ; type of operand
		p = tokens + 16
		out = parse_expression(p, tokens_end, out)
		p = types + *4a
		if c == EXPRESSION_BITWISE_NOT goto unary_type_integral
		if c == EXPRESSION_UNARY_PLUS goto unary_type_promote
		if c == EXPRESSION_UNARY_MINUS goto unary_type_promote
		if c == EXPRESSION_LOGICAL_NOT goto unary_type_logical_not
		if c == EXPRESSION_ADDRESS_OF goto unary_address_of
		if c == EXPRESSION_DEREFERENCE goto unary_dereference
		if c == EXPRESSION_PRE_INCREMENT goto unary_type_arithmetic_nopromote
		if c == EXPRESSION_PRE_DECREMENT goto unary_type_arithmetic_nopromote
		fputs(2, .str_unop_this_shouldnt_happen)
		exit(1)
		:str_unop_this_shouldnt_happen
			string Bad unary symbol (this shouldn't happen).
			byte 10
			byte 0
	:parse_cast
		local cast_base_type
		local cast_prefix
		local cast_suffix
		local cast_suffix_end
		
		cast_base_type = best + 16
		cast_prefix = type_get_base_end(cast_base_type)
		cast_suffix = type_get_prefix_end(cast_prefix)
		cast_suffix_end = type_get_suffix_end(cast_prefix)
		
		a = types_bytes_used
		
		parse_type_declarators(cast_prefix, cast_suffix, cast_suffix, cast_suffix_end)
		parse_base_type(cast_base_type)
		
		p = cast_suffix_end
		
		if *1p != SYMBOL_RPAREN goto bad_cast ; e.g. (int ,)5
		out += 4
		*4out = a
		out += 4
		p += 16
		out = parse_expression(p, tokens_end, out)
		return out
		:bad_cast
			token_error(tokens, .str_bad_cast)
		:str_bad_cast
			string Bad cast.
			byte 0		
	:unary_address_of
		*4type = type_create_pointer(*4a)
		return out
	:unary_dereference
		if *1p != TYPE_POINTER goto unary_bad_type
		*4type = *4a + 1
		return out
	:unary_type_logical_not
		if *1p > TYPE_POINTER goto unary_bad_type
		*4type = TYPE_INT
		return out
	:unary_type_integral
		if *1p >= TYPE_FLOAT goto unary_bad_type
		goto unary_type_promote
	:unary_type_promote
		if *1p > TYPE_DOUBLE goto unary_bad_type
		*4type = type_promotion(*4a)
		return out
	:unary_type_arithmetic_nopromote
		if *1p > TYPE_DOUBLE goto unary_bad_type
		*4type = *4a
		return out
	:unary_bad_type
		fprint_token_location(1, tokens)
		puts(.str_unary_bad_type)
		print_type(*4a)
		putc(10)
		exit(1)
	:str_unary_bad_type
		string : Bad type for unary operator:
		byte 32
		byte 0
	
	:parse_sizeof
		local sizeof_base_type
		local sizeof_prefix
		local sizeof_suffix
		local sizeof_suffix_end
		
		*1out = EXPRESSION_CONSTANT_INT
		out += 4
		*1out = TYPE_UNSIGNED_LONG
		out += 4
		p = best + 16
		if *1p != SYMBOL_LPAREN goto parse_sizeof_expr
		p += 16
		b = token_is_type(p)
		if b == 0 goto parse_sizeof_expr
			; it's a type, e.g. sizeof(int)
			sizeof_base_type = p
			sizeof_prefix = type_get_base_end(sizeof_base_type)
			sizeof_suffix = type_get_prefix_end(sizeof_prefix)
			sizeof_suffix_end = type_get_suffix_end(sizeof_prefix)
			p = sizeof_suffix_end
			a = types_bytes_used
			parse_type_declarators(sizeof_prefix, sizeof_suffix, sizeof_suffix, sizeof_suffix_end)
			parse_base_type(sizeof_base_type)
			if *1p != SYMBOL_RPAREN goto bad_expression ; e.g. sizeof(int ,)
			*8out = type_sizeof(a)
			goto parse_sizeof_finish
		:parse_sizeof_expr
			; it's an expression, e.g. sizeof(x+3)
			local temp
			temp = malloc(4000)
			p = best + 16
			parse_expression(p, tokens_end, temp)
			p = temp + 4
			*8out = type_sizeof(*4p)
			free(temp)
		:parse_sizeof_finish
		out += 8
		return out
		
	:parse_expr_member ; -> or .
		p = best + 16
		if *1p != TOKEN_IDENTIFIER goto bad_expression
		
		a = out + 4 ; pointer to type ID
		out = parse_expression(tokens, best, out)
		a = types + *4a
		if c == EXPRESSION_DOT goto type_dot
			if *1a != TYPE_POINTER goto arrow_non_pointer
			a += 1
		:type_dot
		if *1a != TYPE_STRUCT goto member_non_struct
		a += 1
		a = *8a ; pointer to struct data
		p += 8
		c = ident_list_lookup(a, *8p)
		if c == 0 goto member_not_in_struct
		*8out = c & 0xffffffff ; offset
		*4type = c > 32 ; type
		out += 8
		p += 8
		if p != tokens_end goto bad_expression ; e.g. foo->bar hello
		return out
		:arrow_non_pointer
			token_error(p, .str_arrow_non_pointer)
		:str_arrow_non_pointer
			string Trying to use -> operator on a non-pointer type.
			byte 0
		:member_non_struct
			token_error(p, .str_member_non_struct)
		:str_member_non_struct
			string Trying to access member of something other than a (complete) structure/union.
			byte 0
		:member_not_in_struct
			token_error(p, .str_member_not_in_struct)
		:str_member_not_in_struct
			string Trying to access non-existent member of structure or union.
			byte 0
	:parse_conditional
		depth = 0 ; bracket depth
		n = 0 ; ? : depth
		; find : associated with this ?
		p = best + 16
		:parse_conditional_loop
			if p >= tokens_end goto bad_expression
			if *1p == SYMBOL_QUESTION goto parse_cond_incn
			if *1p == SYMBOL_COLON goto parse_cond_decn
			if *1p == SYMBOL_LPAREN goto parse_cond_incdepth
			if *1p == SYMBOL_RPAREN goto parse_cond_decdepth
			if *1p == SYMBOL_LSQUARE goto parse_cond_incdepth
			if *1p == SYMBOL_RSQUARE goto parse_cond_decdepth
			:parse_cond_cont
			p += 16
			goto parse_conditional_loop
			
			:parse_cond_incdepth
				depth += 1
				goto parse_cond_cont
			:parse_cond_decdepth
				depth -= 1
				goto parse_cond_cont
			:parse_cond_incn
				n += 1
				goto parse_cond_cont
			:parse_cond_decn
				n -= 1
				if n >= 0 goto parse_cond_cont
				if depth > 0 goto parse_cond_cont
		; okay, q now points to the :
		*1out = EXPRESSION_CONDITIONAL
		out += 8
		out = parse_expression(tokens, best, out)
		a = out + 4 ; type of left branch of conditional
		best += 16
		out = parse_expression(best, p, out)
		b = out + 4 ; type of right branch of conditional
		p += 16
		out = parse_expression(p, tokens_end, out)
		p = types + *4a
		if *1p == TYPE_STRUCT goto parse_cond_ltype
		if *1p == TYPE_VOID goto parse_cond_ltype
		if *1p == TYPE_POINTER goto parse_cond_ltype ; @NONSTANDARD: we don't handle  sizeof *(0 ? (void*)0 : "hello")  correctly--it should be 1 (a standard-compliant implementation is annoyingly complicated)
		*4type = expr_binary_type_usual_conversions(tokens, *4a, *4b)
		return out
		:parse_cond_ltype
		; no conversions
		*4type = *4a
		return out
	:parse_postincrement
		*1out = EXPRESSION_POST_INCREMENT
		p = tokens_end - 16
		if *1p != SYMBOL_PLUS_PLUS goto bad_expression ; e.g. a ++ b
		out += 8
		a = out + 4 ; type of operand 
		out = parse_expression(tokens, p, out)
		*4type = *4a ; this expression's type is the operand's type (yes even for types smaller than int)
		return out
		
	:parse_postdecrement
		*1out = EXPRESSION_POST_DECREMENT
		p = tokens_end - 16
		if *1p != SYMBOL_MINUS_MINUS goto bad_expression ; e.g. a -- b
		out += 8
		a = out + 4 ; type of operand 
		out = parse_expression(tokens, p, out)
		*4type = *4a ; type of this = type of operand
		return out
	
	:single_token_expression
		in = tokens
		c = *1in
		if c == TOKEN_CONSTANT_INT goto expression_integer
		if c == TOKEN_CONSTANT_CHAR goto expression_integer ; character constants are basically the same as integer constants
		if c == TOKEN_CONSTANT_FLOAT goto expression_float
		if c == TOKEN_STRING_LITERAL goto expression_string_literal
		if c == TOKEN_IDENTIFIER goto expression_identifier
		goto unrecognized_expression
		
	:expression_identifier
		in += 8
		a = *8in
		in += 8
		; check if it's an enumerator
		c = ident_list_lookup_check(enumerators, a, &n)
		if c == 0 goto not_enumerator
			; it is an enumerator
			*1out = EXPRESSION_CONSTANT_INT
			out += 4
			*4out = TYPE_INT
			out += 4
			*8out = n
			out += 8
			return out
		:not_enumerator
			in -= 16
			token_error(in, .str_undeclared_variable)
		:str_undeclared_variable
			string Undeclared variable.
			byte 0
	:expression_integer
		*1out = EXPRESSION_CONSTANT_INT
		p = in + 8
		value = *8p
		p = out + 8
		*8p = value
		
		p = in + 1
		a = int_suffix_to_type(*1p) ; what the suffix says the type should be
		b = int_value_to_type(value) ; what the value says the type should be (if the value is too large to fit in int)
		a = max_signed(a, b) ; take the maximum of the two types
		; make sure that if the integer has a u suffix, the type will be unsigned
		a &= b | 0xfe
		p = out + 4
		*4p = a
		in += 16
		out += 16
		return out
	
	:expression_float
		*1out = EXPRESSION_CONSTANT_FLOAT
		p = in + 8
		value = *8p
		p = out + 8
		*8p = value
		
		p = in + 1
		a = float_suffix_to_type(*1p)
		
		p = out + 4
		*4p = a
		
		in += 16
		out += 16
		return out
		
	:expression_string_literal
		*1out = EXPRESSION_STRING_LITERAL
		p = in + 8
		value = *8p
		p = out + 8
		*8p = value
		
		; we already know this is char*
		p = out + 4
		*4p = TYPE_POINTER_TO_CHAR
		
		in += 16
		out += 16
		return out
	
	
	:empty_expression
		token_error(tokens, .str_empty_expression)
	:str_empty_expression
		string Empty expression.
		byte 0
	:bad_expression
		token_error(tokens, .str_bad_expression)
	:str_bad_expression
		string Bad expression.
		byte 0
	:unrecognized_expression
		token_error(tokens, .str_unrecognized_expression)
	:str_unrecognized_expression
		string Unrecognized expression.
		byte 0
	:expr_too_many_closing_brackets
		token_error(tokens, .str_too_many_closing_brackets)
	:str_too_many_closing_brackets
		string Too many closing brackets.
		byte 0
:return_type_int
	return TYPE_INT
:return_type_long
	return TYPE_LONG
:return_type_unsigned_int
	return TYPE_UNSIGNED_INT
:return_type_unsigned_long
	return TYPE_UNSIGNED_LONG
:return_type_float
	return TYPE_FLOAT
:return_type_double
	return TYPE_DOUBLE

function type_sizeof
	argument type
	local p
	local c
	p = types + type
	c = *1p
	if c == TYPE_CHAR goto return_1
	if c == TYPE_UNSIGNED_CHAR goto return_1
	if c == TYPE_SHORT goto return_2
	if c == TYPE_UNSIGNED_SHORT goto return_2
	if c == TYPE_INT goto return_4
	if c == TYPE_UNSIGNED_INT goto return_4
	if c == TYPE_LONG goto return_8
	if c == TYPE_UNSIGNED_LONG goto return_8
	if c == TYPE_FLOAT goto return_4
	if c == TYPE_DOUBLE goto return_8
	if c == TYPE_VOID goto return_1
	if c == TYPE_POINTER goto return_8
	if c == TYPE_FUNCTION goto return_8
	if c == TYPE_ARRAY goto sizeof_array
	if c == TYPE_STRUCT goto sizeof_struct
	
	fputs(2, .str_sizeof_bad)
	exit(1)
	:str_sizeof_bad
		string type_sizeof bad type.
		byte 10
		byte 0
	
	:sizeof_array
		local n
		p += 1
		n = *8p
		p += 8
		p -= types
		c = type_sizeof(p)
		return n * c

	:sizeof_struct
		; size of struct is  offset of last member + size of last member,
		;  rounded up to fit alignment
		local align
		local offset
		local member
		align = type_alignof(type)
		p += 1
		member = *8p
		:sizeof_struct_loop
			if *1member == 0 goto sizeof_struct_loop_end
			member = memchr(member, 0) ; don't care about name
			member += 1 ; skip null terminator
			c = *8member
			member += 8
			offset = c & 0xffffffff
			c >= 32 ; extract type
			offset += type_sizeof(c)
			goto sizeof_struct_loop
		:sizeof_struct_loop_end
		
		offset += align - 1
		offset /= align
		offset *= align
		
		return offset

function type_alignof
	argument type
	local p
	local c
	p = types + type
	c = *1p
	if c == TYPE_CHAR goto return_1
	if c == TYPE_UNSIGNED_CHAR goto return_1
	if c == TYPE_SHORT goto return_2
	if c == TYPE_UNSIGNED_SHORT goto return_2
	if c == TYPE_INT goto return_4
	if c == TYPE_UNSIGNED_INT goto return_4
	if c == TYPE_LONG goto return_8
	if c == TYPE_UNSIGNED_LONG goto return_8
	if c == TYPE_FLOAT goto return_4
	if c == TYPE_DOUBLE goto return_8
	if c == TYPE_VOID goto return_1
	if c == TYPE_POINTER goto return_8
	if c == TYPE_FUNCTION goto return_8
	if c == TYPE_ARRAY goto alignof_array
	if c == TYPE_STRUCT goto alignof_struct
	
	fputs(2, .str_alignof_bad)
	exit(1)
	:str_alignof_bad
		string type_alignof bad type.
		byte 10
		byte 0
	:alignof_struct
		; alignment of struct is max alignment of members
		local align
		local member
		local a
		align = 1
		p += 1
		member = *8p
		:alignof_struct_loop
			if *1member == 0 goto alignof_struct_loop_end
			member = memchr(member, 0) ; don't care about name
			member += 1 ; skip null terminator
			c = *8member
			member += 8
			c >= 32 ; ignore offset
			a = type_alignof(c)
			if a <= align goto alignof_struct_loop
			align = a
			goto alignof_struct_loop
		:alignof_struct_loop_end
		return align
	:alignof_array
		p = type + 9 ; skip TYPE_ARRAY and size
		return type_alignof(p)

; evaluate an expression which can be the size of an array, e.g.
;    enum { A, B, C };
;    int x[A * sizeof(float) + 3 << 5];
; @NONSTANDARD: doesn't handle floats, but really why would you use floats in an array size
;                 e.g.   SomeType x[(int)3.3];
; this is also used for #if evaluation
; token is used for error messages (e.g. if this "constant" expression is *x or something)
; NOTE: this returns the end of the expression, not the value (which is stored in *8p_value)
function evaluate_constant_expression
	argument token
	argument expr
	argument p_value
	local a
	local b
	local c
	local p
	local mask
	local type
	
	type = expr + 4
	type = *4type
	
	c = *1expr
	
	if c == EXPRESSION_CONSTANT_INT goto eval_constant_int
	if c == EXPRESSION_UNARY_PLUS goto eval_unary_plus
	if c == EXPRESSION_UNARY_MINUS goto eval_unary_minus
	if c == EXPRESSION_BITWISE_NOT goto eval_bitwise_not
	if c == EXPRESSION_LOGICAL_NOT goto eval_logical_not
	if c == EXPRESSION_CAST goto eval_cast
	if c == EXPRESSION_ADD goto eval_add
	if c == EXPRESSION_SUB goto eval_sub
	if c == EXPRESSION_MUL goto eval_mul
	if c == EXPRESSION_DIV goto eval_div
	if c == EXPRESSION_REMAINDER goto eval_remainder
	if c == EXPRESSION_LSHIFT goto eval_lshift
	if c == EXPRESSION_RSHIFT goto eval_rshift
	if c == EXPRESSION_EQ goto eval_eq
	if c == EXPRESSION_NEQ goto eval_neq
	if c == EXPRESSION_LT goto eval_lt
	if c == EXPRESSION_GT goto eval_gt
	if c == EXPRESSION_LEQ goto eval_leq
	if c == EXPRESSION_GEQ goto eval_geq
	if c == EXPRESSION_BITWISE_AND goto eval_bitwise_and
	if c == EXPRESSION_BITWISE_OR goto eval_bitwise_or
	if c == EXPRESSION_BITWISE_XOR goto eval_bitwise_xor
	if c == EXPRESSION_LOGICAL_AND goto eval_logical_and
	if c == EXPRESSION_LOGICAL_OR goto eval_logical_or
	if c == EXPRESSION_CONDITIONAL goto eval_conditional
	
	
	token_error(token, .str_eval_bad_exprtype)
	
	:str_eval_bad_exprtype
		string Can't evaluate constant expression.
		byte 0
	:eval_cast
		p = types + type
		if *1p == TYPE_VOID goto eval_cast_bad_type
		if *1p > TYPE_UNSIGNED_LONG goto eval_cast_bad_type
		expr += 8
		; @NONSTANDARD: we don't support, for example,  int x[(int)(float)5];
		expr = evaluate_constant_expression(token, expr, p_value)
		goto eval_fit_to_type
		:eval_cast_bad_type
			token_error(token, .str_eval_cast_bad_type)
		:str_eval_cast_bad_type
			string Bad type for constant cast (note: floating-point casts are not supported even though they are standard).
			byte 0
	:eval_constant_int
		expr += 8
		*8p_value = *8expr
		expr += 8
		return expr
	:eval_unary_plus
		expr += 8
		expr = evaluate_constant_expression(token, expr, p_value)
		return expr
	:eval_unary_minus
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		*8p_value = 0 - a
		goto eval_fit_to_type
	:eval_bitwise_not
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		*8p_value = ~a
		goto eval_fit_to_type
	:eval_logical_not
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		if a == 0 goto eval_value_1
		goto eval_value_0
	:eval_add
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a + b
		goto eval_fit_to_type
	:eval_sub
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a - b
		goto eval_fit_to_type
	:eval_mul
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a * b
		goto eval_fit_to_type
	:eval_div
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		if *1p == TYPE_UNSIGNED_LONG goto eval_div_unsigned
			; division is signed or uses a small type, so we can use 64-bit signed division
			*8p_value = a / b
			goto eval_fit_to_type
		:eval_div_unsigned
			; must use unsigned division
			divmod_unsigned(a, b, p_value, &a)
			goto eval_fit_to_type
	:eval_remainder
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		p = types + type
		if *1p == TYPE_UNSIGNED_LONG goto eval_rem_unsigned
			*8p_value = a % b
			goto eval_fit_to_type
		:eval_rem_unsigned
			divmod_unsigned(a, b, &a, p_value)
			goto eval_fit_to_type
	:eval_lshift
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a < b
		goto eval_fit_to_type
	:eval_rshift
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		p = types + type
		p = *1p
		p &= 1 ; signed types are odd
		if p == 1 goto eval_signed_rshift
			*8p_value = a > b
			goto eval_fit_to_type
		:eval_signed_rshift
			local v
			mask = a > 63 ; sign bit
			
			; sign extension
			mask <= b
			mask -= 1
			mask <= 64 - b
			
			v = a > b
			v += mask
			*8p_value = v
			goto eval_fit_to_type
	
	; comparison masks:
	;   1 = less than
	;   2 = equal to
	;   4 = greater than
	; e.g. not-equal is 1|4 = 5 because not equal = less than or greater than
	:eval_eq
		mask = 2
		goto eval_comparison
	:eval_neq
		mask = 5
		goto eval_comparison
	:eval_lt
		mask = 1
		goto eval_comparison
	:eval_gt
		mask = 4
		goto eval_comparison
	:eval_leq
		mask = 3
		goto eval_comparison
	:eval_geq
		mask = 6
		goto eval_comparison
	:eval_comparison
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		
		p = types + type
		p = *1p
		p &= 1
		
		if a == b goto eval_comparison_eq
		
		; for checking < and >, we care about whether a and b are signed
		if p == 1 goto eval_signed_comparison
			if a ] b goto eval_comparison_gt
			goto eval_comparison_lt
		:eval_signed_comparison
			if a > b goto eval_comparison_gt
			goto eval_comparison_lt
		
		:eval_comparison_eq
			; a == b
			mask &= 2
			goto eval_comparison_done
		:eval_comparison_lt
			; a < b
			mask &= 1
			goto eval_comparison_done
		:eval_comparison_gt
			; a > b
			mask &= 4
			goto eval_comparison_done
		:eval_comparison_done
			if mask != 0 goto eval_value_1
			goto eval_value_0
	:eval_bitwise_and
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a & b
		goto eval_fit_to_type
	:eval_bitwise_or
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a | b
		goto eval_fit_to_type
	:eval_bitwise_xor
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		*8p_value = a ^ b
		goto eval_fit_to_type
	:eval_logical_and
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		if a == 0 goto eval_value_0
		expr = evaluate_constant_expression(token, expr, &b)
		if b == 0 goto eval_value_0
		goto eval_value_1
	:eval_logical_or
		expr += 8
		expr = evaluate_constant_expression(token, expr, &a)
		if a != 0 goto eval_value_1
		expr = evaluate_constant_expression(token, expr, &b)
		if b != 0 goto eval_value_1
		goto eval_value_0
	:eval_conditional
		expr += 8
		expr = evaluate_constant_expression(token, expr, &mask)
		expr = evaluate_constant_expression(token, expr, &a)
		expr = evaluate_constant_expression(token, expr, &b)
		if mask == 0 goto eval_conditional_b
			*8p_value = a
			goto eval_fit_to_type
		:eval_conditional_b
			*8p_value = b
			goto eval_fit_to_type
		
	:eval_fit_to_type
	*8p_value = fit_to_type(*8p_value, type)
	return expr
	:eval_value_0
	*8p_value = 0
	return expr
	:eval_value_1
	*8p_value = 1
	return expr
	

; value is the output of some arithmetic expression; correct it to be within the range of type.
function fit_to_type
	argument value
	argument type
	local c
	local s
	c = types + type
	c = *1c
	if c == TYPE_CHAR goto fit_to_type_char
	if c == TYPE_UNSIGNED_CHAR goto fit_to_type_uchar
	if c == TYPE_SHORT goto fit_to_type_short
	if c == TYPE_UNSIGNED_SHORT goto fit_to_type_ushort
	if c == TYPE_INT goto fit_to_type_int
	if c == TYPE_UNSIGNED_INT goto fit_to_type_uint
	if c == TYPE_LONG goto fit_to_type_long
	if c == TYPE_UNSIGNED_LONG goto fit_to_type_ulong
	fputs(2, .str_bad_fit_to_type)
	exit(1)
	:str_bad_fit_to_type
		string Bad type passed to fit_to_type.
		byte 10
		byte 0
	; yes, signed integer overflow is undefined behavior and
	; casting to a signed integer is implementation-defined;
	;   i'm going to play it safe and implement it properly
	:fit_to_type_char
		value &= 0xff
		s = value > 7 ; sign bit
		value += s * 0xffffffffffffff00 ; sign-extend
		return value
	:fit_to_type_uchar
		value &= 0xff
		return value
	:fit_to_type_short
		value &= 0xffff
		s = value > 15 ; sign bit
		value += s * 0xffffffffffff0000 ; sign-extend
		return value
	:fit_to_type_ushort
		value &= 0xffff
		return value
	:fit_to_type_int
		value &= 0xffffffff
		s = value > 31 ; sign bit
		value += s * 0xffffffff00000000 ; sign-extend
		return value
	:fit_to_type_uint
		value &= 0xffffffff
		return value
	:fit_to_type_long
	:fit_to_type_ulong
		return value
; the "usual conversions" for binary operators, as the C standard calls it
function expr_binary_type_usual_conversions
	argument token ; for errors
	argument type1
	argument type2
	
	local ptype1
	local ptype2
	local kind1
	local kind2
	
	if type1 == 0 goto return_0
	if type2 == 0 goto return_0
	
	ptype1 = types + type1
	ptype2 = types + type2
	
	kind1 = *1ptype1
	kind2 = *1ptype2
	
	if kind1 > TYPE_DOUBLE goto usual_bad_types_to_operator
	if kind2 > TYPE_DOUBLE goto usual_bad_types_to_operator
	
	; "if either operand has type double, the other operand is converted to double"
	if kind1 == TYPE_DOUBLE goto return_type_double
	if kind2 == TYPE_DOUBLE goto return_type_double
	; "if either operand has type float, the other operand is converted to float"
	if kind1 == TYPE_FLOAT goto return_type_float
	if kind2 == TYPE_FLOAT goto return_type_float
	; "If either operand has type unsigned long int, the other operand is converted to unsigned long int"
	if kind1 == TYPE_UNSIGNED_LONG goto return_type_unsigned_long
	if kind2 == TYPE_UNSIGNED_LONG goto return_type_unsigned_long
	; "if either operand has type long int, the other operand is converted to long int"
	if kind1 == TYPE_LONG goto return_type_long
	if kind2 == TYPE_LONG goto return_type_long
	; "if either operand has type unsigned int, the other operand is converted to unsigned int."
	if kind1 == TYPE_UNSIGNED_INT goto return_type_unsigned_int
	if kind2 == TYPE_UNSIGNED_INT goto return_type_unsigned_int
	; "Otherwise, both operands have type int."
	goto return_type_int
	
	:str_space_and_space
		string  and
		byte 32
		byte 0
	:usual_bad_types_to_operator
		bad_types_to_operator(token, type1, type2)

function bad_types_to_operator
	argument token
	argument type1
	argument type2
	
	fprint_token_location(1, token)
	puts(.str_bad_types_to_operator)
	print_type(type1)
	puts(.str_space_and_space)
	print_type(type2)
	putc(10)
	exit(1)
	:str_bad_types_to_operator
		string : Bad types to operator:
		byte 32
		byte 0

function type_promotion
	argument type
	local p
	p = types + type
	if *1p < TYPE_INT goto return_type_int
	return type

; return precedence of given operator token, or 0xffff if not an operator
function operator_precedence
	argument token
	argument is_first
	local op
	local b
	
	if is_first != 0 goto operator_precedence_unary
	
	; if an operator is preceded by another, it must be a unary operator, e.g.
	;   in 5 + *x, * is a unary operator
	op = token - 16
	op = *1op
	if op == SYMBOL_RPAREN goto figre_out_rparen_arity
	op = is_operator(op)
	
	; if an operator is immediately followed by another (including lparen), the second must be 
	; unary.
	if op != 0 goto operator_precedence_unary
	
	:operator_precedence_binary
	op = *1token
	
	; see "C OPERATOR PRECEDENCE" in constants.b
	if op == SYMBOL_COMMA goto return_0x10
	if op == SYMBOL_EQ goto return_0x20
	if op == SYMBOL_PLUS_EQ goto return_0x20
	if op == SYMBOL_MINUS_EQ goto return_0x20
	if op == SYMBOL_TIMES_EQ goto return_0x20
	if op == SYMBOL_DIV_EQ goto return_0x20
	if op == SYMBOL_PERCENT_EQ goto return_0x20
	if op == SYMBOL_LSHIFT_EQ goto return_0x20
	if op == SYMBOL_RSHIFT_EQ goto return_0x20
	if op == SYMBOL_AND_EQ goto return_0x20
	if op == SYMBOL_OR_EQ goto return_0x20
	if op == SYMBOL_XOR_EQ goto return_0x20
	if op == SYMBOL_QUESTION goto return_0x30
	if op == SYMBOL_OR_OR goto return_0x40
	if op == SYMBOL_AND_AND goto return_0x50
	if op == SYMBOL_OR goto return_0x60
	if op == SYMBOL_XOR goto return_0x70
	if op == SYMBOL_AND goto return_0x80
	if op == SYMBOL_EQ_EQ goto return_0x90
	if op == SYMBOL_NOT_EQ goto return_0x90
	if op == SYMBOL_LT goto return_0xa0
	if op == SYMBOL_GT goto return_0xa0
	if op == SYMBOL_LT_EQ goto return_0xa0
	if op == SYMBOL_GT_EQ goto return_0xa0
	if op == SYMBOL_LSHIFT goto return_0xb0
	if op == SYMBOL_RSHIFT goto return_0xb0
	if op == SYMBOL_PLUS goto return_0xc0
	if op == SYMBOL_MINUS goto return_0xc0
	if op == SYMBOL_TIMES goto return_0xd0
	if op == SYMBOL_DIV goto return_0xd0
	if op == SYMBOL_PERCENT goto return_0xd0
	if op == SYMBOL_ARROW goto return_0xf0
	if op == SYMBOL_DOT goto return_0xf0
	if op == SYMBOL_LPAREN goto return_0xf0 ; function call
	if op == SYMBOL_LSQUARE goto return_0xf0 ; subscript
	if op == SYMBOL_PLUS_PLUS goto return_0xf0
	if op == SYMBOL_MINUS_MINUS goto return_0xf0
	
	return 0xffff
	
	:operator_precedence_unary
	op = *1token
	
	if op == KEYWORD_SIZEOF goto return_0xe0
	if op == SYMBOL_PLUS_PLUS goto return_0xe0
	if op == SYMBOL_MINUS_MINUS goto return_0xe0
	if op == SYMBOL_AND goto return_0xe0
	if op == SYMBOL_TIMES goto return_0xe0
	if op == SYMBOL_PLUS goto return_0xe0
	if op == SYMBOL_MINUS goto return_0xe0
	if op == SYMBOL_TILDE goto return_0xe0
	if op == SYMBOL_NOT goto return_0xe0
	if op == SYMBOL_LPAREN goto cast_precedence
	
	return 0xffff
	:cast_precedence
	; make sure this actually is a cast
	;   this is necessary to handle both
	;         - (x)->something
	;     and - (int)x->something
	;   correctly (in the first case, the arrow is the top-level operator, but in the second, the cast is)
	token += 16
	b = token_is_type(token)
	if b == 0 goto return_0xffff
	goto return_0xd8 ; it's a cast
	
	:figre_out_rparen_arity
	; given that the token before this one is a right-parenthesis, figure out if
	; this is a unary or binary operator. this is (annoyingly) necessary, because:
	;  (int)-x;   /* cast processed first */
	;  (y)-x;     /* subtraction processed first */
	local p
	p = token - 16
	token_reverse_to_matching_lparen(&p)
	p += 16
	b = token_is_type(p)
	if b != 0 goto operator_precedence_unary ; e.g. (int)-x;	
	goto operator_precedence_binary ; e.g. (y)-x;
	
function unary_op_to_expression_type
	argument op
	if op == SYMBOL_PLUS_PLUS goto return_EXPRESSION_PRE_INCREMENT
	if op == SYMBOL_MINUS_MINUS goto return_EXPRESSION_PRE_DECREMENT
	if op == SYMBOL_AND goto return_EXPRESSION_ADDRESS_OF
	if op == SYMBOL_TIMES goto return_EXPRESSION_DEREFERENCE
	if op == SYMBOL_PLUS goto return_EXPRESSION_UNARY_PLUS
	if op == SYMBOL_MINUS goto return_EXPRESSION_UNARY_MINUS
	if op == SYMBOL_TILDE goto return_EXPRESSION_BITWISE_NOT
	if op == SYMBOL_NOT goto return_EXPRESSION_LOGICAL_NOT
	if op == SYMBOL_LPAREN goto return_EXPRESSION_CAST
	return 0

:return_EXPRESSION_PRE_INCREMENT
	return EXPRESSION_PRE_INCREMENT
:return_EXPRESSION_PRE_DECREMENT
	return EXPRESSION_PRE_INCREMENT
:return_EXPRESSION_ADDRESS_OF
	return EXPRESSION_ADDRESS_OF
:return_EXPRESSION_DEREFERENCE
	return EXPRESSION_DEREFERENCE
:return_EXPRESSION_UNARY_PLUS
	return EXPRESSION_UNARY_PLUS
:return_EXPRESSION_UNARY_MINUS
	return EXPRESSION_UNARY_MINUS
:return_EXPRESSION_BITWISE_NOT
	return EXPRESSION_BITWISE_NOT
:return_EXPRESSION_LOGICAL_NOT
	return EXPRESSION_LOGICAL_NOT
:return_EXPRESSION_CAST
	return EXPRESSION_CAST

; is this operator right-associative? most C operators are left associative,
; but += / -= / etc. are not
function operator_right_associative
	argument op
	if op == SYMBOL_QUESTION goto return_1
	if op < SYMBOL_EQ goto return_0
	if op > SYMBOL_OR_EQ goto return_0
	goto return_1

:binop_table
	byte SYMBOL_COMMA
	byte EXPRESSION_COMMA
	byte SYMBOL_EQ
	byte EXPRESSION_ASSIGN
	byte SYMBOL_PLUS_EQ
	byte EXPRESSION_ASSIGN_ADD
	byte SYMBOL_MINUS_EQ
	byte EXPRESSION_ASSIGN_SUB
	byte SYMBOL_TIMES_EQ
	byte EXPRESSION_ASSIGN_MUL
	byte SYMBOL_DIV_EQ
	byte EXPRESSION_ASSIGN_DIV
	byte SYMBOL_PERCENT_EQ
	byte EXPRESSION_ASSIGN_REMAINDER
	byte SYMBOL_LSHIFT_EQ
	byte EXPRESSION_ASSIGN_LSHIFT
	byte SYMBOL_RSHIFT_EQ
	byte EXPRESSION_ASSIGN_RSHIFT
	byte SYMBOL_AND_EQ
	byte EXPRESSION_ASSIGN_AND
	byte SYMBOL_OR_EQ
	byte EXPRESSION_ASSIGN_OR
	byte SYMBOL_XOR_EQ
	byte EXPRESSION_ASSIGN_XOR
	byte SYMBOL_OR_OR
	byte EXPRESSION_LOGICAL_OR
	byte SYMBOL_AND_AND
	byte EXPRESSION_LOGICAL_AND
	byte SYMBOL_OR
	byte EXPRESSION_BITWISE_OR
	byte SYMBOL_XOR
	byte EXPRESSION_BITWISE_XOR
	byte SYMBOL_AND
	byte EXPRESSION_BITWISE_AND
	byte SYMBOL_EQ_EQ
	byte EXPRESSION_EQ
	byte SYMBOL_NOT_EQ
	byte EXPRESSION_NEQ
	byte SYMBOL_LT
	byte EXPRESSION_LT
	byte SYMBOL_GT
	byte EXPRESSION_GT
	byte SYMBOL_LT_EQ
	byte EXPRESSION_LEQ
	byte SYMBOL_GT_EQ
	byte EXPRESSION_GEQ
	byte SYMBOL_LSHIFT
	byte EXPRESSION_LSHIFT
	byte SYMBOL_RSHIFT
	byte EXPRESSION_RSHIFT
	byte SYMBOL_PLUS
	byte EXPRESSION_ADD
	byte SYMBOL_MINUS
	byte EXPRESSION_SUB
	byte SYMBOL_TIMES
	byte EXPRESSION_MUL
	byte SYMBOL_DIV
	byte EXPRESSION_DIV
	byte SYMBOL_PERCENT
	byte EXPRESSION_REMAINDER
	byte SYMBOL_ARROW
	byte EXPRESSION_ARROW
	byte SYMBOL_DOT
	byte EXPRESSION_DOT
	byte SYMBOL_LSQUARE
	byte EXPRESSION_SUBSCRIPT
	byte 0
	byte 0

function binop_symbol_to_expression_type
	argument op
	local p
	p = .binop_table
	:binop_symbol_to_expression_type_loop
		if *1p == op goto binop_symbol_to_expression_type_found
		p += 2
		if *1p != 0 goto binop_symbol_to_expression_type_loop
	return 0
	:binop_symbol_to_expression_type_found
		p += 1
		return *1p

function is_operator
	argument symbol
	local b
	b = binop_symbol_to_expression_type(symbol)
	if b != 0 goto return_1
	b = unary_op_to_expression_type(symbol)
	if b != 0 goto return_1
	goto return_0

function binop_expression_type_to_symbol
	argument exprtype
	local p
	p = .binop_table
	:binop_expr2symb_type_loop
		p += 1
		if *1p == exprtype goto binop_expr2symb_type_found
		p += 1
		if *1p != 0 goto binop_expr2symb_type_loop
	return 0
	:binop_expr2symb_type_found
		p -= 1
		return *1p



function int_suffix_to_type
	argument suffix
	if suffix == NUMBER_SUFFIX_L goto return_type_long
	if suffix == NUMBER_SUFFIX_U goto return_type_unsigned_int
	if suffix == NUMBER_SUFFIX_UL goto return_type_unsigned_long
	goto return_type_int

function float_suffix_to_type
	argument suffix
	if suffix == NUMBER_SUFFIX_F goto return_type_float
	goto return_type_double

; smallest integer type which can fit this value, only using unsigned if necessary
function int_value_to_type
	argument value
	if value [ 0x80000000 goto return_type_int
	if value [ 0x8000000000000000 goto return_type_long
	goto return_type_unsigned_long

; returns pointer to end of expression
function print_expression
	argument expression
	local c
	local b
	local p
	p = expression + 4
	if *4p == 0 goto print_expr_skip_type
	putc(40)
	print_type(*4p)
	putc(41)
	:print_expr_skip_type
	c = *1expression
	
	if c == EXPRESSION_CONSTANT_INT goto print_expr_int
	if c == EXPRESSION_CONSTANT_FLOAT goto print_expr_float
	if c == EXPRESSION_STRING_LITERAL goto print_expr_str
	if c == EXPRESSION_POST_INCREMENT goto print_post_increment
	if c == EXPRESSION_POST_DECREMENT goto print_post_decrement
	if c == EXPRESSION_DOT goto print_expr_dot
	if c == EXPRESSION_ARROW goto print_expr_arrow
	if c == EXPRESSION_PRE_INCREMENT goto print_pre_increment
	if c == EXPRESSION_PRE_DECREMENT goto print_pre_decrement
	if c == EXPRESSION_ADDRESS_OF goto print_address_of
	if c == EXPRESSION_DEREFERENCE goto print_dereference
	if c == EXPRESSION_UNARY_PLUS goto print_unary_plus
	if c == EXPRESSION_UNARY_MINUS goto print_unary_minus
	if c == EXPRESSION_BITWISE_NOT goto print_bitwise_not
	if c == EXPRESSION_LOGICAL_NOT goto print_logical_not
	if c == EXPRESSION_CAST goto print_cast

	b = binop_expression_type_to_symbol(c)
	if b != 0 goto print_expr_binop
	
	puts(.str_print_bad_expr)
	exit(1)
	
	:str_print_bad_expr
		string Bad expression passed to print_expression.
		byte 10
		byte 0
	:print_cast
		; we've already printed the type
		expression += 8
		expression = print_expression(expression)
		return expression
	:print_expr_int
		expression += 8
		putn_signed(*8expression)
		expression += 8
		return expression
	:print_expr_float
		expression += 8
		putx64(*8expression)
		expression += 8
		return expression
	:print_expr_str
		expression += 8
		putc('0)
		putc('x)
		putx32(*8expression)
		expression += 8
		return expression
	:print_expr_binop
		putc(40)
		expression += 8
		expression = print_expression(expression) ; 1st operand
		b = get_keyword_str(b)
		puts(b)
		expression = print_expression(expression) ; 2nd operand
		putc(41)
		return expression
	:print_expr_dot
		putc(40)
		expression += 8
		expression = print_expression(expression)
		puts(.str_dot)
		putn(*8expression)
		expression += 8
		putc(41)
		return expression
	:print_expr_arrow
		putc(40)
		expression += 8
		expression = print_expression(expression)
		puts(.str_arrow)
		putn(*8expression)
		expression += 8
		putc(41)
		return expression
	:print_post_increment
		putc(40)
		expression += 8
		expression = print_expression(expression)
		putc('+)
		putc('+)
		putc(41)
		return expression
	:print_post_decrement
		putc(40)
		expression += 8
		expression = print_expression(expression)
		putc('-)
		putc('-)
		putc(41)
		return expression
	:print_pre_increment
		putc(40)
		putc('+)
		putc('+)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_pre_decrement
		putc(40)
		putc('-)
		putc('-)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_address_of
		putc(40)
		putc('&)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_dereference
		putc(40)
		putc('*)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_unary_plus
		putc(40)
		putc('+)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_unary_minus
		putc(40)
		putc('-)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_bitwise_not
		putc(40)
		putc('~)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression
	:print_logical_not
		putc(40)
		putc('!)
		expression += 8
		expression = print_expression(expression)
		putc(41)
		return expression

; NOTE: to make things easier, the format which this outputs isn't the same as C's, specifically we have
;    *int for pointer to int and [5]int for array of 5 ints
function print_type
	argument type
	local c
	:print_type_top
	c = types + type
	c = *1c
	if c == TYPE_VOID goto print_type_void
	if c == TYPE_CHAR goto print_type_char
	if c == TYPE_UNSIGNED_CHAR goto print_type_unsigned_char
	if c == TYPE_SHORT goto print_type_short
	if c == TYPE_UNSIGNED_SHORT goto print_type_unsigned_short
	if c == TYPE_INT goto print_type_int
	if c == TYPE_UNSIGNED_INT goto print_type_unsigned_int
	if c == TYPE_LONG goto print_type_long
	if c == TYPE_UNSIGNED_LONG goto print_type_unsigned_long
	if c == TYPE_FLOAT goto print_type_float
	if c == TYPE_DOUBLE goto print_type_double
	if c == TYPE_POINTER goto print_type_pointer
	if c == TYPE_ARRAY goto print_type_array
	if c == TYPE_STRUCT goto print_type_struct
	if c == TYPE_FUNCTION goto print_type_function
	fputs(2, .str_bad_print_type)
	exit(1)
	:str_bad_print_type
		string Bad type passed to print_type.
		byte 10
		byte 0
	:print_type_void
		return puts(.str_void)
	:print_type_char
		return puts(.str_char)
	:print_type_unsigned_char
		return puts(.str_unsigned_char)
	:print_type_short
		return puts(.str_short)
	:print_type_unsigned_short
		return puts(.str_unsigned_short)
	:print_type_int
		return puts(.str_int)
	:print_type_unsigned_int
		return puts(.str_unsigned_int)
	:print_type_long
		return puts(.str_long)
	:print_type_unsigned_long
		return puts(.str_unsigned_long)
	:print_type_float
		return puts(.str_float)
	:print_type_double
		return puts(.str_double)
	:print_type_pointer
		putc('*)
		type += 1
		goto print_type_top
	:print_type_array
		putc('[)
		type += 1
		c = types + type
		putn(*8c) ; UNALIGNED
		putc('])
		type += 8
		goto print_type_top
	:print_type_struct
		return puts(.str_struct)
	:print_type_function
		type += 1
		putc(40)
		putc(40)
		:print_type_function_loop
			c = types + type
			if *1c == 0 goto print_type_function_loop_end
			print_type(type)
			putc(44)
			type += type_length(type)
			goto print_type_function_loop
		:print_type_function_loop_end
		type += 1 ; 0 terminator
		putc(41)
		putc(32)
		putc('-)
		putc('>)
		putc(32)
		print_type(type)
		putc(41)
		return