~pixelherodev/zyg

ref: 7dccbd7d9dc502f2cd859c4c7ebd84a0d8f3acd7 zyg/parser.c -rw-r--r-- 32.5 KiB
7dccbd7dNoam Preil merge 2 months ago
                                                                                
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
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

#include "c3/tags.h"
#include "c3/c3.h"
#include "c3/util.h"
#include "c3/stb.h"

#include "package.h"
#include "context.h"
#include "lexer.h"
#include "queue.h"

#undef FATAL
#define FATAL(x, ...) {ERROR(x, __VA_ARGS__);return -1;}

// Functions parser_foo are functions specific to the parser - which sounds
// obvious but carries an important distinction: functions p_foo are parser
// functions which MUST leave token_index equal to their last token when they
// return.
//
// Since we use multiprocessing, the statics are uniqued to each process

static struct {
	// The current index into the zig file's tokens
	size_t i;
	c3ctx tree;
	struct zygtokens tokens;
	char *path;
	// This stores several lists for accelerated deduplication
	struct {
		uint32_t *values;
		uint32_t *ints;
		uint32_t *strs;
		uint32_t *idents;
		uint32_t *misc;
		uint32_t token_count;
	} accel;
} ctx;

static uint32_t p_assign_expr(void);
static uint32_t p_block(void);
static uint32_t p_expr(void);
static uint32_t p_fnexpr(int legacy, int external);
static uint32_t p_type_expr(void);
static uint32_t p_ident(void);
static uint32_t p_str(void);
static uint32_t p_var_decl(void);

static void
parser_init(char *path)
{
	struct zyglexed lexed = zyg_lex(path);
	ctx.path = path;
	ctx.tree = lexed.tree;
	ctx.tokens = lexed.tokens;
	ctx.accel.values = NULL;
	ctx.accel.ints = NULL;
	ctx.accel.strs = NULL;
	ctx.accel.idents = NULL;
	ctx.accel.misc = NULL;
	ctx.accel.token_count = stb_sb_count(ctx.tokens.tags);
}

static void
parser_cleanup(void)
{
	stb_sb_free(ctx.accel.values);
	stb_sb_free(ctx.accel.ints);
	stb_sb_free(ctx.accel.strs);
	stb_sb_free(ctx.accel.idents);
	stb_sb_free(ctx.accel.misc);
}

//// Parser subfunctions

#define NEXPECT(etag,...) \
	if(ctx.i >= ctx.accel.token_count || ctx.tokens.tags[ctx.i] == etag) \
		FATAL(__VA_ARGS__);

#define EXPECT(etag,...) \
	if(ctx.i >= ctx.accel.token_count || ctx.tokens.tags[ctx.i] != etag){ \
		ERROR(__VA_ARGS__);abort();}

#define IS_CMP() (ctx.i< ctx.accel.token_count && ( \
	ctx.tokens.tags[ctx.i] == C3_DEQ || \
	ctx.tokens.tags[ctx.i] == C3_NEQ || \
	ctx.tokens.tags[ctx.i] == C3_LE || \
	ctx.tokens.tags[ctx.i] == C3_GT || \
	ctx.tokens.tags[ctx.i] == C3_LEQ || \
	ctx.tokens.tags[ctx.i] == C3_GEQ))

static int parser_is(uint8_t offset, uint16_t tag);

// Advances to the next token, transparently skipping all whitespace and comment tokens
static void
parser_advance(void)
{
	// TODO: once whitespace and comment tokens are added again in the lexer,
	// need to handle them here when applicable. Maybe use a fnptr to select
	// parser_advance based on driver mode?
//	do {
    		ctx.i += 1;
//	}
   // 	while(parser_is(0, C3_SPACE) || parser_is(0, C3_COMMENT));
}

static int
parser_is(uint8_t offset, uint16_t tag)
{
	uint32_t cur;
	int ret;
	cur = ctx.i;
	uint8_t i;
	for(i = 0; i < offset; i += 1)
		parser_advance();
	ret = ctx.i < ctx.accel.token_count && ctx.tokens.tags[ctx.i] == tag;
	ctx.i = cur;
	return ret;
}

// Returns 1 if the current token is the matched tag, and 0 if not
// If this matches, we advance to the next token
static int
parser_is_consume(uint16_t tag)
{
	int ret = parser_is(0, tag);
	if(ret)
		parser_advance();
	return ret;
}

static uint32_t
parser_dedup_node_tag(uint32_t **list, uint32_t tag)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(*list); i += 1)
		if(c3nodetag(ctx.tree, (*list)[i]) == tag)
			return (*list)[i];
	// Not yet present!
	i = c3append(&ctx.tree, tag, 0);
	if(i == -1 || !stb_sb_push_u32(list, i))
		OOM();
	return i;
}

static uint32_t
parser_dedup_node(uint32_t **list, c3tag tag, uint32_t kid)
{
	uint32_t i;
	for(i = 0; i < stb_sb_count(*list); i += 1)
		if(c3nodetag(ctx.tree, (*list)[i]) == tag && c3nodechild(ctx.tree, (*list)[i], 0) == kid)
			return (*list)[i];
	// Not yet present!
	i = c3append(&ctx.tree, tag, 1, kid);
	if(i == -1 || !stb_sb_push_u32(list, i))
		OOM();
	return i;
}

static uint32_t
p_test_decl(void)
{
	uint32_t name;
	uint32_t block;
	uint32_t index;
	name = -1;
	parser_advance();
	if(parser_is(0, C3_LITERAL_STRING)){
		name = p_str();
		parser_advance();
	}
	block = p_block();
	if(block == -1)
		FATAL("Parsing test block failed", 0);
	index = c3append(&ctx.tree, C3_TEST, 2, name, block);
	if(index == -1)
		OOM();
	return index;
}

