-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlua-ast.lua
More file actions
238 lines (192 loc) · 7.04 KB
/
lua-ast.lua
File metadata and controls
238 lines (192 loc) · 7.04 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
local function build(kind, node)
node.kind = kind
return node
end
local function ident(name, line)
return build("Identifier", { name = name, line = line })
end
local function does_multi_return(expr)
local k = expr.kind
return k == "CallExpression" or k == "SendExpression" or k == "Vararg"
end
local AST = { }
local function func_decl(id, body, params, vararg, locald, firstline, lastline)
return build("FunctionDeclaration", {
id = id,
body = body,
params = params,
vararg = vararg,
locald = locald,
firstline = firstline,
lastline = lastline,
line = firstline,
})
end
local function func_expr(body, params, vararg, firstline, lastline)
return build("FunctionExpression", { body = body, params = params, vararg = vararg, firstline = firstline, lastline = lastline })
end
function AST.expr_function(ast, args, body, proto)
return func_expr(body, args, proto.varargs, proto.firstline, proto.lastline)
end
function AST.local_function_decl(ast, name, args, body, proto)
local id = ast:var_declare(name)
return func_decl(id, body, args, proto.varargs, true, proto.firstline, proto.lastline)
end
function AST.function_decl(ast, path, args, body, proto)
return func_decl(path, body, args, proto.varargs, false, proto.firstline, proto.lastline)
end
function AST.chunk(ast, body, chunkname, firstline, lastline)
return build("Chunk", { body = body, chunkname = chunkname, firstline = firstline, lastline = lastline })
end
function AST.local_decl(ast, vlist, exps, line)
local ids = {}
for k = 1, #vlist do
ids[k] = ast:var_declare(vlist[k])
end
return build("LocalDeclaration", { names = ids, expressions = exps, line = line })
end
function AST.assignment_expr(ast, vars, exps, line)
return build("AssignmentExpression", { left = vars, right = exps, line = line })
end
function AST.assignment_algebra_expr(ast, vars, exps, line)
return build("AssignmentAlgebraExpression", { left = vars, right = exps, line = line })
end
function AST.expr_index(ast, v, index, line)
return build("MemberExpression", { object = v, property = index, computed = true, line = line })
end
function AST.expr_algebra_index(ast, v, line)
return build("IndexAlgebraExpression", { object = v, line = line })
end
function AST.expr_property(ast, v, prop, line)
local index = ident(prop, line)
return build("MemberExpression", { object = v, property = index, computed = false, line = line })
end
function AST.literal(ast, val)
return build("Literal", { value = val })
end
function AST.expr_vararg(ast)
return build("Vararg", { })
end
function AST.expr_brackets(ast, expr)
expr.bracketed = true
return expr
end
function AST.set_expr_last(ast, expr)
if expr.bracketed and does_multi_return(expr) then
expr.bracketed = nil
return build("ExpressionValue", { value = expr })
else
return expr
end
end
function AST.expr_table(ast, keyvals, line)
return build("Table", { keyvals = keyvals, line = line })
end
function AST.expr_unop(ast, op, v, line)
return build("UnaryExpression", { operator = op, argument = v, line = line })
end
function AST.expr_algebra_unop(ast, op, v, line)
return build("UnaryAlgebraExpression", { operator = op, argument = v, line = line })
end
local function concat_append(ts, node)
local n = #ts
if node.kind == "ConcatenateExpression" then
for k = 1, #node.terms do ts[n + k] = node.terms[k] end
else
ts[n + 1] = node
end
end
function AST.expr_binop(ast, op, expa, expb, line)
local binop_body = (op ~= '..' and { operator = op, left = expa, right = expb, line = line })
if binop_body then
if op == 'and' or op == 'or' then
return build("LogicalExpression", binop_body)
else
return build("BinaryExpression", binop_body)
end
else
local terms = { }
concat_append(terms, expa)
concat_append(terms, expb)
return build("ConcatenateExpression", { terms = terms, line = expa.line })
end
end
function AST.expr_algebra_binop(ast, op, expa, expb, line)
local binop_body = (op ~= '..' and { operator = op, left = expa, right = expb, line = line })
if binop_body then
if op == 'and' or op == 'or' then
error('not yet implemented')
else
return build("BinaryAlgebraExpression", binop_body)
end
else
error('not yet implemented')
end
end
function AST.identifier(ast, name)
return ident(name)
end
function AST.expr_method_call(ast, v, key, args, line)
local m = ident(key)
return build("SendExpression", { receiver = v, method = m, arguments = args, line = line })
end
function AST.expr_function_call(ast, v, args, line)
return build("CallExpression", { callee = v, arguments = args, line = line })
end
function AST.return_stmt(ast, exps, line)
return build("ReturnStatement", { arguments = exps, line = line })
end
function AST.break_stmt(ast, line)
return build("BreakStatement", { line = line })
end
function AST.label_stmt(ast, name, line)
return build("LabelStatement", { label = name, line = line })
end
function AST.new_statement_expr(ast, expr, line)
return build("ExpressionStatement", { expression = expr, line = line })
end
function AST.if_stmt(ast, tests, cons, else_branch, line)
return build("IfStatement", { tests = tests, cons = cons, alternate = else_branch, line = line })
end
function AST.do_stmt(ast, body, line, lastline)
return build("DoStatement", { body = body, line = line, lastline = lastline})
end
function AST.while_stmt(ast, test, body, line, lastline)
return build("WhileStatement", { test = test, body = body, line = line, lastline = lastline })
end
function AST.repeat_stmt(ast, test, body, line, lastline)
return build("RepeatStatement", { test = test, body = body, line = line, lastline = lastline })
end
function AST.for_stmt(ast, var, init, last, step, body, line, lastline)
local for_init = build("ForInit", { id = var, value = init, line = line })
return build("ForStatement", { init = for_init, last = last, step = step, body = body, line = line, lastline = lastline })
end
function AST.for_iter_stmt(ast, vars, exps, body, line, lastline)
local names = build("ForNames", { names = vars, line = line })
return build("ForInStatement", { namelist = names, explist = exps, body = body, line = line, lastline = lastline })
end
function AST.goto_stmt(ast, name, line)
return build("GotoStatement", { label = name, line = line })
end
local function new_scope(parent_scope)
return {
vars = { },
parent = parent_scope,
}
end
function AST.var_declare(ast, name)
local id = ident(name)
ast.current.vars[name] = true
return id
end
function AST.fscope_begin(ast)
ast.current = new_scope(ast.current)
end
function AST.fscope_end(ast)
ast.current = ast.current.parent
end
local ASTClass = { __index = AST }
local function new_ast()
return setmetatable({ }, ASTClass)
end
return { New = new_ast }