-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
executable file
·606 lines (530 loc) · 12.4 KB
/
main.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
--[[
usage:
{
ctx = ml.c "CODE"
ctx.some_function(args,...)
local data = ctx.some_data()
}
e.g.
{
ctx = ml.c "fun twice x = x + x"
print(ctx.twice(3)) --> 6
ctx = ml.c "fun mag_sqr (x,y) = x*x + y*y"
print(ctx.mag_sqr(3, 4)) --> 25
ctx = ml.c "val magic_number = 42"
print(ctx.magic_number()) --> 42
ctx = ml.c "datatype color = R|G|B fun isRed R = true | isRed _ = false"
print(ctx.isRed(ctx.G())) --> false
}
]]--
function push (a, t)
a[#a + 1] = t
end
function pop (a)
local t = a[#a]
a[#a] = nil
return t
end
-----------------------------
----[[ Lexical Scanner ]]----
-----------------------------
local REG_NUMBER = "%d+"
local REG_IDENT = "[%a_][%w_%^]*"
local REG_MAYBE_NUMBER = "%d"
local REG_OPEN_COMMENT = "%(%*"
local REG_CLOSE_COMMENT = "%*%)"
local REG_SPACES = "%s+"
local SYMS = {
{"(", pat="%("}, {")", pat="%)"},
{"[", pat="%["}, {"]", pat="%]"},
{"{", pat="{"}, {"}", pat="}"},
{"|", pat="|"}, {".", pat="%."},
{",", pat=","}, {"+", pat="%+"},
{"-", pat="%-"}, {"*", pat="%*"},
{"/", pat="/"}, {"%", pat="%%"},
{";", pat=";"}, {"=>", pat="=>"},
{"=", pat="="}, {"<>", pat="<>"},
{">=", pat=">="}, {"<=", pat="<="},
{">", pat=">"}, {"<", pat="<"},
{"::", pat="::"}, {"@", pat="@"}
}
local KEYWORDS = {
"fn", "if", "in", "of",
"end", "fun", "let", "val",
"case", "else", "then", "type"
}
local function Scanner (input)
local scan_pos = 1
local len = #input
-- ml uses "~" as negative
function ml_tonumber (s)
if string.find(s, "~") == 1 then
return -tonumber(string.sub(s, 2))
else
return tonumber(s)
end
end
-- somewhat pretty
function die (msg, p)
if p == nil then p = scan_pos end
local line_num = 1
local j = 1
while true do
j,i = string.find(input, "\n", j)
if j == nil or j > p then
break
else
j = j + 1
line_num = line_num + 1
end
end
return error("error, line " .. tostring(line_num) .. ": " .. msg, 0)
end
-- trim whitespace and comments from left side
function triml ()
local i,j
while scan_pos <= len do
-- find comment (*
i,j = string.find(input, REG_OPEN_COMMENT, scan_pos)
local start_pos = i
if i == scan_pos then
scan_pos = j + 1
-- find comment *)
i,j = string.find(input, REG_CLOSE_COMMENT, scan_pos)
if i == scan_pos then
scan_pos = j + 1
else
-- none!
return die("unclosed (*", start_pos)
end
end
-- find spaces
i,j = string.find(input, REG_SPACES, scan_pos)
if i == scan_pos then
scan_pos = j + 1
else
-- no more spaces
break
end
end
end
-- scan: symbols
function scan_sym ()
for _,sym in pairs(SYMS) do
-- match pattern
local i,j = string.find(input, sym.pat, scan_pos)
if i == scan_pos then
scan_pos = j + 1
-- return on success
return sym[1]
end
end
-- not a symbol?
return nil
end
-- scan: numbers
function scan_num ()
local i,j = string.find(input, REG_NUMBER, scan_pos)
if i == scan_pos then
scan_pos = j + 1
return ml_tonumber(string.sub(input, i, j))
else
return die("invalid number literal")
end
end
-- scan: keywords
function scan_word ()
-- just parse the identifier
local i,j = string.find(input, REG_IDENT, scan_pos)
if i == scan_pos then
scan_pos = j + 1
else
return die("invalid token '" ..
string.sub(input, scan_pos, scan_pos) .. "'")
end
-- check for known keywords
local word = string.sub(input, i, j)
for _,kw in pairs(KEYWORDS) do
if word == kw then
return true,kw
end
end
return false,word
end
-- move the lexer
function advance (self)
self.span = scan_pos
self.val = nil
triml()
if scan_pos > len then
self.tok = "<eof>"
return self
end
-- check symbol first
local sym = scan_sym()
if sym ~= nil then
self.tok = sym
return self
end
-- then number
local i,j = string.find(input, REG_MAYBE_NUMBER, scan_pos)
if i == scan_pos then
local num = scan_num()
self.tok = "<num>"
self.val = num
return self
end
-- then keyword
local is_kw,word = scan_word()
if is_kw then
self.tok = word
else
self.tok = "<id>"
self.val = word
end
return self
end
-- return current token and move to next
function shift (self)
local tok,val,span = self.tok, self.val, self.span
self:advance()
return tok,val,span
end
-- expect given token
function eat (self, t)
if self.tok == t then
local tok,val,span = self:shift()
return val,span
else
return die("expected '" .. t ..
"', got '" .. self.tok .. "'", self.span)
end
end
-- scanner object
return ({
advance = advance,
shift = shift,
eat = eat,
die = function (self, msg, p)
return die(msg, p)
end,
span = 1,
tok = "<empty>",
val = nil
}):advance()
end
------------------------
----[[ AST Parser ]]----
------------------------
local Parse = {}
--[[ Declarations ]]--
function Parse.decl (sc)
local tok,_,span = sc:shift()
if tok == "fun" then
return Parse.funDecl(sc, span)
elseif tok == "val" then
return Parse.valDecl(sc, span)
else
return sc:die("invalid declaration: " .. sc.tok, span)
end
end
function Parse.funDecl (sc, span)
local name = sc.val
local sigs = {}
-- at least one signature
Parse.funSig(sc, name, sigs)
while sc.tok == "|" do
sc:shift()
Parse.funSig(sc, name, sigs)
end
return {decl="fun", span=span, name=name, sigs=sigs}
end
function Parse.funSig (sc, name, sigs)
-- check they entered the name correctly
given_name,span = sc:eat("<id>")
if given_name ~= name then
return sc:die("expected function name '" .. name ..
"', got '" .. given_name .. "'", span)
end
-- parse the argument
local arg = Parse.pattern(sc)
-- function body
sc:eat("=")
local body = Parse.expr(sc)
push(sigs, {decl="sig", span=span, arg=arg, body=body})
end
function Parse.valDecl (sc, span)
local pat = Parse.pattern(sc)
sc:eat("=")
local body = Parse.expr(sc)
return {decl="val", span=span, pat=pat, body=body}
end
--[[ Expressions ]]--
function Parse.expr (sc)
if sc.tok == "let" then -- let ... in ...
local _,_,span = sc:shift()
return Parse.letExpr(sc, span)
elseif sc.tok == "case" then -- case ... of ... => ...
local _,_,span = sc:shift()
return Parse.caseExpr(sc, span)
elseif sc.tok == "fn" then -- fn ... => ...
local _,_,span = sc:shift()
local pat,body = Parse.patternCase(sc)
return {k="lambda", span=span, arg=pat, body=body}
elseif sc.tok == "if" then -- if ... then ... else ...
local _,_,span = sc:shift()
local e1 = Parse.expr(sc)
sc:eat("then")
local e2 = Parse.expr(sc)
sc:eat("else")
local e3 = Parse.expr(sc)
return {k="if", span=span, e1, e2, e3}
else -- term | expr term
return Parse.infix(sc)
end
end
-- let <decls> in <expr> end
function Parse.letExpr (sc, span)
local decls = {Parse.decl(sc)}
while sc.tok ~= "in" do
push(decls, Parse.decl(sc))
end
sc:shift()
local body = Parse.expr(sc)
sc:eat("end")
return {k="let", span=span, decls=decls, body=body}
end
-- case <expr> of <cases> end
function Parse.caseExpr (sc, span)
local cond = Parse.expr(sc)
local cases = {}
sc:eat("of")
cases[1] = Parse.patternCase(sc)
while sc.tok == "|" do
sc:shift()
push(cases, Parse.patternCase(sc))
end
sc:eat("end")
return {k="case", span=span, cond=cond, cases=cases}
end
-- <pat> => <expr>
function Parse.patternCase (sc)
local pat = Parse.pattern(sc)
sc:eat("=>")
local expr = Parse.expr(sc)
return {pat,expr}
end
-- <expr> {<op> <expr>}
function Parse.infix (sc)
local fst = Parse.appl(sc)
local exprs = {fst}
local opers = {}
-- pop ops and expr and combine
local function reduce ()
local op = pop(opers)
local e2 = pop(exprs)
local e1 = pop(exprs)
local e = {k="infix", span=op.span, op=op.name, e1, e2}
push(exprs, e)
end
-- based on simultaneous shunting yard and RPN reducer
local function add_shunting (op)
while #opers > 0 and precedes(op, opers[#opers]) do
reduce()
end
push(opers, op)
end
-- does op1 precede op2?
local function precedes (op1, op2)
if op1.is_right then
return op1.prec < op2.prec
else
return op1.prec <= op2.prec
end
end
local op = Parse.infixOp(sc)
while op ~= nil do
add_shunting(op)
push(exprs, Parse.appl(sc))
op = Parse.infixOp(sc)
end
while #opers > 0 do
reduce()
end
return exprs[1]
end
local INFIX = {
{";", 0, false},
{"=", 1, false},
{"<>", 1, false},
{">", 1, false},
{"<", 1, false},
{">=", 1, false},
{"<=", 1, false},
{"::", 4, true},
{"@", 4, true},
{"+", 2, false},
{"-", 2, false},
{"*", 3, false},
{"/", 3, false},
{"%", 3, false}
}
function Parse.infixOp (sc)
for _,v in ipairs(INFIX) do
local t,prec,r = unpack(v)
if sc.tok == t then
local _,_,span = sc:shift()
return {name=t, is_right=r, prec=prec}
end
end
return nil
end
function Parse.term (sc, opt)
if sc.tok == "<id>" then
return opt.parseId(sc)
elseif sc.tok == "<num>" then
local _,val,span = sc:shift()
return {k="num", span=span, val=val}
elseif sc.tok == "(" then
local _,_,span = sc:shift()
-- unit: ()
if sc.tok == ")" then
sc:shift()
return {k="unit", span=span}
end
-- tuple: (e1, e2, ...)
local e = opt.parseOne(sc)
if sc.tok == "," then
e = Parse.tuple(sc, opt, span, e)
end
sc:eat(")")
return e
else
return sc:die("invalid term: " .. sc.tok, sc.span)
end
end
function Parse.tuple (sc, opt, span, fst)
local elems = {fst}
while sc.tok == "," do
-- parse one and add it
sc:shift()
push(elems, Parse.expr(sc))
end
elems.k = "tuple"
elems.span = span
return elems
end
function Parse.isTerm (tok)
return tok == "<id>" or
tok == "<num>" or
tok == "("
end
function Parse.appl (sc)
local acc = Parse.term(sc, Parse.exprTerm)
while Parse.isTerm(sc.tok) do
local t = Parse.term(sc, Parse.exprTerm)
acc = {k="app", span=t.span, acc, t}
end
return acc
end
function Parse.pattern (sc)
local pat = Parse.term(sc, Parse.patTerm)
if sc.tok == "::" then
local _,_,span = sc:shift()
local tail = Parse.pattern(sc)
return {k="cons", span=span, pat, tail}
else
return pat
end
end
Parse.exprTerm = {
parseOne = Parse.expr,
parseId = function (sc)
local name,span = sc:eat("<id>")
return {k="var", span=span, name=name}
end
}
Parse.patTerm = {
parseOne = Parse.pattern,
parseId = function (sc)
local name,span = sc:eat("<id>")
if sc.tok == "(" then
local arg = Parse.pattern(sc)
return {k="enum", span=span, name=name, arg=arg}
else
-- might be 'enum', to be determined by context
return {k="var", span=span, name=name}
end
end
}
---------------------------
----[[ For Debugging ]]----
---------------------------
function ASTKind (e)
return e.k or e.decl
end
function show (v)
if type(v) == "table" and ASTKind(v) ~= nil then
return showAST(v)
elseif type(v) == "table" then
return showASTList(v)
elseif type(v) == "string" then
return string.format("%q", v)
else
return tostring(v)
end
end
function showASTList (lst)
local buf = {"["}
for i,v in ipairs(lst) do
if #buf > 1 then
push(buf, ", ")
end
push(buf, show(v))
end
push(buf, "]")
return table.concat(buf)
end
function showAST (e)
local buf = {ASTKind(e), " {"}
local function ignore (key)
if type(key) ~= "string" then
return true
elseif key == "k" or key == "decl"
or key == "span" then
return true
else
return false
end
end
for key,v in pairs(e) do
if not ignore(key) then
if #buf > 2 then
push(buf, ", ")
end
push(buf, tostring(key))
push(buf, " = ")
push(buf, show(v))
end
end
for i,v in ipairs(e) do
if #buf > 2 then
push(buf, ", ")
end
push(buf, show(v))
end
push(buf, "}")
return table.concat(buf)
end
function go (input)
local scanner = Scanner(input)
local fn = Parse.decl(scanner)
for i,sig in ipairs(fn.sigs) do
print(show(sig.arg))
print(" " .. show(sig.body))
end
end
go [[
fun sum 0 = 0
| sum n = n + sum (pred n)
]]