-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambdavm.lua
66 lines (50 loc) · 1.36 KB
/
lambdavm.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
local Source = require("source")
local Error = require("errors.base")
local Source = require("source")
local Lexer = require("lexer")
local Parser = require("parser")
local Reducer = require("reducer")
local LambdaVM = class("LambdaVM")
function LambdaVM:initialize()
self.show_steps = false
self.loopback = false
end
function LambdaVM:runString(text, origin)
local source = Source(text, origin)
local lexer = Lexer(source)
local tokens, error = lexer:tokenize()
if error then
return self:onError(error)
end
local parser = Parser(tokens)
local result = parser:parse()
if result.error then
return self:onError(result.error)
end
local ast = result.node
local reducer = Reducer(ast)
reducer:checkStructure()
local redex
if self.loopback then
if self.show_steps then
redex = reducer:findRedex()
end
print(reducer.ast:toprettytext(0, redex))
end
local modified
repeat
modified = reducer:step()
if self.show_steps and modified then
local redex = reducer:findRedex()
print(reducer.ast:toprettytext(0, redex))
end
until not modified
return reducer.ast
end
function LambdaVM:onError(error)
-- print("Failed.")
-- print("----------")
-- print(error)
return error
end
return LambdaVM