static uint32_t
p_tl_comptime(void)
{
	uint32_t block, index;
	parser_advance();
	block = p_block();
	if(block == -1)
		return -1;
	index = c3append(&ctx.tree, C3_COMPTIME, 1, block);
	if(index == -1)
		OOM();
	return index;
}

// returns the tag of the current prefixop, or 0 for none
static uint16_t
p_prefixops(void)
{
	if(parser_is(0, C3_EXCLAIM)){
		parser_advance();
		return C3_EXPR_NOT;
	}
	if(parser_is(0, C3_MINUS)){
		parser_advance();
		return C3_EXPR_NEGATIVE;
	}
	if(parser_is(0, C3_TILDE)){
		parser_advance();
		return C3_EXPR_BITWISE_NOT;
	}
	if(parser_is(0, C3_WMINUS)){
		parser_advance();
		return C3_EXPR_WRAP_NEGATIVE;
	}
	if(parser_is(0, C3_AMPERSAND)){
		parser_advance();
		return C3_EXPR_ADDR_OF;
	}
	if(parser_is(0, C3_TRY)){
		parser_advance();
		return C3_EXPR_TRY;
	}
	if(parser_is(0, C3_AWAIT)){
		parser_advance();
		return C3_EXPR_AWAIT;
	}
	return C3_BAD;
}

static uint32_t
p_return(void)
{
	uint32_t operand;
	if(parser_is(1, C3_SCOLON))
		operand = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_VOID);
	else{
		parser_advance();
		operand = p_expr();
	}
	if(operand == -1)
		return -1;
	return c3append(&ctx.tree, C3_EXPR_RETURN, 1, operand);
}

