-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_codegen.lua
More file actions
425 lines (378 loc) · 7.61 KB
/
Copy pathlua_codegen.lua
File metadata and controls
425 lines (378 loc) · 7.61 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
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
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local type = type
local strfmt = string.format
local tinsert, tremove = table.insert, table.remove
local codegen_functions
local function loadwrapper(src, name, env)
if setfenv then
local chunk, err = loadstring(src, name)
if chunk and env then setfenv(chunk, env) end
return chunk, err
else
return load(src, name, "bt", env)
end
end
local function fix_line(state, node, ostream)
if node.line then
while node.line > state.line do
ostream'\n'
state.line = state.line + 1
end
end
end
local function codegen(state, node, ostream)
local f = codegen_functions[node[1]]
if not f then
error("invalid ast node: "..node[1])
end
return f(state, node, ostream)
end
local function codegen_list(state, node, o, i_start, i_end)
local first = true
for i = i_start, i_end do
if first then
first = false
else
o','
end
codegen(state, node[i], o)
end
end
local function str_escape_fn(c)
return ('\\%03d'):format(c:byte())
end
codegen_functions = {
["parens"] = function (state, node, o)
o"("
codegen(state, node[2], o)
o")"
end,
["void"] = function (state, node, o)
end,
["comma"] = function (state, node, o)
for i = 2, #node do
if i > 2 then o',' end
codegen(state, node[i], o)
end
end,
["if"] = function (state, node, o)
fix_line(state, node, o)
o" if "
codegen(state, node[2], o)
o" then "
codegen(state, node[3], o)
local i = 4
while node[i+1] do
o" elseif "
codegen(state, node[i], o)
o" then "
codegen(state, node[i+1], o)
i=i+2
end
if node[i] then
o" else "
codegen(state, node[i], o)
end
o" end "
end,
["while"] = function (state, node, o)
fix_line(state, node, o)
o" while "
codegen(state, node[2], o)
o" do "
codegen(state, node[3], o)
o" end "
end,
["repeat"] = function (state, node, o)
fix_line(state, node, o)
o" repeat "
codegen(state, node[2], o)
o" until "
codegen(state, node[3], o)
end,
["for_num"] = function (state, node, o)
fix_line(state, node, o)
o" for "
o(node[2])
o"="
codegen(state, node[3], o)
o","
codegen(state, node[4], o)
if node[5] then
o","
codegen(state, node[5], o)
end
o" do "
codegen(state, node[6], o)
o" end "
end,
["for_iter"] = function (state, node, o)
fix_line(state, node, o)
o" for "
codegen(state, node[2], o)
o" in "
codegen(state, node[3], o) -- TODO list
o" do "
codegen(state, node[4], o)
o" end "
end,
["do"] = function (state, node, o)
fix_line(state, node, o)
o" do "
codegen(state, node[2], o)
o" end "
end,
["function"] = function (state, node, o)
fix_line(state, node, o)
o"(function("
for i, v in ipairs(node[2].lua) do
if i ~= 1 then o"," end
o(v)
end
o")"
codegen(state, node[4], o)
o" end)"
end,
["binop"] = function (state, node, o)
o"("
codegen(state, node[3], o)
o' '
o(node[2])
o' '
codegen(state, node[4], o)
o")"
end,
["unop"] = function (state, node, o)
o"("
o(node[2])
o' '
codegen(state, node[3], o)
o")"
end,
["field"] = function (state, node, o)
codegen(state, node[2], o)
o"."
o(node[3])
end,
["num_field"] = function (state, node, o)
codegen(state, node[2], o)
o"["
o(tostring(node[3]))
o']'
end,
["index"] = function (state, node, o)
codegen(state, node[2], o)
o"["
codegen(state, node[3], o)
o"]"
end,
["call"] = function (state, node, o)
codegen(state, node[2], o)
o"("
codegen_list(state, node, o, 3, #node)
o")"
end,
["method_call"] = function (state, node, o)
codegen(state, node[2], o)
o":"
o(node[3])
o"("
codegen_list(state, node, o, 4, #node)
o")"
end,
["assign"] = function (state, node, o)
codegen(state, node[2], o)
o"="
codegen(state, node[4], o)
end,
["local"] = function (state, node, o)
fix_line(state, node, o)
o" local "
codegen(state, node[2], o)
if node[4] then
o"="
codegen(state, node[4], o)
end
end,
["goto"] = function (state, node, o)
fix_line(state, node, o)
o" goto "
o(node[2])
end,
["label"] = function (state, node, o)
fix_line(state, node, o)
o"::"
o(node[2])
o"::"
end,
["break"] = function (state, node, o)
fix_line(state, node, o)
o" break "
end,
["return"] = function (state, node, o)
fix_line(state, node, o)
o" return "
codegen_list(state, node, o, 2, #node)
end,
["seq"] = function (state, node, o)
for i = 2, #node do
if not (node[i][1] == 'void' or (node[i][1] == 'seq' and not node[i][2])) then
if i ~= 2 then o';' end
codegen(state, node[i], o)
end
end
end,
["nil"] = function (state, node, o)
fix_line(state, node, o)
o'(nil)'
end,
["true"] = function (state, node, o)
fix_line(state, node, o)
o'(true)'
end,
["false"] = function (state, node, o)
fix_line(state, node, o)
o'(false)'
end,
["boolean"] = function (state, node, o)
fix_line(state, node, o)
o'('
o(node[2] and 'true' or 'false')
o')'
end,
["number"] = function (state, node, o)
fix_line(state, node, o)
o"("
o(node[2])
o")"
end,
["float"] = function (state, node, o)
fix_line(state, node, o)
o"("
o(node[2])
o")"
end,
["int"] = function (state, node, o)
fix_line(state, node, o)
o"("
o(node[2])
o")"
end,
["string"] = function (state, node, o)
fix_line(state, node, o)
o'"'
o(node[2]:gsub('[^\32\33\35-\126]', str_escape_fn))
o'"'
end,
["literal"] = function (state, node, o)
fix_line(state, node, o)
local v = node[2]
local t = type(v)
o"("
if t == "number" then
o(tostring(v))
elseif t == "string" then
o(strfmt("%q", v))
elseif t == "boolean" then
o(v and "true" or "false")
elseif t == "nil" then
o"nil"
end
o")"
end,
["table"] = function (state, node, o)
fix_line(state, node, o)
o"({"
for i = 2, #node, 2 do
if node[i] then
o"["
codegen(state, node[i], o)
o"]="
end
codegen(state, node[i+1], o)
o","
end
o"})"
end,
["name"] = function (state, node, o)
fix_line(state, node, o)
o(node[2])
end,
["vararg"] = function (state, node, o)
fix_line(state, node, o)
o'...'
end,
["quote"] = function (state, node, o)
tinsert(state.quote_stack, {
'quote',
})
local function quote_rec(node)
o"{"
if node[1] == 'table' then
o'hash='
quote_rec(node.hash)
o','
end
for i, v in ipairs(node) do
if i ~= 1 then
o","
end
local t = type(v)
if t == "string" then
o(strfmt("%q", v))
elseif t == "booelan" then
o(v and "true" or "false")
elseif t == "nil" then
o"nil"
elseif t == "number" then
o(tostring(v))
else
quote_rec(v)
end
end
o"}"
end
quote_rec(node[2])
tremove(state.quote_stack)
end,
["dequote"] = function (state, node, o)
local level = state.dequote_level + 1
state.dequote_level = level
if state.safemode then
error("running code at compile-time is not allowed")
end
local i, t = 1, {}
local function ostream(str)
i, t[i] = i+1, str
end
codegen(state, node[2], ostream)
local env = state.dequote_envs[level]
if not env then
env = setmetatable({}, {__index=_G})
state.dequote_envs[level] = env
end
local chunk, err = loadwrapper(table.concat(t), "(dequoted block)", env)
if not chunk then
error("error loading dequoted block: "..err)
end
local success, result = pcall(chunk)
if not success then
error("error running dequoted block: "..tostring(err))
end
if type(result) ~= 'table' then
result = {'literal', result}
end
codegen(state, result, o)
tremove(state.quote_stack)
end,
}
return function (ast, ostream)
local state = {
safemode = false,
quote_stack = {},
line = 1,
}
codegen(state, ast, ostream)
end