summaryrefslogtreecommitdiff
path: root/tnslc/parse/ast.tnsl
blob: c3ffe23ca7dccd26f7837141423c362307577d12 (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

uint16 NTYPE_MODULE = 0
uint16 NTYPE_EXPORT = 1
uint16 NTYPE_STRUCT = 2
uint16 NTYPE_TYPE = 3
uint16 NTYPE_ID = 4
uint16 NTYPE_BIN_OP = 5
uint16 NTYPE_PRE_OP = 6
uint16 NTYPE_POST_OP = 7
uint16 NTYPE_FUNCTION = 8
uint16 NTYPE_METHOD = 9
uint16 NTYPE_TLIST = 10
uint16 NTYPE_DLIST = 11
uint16 NTYPE_ELIST = 12
uint16 NTYPE_LITERAL = 13
uint16 NTYPE_KEY_TYPE = 14
uint16 NTYPE_ENUM = 15
uint16 NTYPE_DECL = 16
uint16 NTYPE_VLIST = 17
uint16 NTYPE_VALUE = 18
uint16 NTYPE_ASM = 998

struct Node {
	uint16 _type,
	~uint8 data,
	utils.Vector sub
}

/; method Node
	/; init (uint16 _type, ~uint8 data)
		self.data = data
		self._type = _type

		Node n
		self.sub.init(len n)
	;/

	/; end
		_delete(self.data)
		
		/; loop (uint i = 0; i < self.sub.count) [i++]
			~Node cur = self.sub.get(i)
			cur`.end()
		;/
		self.sub.end()
	;/
;/

/; _get_closing_delim(uint8 ch) [uint8]
	/; if (ch == '/' || ch == ';')
		return ';'
	;; else if (ch == '(')
		return ')'
	;; else if (ch == '[')
		return ']'
	;; else if (ch == '{')
		return '}'
	;/
	return 0
;/

/; _is_closing(~Token tok) [bool]
	/; if (tok`._type !== TTYPE_DELIM)
		return false
	;/
	
	uint8 ch = tok`.data`
	/; if (ch == ';' || ch == '}' || ch == ']' || ch == ')')
		return true
	;/
	return false
;/

/; _advance_check(~utils.File fin, ~Token tok, ~uint8 eq) [bool]
	/; if (tok`._type !== TTYPE_ERR && tok`.eq(eq) == true)
		Token tmp = produce_next_token(fin, tok`)
		tok`.end()
		tok` = tmp
		return true
	;/
	return false
;/

/; _ast_print_err (~Token tok, ~uint8 msg)
	_printf(msg)
	_printf(":\n\0")
	_printf("  \0")
	print_token(tok`)
	_printf("\n\0")
;/



# AST values

/; _op_prefix(~Token op) [bool]
	int l = utils.strlen(op`.data)
	/; if (op`._type !== TTYPE_AUG)
		return false
	;/
	
	/; if (op`.eq("len\0") || op`.eq("--\0") || op`.eq("++\0"))
		return true
	;/

	/; if (l !== 1)
		return false
	;/

	/; if (op`.data` == '-' || op`.data` == '!' || op`.data` == '~')
		return true
	;/

	return false
;/

/; _op_postfix(~Token op) [bool]
	/; if (op`.eq("`\0") || op`.eq("++\0") || op`.eq("--\0"))
		return true
	;/
	return false
;/