static uint32_t
p_while(void)
{
	uint32_t condition;
	uint32_t block;
	parser_advance();
	EXPECT(C3_LPAREN, "Expected '(' after 'while', found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	condition = p_expr();
	if(condition == -1)
		return -1;
	parser_advance();
	EXPECT(C3_RPAREN, "Expected 'R' after WhileExpr condition, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	if(parser_is(0, C3_PIPE))
		FATAL("TODO: while payload", 0);
	if(parser_is(0, C3_COLON))
		FATAL("TODO: while continue expr", 0);
	block = p_block();
	if(block == -1)
		return -1;
	if(parser_is(1, C3_KW_ELSE))
		FATAL("TODO: while else", 0);
	return c3append(&ctx.tree, C3_LOOP, 2, condition, block);
}

static uint32_t
p_if(void)
{
	uint32_t condition;
	uint32_t block;
	parser_advance();
	EXPECT(C3_LPAREN, "Expected '(' after 'if', found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	condition = p_expr();
	if(condition == -1)
		return -1;
	parser_advance();
	EXPECT(C3_RPAREN, "Expected 'R' after IfStmt condition, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	if(parser_is(0, C3_PIPE))
		FATAL("TODO: PtrPayload", 0);
	if(parser_is(0, C3_LBRACE))
		block = p_block();
	else
		FATAL("TODO: single-statement if", 0);
	if(block == -1)
		return -1;
	if(parser_is(1, C3_KW_ELSE))
		FATAL("TODO: if/else", 0);
	return c3append(&ctx.tree, C3_EXPR_IF, 2, condition, block);
}

static uint32_t
p_for(void)
{
	FATAL("TODO: for loops", 0);
}

static uint32_t
p_loop(void)
{
	int is_inline;
	uint32_t loop;
	is_inline = parser_is(0, C3_INLINE);
	if(is_inline)
		parser_advance();
	if(parser_is(0, C3_FOR))
		loop = p_for();
	else if(parser_is(0, C3_WHILE))
		loop = p_while();
	else
		FATAL("Expected 'for' or 'while' for LoopExpr", 0);
	if(is_inline)
		FATAL("TODO: inline loops", 0);
	return loop;
}

static uint32_t
p_init(void)
{
	uint32_t *fields;
	uint32_t ident;
	uint32_t expr;
	uint32_t field;
	fields = NULL;
	parser_advance();
	if(!parser_is(0, C3_DOT))
		FATAL("TODO: initlist !field",0);
	while(parser_is(0, C3_DOT)){
		parser_advance();
		if(!parser_is(0, C3_IDENT))
			FATAL("expected ident",0);
		ident = p_ident();
		parser_advance();
		if(!parser_is(0, C3_EQ))
			FATAL("expected =",0);
		parser_advance();
		expr = p_expr();
		field = c3append(&ctx.tree, C3_FIELD_INIT, 2, ident, expr);
		if(field == -1)
			OOM();
		if(!stb_sb_push_u32(&fields, field))
			OOM();
		if(parser_is(1, C3_COMMA)){
			parser_advance();
			if(parser_is(1, C3_DOT))
				parser_advance();
		}
	}
	if(!parser_is(1, C3_RBRACE))
		FATAL("Expected } after init list", 0);
	parser_advance();
	// DOT IDENT EQUAL EXPR
	expr = c3appendarr(&ctx.tree, C3_INITIALIZED, stb_sb_count(fields), fields);
	stb_sb_free(fields);
	if(expr == -1)
		OOM();
	return expr;
}

static uint32_t
p_curly_suffix_expr(void)
{
	uint32_t type;
	uint32_t init;
	type = p_type_expr();
	init = -1;
	if(parser_is(1, C3_LBRACE)){
		parser_advance();
		init = p_init();
	}
	if(init != -1)
		return c3append(&ctx.tree, C3_INITIALIZED, 2, type, init);
	return type;
}

static uint32_t
p_asm(void)
{
	uint32_t *kids;
	uint32_t arg;
	parser_advance();
	if(!parser_is(0, C3_LPAREN))
		FATAL("Expected ( after asm",0);
	parser_advance();
	if(!parser_is(0, C3_LITERAL_STRING))
		FATAL("Expected string literal for inline asm, found %s", c3tagstr(ctx.tokens.tags[ctx.i]));
	kids = NULL;
	arg = p_str();
	if(arg == -1)
		FATAL("failed to read asm string", 0);
	if(!stb_sb_push_u32(&kids, arg))
		OOM();
	parser_advance();
	while(parser_is_consume(C3_COMMA)){
		arg = p_expr();
		if(arg == -1)
			FATAL("Failed to parse identifier", 0);
		if(!stb_sb_push_u32(&kids, arg))
			OOM();
		parser_advance();
	}
	if(!parser_is(0, C3_RPAREN)){
		ERROR("Expected ) to close inline asm", 0);
		stb_sb_free(kids);
		return -1;
	}
	arg = c3appendarr(&ctx.tree, C3_ASM, stb_sb_count(kids), kids);
	if(arg == -1)
		OOM();
	stb_sb_free(kids);
	return arg;
}

static uint32_t
p_primary_expr(void)
{
	if(parser_is(0, C3_KW_IF))
		FATAL("TODO: if", 0);
	if(parser_is(0, C3_INLINE) || parser_is(0, C3_WHILE) || parser_is(0, C3_FOR))
		return p_loop();
	if(parser_is(0, C3_CONTINUE))
		FATAL("TODO: continue", 0);
	if(parser_is(0, C3_BREAK))
		FATAL("TODO: break", 0);
	if(parser_is(0, C3_RESUME))
		FATAL("TODO: resume", 0);
	if(parser_is(0, C3_IDENT) && parser_is(1, C3_COLON))
		FATAL("TODO: labelled loop", 0);
	if(parser_is(0, C3_RETURN))
		return p_return();
	if(parser_is(0, C3_FN))
		return p_fnexpr(0, 0);
	if(parser_is(0, C3_LBRACE))
		return p_block();
	if(parser_is(0, C3_LBRACE))
		FATAL("TODO: curlysuffixexpr '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	if(parser_is(0, C3_ASM))
		return p_asm();
	if(parser_is(0, C3_COMPTIME))
		FATAL("TODO: comptime expr", 0);
	if(parser_is(0, C3_NOSUSPEND))
		FATAL("TODO: nosuspend expr", 0);
	return p_curly_suffix_expr();
}

static uint32_t
p_prefix_expr(void)
{
	uint16_t prefix;
	uint32_t deepest;
	uint32_t prefix_root;
	uint32_t primary;
	deepest = -1;
	prefix_root = -1;
	prefix = p_prefixops();
	while(prefix != C3_BAD){
		if(deepest == -1){
			prefix_root = c3append(&ctx.tree, prefix, 1, -1);
			if(prefix_root == -1)
				FATAL("ICE: OOM", 0);
			deepest = prefix_root;
		}
		else{
			ctx.tree.ast.nodes[deepest + 1].node_index = c3append(&ctx.tree, prefix, 1, -1);
			if(ctx.tree.ast.nodes[deepest + 1].node_index == -1)
				FATAL("ICE: OOM", 0);
			deepest = ctx.tree.ast.nodes[deepest + 1].node_index;
		}
		prefix = p_prefixops();
	}
	primary = p_primary_expr();
	if(primary == -1)
		FATAL("Failed to read primary expr", 0);
	if(prefix_root != -1){
		ctx.tree.ast.nodes[deepest + 2].node_index = primary;
		return prefix_root;
	}
	return primary;
}

static uint32_t
p_multiply_expr(void)
{
	uint32_t prefix;
	prefix = p_prefix_expr();
	if(parser_is(1, C3_ASTERISK))
		FATAL("TODO: multiply", 0);
	return prefix;
}

static uint32_t
p_addition_expr(void)
{
	uint32_t mult;
	uint32_t rhs;
	uint32_t add;
	uint16_t tag;
	int b;
	mult = p_multiply_expr();
	if(mult == -1)
		FATAL("Failed to read multiply expr", 0);
	b = parser_is(1, C3_PLUS);
	if(b || parser_is(1, C3_MINUS)){
	  tag = b ? C3_EXPR_ADD : C3_EXPR_SUB;
	  parser_advance();
	  parser_advance();
	  rhs = p_multiply_expr();
	  if(rhs == -1)
		  FATAL("Failed to read multiply expr", 0);
	  add = c3append(&ctx.tree, tag, 2, mult, rhs);
	  if (add == -1)
		FATAL("ICE: OOM", 0);
	  return add;
	}
	return mult;
}

static uint32_t
p_bitshift_expr(void)
{
	uint32_t add;
	uint32_t rhs;
	uint16_t tag;
	uint32_t shift;
	add = p_addition_expr();
	if(add == -1)
		FATAL("Failed to read bitshift expr", 0);
	if(parser_is(1, C3_LSHIFT) || parser_is(1, C3_RSHIFT)){
		if(parser_is(1, C3_LSHIFT))
			tag = C3_EXPR_BITSHIFT_LEFT;
		else
			FATAL("TODO: right shift", 0);
		parser_advance();
		parser_advance();
		rhs = p_addition_expr();
		if(rhs == -1)
			FATAL("Failed to read rhs of %s", c3tagstr(tag));
		shift = c3append(&ctx.tree, tag, 2, add, rhs);
		if(shift == -1)
			FATAL("ICE: OOM", 0);
		return shift;
	}
	return add;
}

static uint32_t
p_bitwise_expr(void)
{
	uint32_t shift;
	uint32_t rhs;
	uint16_t tag;
	uint32_t wise;
	shift = p_bitshift_expr();
	if(shift == -1)
		FATAL("Failed to read bitwise expr", 0);
	if(parser_is(1, C3_AMPERSAND) || parser_is(1, C3_CARET) || parser_is(1, C3_PIPE) || parser_is(1, C3_ORELSE) || parser_is(1, C3_CATCH)){
		if(parser_is(1, C3_PIPE))
			tag = C3_EXPR_BITWISE_OR;
		else
			FATAL("TODO: bitwise", 0);
		parser_advance();
		parser_advance();
		rhs = p_bitshift_expr();
		if(rhs == -1)
			FATAL("Failed to read rhs of %s", c3tagstr(tag));
		wise = c3append(&ctx.tree, tag, 2, shift, rhs);
		if(wise == -1)
			FATAL("ICE: OOM", 0);
		return wise;
	}
	return shift;
}

static uint32_t
p_compare_expr(void)
{
	uint32_t bit;
	bit = p_bitwise_expr();
	if(IS_CMP())
		FATAL("TODO: compare",0);
	return bit;
}

static uint32_t
p_booland_expr(void)
{
	uint32_t cmp;
	cmp = p_compare_expr();
	while(parser_is(0, C3_AND))
		FATAL("TODO: and",0);
	return cmp;
}

static uint32_t
p_boolor_expr(void)
{
	uint32_t and;
	and = p_booland_expr();
	while(parser_is(0, C3_OR))
		FATAL("TODO: or",0);
	return and;
}

static uint32_t
p_import(void)
{
	// Imports are internal to zyg, and are resolved before a tree is exposed, so we can use a
	// zyg-specific node for them
	uint32_t ident;
	uint32_t index;
	c3string string;
	char *buf;
	parser_advance();
	EXPECT(C3_LPAREN, "Expected lparen in @import!", 0);
	parser_advance();
	EXPECT(C3_LITERAL_STRING, "Expected string literal in @import!", 0);
	ident = ctx.tokens.strings[ctx.i];
	string = ctx.tree.core.strings[ident];
	buf = file_rel_dir(ctx.path, &ctx.tree.core.chars[string.index], string.length, NULL);
	queue_job(buf);
	free(buf);
	parser_advance();
	EXPECT(C3_RPAREN, "Expected rparen to close @import!", 0);
	index = c3append(&ctx.tree, C3_IMPORT, 1, ident);
	if(index == -1)
		OOM();
	return index;
}

static uint32_t
p_defer(void)
{
	uint32_t assn;
	uint32_t index;
	parser_advance();
	if(parser_is(0, C3_IDENT) && parser_is(1, C3_COLON)){
		ERROR("TODO: defer BlockExpr", 0);
		return -1;
	}
	assn = p_assign_expr();
	if(assn == -1)
		return -1;
	parser_advance();
	EXPECT(C3_SCOLON, "Expected ';' after AssignExpr, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	index = c3append(&ctx.tree, C3_EXPR_DEFER, 1, assn);
	if(index == -1)
		OOM();
	return index;
}

static uint32_t
p_stmt(void)
{
	uint32_t assn;
	NEXPECT(C3_COMPTIME, "TODO: comptime statements", 0);
	NEXPECT(C3_SUSPEND, "TODO: suspend statements", 0);
	NEXPECT(C3_NOSUSPEND, "TODO: nosuspend statements", 0);
	if(parser_is(0, C3_DEFER))
		return p_defer();
	NEXPECT(C3_ERRDEFER, "TODO: errdefer statements", 0);
	if(parser_is(0, C3_VAR) || parser_is(0, C3_CONST))
		return p_var_decl();
	if(parser_is(0, C3_KW_IF))
		return p_if();
	if(parser_is(0, C3_IDENT) && ctx.i + 1 < ctx.accel.token_count && ctx.tokens.tags[ctx.i + 1] == C3_COLON)
		FATAL("TODO: labeled statements", 0);
	if(parser_is(0, C3_INLINE) || parser_is(0, C3_WHILE) || parser_is(0, C3_FOR))
		return p_loop();
	NEXPECT(C3_SWITCH, "TODO: switch statements", 0);
	assn = p_assign_expr();
	if(assn == -1)
		return -1;
	parser_advance();
	EXPECT(C3_SCOLON, "Expected ';' after AssignExpr, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	return assn;
}

static uint32_t
p_block(void)
{
	uint32_t *kids;
	uint32_t index;
	uint32_t kid;
	kids = NULL;
	kid = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_UNRES);
	if(!stb_sb_push_u32(&kids, kid))
		return -1;
	EXPECT(C3_LBRACE, "Expected lbrace to open block, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	while(!parser_is(0, C3_RBRACE)){
		kid = p_stmt();
		if(kid == -1 || !stb_sb_push_u32(&kids, kid))
			return -1;
		parser_advance();
	}
	if(stb_sb_count(kids) > 0xFFFFFF){
		ERROR("Too many kids for block", 0);
		return -1;
	}
	EXPECT(C3_RBRACE, "Expected rbrace to close block, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
	index = c3appendarr(&ctx.tree, C3_BLOCK, stb_sb_count(kids), kids);
	stb_sb_free(kids);
	if(index == -1)
		OOM();
	return index;
}

struct prefix {
	enum{
		prefix_pointer,
		prefix_manypointer,
		prefix_array,
		prefix_slice,
		prefix_error,
		prefix_const,
	}tag;
	uint32_t value;
};

static struct prefix failed = { prefix_error };

// Returns a series of operations to perform, or the address of failed on failure
static struct prefix *
p_prefixtypeops(void)
{
	struct prefix *ops;
	ops = NULL;
	while(ctx.i < ctx.accel.token_count){
		if(parser_is(0, C3_QUESTION) || parser_is(0, C3_ASTERISK)){
		//	int tag = parser_is(0, C3_QUESTION) ? C3_TYPE_OPTIONAL : C3_TYPE_PTR;
			ERROR("TODO: optionals / ptrs", 0);
			return &failed;
		}
		if(parser_is(0, C3_PTRC) || parser_is(0, C3_PTRUNKNOWN)){
			ERROR("TODO: ptr-to-unknown and cptr", 0);
			return &failed;
		}
		if(parser_is(0, C3_ANYFRAME)){
			ERROR("TODO: frames", 0);
			return &failed;
		}
		if(parser_is(0, C3_LSQUARE)){
			parser_advance();
			if(!stb_sb_ensure_capacity(&ops, 1, sizeof(struct prefix))){
				ERROR("ICE: OOM", 0);
				return &failed;
			}
			if(parser_is(0, C3_ASTERISK) && parser_is(1, C3_RSQUARE)){
				ops[stb_sb_count(ops)].tag = prefix_slice;
				parser_advance();
				parser_advance();
			}
			else if(!parser_is(0, C3_RSQUARE)){
				ERROR("TODO: arrays %s", c3tagstr(ctx.tokens.tags[ctx.i]));
				return &failed;
			} else {
				ops[stb_sb_count(ops)].tag = prefix_slice;
				parser_advance();
			}
			stb_sb_increment(ops, 1);
			if(parser_is(0, C3_CONST)){
				if(!stb_sb_ensure_capacity(&ops, 1, sizeof(struct prefix))){
					ERROR("ICE: OOM", 0);
					return &failed;
				}
				ops[stb_sb_count(ops)].tag = prefix_const;
				stb_sb_increment(ops, 1);
				parser_advance();
			}
			continue;
		}
		break;
	}
	return ops;
}

static uint32_t p_tl(void);

static uint32_t
p_container(uint16_t tag)
{
	uint32_t root;
	uint32_t kid;
	uint32_t *kids;
	uint32_t init_token;
	uint32_t last;
	kids = NULL;
	parser_advance();
	if(!parser_is(0, C3_LBRACE)){
		ERROR("Expected { after STRUCT, found '%s'", c3tagstr(ctx.tokens.tags[ctx.i]));
		return -1;
	}
	parser_advance();
	last = 0;
	while(!parser_is(0, C3_RBRACE)){
		init_token = ctx.i;
		if(last == init_token){
			ERROR("ICE: infinite loop detected", 0);
			return -1;
		}
		last = init_token;
		kid = p_tl();
		if(kid == -1){
			ERROR("failed to parse %s", c3tagstr(ctx.tokens.tags[init_token]));
			return -1;
		}
		if(kid != 0 && !stb_sb_push_u32(&kids, kid))
			OOM();
		parser_advance();
	}
	root = c3appendarr(&ctx.tree, tag, stb_sb_count(kids), kids);
	stb_sb_free(kids);
	return root;
}

static uint32_t
p_enum(void)
{
	return p_container(C3_ENUM);
}

static uint32_t
p_struct(void)
{
	return p_container(C3_STRUCT);
}

static uint32_t
p_enum_literal(void)
{
	parser_advance();
	return c3append(&ctx.tree, C3_ENUM_LITERAL, 1, ctx.tokens.strings[ctx.i]);
}

static uint32_t
p_ident(void)
{
	return parser_dedup_node(&ctx.accel.idents, C3_IDENT, ctx.tokens.strings[ctx.i]);
}

static uint32_t
p_str(void)
{
	return parser_dedup_node(&ctx.accel.strs, C3_LITERAL_STRING, ctx.tokens.strings[ctx.i]);
}

static uint32_t
p_fncall_args(void)
{
	uint32_t *exprs;
	uint32_t args;
	int more;
	uint32_t arg;
	exprs = NULL;
	EXPECT(C3_LPAREN, "Expected ( to begin FnCallArguments '%s'!", c3tagstr(ctx.tokens.tags[ctx.i]));
	parser_advance();
	more = 0;
	while(!parser_is(0, C3_RPAREN)){
		arg = p_expr();
		parser_advance();
		if(parser_is(0, C3_COMMA)){
			more = 1;
			parser_advance();
		} else{
			more = 0;
		}
		if(arg == -1){
			ERROR("failed to parse function call argument", 0);
			return -1;
		}
		if(!stb_sb_push_u32(&exprs, arg)){
			ERROR("ICE: OOM", 0);
			return -1;
		}
	}
	if(more){
		ERROR("Expected fnarg after ','", 0);
		return -1;
	}
	args = c3appendarr(&ctx.tree, C3_EXPR_LIST, stb_sb_count(exprs), exprs);
	stb_sb_free(exprs);
	if(args == -1)
		OOM();
	return args;
}

static uint32_t
p_primary_type_expr(void)
{
	if(parser_is(0, C3_IDENT))
		return p_ident();
	if(parser_is(0, C3_TRUE))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_TRUE);
	if(parser_is(0, C3_FALSE))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_FALSE);
	if(parser_is(0, C3_KEYWORD_NULL))
		return parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_NULL);
	if(parser_is(0, C3_NORETURN))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_NORETURN);
	if(parser_is(0, C3_VOID))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_VOID);
	if(parser_is(0, C3_TYPE_BOOL))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_BOOL);
	if(parser_is(0, C3_TYPE_U8))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_U8);
	if(parser_is(0, C3_TYPE_U32))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_U32);
	if(parser_is(0, C3_DISCARD))
		return parser_dedup_node_tag(&ctx.accel.misc, C3_DISCARD);
	if(parser_is(0, C3_IMPORT))
		return p_import();
	if(parser_is(0, C3_BUILTIN_PANIC)){
		parser_advance();
		return c3append(&ctx.tree, C3_BUILTIN_PANIC, 1, p_fncall_args());
	}
	if(parser_is(0, C3_BUILTIN_THIS))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_BUILTIN_THIS);
	if(parser_is(0, C3_STRUCT))
		return p_struct();
	if(parser_is(0, C3_TYPE))
		return parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE);
	if(parser_is(0, C3_ENUM))
		return p_enum();
	if(parser_is(0, C3_LINT))
		return parser_dedup_node(&ctx.accel.ints, C3_LINT, ctx.tokens.strings[ctx.i]);
	if(parser_is(0, C3_LITERAL_STRING))
		return p_str();
	if(parser_is(0, C3_DOT) && parser_is(1, C3_IDENT))
		return p_enum_literal();
	ERROR("TODO: non-ident primary type expr: %s", c3tagstr(ctx.tokens.tags[ctx.i]));
	return -1;
}

