-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
570 lines (515 loc) · 11.2 KB
/
Copy pathparser.lua
File metadata and controls
570 lines (515 loc) · 11.2 KB
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
local nexttok = require 'lexer'
local ast_node = require 'ast_node'.new_node
local function expect(s, t, msg)
if s.tok ~= t then
s.error(msg or ('expected `%s`, got `%s`'):format(t, s.tok), s.tok == '<eof>')
end
nexttok(s)
end
local expr_start_tokens = {
['<number>'] = true,
['<name>'] = true,
['<string>'] = true,
['('] = true,
['-'] = true,
['not'] = true,
['fn'] = true,
['true'] = true,
['false'] = true,
['nil'] = true,
['if'] = true,
['while'] = true,
['repeat'] = true,
['do'] = true,
['type'] = true,
['class'] = true,
['struct'] = true,
['variant'] = true,
['return'] = true,
['new'] = true,
['let'] = true,
['var'] = true,
['...'] = true,
}
local typedecl_start_tokens = {
['type'] = true,
['class']= true,
['struct'] = true,
}
local nt = {}
function nt.base_expr(s)
if s.tok == '<number>' then
local text = s.tokval
nexttok(s)
return ast_node(s, 'float', text)
elseif s.tok == '<name>' then
local text = s.tokval
nexttok(s)
return ast_node(s, 'name', text)
elseif s.tok == '<string>' then
local text = s.tokval
nexttok(s)
return ast_node(s, 'string', text)
elseif s.tok == 'true' then
nexttok(s)
return ast_node(s, 'boolean', true)
elseif s.tok == 'false' then
nexttok(s)
return ast_node(s, 'boolean', false)
elseif s.tok == 'nil' then
nexttok(s)
return ast_node(s, 'nil')
elseif s.tok == '(' then
local l,c = s.line, s.col
nexttok(s)
if s.tok == ')' then
nexttok(s)
return ast_node(s, 'void')
end
local e = nt.seq(s)
if s.tok == ',' then
nexttok(s)
e = ast_node(s, 'tuple', e)
nt.comma_separated_seq(s, e)
else
e = ast_node(s, 'parens', e)
end
if s.tok ~= ')' then
s.error(('expected `)` to close `(` on line %d, col %d'):format(l, c), s.tok=='<eof>')
end
nexttok(s)
return e
elseif s.tok == 'if' then
nexttok(s)
expect(s, '(')
local cond = nt.seq(s)
expect(s, ')')
local true_branch = nt.expr(s)
local false_branch
if s.tok == 'else' then
nexttok(s)
false_branch = nt.expr(s)
else
fale_branch = ast_node(s, 'void')
end
return ast_node(s, 'if', cond, true_branch, false_branch)
elseif s.tok == 'while' then
nexttok(s)
expect(s, '(')
local cond = nt.seq(s)
expect(s, ')')
local body = nt.expr(s)
return ast_node(s, 'while', cond, body)
elseif s.tok == 'repeat' then
nexttok(s)
local body = nt.expr(s)
expect(s, 'until')
--expect(s, '(')
local cond = nt.expr(s)
--expect(s, ')')
return ast_node(s, 'repeat', body, cond)
elseif s.tok == 'do' then
nexttok(s)
return ast_node(s, 'do', nt.expr(s))
elseif s.tok == 'fn' then
nexttok(s)
local name = nil
if s.tok == '<name>' then
name = s.tokval
nexttok(s)
end
expect(s, '(')
local params = {}
local first = true
while (first and (s.tok == '<name>' or s.tok == '...')) or ((not first) and s.tok == ',') do
if first then
first = false
else
expect(s, ',')
end
if s.tok == '<name>' then
local param = {name = s.tokval}
nexttok(s)
if s.tok == ':' then
nexttok(s)
param.ty = nt.tyexpr(s)
end
params[#params + 1] = param
elseif s.tok == '...' then
nexttok(s)
params.vararg = true
break
else
s.error('expected parameter name or `...`')
end
end
local rettype = nil
if s.tok == '->' then
nexttok(s)
rettype = nt.tyexpr(s)
end
expect(s, ')')
local body = nt.expr(s)
local e = ast_node(s, 'function', params, rettype, body)
if name then
ast_node(s, 'let', {'name', name}, nil, e, true)
end
return e
elseif s.tok == 'return' then
nexttok(s)
return ast_node(s, 'return', nt.expr(s))
elseif s.tok == 'new' then
nexttok(s)
local ty = nt.tyexpr(s)
if s.tok == '.' then
nexttok(s)
local name = s.tokval
expect(s, '<name>')
expect(s, '(')
local val
if s.tok ~= ')' then
val = nt.expr(s)
else
val = ast_node(s, 'void')
end
expect(s, ')')
return ast_node(s, 'new_variant', ty, name, val)
end
expect(s, '{')
local e = ast_node(s, 'new_kv', ty, {})
local first = true
while s.tok ~= '}' do
if first then
first = false
else
expect(s, ',')
end
local name = s.tokval
expect(s, '<name>')
expect(s, '=')
local val = nt.expr(s)
table.insert(e[3], {name=name, val=val})
end
nexttok(s)
return e
elseif s.tok == 'type' then
nexttok(s)
local name = s.tokval
nexttok(s)
expect(s, '=')
return ast_node(s, 'typedef', name, nt.tyexpr(s))
elseif s.tok == 'class' then
nexttok(s)
local name = s.tokval
expect(s, '<name>')
local t = nt.class_tyexpr(s)
return ast_node(s, 'typedef', name, t)
elseif s.tok == 'struct' then
nexttok(s)
local name = s.tokval
expect(s, '<name>')
local t = nt.struct_tyexpr(s)
return ast_node(s, 'typedef', name, t)
elseif s.tok == 'variant' then
nexttok(s)
local name = s.tokval
expect(s, '<name>')
local t = nt.variant_tyexpr(s)
return ast_node(s, 'typedef', name, t)
elseif s.tok == '-' then
nexttok(s)
return ast_node(s, 'unop', '-', nt.expr(s))
elseif s.tok == 'not' then
nexttok(s)
return ast_node(s, 'unop', 'not', nt.expr(s))
elseif s.tok == '...' then
nexttok(s)
return ast_node(s, 'vararg')
elseif s.tok == '[' then
nexttok(s)
local t
if s.tok == '>' then
t = 'quote'
elseif s.tok == '<' then
t = 'unquote'
else
s.error('expected `>` or `<`')
end
nexttok(s)
local e = nt.seq(s)
expect(s, ']')
return ast_node(s, t, e)
end
s.error('expected expression, got `' .. s.tok .. '`')
end
function nt.suffix_expr(s)
local e = nt.base_expr(s)
while true do
if s.tok == '.' then
nexttok(s)
local text = s.tokval
expect(s, '<name>')
e = ast_node(s, 'field', e, text)
elseif s.tok == ':' then
nexttok(s)
local name = s.tokval
e = ast_node(s, 'method_call', e, name)
expect(s, '<name>')
expect(s, '(')
nt.comma_separated_seq(s, e)
expect(s, ')')
elseif s.tok == '[' then
nexttok(s)
local key = nt.expr(s)
expect(s, ']')
e = ast_node(s, 'index', e, key)
elseif s.tok == '(' then
e = ast_node(s, 'call', e)
nexttok(s)
nt.comma_separated_seq(s, e)
expect(s, ')')
else
return e
end
end
end
local binops = {
['or'] = {prec = 10, rassoc = false},
['and'] = {prec = 20, rassoc = false},
['=='] = {prec = 25, rassoc = false},
['!='] = {prec = 25, rassoc = false},
['>='] = {prec = 25, rassoc = false},
['<='] = {prec = 25, rassoc = false},
['>'] = {prec = 25, rassoc = false},
['<'] = {prec = 25, rassoc = false},
['..'] = {prec = 30, rassoc = false},
['+'] = {prec = 40, rassoc = false},
['-'] = {prec = 40, rassoc = false},
['*'] = {prec = 50, rassoc = false},
['/'] = {prec = 50, rassoc = false},
['^'] = {prec = 60, rassoc = true},
}
function nt.binop_expr(s, min_prec)
min_prec = min_prec or 0
local result = nt.suffix_expr(s)
while true do
local op_info = binops[s.tok]
if not (op_info and op_info.prec >= min_prec) then break end
local op = s.tok
nexttok(s)
local next_min_prec = op_info.prec
if not op_info.rassoc then
next_min_prec = next_min_prec + 1
end
result = ast_node(s, 'binop', op, result, nt.binop_expr(s, next_min_prec))
end
return result
end
function nt.assign_expr(s)
if s.tok == 'let' then
local node_type = s.tok
nexttok(s)
local lval = nt.binop_expr(s)
local explicit_type = nil
if s.tok == ':' then
nexttok(s)
explicit_type = nt.tyexpr(s)
end
expect(s, '=')
return ast_node(s, 'let', lval, explicit_type, nt.assign_expr(s))
elseif s.tok == 'var' then
nexttok(s)
local lval = nt.binop_expr(s)
local explicit_type = nil
if s.tok == ':' then
nexttok(s)
explicit_type = nt.tyexpr(s)
end
if s.tok == '=' then
nexttok(s)
return ast_node(s, 'var', lval, explicit_type, nt.assign_expr(s))
else
return ast_node(s, 'var', lval, explicit_type)
end
else
local e = nt.binop_expr(s)
if s.tok == '=' then
nexttok(s)
return ast_node(s, 'assign', e, nil, nt.assign_expr(s))
end
return e
end
end
nt.expr = nt.assign_expr
function nt.seq(s)
local e = nt.expr(s)
if s.tok == ';' then
e = ast_node(s, 'seq', e)
repeat
nexttok(s)
if expr_start_tokens[s.tok] then
e[#e + 1] = nt.expr(s)
else
e[#e + 1] = ast_node(s, 'void')
break
end
until s.tok ~= ';'
end
return e
end
function nt.comma_separated_seq(s, e)
while expr_start_tokens[s.tok] do
e[#e + 1] = nt.seq(s)
if s.tok ~= ',' then
break
end
nexttok(s)
end
end
function nt.comma_separated_tyexpr(s, e)
while expr_start_tokens[s.tok] do
e[#e + 1] = nt.tyexpr(s)
if s.tok ~= ',' then
break
end
nexttok(s)
end
end
function nt.struct_tyexpr(s)
local l,c = s.line, s.col
expect(s, '{')
local t = {'ty_struct', {}}
local first = true
while s.tok ~= '}' do
if first then
first = false
else
expect(s, ',')
end
if s.tok == '<name>' then
local name = s.tokval
nexttok(s)
expect(s, ':')
local ty = nt.tyexpr(s)
table.insert(t[2], {name=name, ty=ty})
else
expect(s, '}')
end
end
nexttok(s)
return t;
end
function nt.class_tyexpr(s)
local l,c = s.line, s.col
local base = nil
if s.tok == ':' then
nexttok(s)
base = nt.tyexpr(s)
end
expect(s, '{')
local t = {'ty_class', {}, base}
local first = true
while s.tok ~= '}' do
if first then
first = false
else
expect(s, ',')
end
if s.tok == '<name>' then
local name = s.tokval
nexttok(s)
expect(s, ':')
local ty = nt.tyexpr(s)
table.insert(t[2], {name=name, ty=ty})
else
expect(s, '}')
end
end
nexttok(s)
return t;
end
function nt.variant_tyexpr(s)
local l,c = s.line, s.col
expect(s, '{')
local t = ast_node(s, 'ty_variant', {})
local first = true
while s.tok ~= '}' do
if first then
first = false
else
expect(s, ',')
end
if s.tok == '<name>' then
local name = s.tokval
nexttok(s)
local ty
if s.tok ~= ',' and s.tok ~= '}' then
ty = nt.tyexpr(s)
else
ty = ast_node(s, 'ty_tuple')
end
table.insert(t[2], {name=name, ty=ty})
else
expect(s, '}')
end
end
nexttok(s)
return t;
end
function nt.tyexpr(s)
if s.tok == '<name>' then
local text = s.tokval
nexttok(s)
return ast_node(s, 'ty_name', text)
elseif s.tok == '(' then
nexttok(s)
local t = ast_node(s, 'ty_tuple')
nt.comma_separated_tyexpr(s, t)
expect(s, ')')
return t
elseif s.tok == 'struct' then
nexttok(s)
return nt.struct_tyexpr(s)
elseif s.tok == 'class' then
nexttok(s)
return nt.class_tyexpr(s)
elseif s.tok == 'variant' then
nexttok(s)
return nt.variant_tyexpr(s)
elseif s.tok == '...' then
nexttok(s)
return ast_node(s, 'ty_vararg')
elseif s.tok == '!' then
nexttok(s)
return ast_node(s, 'ty_noret')
elseif s.tok == 'fn' then
nexttok(s)
expect(s, '(')
local rettype, argtypes, vararg = nil, {}, false
local first = true
while true do
if s.tok == ')' or s.tok == '->' then break end
if first then first=false else expect(s, ',') end
if nexttok == '...' then
vararg = true
break
end
table.insert(argtypes, {ty=nt.tyexpr(s)})
end
if s.tok == '->' then
nexttok(s)
rettype = nt.tyexpr(s)
end
expect(s, ')')
rettype = rettype or ast_node(s, 'ty_tuple')
return ast_node(s, 'ty_function', argtypes, rettype, vararg)
end
s.error('expected type expression, got `' .. s.tok .. '`')
end
local function parse(s)
nexttok(s)
local root = nt.seq(s)
expect(s, '<eof>')
return root
end
return parse