/; _op_order(~Token op) [int]
	/; if (op`._type !== TTYPE_AUG)
		_ast_print_err(op, "[FATAL] [CMPERR] _op_order was called on a non-aug token\0")
		return 999
	;/

	int l = utils.strlen(op`.data)

	/; if (l == 1)
		uint8 ch = op`.data`
		/; if (ch == '`')
			return 0
		;; else if (ch == '.')
			return 1
		;; else if (ch == '~')
			return 2
		;; else if (ch == '*' || ch == '/' || ch == '%')
			return 5
		;; else if (ch == '+' || ch == '-')
			return 6
		;; else if (ch == '!' || ch == '&' || ch == '|' || ch == '^')
			return 7
		;; else if (ch == '<' || ch == '>')
			return 8
		;; else if (ch == '=')
			return 10
		;/
	;; else if (l == 2)
		/; if (op`.data{0} == op`.data{1})
			uint8 ch = op`.data`
			/; if (ch == '+' || ch == '-')
				return 3
			;; else if (ch == '<' || ch == '>')
				return 7
			;; else if (ch == '=')
				return 8
			;/
			return 9
		;/

		/; if (op`.data{1} == '<' || op`.data{1} == '>')
			return 8
		;; else if (op`.data{1} == '=')
			return 10
		;; else if (op`.data{0} == '!')
			return 7
		;/
	;; else if (l == 3)
		/; if (op`.eq("len\0"))
			return 4
		;; else if (op`.data{1} == '=')
			return 8
		;/
		return 9
	;/

	_ast_print_err(op, "[FATAL] [CMPERR] _op_order: Augment not implemented in ordering\0")
	return 999
;/

/; _ast_post_id (~utils.File fin, ~Node mod, ~Token first)
;/