static uint32_t
p_suffix_expr(void)
{
	uint32_t primary;
	uint32_t suffix;
	if(parser_is(0, C3_ASYNC))
		FATAL("TODO: async",0);
	primary = p_primary_type_expr();
	if(primary == -1){
		ERROR("Failed to read primary type expr", 0);
		return -1;
	}
	while(1){
		if(parser_is(1, C3_LPAREN)){
			parser_advance();
			suffix = p_fncall_args();
			if(suffix == -1)
				FATAL("Failed to read function call arguments", 0);
			primary = c3append(&ctx.tree, C3_EXPR_CALL, 2, primary, suffix);
		} else if(parser_is(1, C3_DOT) && parser_is(2, C3_IDENT)){
			parser_advance();
			parser_advance();
			suffix = p_ident();
			if(suffix == -1)
				OOM();
			primary = c3append(&ctx.tree, C3_EXPR_MEMBER, 2, primary, suffix);
		} else
			break;
	}
	if(primary == -1)
		OOM();
	return primary;
}

static uint32_t
p_error_union_expr(void)
{
	uint32_t suffix;
	suffix = p_suffix_expr();
	if(suffix == -1)
		ERROR("unable to read SuffixExpr", 0);
	if(parser_is(1, C3_EXCLAIM)){
		FATAL("TODO: error union", 0);
	}
	return suffix;
}

