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

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_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()
	;/
;/

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

	~uint8 path = utils.unquote_str(tmp.data)
	utils.File _import = fin`.relative(path)
	_delete(path)
	path = _import.path.to_cstr('/')
	
	_printf("Importing ./\0")
	_printf(path)
	_printf("\n\0")
	_delete(path)

	_ast_file(~_import, mod)
	_import.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
;/

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

	# Handle pointer and array types
	/; loop (first`.eq("~\0") == true || first`.eq("{\0") == true)
		Node pre
		pre.init(NTYPE_PRE_OP, first`.data)

		/; if (first`.eq("{\0") == true)
			first` = produce_next_token(fin, first`)
			/; if (first`._type == TTYPE_LITRL && is_numeric(first`.data`))
				Node i
				i.init(NTYPE_LITERAL, first`.data)
				pre.sub.push(~i)
			;; else if (first`.eq("}\0") == false)
				_printf("Expected integer for array type or '}':")
				_printf("  \0")
				print_token(first`)
				_printf("\n\0")
			;; else
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
			;/
		;/

		out.sub.push(~pre)
		first` = produce_next_token(fin, first`)
	;/

	/; if (first`._type !== TTYPE_KEYTP && first`._type !== TTYPE_USRWD)
		_printf("Expected key type or user type:")
		_printf("  \0")
		print_token(first`)
		_printf("\n\0")
	;/
	
	# Are we a void type? (Matters for getting params or )
	bool vd = false
	/; if (first`.eq("void\0") == true)
		vd = true
	;/

	# Fully qualified type name
	/; if (first`._type == TTYPE_USRWD)
		/; loop (bool run = true; run == true)
			Node usr
			usr.init(NTYPE_ID, first`.data)
			first` = produce_next_token(fin, first`)
			out.sub.push(~usr)

			/; if (first`.eq(".\0") == true)
				Token tmp = produce_next_token(fin, first`)
				first`.end()
				first` = tmp
				/; if (first`._type !== TTYPE_USRWD)
					run = false
					_printf("Expected completion of user type:")
					_printf("  \0")
					print_token(tmp)
					_printf("\n\0")
				;/
			;; else
				run = false
			;/
		;/
	;; else
		Node tp
		tp.init(NTYPE_ID, first`.data)
		out.sub.push(~tp)

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

	# Generics or void call type
	/; if (first`._type == TTYPE_DELIM && first`.data` == '(')
		_ast_type_list(fin, ~out, first)
		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
	;/

	# Void return type
	/; if (vd && first`._type == TTYPE_DELIM && first`.data` == '[')
		_ast_type_list(fin, ~out, first)
		Token tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
	;/

	# Reference
	/; if (first`.eq("`\0") == true)
		Node post
		post.init(NTYPE_PRE_OP, first`.data)

		out.sub.push(~post)
		first` = produce_next_token(fin, first`)
	;/

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

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

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

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

;/

/; _ast_maybe_expr_decl (~utils.File fin, ~Node mod, ~Token first)
	# Handle the case where it's ambiguous whether we have a type definition or an expression

;/

/; _ast_expr_list(~utils.File fin, ~Node mod, ~Token first)
	uint8 closing = _get_closing_delim(first`.data`)

	Node expr_list
	expr_list.init(NTYPE_ELIST, first`.data)

	first` = produce_next_token(fin, first`)
	bool run = true

	/; if (first`._type == TTYPE_DELIM && first`.data` == closing)
		run = false
	;; else
		_ast_maybe_expr_decl(fin, ~expr_list, first)
	;/

	/; loop (run == true && first`._type !== TTYPE_ERR)
		/; if (first`._type == TTYPE_DELIM && first`.data` == closing)
			run = false
		;; else if (first`._type == TTYPE_SEP && first`.eq(";\0"))
			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp

			_ast_maybe_expr_decl(fin, ~expr_list, first)
		;; else
			_printf("Unexpected token when parsing a list of types:\n\0")
			_printf("  \0")
			print_token(first`)
			_printf("\n\0")

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

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

/; _ast_type_list (~utils.File fin, ~Node mod, ~Token first)
	uint8 closing = _get_closing_delim(first`.data`)

	Node type_list
	type_list.init(NTYPE_TLIST, first`.data)

	Token tmp = produce_next_token(fin, first`)
	first` = tmp
	bool run = true
	/; if (first`._type == TTYPE_DELIM && first`.data` == closing)
		run = false
	;; else if (first`._type == TTYPE_KEYTP || first`._type == TTYPE_USRWD || first`.eq("~\0") == true)
		_ast_type(fin, ~type_list, first)
	;; else
		_printf("Expected type or closing of type list:\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
	;/

	/; loop (bool run = true; run == true && first`._type !== TTYPE_ERR)
		/; if (first`._type == TTYPE_DELIM && first`.data` == closing)
			run = false
		;; else if (first`._type == TTYPE_SEP && first`.eq(",\0"))
			Token tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp

			/; if (first`._type == TTYPE_KEYTP || first`._type == TTYPE_USRWD || first`.eq("~\0") == true)
				_ast_type(fin, ~type_list, first)
			;/
		;; else
			_printf("Expected comma ',' and then type:\n\0")
			_printf("  \0")
			print_token(tmp)
			_printf("\n\0")

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

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

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

	/; if (tmp._type !== TTYPE_USRWD)
		_printf("Unexpected token after struct keyword:\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
		return
	;/

	Node out
	out.init(NTYPE_STRUCT, tmp.data)

	first` = produce_next_token(fin, tmp)
	_ast_decl_list(fin, out, first)

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

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

	/; if (tmp._type !== TTYPE_USRWD)
		_printf("Expected a user-defined type after 'method':\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
		return
	;/

	Node out
	out.init(NTYPE_METHOD, tmp.data)

	tmp = produce_next_token(fin, tmp)
	first` = tmp
	/; loop (first`._type !== TTYPE_ERR && first`.eq(";;\0") == false && first`.eq(";/\0") == false)
		/; if (first`.eq("/;\0") == true)
			tmp = produce_next_token(fin, tmp)
			first`.end()
			first` = tmp
			_ast_function(fin, ~out, first)

			/; loop (first`.eq(";;") == true)
				tmp = produce_next_token(fin, tmp)
				first`.end()
				first` = tmp
				_ast_function(fin, ~out, first)
			;/
		;; else
			_printf("Expected only function blocks in method block:\n\0")
			_printf("  \0")
			print_token(tmp)
			_printf("\n\0")

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

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

/; _ast_asm(~utils.File fin, ~Node mod, ~Token first)
	Token tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp
	
	/; if (tmp._type !== TTYPE_LITRL || tmp.data{0} !== '"')
		_printf("Expected string token after 'asm' keyword:\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
		return
	;/

	Node out
	out.init(NTYPE_ASM, utils.unquote_str(tmp.data))

	mod`.sub.push(~out)
	first` = produce_next_token(fin, tmp)
	tmp.end()
;/

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

	/; if (tmp._type !== TTYPE_USRWD)
		_printf("Expected function name (Compiler bug, not user program issue):\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
		return
	;/

	uint line = first`.line
	/; loop (line == first`.line)
	;/
;/

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

	/; if (tmp._type == TTYPE_USRWD)
		_ast_function(fin, mod, first)
	;; else if (tmp.eq("method\0") == true)
		_ast_method(fin, mod, first)
	;; else if (tmp.eq("module\0") == true || tmp.eq("export\0") == true)
		_ast_module(fin, mod, first)
	;; else
		_printf("Expected function definition, 'method', or 'module' after block opening (root of file or module):\n\0")
		_printf("  \0")
		print_token(tmp)
		_printf("\n\0")
	;/

	/; if (first`.eq(";;\0") == true)
		_ast_block(fin, mod, first)
	;; else
		tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
	;/
;/

/; _ast_module (~utils.File fin, ~Node mod, ~Token first)
	Token tmp
	uint16 _type = NTYPE_MODULE
	/; if (first`.eq("export\0"))
		_type = NTYPE_EXPORT
		tmp = produce_next_token(fin, first`)
		first`.end()
		first` = tmp
	;/

	tmp = produce_next_token(fin, first`)
	first`.end()
	first` = tmp

	/; if (tmp._type !== TTYPE_USRWD)
		_printf("Expected module name: \0")
		print_token(tmp)
		return
	;/

	Node out
	out.init(_type, tmp.data)
	
	tmp = produce_next_token(fin, first`)
	first` = tmp
	/; loop(first`._type !== TTYPE_ERR && first`.eq(";;\0") == false && first`.eq(";/\0") == false)
		/; if (first`.eq("import\0") == true)
			_ast_import(fin, ~out, first)
		;; else if (first`.eq("struct\0") == true)
			_ast_struct(fin, ~out, first)
		;; else if (first`.eq("asm\0") == true)
			_ast_asm(fin, ~out, first)
		;; else if (first`.eq("/;\0") == true)
			_ast_block(fin, ~out, first)
		;; else if (first`._type == TTYPE_KEYTP || first`._type == TTYPE_USRWD || first`.eq("~\0") == true)
			_ast_decl(fin, ~out, first)
		;; else
			tmp = produce_next_token(fin, first`)
			first`.end()
			first` = tmp
		;/
	;/

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

/; _ast_file (~utils.File fin, ~Node mod)
	fin`.open()
	
	Token first = produce_first_token(fin)
	/; loop (first._type !== TTYPE_ERR)
		/; if (first.eq("import\0") == true)
			_ast_import(fin, mod, ~first)
		;; else if (first.eq("struct\0") == true)
			_ast_struct(fin, mod, ~first)
		;; else if (first.eq("asm\0") == true)
			_ast_asm(fin, mod, ~first)
		;; else if (first.eq("/;\0") == true)
			_ast_block(fin, mod, ~first)
		;; else if (first._type == TTYPE_KEYTP || first._type == TTYPE_USRWD || first.eq("~\0") == true)
			_ast_decl(fin, mod, ~first)
		;; else
			Token tmp = produce_next_token(fin, first)
			first.end()
			first = tmp
		;/
	;/

	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("Mod\0")
	;; else if (n`._type == NTYPE_EXPORT)
		_printf("Exported Module\0")
	;; else if (n`._type == NTYPE_STRUCT)
		_printf("Struct\0")
	;; else if (n`._type == NTYPE_ID)
		_printf("ID\0")
	;; else if (n`._type == NTYPE_BIN_OP)
		_printf("Binary 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_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)
;/