/; _ast_value_bare(~utils.File fin, ~Node mod, ~Token first) [bool]

	~Node cur = mod
	/; if (first`.eq("{\0") == true)
		_ast_list_value(fin, mod, first)
		/; if (_advance_check(fin, first, "}\0") == false)
			_ast_print_err(first, "Expected closing '}' for singlet\0")
			return false
		;/
	;; else if (_advance_check(fin, first, "(\0") == true)
		_ast_value(fin, mod, first)
		/; if (_advance_check(fin, first, ")\0") == false)
			_ast_print_err(first, "Expected closing ')' for singlet\0")
			return false
		;/
	;; else
		/; loop (first`._type == TTYPE_AUG)
			/; if (_op_prefix(first) == false)
				_ast_print_err(first, "Only certain augments (-, !, ~, --, ++, len) can be used as prefixes for bare values\0")
				return false
			;/
			Node pre
			pre.init(NTYPE_PRE_OP, first`.data)

			cur`.sub.push(~pre)
			cur = cur`.sub.get(cur`.sub.count - 1)
			first` = produce_next_token(fin, first`)
		;/
		
		Node val
		
		/; if (first`._type == TTYPE_USRWD)
			val.init(NTYPE_ID, first`.data)
			first` = produce_next_token(fin, first`)
			_ast_post_id(fin, ~val, first)
		;; else if (first`._type == TTYPE_LITRL)
			val.init(NTYPE_LITERAL, first`.data)
			first` = produce_next_token(fin, first`)
		;; else
			_ast_print_err(first, "Expected identifier or literal in bare value\0")
			return false
		;/

		cur`.sub.push(~val)
	;/

	/; loop (_advance_check(fin, first, ".\0") == true)
		/; if (first`._type !== TTYPE_USRWD)
			_ast_print_err(first, "Expected method/member name after '.'\0")
			return false
		;/
		
		~Node repl = cur`.sub.get(cur`.sub.count - 1)
		Node dot
		dot.init(NTYPE_BIN_OP, utils.strcpy(".\0"))
		dot.sub.push(repl)

		Node val
		val.init(NTYPE_ID, first`.data)
		first` = produce_next_token(fin, first`)
		_ast_post_id(fin, ~val, first)
		dot.sub.push(~val)

		cur`.sub.pop()
		cur`.sub.push(~dot)
		cur = cur`.sub.get(cur`.sub.count - 1)
	;/

	return true
;/

/; _ast_value (~utils.File fin, ~Node mod, ~Token first)

	Node val
	val.init(NTYPE_VALUE, utils.strcpy("\0"))

	~Node cur = ~val

	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		_ast_value_bare(fin, ~cur, first)
	;/

	mod`.sub.push(~val)
;/

/; _ast_keyword_expr (~utils.File fin, ~Node mod, ~Token first)
	Token tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp
;/



# AST types

/; _type_helper_pre (~utils.File fin, ~Node mod, ~Token first)

	/; loop (bool run = true; first`._type !== TTYPE_ERR && run == true)
		/; if (first`._type == TTYPE_AUG && first`.eq("~\0") == true)
			# Pointer
			Node ptr
			ptr.init(NTYPE_PRE_OP, first`.data)
			mod`.sub.push(~ptr)
			first` = produce_next_token(fin, first`)
		;; else if (first`._type == TTYPE_DELIM && first`.eq("{\0") == true)
			# Array
			Node arr
			arr.init(NTYPE_PRE_OP, first`.data)
			first` = produce_next_token(fin, first`)
			
			/; if (first`._type == TTYPE_LITRL && is_numeric(first`.data`) == true)
				Node num
				num.init(NTYPE_LITERAL, first`.data)
				arr.sub.push(~num)

				first` = produce_next_token(fin, first`)

				/; if (first`.eq("}\0") == true)
					Token tmp = produce_next_token(fin, first`)
					first`.end()
					first` = tmp
				;; else
					_ast_print_err(first, "Expected closing '}' after integer in type declaration\0")
					run = false
				;/
			;; else if (first`.eq("}\0") == true)
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
			;; else
				_ast_print_err(first, "Expected integer or '}' to define either a fixed sized array or pointer to unknown sized array\0")
				run = false
			;/

			mod`.sub.push(~arr)

		;; else
			run = false
		;/
	;/

;/

/; _type_helper_func (~utils.File fin, ~Node mod, ~Token first)
	/; if (first`.eq("(\0") == true)
		_ast_list_type(fin, mod, first)
	;/

	/; if (first`.eq("[\0") == true)
		_ast_list_type(fin, mod, first)
	;/
;/

/; _type_helper_usertype (~utils.File fin, ~Node mod, ~Token first)
	
	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (first`._type == TTYPE_USRWD)
			Node utp
			utp.init(NTYPE_ID, first`.data)
			first` = produce_next_token(fin, first`)
			mod`.sub.push(~utp)

			/; if (first`._type !== TTYPE_AUG || first`.eq(".\0") == false)
				run = false
			;; else
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
			;/
		;; else
			_ast_print_err(first, "Expected identifier in fully qualified type chain\0")
			run = false
		;/
	;/

	/; if (first`._type == TTYPE_DELIM && first`.eq("(\0") == true)
		_ast_list_type(fin, mod, first)
	;/
;/

/; _ast_type (~utils.File fin, ~Node mod, ~Token first)

	Node typ
	typ.init(NTYPE_TYPE, utils.strcpy("\0"))

	# Prefix values (~, {}, {#})
	_type_helper_pre(fin, ~typ, first)

	# Check to see if this is a function pointer type
	/; if (first`.eq("void\0") == true)
		Node ktp
		ktp.init(NTYPE_ID, first`.data)
		typ.sub.push(~ktp)
		first` = produce_next_token(fin, first`)
		
		_type_helper_func(fin, ~typ, first)
		mod`.sub.push(~typ)
		return
	;/

	# user type or keytype
	/; if (first`._type == TTYPE_USRWD)
		_type_helper_usertype(fin, ~typ, first)
	;; else if (first`._type == TTYPE_KEYTP)
		Node ktp
		ktp.init(NTYPE_ID, first`.data)
		typ.sub.push(~ktp)
		first` = produce_next_token(fin, first`)
	;; else
		_ast_print_err(first, "Expected keytype or usertype when parsing type\0")
		mod`.sub.push(~typ)
		return
	;/

	# The ol' reference
	/; if (first`.eq("`\0"))
		Node post
		post.init(NTYPE_POST_OP, first`.data)
		typ.sub.push(~post)
		first` = produce_next_token(fin, first`)
	;/

	mod`.sub.push(~typ)
;/



# AST lists

/; _ast_list_value (~utils.File fin, ~Node mod, ~Token first)
	Node list
	list.init(NTYPE_VLIST, first`.data)

	uint8 end = _get_closing_delim(first`.data`)

	first` = produce_next_token(fin, first`)

	/; loop (first`._type !== TTYPE_ERR && first`.data` !== end)
		_ast_value(fin, ~list, first)

		/; if (_advance_check(fin, first, ",\0") == false && (first`._type !== TTYPE_DELIM || first`.data` !== end))
			_ast_print_err(first, "Expected ',' to continue the value list or a closing delimiter\0")
			mod`.sub.push(~list)
			return
		;/
	;/

	mod`.sub.push(~list)

	Token next = produce_next_token(fin, first`)
	first`.end()
	first` = next
;/

/; _maybe_helper_decl (~utils.File fin, ~Node mod, ~Token first)
	# either a continuation of prev type or a new type then an id
	Token next = produce_next_token(fin, first`)
	/; if (next._type !== TTYPE_ERR && next.eq(",\0") || _is_closing(~next) == true)
		# Another id
		Node id
		id.init(NTYPE_ID, first`.data)
		mod`.sub.push(~id)

		first` = next
		return
	;/

	# have to parse as a type
	Node tp
	tp.init(NTYPE_TYPE, utils.strcpy("\0"))

	Node utmp
	utmp.init(NTYPE_ID, first`.data)

	tp.sub.push(~utmp)
	first` = next

	bool run = true
	/; if (first`._type !== TTYPE_AUG || first`.eq(".\0") == false)
		run = false
	;; else
		next = produce_next_token(fin, first`)
		first`.end()
		first` = next
	;/

	# Adapted from _type_helper_usertype
	/; loop (run == true && first`._type !== TTYPE_ERR)
		/; if (first`._type == TTYPE_USRWD)
			Node utp
			utp.init(NTYPE_ID, first`.data)
			first` = produce_next_token(fin, first`)
			tp.sub.push(~utp)

			/; if (first`._type !== TTYPE_AUG || first`.eq(".\0") == false)
				run = false
			;; else
				next = produce_next_token(fin, first`)
				first`.end()
				first` = next
			;/
		;; else
			_ast_print_err(first, "Expected identifier in fully qualified type chain\0")
			run = false
		;/
	;/

	/; if (first`._type == TTYPE_DELIM && first`.eq("(\0") == true)
		_ast_list_type(fin, ~tp, first)
	;/

	mod`.sub.push(~tp)

	/; if (first`._type !== TTYPE_USRWD)
		_ast_print_err(first, "Expected identifier after user type in type list\0")
		return
	;/

	Node id
	id.init(NTYPE_ID, first`.data)
	mod`.sub.push(~id)

	first` = produce_next_token(fin, first`)
;/

/; _ast_list_decl (~utils.File fin, ~Node mod, ~Token first)
	Node list
	list.init(NTYPE_DLIST, first`.data)

	uint8 end = _get_closing_delim(first`.data`)

	first` = produce_next_token(fin, first`)
	bool seen = false

	/; loop (first`._type !== TTYPE_ERR && first`.data` !== end)
		/; if (seen == false || first`._type == TTYPE_KEYTP || first`.eq("~\0") == true || first`.eq("{\0") == true)
			_ast_type(fin, ~list, first)

			/; if (first`._type !== TTYPE_USRWD)
				/; if (seen == false)
					_ast_print_err(first, "Expected type then identifier as first items in declaration list\0")
				;; else
					_ast_print_err(first, "Expected identifier after type in params/struct def\0")
				;/
				mod`.sub.push(~list)
				return
			;/

			seen = true

			Node id
			id.init(NTYPE_ID, first`.data)

			list.sub.push(~id)
			first` = produce_next_token(fin, first`)

		;; else if (first`._type == TTYPE_USRWD)
			_maybe_helper_decl(fin, ~list, first)
		;; else
			_ast_print_err(first, "Expected type or parameter name in declaration list\0")
			mod`.sub.push(~list)
			return
		;/

		/; if (_advance_check(fin, first, ",\0") == false && _is_closing(first) == false)
			_ast_print_err(first, "Expected ',' to continue the declaration list or a closing delimiter\0")
			mod`.sub.push(~list)
			return
		;/
	;/

	mod`.sub.push(~list)
	
	Token next = produce_next_token(fin, first`)
	first`.end()
	first` = next
;/

/; _ast_list_enum (~utils.File fin, ~Node mod, ~Token first)
	Node list
	list.init(NTYPE_ELIST, first`.data)

	uint8 end = _get_closing_delim(first`.data`)

	first` = produce_next_token(fin, first`)

	/; loop (first`._type !== TTYPE_ERR && first`.data` !== end)
		/; if (first`._type == TTYPE_USRWD || first`._type == TTYPE_KEYTP || first`.eq("~\0") == true || first`.eq("{\0") == true)
			Node enum_id
			enum_id.init(NTYPE_ID, first`.data)
			
			first` = produce_next_token(fin, first`)

			/; if (_advance_check(fin, first, "=\0"))
				_ast_value(fin, ~enum_id, first)
			;/

			list.sub.push(~enum_id)
			
		;; else
			_ast_print_err(first, "Expected identifier in body of enum declaration\0")
			mod`.sub.push(~list)
			return
		;/

		/; if (_advance_check(fin, first, ",\0") == false && _is_closing(first) == false)
			_ast_print_err(first, "Expected ',' to continue the type list or a closing delimiter\0")
			mod`.sub.push(~list)
			return
		;/
	;/

	mod`.sub.push(~list)

	Token next = produce_next_token(fin, first`)
	first`.end()
	first` = next
;/

/; _ast_list_type (~utils.File fin, ~Node mod, ~Token first)
	Node list
	list.init(NTYPE_TLIST, first`.data)

	uint8 end = _get_closing_delim(first`.data`)

	first` = produce_next_token(fin, first`)

	/; loop (first`._type !== TTYPE_ERR && first`.data` !== end)

		/; if (first`._type == TTYPE_USRWD || first`._type == TTYPE_KEYTP || first`.eq("~\0") == true || first`.eq("{\0") == true)
			_ast_type(fin, ~list, first)
		;; else
			_ast_print_err(first, "Expected type in type list\0")
			mod`.sub.push(~list)
			return
		;/

		/; if (_advance_check(fin, first, ",\0") == false && _is_closing(first) == false)
			_ast_print_err(first, "Expected ',' to continue the type list or a closing delimiter\0")
			mod`.sub.push(~list)
			return
		;/
	;/

	mod`.sub.push(~list)

	Token next = produce_next_token(fin, first`)
	first`.end()
	first` = next
;/



# Method blocks

/; _ast_method (~utils.File fin, ~Node mod, ~Token first)

;/



# function blocks

/; _ast_if (~utils.File fin, ~Node mod, ~Token first)
;/

/; _ast_loop (~utils.File fin, ~Node mod, ~Token first)
;/

/; _ast_fun_block (~utils.File fin, ~Node mod, ~Token first)
	Token blf = first`
	first` = produce_next_token(fin, first`)

	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (_advance_check(fin, first, "if\0") == true)
			_ast_if(fin, mod, first)
		;; else if (_advance_check(fin, first, "loop\0") == true)
			_ast_loop(fin, mod, first)
		# ;; else if (first`._type == TTYPE_USRWD)
		#	_ast_function(fin, mod, first)
		;; else
			_ast_print_err(first, "Expected 'if' or 'loop' for function level block\0")
		;/

		run = _advance_check(fin, first, ";;\0")
		/; if (run == false && first`.eq(";/\0") == false)
			_ast_print_err(first, "Block being skipped. Parsing will pick up after the end\0")
		;/
	;/

	/; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR)
		/; if (first`.eq(";/\0"))
			deep = deep - 1
		;; else if (first`.eq("/;\0"))
			deep = deep + 1
		;/

		/; if (deep > 0)
			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp
		;/
	;/

	/; if (_advance_check(fin, first`, ";/\0") == false)
		_ast_print_err(~blf, "Could not find closing ;/ for top block\0")
	;/
	blf.end()
;/

/; _maybe_helper_fun (~utils.File fin, ~Node mod, ~Token first)
	Token next = produce_next_token(fin, first`)
	first`.end()
	first` = next
;/

/; _ast_function (~utils.File fin, ~Node mod, ~Token first)
	/; if (first`._type !== TTYPE_USRWD)
		_ast_print_err(first, "[FATAL] [CMPERR] Expected function name\0")
		return
	;/

	Node fn
	fn.init(NTYPE_FUNCTION, first`.data)
	first` = produce_next_token(fin, first`)

	/; if (first`.eq("(\0"))
		_ast_list_decl(fin, ~fn, first)
	;/

	/; if (first`.eq("[\0"))
		_ast_list_type(fin, ~fn, first)
	;/
	
	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (_advance_check(fin, first, "asm\0") == true)
			_ast_asm(fin, mod, first)
		;; else if (first`.eq("/;\0") == true)
			_ast_fun_block(fin, mod, first)
		;; else if (first`._type == TTYPE_KEYTP || first`.eq("{\0") == true)
			_ast_decl(fin, mod, first)
		;; else if (first`._type == TTYPE_USRWD || first`.eq("~\0") == true)
			_maybe_helper_fun(fin, mod, first)
		;; else if (first`._type == TTYPE_KEYWD)
			_ast_keyword_expr(fin, mod, first)
		;; else if (first`._type == TTYPE_DELIM && first`.data` == ';')
			run = false
		;; else
			_ast_print_err(first, "Expected definition, expression, or sub block in function\0")

			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp
		;/
	;/

	mod`.sub.push(~fn)
;/



# Top level directives

/; _ast_import (~utils.File fin, ~Node mod, ~Token first)

	/; if (first`._type !== TTYPE_LITRL || first`.data` !== '"')
		_ast_print_err(first, "Expected string or import literal after 'import'\0")

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/

	~uint8 rel = utils.unquote_str(first`.data)
	utils.File imp = fin`.relative(rel)
	_delete(rel)

	rel = imp.path.to_cstr('/')
	_printf("Importing \0")
	_printf(rel)
	_printf("\n\0")
	_delete(rel)

	_ast_file(~imp, mod)
	imp.end()

	Token tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp
;/

/; _ast_asm (~utils.File fin, ~Node mod, ~Token first)

	/; if (first`._type !== TTYPE_LITRL || first`.data` !== '"')
		_ast_print_err(first, "Expected string literal after 'ast'\0")

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/
	
	Node an
	an.init(NTYPE_ASM, utils.unquote_str(first`.data))
	mod`.sub.push(~an)

	Token tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp
;/

/; _ast_struct (~utils.File fin, ~Node mod, ~Token first)
	
	# Identifier check
	/; if (first`._type !== TTYPE_USRWD)
		_ast_print_err(first, "Expected new identifier for typedef after 'struct'\0")

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/

	# Root struct node
	Node sct
	sct.init(NTYPE_STRUCT, first`.data)
	first` = produce_next_token(fin, first`)

	# Check for def list
	/; if (first`._type !== TTYPE_DELIM || first`.data` !== '{')
		_ast_print_err(first, "Expected new identifier for typedef after 'struct'\0")
		mod`.sub.push(~sct)

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/

	# Parse deflist and push root node into module
	_ast_list_decl(fin, ~sct, first)
	mod`.sub.push(~sct)
;/

/; _ast_enum (~utils.File fin, ~Node mod, ~Token first)
	# Identifier check
	/; if (first`._type !== TTYPE_USRWD)
		_ast_print_err(first, "Expected new identifier for def after 'enum'\0")

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/

	# Root struct node
	Node sct
	sct.init(NTYPE_ENUM, first`.data)
	first` = produce_next_token(fin, first`)

	# Check for enum type
	/; if (first`.data` == '[')
		_ast_list_type(fin, ~sct, first)

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
	;/

	# Check for def list
	/; if (first`._type !== TTYPE_DELIM || first`.data` !== '{')
		_ast_print_err(first, "Expected new identifier for typedef after 'struct'\0")
		mod`.sub.push(~sct)

		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
		
		return
	;/

	# Parse deflist and push root node into module
	_ast_list_enum(fin, ~sct, first)
	mod`.sub.push(~sct)
	
	Token tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp
;/

/; _ast_decl (~utils.File fin, ~Node mod, ~Token first)
	Node decl
	decl.init(NTYPE_DECL, utils.strcpy("\0"))
	
	_ast_type(fin, ~decl, first)

	/; loop (bool run = true; run == true)
		/; if (first`._type == TTYPE_USRWD)
			Node var
			var.init(NTYPE_ID, first`.data)

			first` = produce_next_token(fin, first`)
			/; if (first`.eq("=\0"))
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
				_ast_value(fin, ~var, first)
			;/

			decl.sub.push(~var)

			/; if (first`.eq(",\0") == false)
				run = false
			;; else
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
			;/
		;; else
			_ast_print_err(first, "Expected variable name in declaration\0")
			run = false
		;/
	;/

	mod`.sub.push(~decl)
;/

/; _ast_top_block(~utils.File fin, ~Node mod, ~Token first)

	Token blf = first`
	first` = produce_next_token(fin, first`)

	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (first`.eq("module\0") == true || first`.eq("export\0") == true)
			_ast_module(fin, mod, first)
		;; else if (_advance_check(fin, first, "method\0") == true)
			_ast_method(fin, mod, first)
		;; else if (first`._type == TTYPE_USRWD)
			_ast_function(fin, mod, first)
		;; else
			_ast_print_err(first, "Expected module, method, or function for top level block\0")
		;/

		run = _advance_check(fin, first, ";;\0")
		/; if (run == false && first`.eq(";/\0") == false)
			_ast_print_err(first, "Block being skipped. Parsing will pick up after the end\0")
		;/
	;/

	/; loop (int deep = 1; deep > 0 && first`._type !== TTYPE_ERR)
		/; if (first`.eq(";/\0"))
			deep = deep - 1
		;; else if (first`.eq("/;\0"))
			deep = deep + 1
		;/

		/; if (deep > 0)
			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp
		;/
	;/

	/; if (_advance_check(fin, first`, ";/\0") == false)
		_ast_print_err(~blf, "Could not find closing ;/ for top block\0")
	;/
	blf.end()
;/

/; _ast_module(~utils.File fin, ~Node mod, ~Token first)
	uint16 nt = NTYPE_MODULE

	/; if (_advance_check(fin, first, "export\0") == true)
		/; if (_advance_check(fin, first, "module\0") == false)
			_ast_print_err(first, "Expected 'module' keyword after 'export'\0")
			return
		;/
		nt = NTYPE_EXPORT
	;; else if (_advance_check(fin, first, "module\0") == false)
		_printf("[FATAL] The following issue is with the compiler, not your program.\n\0")
		_printf("[FATAL] Please report the following issue to tnslc upstream.\n\0")
		_ast_print_err(first, "[FATAL] [CMPERR] Should only call _ast_module when 'module' or 'export' are seen\0")
	;/

	/; if (first`._type !== TTYPE_USRWD)
		_ast_print_err(first, "Expected module name (identifier) after 'module'\0")
	;/

	Node nmod
	nmod.init(nt, first`.data)
	first` = produce_next_token(fin, first`)

	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (_advance_check(fin, first, "import\0") == true)
			_ast_import(fin, ~nmod, first)
		;; else if (_advance_check(fin, first, "struct\0") == true)
			_ast_struct(fin, ~nmod, first)
		;; else if (_advance_check(fin, first, "enum\0") == true)
			_ast_enum(fin, ~nmod, first)
		;; else if (_advance_check(fin, first, "asm\0") == true)
			_ast_asm(fin, ~nmod, first)
		;; else if (first`.eq("/;\0") == true)
			_ast_top_block(fin, ~nmod, first)
		;; else if (first`._type == TTYPE_KEYTP || first`._type == TTYPE_USRWD || first`.eq("~\0") == true || first`.eq("{\0") == true)
			_ast_decl(fin, ~nmod, first)
		;; else if (first`._type == TTYPE_DELIM && first`.data` == ';')
			run = false
		;; else
			_ast_print_err(first, "Expected 'import', 'struct', 'asm', block, or declaration in top level\0")

			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp
		;/
	;/

	mod`.sub.push(~nmod)
;/

/; _ast_file (~utils.File fin, ~Node mod)
	fin`.open()
	
	Token first = produce_first_token(fin)
	/; loop (first._type !== TTYPE_ERR)
		/; if (_advance_check(fin, ~first, "import\0") == true)
			_ast_import(fin, mod, ~first)
		;; else if (_advance_check(fin, ~first, "struct\0") == true)
			_ast_struct(fin, mod, ~first)
		;; else if (_advance_check(fin, ~first, "enum\0") == true)
			_ast_enum(fin, mod, ~first)
		;; else if (_advance_check(fin, ~first, "asm\0") == true)
			_ast_asm(fin, mod, ~first)
		;; else if (first.eq("/;\0") == true)
			_ast_top_block(fin, mod, ~first)
		;; else if (first._type == TTYPE_KEYTP || first._type == TTYPE_USRWD || first.eq("~\0") == true || first.eq("{\0") == true)
			_ast_decl(fin, mod, ~first)
		;; else
			_ast_print_err(~first, "Expected 'import', 'struct', 'asm', block, or declaration in top level\0")

			Token tmp = produce_next_token(fin, first)
			first.end()
			first = tmp
		;/
	;/

	first.end()

	fin`.close()
;/

/; generate_ast (~utils.File fin) [Node]
	Node out

	utils.Vector v
	v.init(1)

	out.init(NTYPE_MODULE, v.as_cstr())

	_ast_file(fin, ~out)

	return out
;/

#
# Print out the AST from a specific node
#

/; print_node_type (~Node n)
	/; if (n`._type == NTYPE_MODULE)
		_printf("MODULE\0")
	;; else if (n`._type == NTYPE_EXPORT)
		_printf("EXPORT\0")
	;; else if (n`._type == NTYPE_STRUCT)
		_printf("STRUCT\0")
	;; else if (n`._type == NTYPE_TYPE)
		_printf("TYPE\0")
	;; else if (n`._type == NTYPE_ID)
		_printf("ID\0")
	;; else if (n`._type == NTYPE_BIN_OP)
		_printf("BIN_OP\0")
	;; else if (n`._type == NTYPE_PRE_OP)
		_printf("PRE_OP\0")
	;; else if (n`._type == NTYPE_POST_OP)
		_printf("POST_OP\0")
	;; else if (n`._type == NTYPE_FUNCTION)
		_printf("FUNCTION\0")
	;; else if (n`._type == NTYPE_METHOD)
		_printf("METHOD\0")
	;; else if (n`._type == NTYPE_TLIST)
		_printf("TLIST\0")
	;; else if (n`._type == NTYPE_DLIST)
		_printf("DLIST\0")
	;; else if (n`._type == NTYPE_ELIST)
		_printf("ELIST\0")
	;; else if (n`._type == NTYPE_LITERAL)
		_printf("LITERAL\0")
	;; else if (n`._type == NTYPE_KEY_TYPE)
		_printf("KEY_TYPE\0")
	;; else if (n`._type == NTYPE_ENUM)
		_printf("ENUM\0")
	;; else if (n`._type == NTYPE_DECL)
		_printf("DECL\0")
	;; else if (n`._type == NTYPE_VLIST)
		_printf("VLIST\0")
	;; else if (n`._type == NTYPE_VALUE)
		_printf("VALUE\0")
	;; else if (n`._type == NTYPE_ASM)
		_printf("ASM\0")
	;/
;/

/; print_node_head (~Node n)
	_printf("{ NODE_TYPE: \0")
	print_node_type(n)
	_printf(", DATA: \0")
	_printf(n`.data)
	_printf("\n\0")
;/

/; print_ast_rec(~Node n, uint depth)
	/; loop (int i = 0; i < depth) [i++]
		_printf("  \0")
	;/
	print_node_head(n)

	/; loop (uint i = 0; i < n`.sub.count) [i++]
		~Node s = n`.sub.get(i)
		print_ast_rec(s, depth + 1)
	;/
	
	/; loop (int i = 0; i < depth) [i++]
		_printf("  \0")
	;/
	_printf("}\n\0")
;/

/; print_ast (~Node n)
	print_ast_rec(n, 0)
;/