// This frees ops!
static uint32_t
parser_apply_prefix(uint32_t node, struct prefix *ops)
{
	uint32_t i;
	uint16_t tag;
	i = stb_sb_count(ops);
	while(i > 0 && node != -1){
		i -= 1;
		switch(ops[i].tag){
		case prefix_slice:
			tag = C3_TYPE_SLICE;
			break;
		case prefix_const:
			tag = C3_CONSTANT;
			break;
		default:
			stb_sb_free(ops);
			FATAL("TODO: prefix types", 0);
			break;
		}
		node = c3append(&ctx.tree, tag, 1, node);
		if(node == -1)
			ERROR("ICE: OOM", 0);
	}
	stb_sb_free(ops);
	return node;
}

static uint32_t
p_type_expr(void)
{
	struct prefix *ops;
	uint32_t expr;
	ops = p_prefixtypeops();
	if(ops == &failed){
		ERROR("Parsing prefix types failed", 0);
		return -1;
	}
	expr = p_error_union_expr();
	if(expr == -1){
		ERROR("failed to read error_union_expr", 0);
		return -1;
	}
	return ops == NULL ? expr : parser_apply_prefix(expr, ops);
}

static uint32_t
p_param_decl(void)
{
	uint32_t ident;
	uint32_t index;
	uint32_t type;
	int comptime;
	comptime = 0;
	if(parser_is(0, C3_NOALIAS)){
		ERROR("TODO: noalias parameters", 0);
		return -1;
	}
	if(parser_is(0, C3_COMPTIME)){
		comptime = 1;
		parser_advance();
	}
	ident = 0;
	if(parser_is(0, C3_IDENT) && parser_is(1, C3_COLON)){
		ident = p_ident();
		parser_advance();
		parser_advance();
	}
	if(ident == 0)
		ERROR("no ident, tag %s",c3tagstr(ctx.tokens.tags[ctx.i]));
	if(parser_is(0, C3_ANYTYPE))
		type = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_ANYTYPE);
	else if(parser_is(0, C3_DOT)){
		ERROR("TODO: extern varargs", 0);
		return -1;
	} else
		type = p_type_expr();
	if(type == -1){
		ERROR("unable to read function parameter type", 0);
		return -1;
	}
	index = c3append(&ctx.tree, C3_FN_PARAM, 2, ident, type);
	if(index != -1 && comptime)
		index = c3append(&ctx.tree, C3_COMPTIME, 1, index);
	if(index == -1)
		OOM();
	return index;
}

