-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
636 lines (584 loc) · 14.3 KB
/
parser.lua
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
-- Parser output documentation
-- toast.parse(ctx, txt)
-- returns the main block of code
-- ctx holds configurable stuff like operators, qualifiers, and string escapes (see toast.defaultctx)
-- {"code", ...}
-- an array of statement blocks
-- {"constant", type, value}
-- constant types:
-- number
-- string
-- type
-- {"reference", name}
-- reference to variable
-- {"ilcall", funcname, {...}}
-- inline function call
-- third argument is a list of function parameters
-- {"push", type, varname}
-- declares a variable name
-- {"assign", expression, expression}
-- assignes a reference to
-- {"func", varname, arglist, rettype, code}
-- function
-- {"while", varname, code}
-- simple while loop
toast.defaultctx = {
operators = { -- later this will just be filled by funcs.lua
{"^", "o_pow", "x_x"},
{"not","o_not", "_x"},
{"!", "o_not", "_x"},
{"-", "o_neg", "_x"},
{"~", "o_xor", "_x"},
{"*", "o_mul", "x_x"},
{"/", "o_div", "x_x"},
{"+", "o_add", "x_x"},
{"-", "o_sub", "x_x"},
{"..", "o_cat", "x_x"},
{"<", "o_lt", "x_x"},
{">", "o_gt", "x_x"},
{"<=", "o_leq", "x_x"},
{">=", "o_geq", "x_x"},
{"!=", "o_neq", "x_x"},
{"~=", "o_neq", "x_x"},
{"==", "o_eq", "x_x"},
{"and","o_and", "x_x"},
{"&&", "o_and", "x_x"},
{"or", "o_or", "x_x"},
{"||", "o_or", "x_x"}
},
type_qualifiers = {
["const"] = true,
["final"] = true,
["constexpr"] = true,
["replace"] = true,
},
func_qualifiers = {
["inline"] = true,
},
escapes = {
a = "\a",
b = "\b",
f = "\f",
n = "\n",
r = "\r",
t = "\t",
v = "\v",
},
types = {
["ref"] = true,
["cell"] = true,
["int"] = true,
["long"] = true,
},
}
function toast.typeSignature(t)
if t[1] == "type" then
local o = ""
for i = 1, #t.quals do
o = o .. t.quals[i] .. " "
end
o = o .. t[2]
for i = 1, #t.tparams do
o = o .. "<"
for j = 1, #t.params[i] do
o = o .. toast.typeSignature(t)
if j ~= #t.params[i] then
o = o .. ","
end
end
o = o .. ">"
end
elseif t[1] == "constant" then
if t[2] == "number" then
return tostring(t[3])
elseif t[2] == "string" then
return (("%q"):format(t[3]):gsub("\\\n", "\\n"))
else
error("cannot serialize constant " .. t[2])
end
else
error("cannot serialize " .. t[1])
end
end
-- lua pattern escaper
local function pescape(txt)
local o=txt:gsub("[%.%[%]%(%)%%%*%+%-%?%^%$]","%%%1"):gsub("%z","%%z")
return o
end
local table_merge
function table_merge(a, b)
for k,v in pairs(b) do
if type(a[k]) == "table" then
table_merge(a[k], v)
else
a[k] = v
end
end
return a
end
function toast.parse(ctx, txt)
local otxt = txt
local function getIdx()
return #otxt - #txt
end
-- adds code index for debugging
local function addIdx(t)
t.idx = getIdx()
return t
end
local types = {ctx.types} -- we have to keep track of type names in the parser
local function isType(tpe)
for l1 = 1, #types do
if types[l1][tpe] then
return true
end
end
return false
end
-- in the lua implementation i am being lazy and using string.sub instead of indexes
-- for noticably better performance it would be better to store the index you are in the source string
local function skip(n)
txt = txt:sub(n+1)
end
local function skipWhitespace()
txt = txt:gsub("^%s+", "")
end
local function readWord()
skipWhitespace()
local o = txt:match("^[%%%a_][%a_%d]*") -- expand pattern to support more characters in variable names
if not o then
return false
end
skip(#o)
return o
end
local readInline
local function readParams() -- reads function and template parameters delimiated by commas
local o = {}
while true do
local param = readInline()
if not param then
return o
end
table.insert(o, param)
skipWhitespace()
if txt:sub(1,1) ~= "," then
return o
end
skip(1)
end
end
-- reads types including their qualifiers and template parameters
local function readType()
local otxt = txt
local quals = {}
while true do -- read qualifiers
local qual = readWord()
if not ctx.type_qualifiers[qual] then
if isType(qual) then -- got typename
skipWhitespace()
local tout = addIdx({"type", qual, quals = quals, tparams = {}})
while txt:sub(1,1) == "<" do -- read template parameters
skip(1)
table.insert(tout.tparams, readParams())
skipWhitespace()
if txt:sub(1,1) ~= ">" then
error("'>' expected")
end
skip(1)
skipWhitespace()
end
return tout
else
txt = otxt
return false
end
else
quals[qual] = true
end
end
end
-- reads constants like 69, "potato", etc return false and restores source otherwise
local function readConst()
local otxt = txt
skipWhitespace()
-- decimal numbers
local num = txt:match("^%-?%d+%.?%d*")
if num then
skip(#num)
return addIdx({"constant", "number", tonumber(num)})
end
-- hexidecimal
local num = txt:match("^%-?0x%x+%.?%x+")
if num then
skip(#num)
return addIdx({"constant", "number", tonumber(num)})
end
-- char
local char = txt:match("^'(.)'")
if char then
skip(3)
return addIdx({"constant", "number", string.byte(char)})
end
-- string
if txt:sub(1,1) == "\"" then
skip(1)
local str = ""
while txt:sub(1,1) ~= "\"" do -- \ handling
if txt == "" then
txt = otxt
exc.throw("parserError", "End of string expected")
end
local c = txt:sub(1,1)
skip(1)
if c == "\\" then -- escapes
str = str .. (ctx.escapes[txt:sub(1,1)] or txt:sub(1,1))
skip(1)
else
str = str .. c
end
end
skip(1)
return addIdx({"constant", "string", str})
end
return false
end
local function getOperator(ctx, opname, optype)
for k,v in pairs(ctx.operators) do
if v[1] == opname and v[3] == optype then
return k
end
end
end
-- reads inline statements, does operator ordering, etc
function readInline()
local otxt = txt
local expSections = {}
local done = false
local last = "none"
while not done do
done = true
skipWhitespace()
local prev = expSections[#expSections]
if txt:sub(1,1) == "(" then -- parse inline parentheses
if last ~= "none" and last ~= "operator" then
skip(1)
local params = readParams()
skipWhitespace()
if txt:sub(1,1) ~= ")" then
exc.throw("parserError", "\")\" Expected")
end
skip(1)
done = false
if prev[1] == "reference" then
table.remove(expSections)
table.insert(expSections, addIdx({"ilcall", prev[2], params}))
else
table.remove(expSections)
table.insert(expSections, addIdx({"ilcall", "s_callptr", {prev}}))
end
else
done = false
skip(1)
table.insert(expSections, readInline())
skipWhitespace()
if txt:sub(1,1) ~= ")" then
exc.throw("parserError", "\")\" Expected")
end
skip(1)
last = "code"
end
else -- parse inline variable / function call
local varname = readWord()
skipWhitespace()
local op
for k = 1, #ctx.operators do -- operators
local v = ctx.operators[k]
if (prev or {"operator"})[1] == "operator" or
v[3] ~= "_x" then -- make sure unary operators dont get placed after an expression
if varname and varname == v[1] or (not varname and txt:match("^"..pescape(v[1]))) then
skip(#v[1])
done = false
op = true
table.insert(expSections, {"operator", k})
break
end
end
end
if not op and not varname then
local const = readConst()
if const then -- parse inline constants
done = false
table.insert(expSections, const)
last = "const"
end
elseif not op and varname then -- variable name
if isType(varname) then
done = false
table.insert(expSections, readType())
else
done = false
table.insert(expSections, {"reference", "u_"..varname})
last = "reference"
end
end
end
end
if #expSections == 0 then
txt = otxt
return false
end
local o = {}
local expq = {{expSections, o}}
while #expq > 0 do -- resolve order of operations
local c = expq[#expq][1]
local co = expq[#expq][2]
table.remove(expq)
local maxn = 0
local maxi = 0
for k,v in pairs(c) do
if v[1] == "operator" then
if v[2] > maxn then
maxn = v[2]
maxi = k
end
end
end
if maxn ~= 0 then
if ctx.operators[maxn][3] == "_x" then -- stuff like (-x)
local r = {}
for l1 = maxi + 1, #c do
table.insert(r, c[l1])
end
table.insert(expq, r)
for l1 = 1, #c do
c[l1] = nil
end
c[1] = "ilcall"
c[2] = ctx.operators[maxn][2]
c[3] = {r}
elseif ctx.operators[maxn][3] == "x_x" then -- stuff like (x + y)
co[1] = "ilcall"
co[2] = ctx.operators[maxn][2]
co[3] = {{}, {}}
local l = {}
for l1 = 1, maxi - 1 do
table.insert(l, c[l1])
end
local r = {}
for l1 = maxi + 1, #c do
table.insert(r, c[l1])
end
table.insert(expq, {l, co[3][1]})
table.insert(expq, {r, co[3][2]})
end
elseif #c == 1 then -- no operators
table_merge(co, c[1])
else
exc.throw("parserError", "Invalid expression")
end
end
return o
end
local readStatement
local readCode
local function readFunction()
local otxt = txt
-- read function qualifiers
local quals = {}
skipWhitespace()
local cn = true
while cn do
cn = false
for k, v in pairs(ctx.func_qualifiers) do
if k == txt:sub(1, #k) then
quals[k] = true
skip(#k)
skipWhitespace()
cn = true
break
end
end
end
skipWhitespace()
local rt = readType()
local word = readWord()
if word ~= "func" then
if not next(quals) then
txt = otxt
return false
end
exc.throw("parserError", "\"func\" Expected")
end
-- read function name
skipWhitespace()
local funcname = readWord()
if not funcname then
exc.throw("parserError", "Function name expected")
end
skipWhitespace()
if txt:sub(1,1) ~= "(" then
exc.throw("parserError", "\"(\" Expected")
end
skip(1)
-- read function args
local argl = {}
while true do
local tpe = readType()
if not tpe then
break
end
local arname = readWord()
if not arname then
exc.throw("parserError", "Argument name expected")
end
table.insert(argl, {tpe, arname:sub(1, 1) == "%" and arname:sub(2) or "u_" .. arname})
skipWhitespace()
if txt:sub(1,1) ~= "," then
break
end
skip(1)
end
skipWhitespace()
if txt:sub(1,1) ~= ")" then
exc.throw("parserError", "\")\" Expected")
end
skip(1)
local typeinfo = {"type", "func", quals = quals, tparams = {argl, next(rt) and rt}}
skipWhitespace()
if txt:sub(1, 1) == ";" then -- decleration
skip(1)
return addsIdx({"push", typeinfo, funcname:sub(1, 1) == "%" and funcname:sub(2) or "u_" .. funcname})
end
local st = assert(readStatement())
-- push is inserted later by compiler.lua
return addIdx({"func", funcname:sub(1, 1) == "%" and funcname:sub(2) or "u_" .. funcname, quals, argl, rt, st})
end
-- read statements, always ends with semicolons
function readStatement()
local func = readFunction()
if func then
skipWhitespace()
if txt:sub(1,1) ~= ";" then
exc.throw("parserError", "\";\" Expected")
end
skip(1)
return func
end
skipWhitespace()
if txt:sub(1,1) == "{" then
skip(1)
local code = readCode()
skipWhitespace()
if txt:sub(1,1) ~= "}" then
exc.throw("parserError", "\"}\" Expected")
end
skip(1)
return code
end
local otxt = txt
local w = readWord()
if w == "while" then
skipWhitespace()
if txt:sub(1,1) ~= "(" then
exc.throw("parserError", "\"(\" Expected")
end
skip(1)
local cond = readInline()
skipWhitespace()
if txt:sub(1,1) ~= ")" then
exc.throw("parserError", "\")\" Expected")
end
skip(1)
local st = readStatement()
if txt:sub(1,1) ~= ";" then
exc.throw("parserError", "\";\" Expected")
end
return addIdx({"while", cond, st})
elseif ctx.type_qualifiers[v] or isType(w) then
txt = otxt
local tpe = readType()
skipWhitespace()
local varname = readWord()
skipWhitespace()
if txt:sub(1,1) == ";" then -- decleration
skip(1)
return {"push", tpe, varname:sub(1, 1) == "%" and varname:sub(2) or "u_" .. varname}
elseif txt:sub(1,1) == "=" then -- decleration + assignment
skip(1)
local il2 = readInline()
if not il2 then
exc.throw("parserError", "Statement expected")
end
if txt:sub(1,1) ~= ";" then
exc.throw("parserError", "\";\" Expected")
end
skip(1)
return {"code",
{"push", tpe, varname:sub(1, 1) == "%" and varname:sub(2) or "u_" .. varname},
{"assign", {"reference", varname:sub(1, 1) == "%" and varname:sub(2) or "u_" .. varname}, il2},
}
else
exc.throw("parserError", "\")\" Expected")
end
else
txt = otxt
end
local il = readInline() -- inline statements like function calls
if il then
skipWhitespace()
if txt:sub(1,1) == "=" then -- assignment
skip(1)
local il2 = readInline()
if not il2 then
exc.throw("parserError", "Statement expected")
end
if txt:sub(1,1) ~= ";" then
exc.throw("parserError", "\";\" Expected")
end
skip(1)
return {"assign", il, il2}
elseif txt:sub(1,1) ~= ";" then
exc.throw("parserError", "\";\" Expected")
end
if il[1] == "reference" or il[1] == "constant" then
exc.throw("parserError", "Statement with no effect")
end
skip(1)
return il
end
return false, "No statement"
end
-- reads a block of code
function readCode()
local o = {"code"}
while true do
local st, err = readStatement()
if not st then
if #o == 1 then
exc.throw("parserError", "Statement expected")
end
return o
end
table.insert(o, st)
end
end
return exc.catch(function()
local code = readCode()
skipWhitespace()
if #txt > 0 then
exc.throw("parserError", "Incomplete statement")
end
return code
end, "parserError", function(msg)
local sidx = #otxt - #txt
local eidx = #otxt - #txt
while sidx ~= 0 and not otxt:sub(sidx, sidx):match("[\t\r\n]") do
sidx = sidx - 1
end
while eidx ~= (#otxt + 1) and otxt:sub(eidx, eidx) ~= "\n" do
eidx = eidx + 1
end
return false, msg .. "\n" .. otxt:sub(sidx + 1, eidx - 1) .. "\n" .. (" "):rep((#otxt - #txt) - sidx) .. "^"
end)
end