-
Notifications
You must be signed in to change notification settings - Fork 3
/
colonize.coffee
452 lines (407 loc) · 15.7 KB
/
colonize.coffee
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
jast = require 'jast'
fs = require 'fs'
# detects if "continue" is used to address the current loop, named or otherwise
keywords = ["end"]
usesLocalContinue = (stat) ->
continueWalker = (n) ->
switch n.type
when "while-stat", "do-while-stat", "for-stat", "for-in-stat", "label-stat", "closure-context"
return []
when "continue-stat"
return [true]
else
return jast.walk(n, continueWalker)
return continueWalker(stat).length > 0
usesLabeledContinue = (stat, label) ->
continueWalker = (n) ->
switch n.type
when "closure-context"
return []
when "label-stat"
return if n.name == label then [] else jast.walk(n, continueWalker)
when "continue-stat"
return if n.label == label then [true] else []
else
return jast.walk(n, continueWalker)
return continueWalker(stat).length > 0
usesContinue = (stat, label) ->
return usesLocalContinue(stat) or (label and usesLabeledContinue(stat, label))
fixRef = (str) ->
if str in keywords
return '_K_' + str
return str.replace(/_/g, '__').replace(/\$/g, '_S')
isSafe = (str) ->
return str not in keywords
truthy = (cond) ->
if cond.type in ["not-op-expr", "lt-op-expr", "lte-op-expr", "gt-op-expr", "gte-op-expr", "eq-op-expr", "eqs-op-expr", "neq-op-expr", "neqs-op-expr", "instanceof-op-expr", "in-op-expr"]
return colonize(cond)
else
return "_JS._truthy(#{colonize(cond)})"
colonizeContext = (ctx) ->
# variable declarations
vars = [].concat((jast.localVars(stat) for stat in ctx.stats)...)
ret = if vars.length then "local #{(fixRef(x) for x in vars).join(', ')};\n" else ""
# hoist functions, then statements
funcs = (stat for stat in ctx.stats when stat.type == 'defn-stat')
stats = (stat for stat in ctx.stats when stat and not (stat.type == 'defn-stat'))
ret += (colonize(stat) for stat in funcs.concat(ctx.stats)).join('\n')
return ret
#
# colonize function
#
labels = []
loops = []
colonize = (n) ->
return "" if not n
switch n.type
# contexts
when "script-context"
return colonizeContext(n)
when "closure-context"
{ln, name, args, stats} = n
# fix references
name = fixRef(name) if name; args = (fixRef(x) for x in args)
# assign self-named function reference only when necessary
namestr = ""
if name in jast.undefinedRefs(n)
namestr = "local #{name} = debug.getinfo(1, 'f').func;\n"
loopsbkp = loops
loops = []
if jast.usesArguments(n)
ret = "_JS._func(function (this, ...)\n" + namestr +
"local arguments = _JS._arr((function (...) return arg; end)(...));\n" +
(if args.length
"local #{args.join(', ')} = ...;\n"
else "") +
colonizeContext(n) + "\n" +
"end)"
else
ret = "_JS._func(function (#{['this'].concat(args).join(', ')})\n" + namestr +
colonizeContext(n) + "\n" +
"end)"
loops = loopsbkp
return ret
# literals
when "num-literal"
{ln, value} = n
return JSON.stringify(value)
when "str-literal"
{ln, value} = n
return "(#{JSON.stringify(value)})"
when "obj-literal"
{ln, props} = n
values = ("[\"#{value.replace('\"', '\\\"')}\"]=#{colonize(expr)}" for {value, expr} in props)
return "_JS._obj({#{values.join(', ')}})"
when "array-literal"
{ln, exprs} = n
return "_JS._arr({})" unless exprs.length
return "_JS._arr({[0]=" + [].concat(colonize(x) for x in exprs).join(', ') + "})"
when "undef-literal"
return "nil"
when "null-literal"
return "_JS._null"
when "boolean-literal"
{ln, value} = n
return if value then "true" else "false"
return value
when "func-literal"
{ln, closure} = n
return "(" + colonize(closure) + ")"
when "regexp-literal"
{ln, expr, flags} = n
return "_JS.Regexp(#{JSON.stringify(expr)})"
# operations
when "and-op-expr", "or-op-expr", "eq-op-expr", "neq-op-expr", "eqs-op-expr", "neqs-op-expr", "sub-op-expr", "mul-op-expr", "div-op-expr", "mod-op-expr", "lt-op-expr", "lte-op-expr", "gt-op-expr", "gte-op-expr"
{ln, left, right} = n
return "(" + colonize(left) + " " + (
#TODO and and or need explicit functions cause they're boolean ish
"and-op-expr": "and"
"or-op-expr": "or"
"eq-op-expr": "=="
"neq-op-expr": "~="
"eqs-op-expr": "=="
"neqs-op-expr": "~="
"sub-op-expr": "-"
"mul-op-expr": "*"
"div-op-expr": "/"
"mod-op-expr": "%"
"lt-op-expr": "<"
"lte-op-expr": "<="
"gt-op-expr": ">"
"gte-op-expr": ">=")[n.type] + " " + colonize(right) + ")"
when "num-op-expr"
{ln, left, right} = n
return "(#{colonize(left)}+0)"
when "add-op-expr"
{ln, left, right} = n
return "_JS._add(#{colonize(left)}, #{colonize(right)})"
when "lsh-op-expr", "bit-or-op-expr"
{ln, left, right} = n
return "_JS._bit." + (
"lsh-op-expr": "lshift"
"bit-or-op-expr": "bor")[n.type] + "(#{colonize(left)}, #{colonize(right)})"
when "neg-op-expr", "not-op-expr"
{ln, expr} = n
return (
"neg-op-expr": "-"
"not-op-expr": "not")[n.type] + colonize(expr)
when "typeof-op-expr"
{ln, expr} = n
return "_JS._typeof(#{colonize(expr)})"
when "void-op-expr"
{ln, expr} = n
return "_JS._void(#{colonize(expr)})"
when "seq-op-expr"
{ln, left, right} = n
return "({#{colonize(left)}, #{colonize(right)}})[2]"
# expressions
when "this-expr"
return "this"
when "scope-ref-expr"
{ln, value} = n
return fixRef(value)
when "static-ref-expr"
{ln, base, value} = n
if not isSafe(value)
return colonize({type: "dyn-ref-expr", ln: ln, base: base, index: {type: "str-literal", ln: ln, value: value}})
return "(#{colonize(base)}).#{value}"
when "dyn-ref-expr"
{ln, base, index} = n
return "(#{colonize(base)})[#{colonize(index)}]"
when "static-method-call-expr"
{ln, base, value, args} = n
return "#{colonize(base)}:#{value}(" + (colonize(x) for x in args).join(', ') + ")"
when "dyn-method-call-expr"
{ln, base, index, args} = n
return "(function () local _b = #{colonize(base)}; return _b[#{colonize(index)}](" + ["_b"].concat(colonize(x) for x in args).join(', ') + "); end)()"
when "scope-delete-expr"
{ln, value} = n
return colonize({type: "scope-assign-expr", ln: ln, value: value, expr: {type: "undef-literal", ln: ln}})
when "static-delete-expr"
{ln, base, value} = n
if not isSafe(value)
return colonize({type: "dyn-delete-expr", ln: ln, base: base, index: {type: "str-literal", ln: ln, value: value}})
return colonize({type: "static-assign-expr", ln: ln, base: base, value: value, expr: {type: "undef-literal", ln: ln}})
when "dyn-delete-expr"
{ln, base, index} = n
return colonize({type: "dyn-assign-expr", ln: ln, base: base, index: index, expr: {type: "undef-literal", ln: ln}})
when "new-expr"
{ln, constructor, args} = n
return "_JS._new(" + [colonize(constructor)].concat(colonize(x) for x in args).join(', ') + ")"
when "call-expr"
{ln, expr, args} = n
return "(" + colonize(expr) + ")(" +
["this"].concat(colonize(x) for x in args).join(', ') + ")"
when "scope-assign-expr"
{ln, value, expr} = n
return "(function () local _r = #{colonize(expr)}; #{fixRef(value)} = _r; return _r end)()"
when "static-assign-expr"
{ln, base, value, expr} = n
if not isSafe(value)
return colonize({type: "dyn-assign-expr", ln: ln, base: base, index: {type: "str-literal", ln: ln, value: value}})
return "(function () local _r = #{colonize(expr)}; #{colonize(base)}.#{value} = _r; return _r end)()"
when "dyn-assign-expr"
{ln, base, index, expr} = n
return "(function () local _r = #{colonize(expr)}; #{colonize(base)}[#{colonize(index)}] = _r; return _r end)()"
when "instanceof-op-expr"
{ln, expr} = n
return "_JS._instanceof(#{colonize(expr)})"
when "if-expr"
{ln, expr, thenExpr, elseExpr} = n
return "(#{colonize(expr)} and {#{colonize(thenExpr)}} or {#{colonize(elseExpr)}})[1]"
when "scope-inc-expr"
{ln, pre, inc, value} = n
value = fixRef(value)
return "(function () " +
(if pre then "local _r = #{value}; #{value} = #{value} + #{inc}; return _r"
else "#{value} = #{value} + #{inc}; return #{value}") +
" end)()"
when "static-inc-expr"
{ln, pre, inc, base, value} = n
return "(function () " +
(if pre then "local _b, _r = #{colonize(base)}, _b.#{value}; _b.#{value} = _r + #{inc}; return _r"
else "local _b = #{colonize(base)}; _b.#{value} = _b.#{value} + #{inc}; return _b.#{value}") +
" end)()"
when "dyn-inc-expr"
{ln, pre, inc, base, index} = n
return "(function () " +
(if pre then "local _b, _i, _r = #{colonize(base)}, #{colonize(index)}, _b[_i]; _b[_i] = _r + #{inc}; return _r"
else "local _b, _i = #{colonize(base)}, #{colonize(index)}; _b[_i] = _b[_i] + #{inc}; return _b[_i]") +
" end)()"
# statements
when "block-stat"
{ln, stats} = n
return (colonize(x) for x in stats).join('\n')
when "expr-stat"
{ln, expr} = n
switch expr.type
when "scope-inc-expr"
{ln, pre, inc, value} = expr
value = fixRef(value)
return "#{value} = #{value} + #{inc};"
when "static-inc-expr"
{ln, pre, inc, base, value} = expr
return "local _b = #{colonize(base)}; _b.#{value} = _b.#{value} + #{inc};"
when "dyn-inc-expr"
{ln, pre, inc, base, index} = expr
return "local _b, _i = #{colonize(base)}, #{index}; _b[_i] = _b[_i] + #{inc};"
when "pre-dec-op-expr", "post-dec-op-expr"
{ln, expr} = expr
when "scope-assign-expr"
{ln, value, expr} = expr
return "#{value} = #{colonize(expr)};"
when "static-assign-expr"
{ln, base, value, expr} = expr
if not isSafe(value)
return colonize({type: "expr-stat", ln: ln, expr: {type: "dyn-assign-expr", ln: ln, base: base, index: {type: "str-literal", ln: ln, value: value}, expr: expr}})
return "#{colonize(base)}.#{value} = #{colonize(expr)};"
when "dyn-assign-expr"
{ln, base, index, expr} = expr
return "#{colonize(base)}[#{colonize(index)}] = #{colonize(expr)};"
when "call-expr", "static-method-call-expr"
return "#{colonize(expr)};"
when "seq-op-expr"
{ln, left, right} = expr
return colonize({type: "expr-stat", ln: ln, expr: left}) + "\n" + colonize({type: "expr-stat", ln: ln, expr: right})
return "_JS._void(" + colonize(expr) + ");"
when "ret-stat"
{ln, expr} = n
# wrap in conditional to allow returns to precede statements
return "if true then return" + (if expr then " " + colonize(expr) else "") + "; end;"
when "if-stat"
{ln, expr, thenStat, elseStat} = n
return "if _JS._truthy(#{colonize(expr)}) then\n#{colonize(thenStat)}\n" +
(if elseStat then "else\n#{colonize(elseStat)}\n" else "") + "end"
# loops
when "while-stat"
{ln, expr, stat} = n
ascend = (x[1] for x in loops when x[0] != 'try' and x[1])
name = labels.pop() or ""
cont = stat and usesContinue(stat, name)
loops.push(["while", name, cont])
ret = "while #{truthy(expr)} do\n" +
(if cont then "local _c#{name} = nil; repeat\n" else "") +
"#{colonize(stat)}\n" +
(if cont then "until true;\nif _c#{name} == _JS._break #{[''].concat(ascend).join(' or _c')} then break end\n" else "") +
"end\n" +
(if ascend.length then "if _c#{ascend.join(' or _c')} then break end\n" else '')
loops.pop()
return ret
when "do-while-stat"
{ln, expr, stat} = n
ascend = [""].concat(x[1] for x in loops when x[0] != 'try' and x[1]).join(' or ')
name = labels.pop() or ""
cont = stat and usesContinue(stat, name)
loops.push(["do", name, cont])
ret = "repeat\n" +
(if cont then "local _c#{name} = nil; repeat\n" else "") +
"#{colonize(stat)}\n" +
(if cont then "until true;\nif _c#{name} == _JS._break #{ascend} then break end\n" else "") +
"until not #{truthy(expr)};"
loops.pop()
return ret
when "for-stat"
{ln, init, expr, step, stat} = n
expr = {type: "boolean-literal", ln: ln, value: true} unless expr
ascend = [""].concat(x[1] for x in loops when x[0] != 'try' and x[1]).join(' or ')
name = labels.pop() or ""
cont = stat and usesContinue(stat, name)
loops.push(["for", name, cont])
ret = (if init then (if init.type == "var-stat" then colonize(init) else colonize({type: "expr-stat", ln: ln, expr: init}) + "\n") else "") +
"while #{truthy(expr)} do\n" +
(if cont then "local _c#{name} = nil; repeat\n" else "") +
colonize(stat) + "\n" +
(if cont then "until true;\n" else "") +
(if step then colonize({type: "expr-stat", ln: step.ln, expr: step}) + "\n" else "") +
# _cname = _JS._break OR ANYTHING ABOVE IT ~= nil then...
(if cont then "if _c#{name} == _JS._break #{ascend} then break end\n" else "") +
"end"
loops.pop()
return ret
when "for-in-stat"
{ln, isvar, value, expr, stat} = n
return "for #{value},_v in pairs(#{colonize(expr)}) do\n#{colonize(stat)}\nend"
when "switch-stat"
{ln, expr, cases} = n
name = labels.pop() or ""
loops.push(["switch", name, false])
ret = "repeat\n" +
(if cases.length then ("local _#{i}#{if v then ' = ' + colonize(v) else ''}; " for i, [v, _] of cases).join('') else '') +
"local _r = #{colonize(expr)};\n" +
(for i, [_, stats] of cases
if _?
"if _r == _#{i} then\n" + (colonize(x) for x in stats).concat(if cases[Number(i)+1] and (not stats.length or stats[-1..][0].type != "break-stat") then ["_r = _#{Number(i)+1};"] else []).join("\n") + "\nend"
else
(colonize(x) for x in stats).join("\n")
).join("\n") + "\n" +
"until true"
loops.pop()
return ret
when "throw-stat"
{ln, expr} = n
return "error(#{colonize(expr)});"
when "try-stat"
{ln, tryStat, catchBlock, finallyStat} = n
l = loops.push(["try", null])-1
ret = """
local _e = nil
local _s, _r = xpcall(function ()
#{colonize(tryStat)}
#{if tryStat.stats[-1..][0].type != 'ret-stat' then "return _JS._cont" else ""}
end, function (err)
_e = err
end)
if _s == false then
#{if catchBlock then catchBlock.value + " = _e;\n" + colonize(catchBlock.stat) else ""}
#{if finallyStat then colonize(finallyStat) else ""}
elseif _r == _JS._break then
#{if loops[-2..-1][0]?[1] in [null, "try"] then "return _JS._break;" else "break;" }
elseif _r ~= _JS._cont then
return _r
end"""
loops.pop()
return ret
when "var-stat"
{ln, vars} = n
# vars already declared, just assign here
return ("#{fixRef(v.value)} = #{colonize(v.expr)};" for v in vars when v.expr).join(' ') + '\n'
when "defn-stat"
{ln, closure} = n
return "#{fixRef(closure.name)} = (" + colonize(closure) + ");\n"
when "label-stat"
{ln, name, stat} = n
labels.push(name)
#TODO change stat to do { } while(false) unless of certain type;
# this makes this labels array work
ret = "#{colonize(stat)}"
labels.pop()
return ret
when "break-stat"
{ln, label} = n
label = label or (x for x in loops when loops[0] != 'try')[-1..][0]?[1] or ""
return "_c#{label} = _JS._break; " +
(if loops[-1..][0][0] == "try" then "return _JS._break;" else "break;")
when "continue-stat"
#TODO _c down the stack is false until the main one
{ln, label} = n
label = label or (x for x in loops when loops[0] != 'try')[-1..][0]?[1] or ""
return "_c#{label} = _JS._cont; " +
(if loops[-1..][0][0] == "try" then "return _JS._break;" else "break;")
# fallback
else
console.log("[ERROR] Undefined compile: " + n.type)
return "####{n}###"
##########################
###########################
###########################
code = fs.readFileSync(process.argv[2...][0], 'utf-8')
mask = ['string', 'math']
locals = ['this', 'Object', 'Array', 'String', 'Math', 'require', 'print']
console.log "local _JS = require('colony-js');"
console.log "local #{mask.join(', ')} = #{('nil' for k in mask).join(', ')};"
console.log "local #{locals.join(', ')} = #{('_JS.'+k for k in locals).join(', ')};"
console.log "local _exports = {}; local exports = _exports;"
console.log ""
console.log colonize(jast.parse(code))
console.log ""
console.log "return _exports;"