static uint32_t
p_call_conv(void)
{
	char *buf;
	uint32_t ret;
	parser_advance();
	ret = -1;
	buf = h_c3str(ctx.tree, ctx.tokens.strings[ctx.i]);
	if(strcmp(buf, "Inline") == 0)
		ret = parser_dedup_node_tag(&ctx.accel.misc, C3_CALL_CONV_INLINE);
	else
		ERROR("Unrecognized calling convention", 0);
	free(buf);
	return ret;
}

static uint32_t
p_fnproto(int legacy)
{
	int more;
	int infer;
	uint32_t callconv;
	uint32_t child;
	uint32_t *params;
	uint32_t ret_type;
	callconv = parser_dedup_node_tag(&ctx.accel.misc, C3_CALL_CONV_DEFAULT);
	params = NULL;
	if(!legacy && !parser_is_consume(C3_FN))
		FATAL("Expected fn token for fnproto",0);
	EXPECT(C3_LPAREN, "Expected lparen after fn",0);
	parser_advance();
	more = 0;
	while(!parser_is(0, C3_RPAREN)){
		child = p_param_decl();
		if(child == -1){
			ERROR("failed to read fn paramdecl",0);
			return -1;
		}
		if(!stb_sb_push_u32(&params, child))
			OOM();
		parser_advance();
		if(parser_is(0, C3_COMMA)){
			more = 1;
			parser_advance();
		} else
			more = 0;
	}
	if(more){
		ERROR("Expected paramdecl after ','", 0);
		return -1;
	}
	parser_advance();
	if(parser_is(0, C3_ALIGN))
		FATAL("TODO: byte align", 0);
	if(parser_is(0, C3_LINK))
		FATAL("TODO: link section", 0);
	if(parser_is(0, C3_CALL_CONV)){
		parser_advance();
		if(!parser_is(0, C3_LPAREN) || !parser_is(1, C3_DOT) || !parser_is(2, C3_IDENT) || !parser_is(3, C3_RPAREN))
			FATAL("Expected (.Convention) after callconv",0);
		parser_advance();
		callconv = p_call_conv();
		if(callconv == C3_BAD)
			FATAL("Failed to read calling convention", 0);
		parser_advance();
		parser_advance();
	}
	infer = parser_is_consume(C3_EXCLAIM);
	if(parser_is(0, C3_ANYTYPE))
		ret_type = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_UNRES);
	else
		ret_type = p_type_expr();
	if(ret_type == -1){
		ERROR("failed to read fnproto return type", 0);
		return -1;
	}
	if(infer == 1)
		FATAL("TODO: inferred error unions", 0);
	child = c3appendarr(&ctx.tree, C3_FN_PARAM_LIST, stb_sb_count(params), params);
	if(child == -1)
		OOM();
	return c3append(&ctx.tree, C3_TYPE_FN, 3, ret_type, child, callconv);
}

