-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc.lua
186 lines (169 loc) · 3.6 KB
/
lc.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
function Env(pa_env)
pa_env = pa_env or {}
local o = {}
local mt = {__index=pa_env}
setmetatable(o, mt)
return o
end
function Var(name)
local o = {type='var', name=name}
return o
end
function Lambda(arg, body)
local o = {type='lambda', arg=arg, body=body}
return o
end
function Apply(func, arg)
local o = {type='apply', func=func, arg=arg}
return o
end
Parser = {}
function Parser:new(tokens)
self.i = 1
self.tokens = tokens
return self
end
function Parser:nextToken()
local o = self.tokens[self.i]
self.i = self.i + 1
return o
end
function Parser:lookahead()
local o = self.tokens[self.i]
return o
end
function Parser:match(s)
if self:lookahead() ~= s then
error(s .. "not match")
end
self:nextToken()
end
function Parser:parseVAR()
local var = Var(self:nextToken())
return var
end
function Parser:parseEXPR()
if not self:lookahead() then
return
end
if self:lookahead() == "^" then
self:match("^")
local arg = self:parseVAR()
self:match(".")
local expr = self:parseAPPL()
return Lambda(arg, expr)
elseif self:lookahead() == "(" then
self:match("(")
local expr = self:parseAPPL()
self:match(")")
return expr
elseif self:lookahead() ~= ")" then
return self:parseVAR()
end
end
function Parser:parseAPPL()
if self:lookahead() == ")" then
return
elseif self:lookahead() then
local exprs = {}, x
exprs[1] = self:parseEXPR()
while true do
local expr = self:parseEXPR()
if expr then
exprs[#exprs+1] =expr
else
break
end
end
x = exprs[1]
for i=2, #exprs do
if exprs[i] then
x = Apply(x, exprs[i])
end
end
return x
end
end
function Parser:parse()
return self:parseAPPL()
end
function lexer(s)
local tokens = {}
local i, st, en
i = 1
while i <= #s do
local c = s:sub(i, i)
st, en = nil, nil
if c=='^' or c=='.' or c=='(' or c==')' then
st, en = i, i
elseif c:match("%a") == c then
st, en = s:find("%a+", i)
end
if st then
tokens[#tokens+1] = s:sub(st, en)
i = en + 1
else
i = i + 1
end
end
return tokens
end
function printTokens(tokens)
for i=1, #tokens do
print(tokens[i])
end
end
function printAST(ast)
if ast.type == 'lambda' then
io.write('^')
printAST(ast.arg)
io.write('.')
io.write('(')
printAST(ast.body)
io.write(')')
elseif ast.type == 'apply' then
io.write('(')
printAST(ast.func)
io.write(' ')
printAST(ast.arg)
io.write(')')
elseif ast.type == 'var' then
io.write(ast.name)
end
end
function eval(expr, env)
if expr.type == "var" then
return env[expr.name] or expr
elseif expr.type == "lambda" then
local new_env = Env(env)
new_env[expr.arg.name] = expr.arg
return Lambda(expr.arg, eval(expr.body, new_env))
elseif expr.type == "apply" then
local a = eval(expr.func, env)
local b = eval(expr.arg, env)
if a.type == "lambda" then
env[a.arg.name] = b
return eval(a.body, env)
else
return Apply(a, b)
end
end
end
function repl()
io.write("<<< ")
s = io.read()
while s do
local toks = lexer(s)
local p = Parser:new(toks)
local a = p:parse()
if a then
local c = eval(a, Env())
io.write(">>> ")
printAST(c)
print('\n')
end
io.write("<<< ")
s = io.read()
end
end
repl()