static uint32_t
p_fnexpr(int legacy, int external)
{
	uint32_t type, block, ident, fn;
	block = -1;
	ident = -1;
	if(legacy){
		ident = p_ident();
		if(ident == -1)
			return -1;
		parser_advance();
	}
	type = p_fnproto(legacy);
	if(type == -1){
		ERROR("Failed to read fnproto", 0);
		return -1;
	}
	if(!external){
		parser_advance();
		block = p_block();
		if(block == -1){
			ERROR("Failed to read block", 0);
			return -1;
		}
	}
	fn = c3append(&ctx.tree, C3_VALUE_FN, 2, type, block);
	stbds_hmput(ctx.analysis.names, fn, ident);
	return fn;
}

static uint32_t
p_fn_legacy(int external)
{
	uint32_t value = p_fnexpr(1, external);
	if(value == -1)
		return -1;
	if(external){
		parser_advance();
		EXPECT(C3_SCOLON, "Expected semicolon to end vardecl, found '%s'",c3tagstr(ctx.tokens.tags[ctx.i]));
	}
	return c3append(&ctx.tree, C3_CONST, 3, c3nodechild(ctx.tree, value, 2), c3nodechild(ctx.tree, value, 0), value);
}

static uint32_t
p_expr(void)
{
	return p_boolor_expr();
}

static uint32_t
p_assign_expr(void)
{
	uint32_t expr;
	uint32_t rhs;
	uint16_t tag;
	expr = p_expr();
	if(expr == -1)
		FATAL("Failed to read assignexpr",0);
	if(parser_is(1, C3_EQ) || parser_is(1, C3_PLUS_EQUAL) || parser_is(1, C3_MINUS_EQUAL)){
		tag = ctx.tokens.tags[ctx.i + 1];
		parser_advance();
		parser_advance();
		rhs = p_expr();
		if(rhs == -1)
			FATAL("Failed to read rhs of assignment",0);
		switch (tag){
			case C3_EQ:
				expr = c3append(&ctx.tree, C3_EXPR_ASSIGN, 2, expr, rhs);
				break;
			case C3_PLUS_EQUAL:
			case C3_MINUS_EQUAL:
				if(tag == C3_PLUS_EQUAL)
					tag = C3_EXPR_ASSIGN_PLUS;
				else
					tag = C3_EXPR_ASSIGN_MINUS;
				expr = c3append(&ctx.tree, tag, 2, expr, rhs);
				break;
		}
		if(expr == -1)
			OOM();
	}
	return expr;
}

static uint32_t
p_var_decl(void)
{
	int is_const;
	uint32_t type;
	uint32_t value;
	uint32_t ident;
	if(!(parser_is(0, C3_CONST)) && !(parser_is(0, C3_VAR)))
		FATAL("Expected var or const at vardecl: %s!", ctx.path);
	is_const = parser_is(0, C3_CONST);
	parser_advance();
	EXPECT(C3_IDENT, "Expected identifier in vardecl, found '%s'!", c3tagstr(ctx.tokens.tags[ctx.i]));
	ident = p_ident();
	parser_advance();
	type = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_UNRES);
	value = parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_UNDEF);
	if(parser_is(0, C3_COLON)){
		parser_advance();
		type = p_type_expr();
		if(type == -1)
			FATAL("Failed to read decl type", 0);
		parser_advance();
	}
	if(parser_is(0, C3_ALIGN))
		FATAL("TODO: byte align", 0);
	if(parser_is(0, C3_LINK))
		FATAL("TODO: link section", 0);
	if(parser_is(0, C3_EQ)){
		parser_advance();
		value = p_expr();
		if(value == -1){
			ERROR("failed to read expr", 0);
			return -1;
		}
		parser_advance();
	}
	EXPECT(C3_SCOLON, "Expected semicolon to end vardecl, found '%s'",c3tagstr(ctx.tokens.tags[ctx.i]));
	return c3append(&ctx.tree, is_const ? C3_CONST : C3_VAR, 3, ident, type, value);
}

static uint32_t
p_tl_decl(void)
{
	int pub;
	int export;
	int ext;
	uint32_t node;
	pub = parser_is_consume(C3_PUB);
	export = parser_is_consume(C3_EXPORT);
	ext = parser_is_consume(C3_EXTERN);
	if(export && ext)
		FATAL("Export and extern are mutually exclusive!", 0);
	if(parser_is(0, C3_THREADLOCAL)){
		FATAL("TODO: thread locals", 0);
	}
	if(parser_is_consume(C3_FN))
		node = p_fn_legacy(ext);
	else if(parser_is(0, C3_USINGNAMESPACE)){
		FATAL("TODO: usingnamespace",0);
	}
	else
		node = p_var_decl();
	if(node == -1)
		return -1;
	if(pub)
		node = c3append(&ctx.tree, C3_PUB, 1, node);
	if(node == -1){
		ERROR("OOM appending pub node", 0);
		return -1;
	}
	if(export)
		node = c3append(&ctx.tree, C3_EXPORT, 1, node);
	if(ext)
		node = c3append(&ctx.tree, C3_EXTERN, 1, node);
	if(node == -1)
		OOM();
	return node;
}

static uint32_t
p_container_field(void)
{
	uint32_t ident;
	uint32_t type;
	uint32_t value;
	uint32_t ret;
	if(parser_is(0, C3_COMPTIME))
		FATAL("TODO: comptime container field", 0);
	if(!parser_is(0, C3_IDENT))
		FATAL("Expected ident for container field", 0);
	ident = p_ident();
	type = parser_dedup_node_tag(&ctx.tree.dedup.types, C3_TYPE_UNRES);
	value = parser_dedup_node_tag(&ctx.accel.values, C3_VALUE_UNDEF);
	if(parser_is(1, C3_COLON)){
		parser_advance();
		parser_advance();
		type = p_type_expr();
		if(type == -1)
			FATAL("Failed to read containerfield typeexpr", 0);
	}
	else if(parser_is(1, C3_EQ)){
		FATAL("TODO: container field default",0);
	}
	else
		FATAL("container field requires type or initializer", 0);
	ret = c3append(&ctx.tree, C3_FIELD, 3, ident, type, value);
	if(ret == -1)
		OOM();
	if(parser_is(1, C3_COMMA))
		parser_advance();
	return ret;
	// if (NOT last token AND next token NOT comma) INVALID
}

static uint32_t
p_tl(void)
{
	switch(ctx.tokens.tags[ctx.i]){
	case C3_SPACE:
	case C3_COMMENT:
		while(parser_is(0, C3_SPACE) || parser_is(0, C3_COMMENT))
			ctx.i += 1;
		ctx.i -= 1;
		return 0;
	case C3_TEST:
		return p_test_decl();
	case C3_COMPTIME:
		return p_tl_comptime();
	// TopLevelDecl <- KEYWORD_pub? ((KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl) / (KEYWORD_usingnamespace Expr SEMICOLON)
	case C3_PUB:
	case C3_EXPORT:
	case C3_EXTERN:
	case C3_THREADLOCAL:
	case C3_FN:
	case C3_CONST:
	case C3_VAR:
	case C3_USINGNAMESPACE:
		return p_tl_decl();
	case C3_IDENT:
		return p_container_field();
	default:
	ERROR("TODO: parse '%s'",c3tagstr(ctx.tokens.tags[ctx.i]));
		return -1;
	}
}

static int
parser_main(void)
{
	uint32_t index;
	uint32_t root;
	uint32_t *kids;
	uint32_t init_token;
	kids = NULL;
	for(ctx.i = 0; ctx.i < ctx.accel.token_count; ctx.i += 1){
		init_token = ctx.i;
		index = p_tl();
		if(index == -1){
			ERROR("failed to parse %s", c3tagstr(ctx.tokens.tags[init_token]));
			return 0;
		}
		if(index != 0 && !stb_sb_push_u32(&kids, index))
				OOM();
	}
	root = c3appendarr(&ctx.tree, C3_STRUCT, stb_sb_count(kids), kids);
	if(root == -1)
		OOM();
	stb_sb_free(kids);
	ctx.tree.ast.nodes[1].node_index = root;
	return root != -1;
}

int
parse(char *path, c3ctx *tree)
{
	int ret;
	parser_init(path);
	ret = parser_main();
	if(ret)
		*tree = ctx.tree;
	parser_cleanup();
	return ret;
}