-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathstatement.moon
More file actions
552 lines (439 loc) · 14.1 KB
/
Copy pathstatement.moon
File metadata and controls
552 lines (439 loc) · 14.1 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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import Transformer from require "moonscript.transform.transformer"
import NameProxy, LocalName, is_name_proxy from require "moonscript.transform.names"
import Run, transform_last_stm, implicitly_return, last_stm, find_continues
from require "moonscript.transform.statements"
types = require "moonscript.types"
import build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
from types
import insert from table
destructure = require "moonscript.transform.destructure"
import construct_comprehension from require "moonscript.transform.comprehension"
import unpack from require "moonscript.util"
import user_error from require "moonscript.errors"
-- binds the continue statements in a loop body to a loop-local flag,
-- wrapping the body in a repeat block that continue can break out of
apply_continue = (body) ->
continues = find_continues body
return body unless continues[1]
continue_name = NameProxy "continue"
-- attach the name in place, the continue transformer expands the node when
-- the compiler reaches it so cascading transforms still see a continue
for node in *continues
node[2] = continue_name
last_type = ntype last_stm body
-- a trailing continue expands to a break, which must also be enclosed
repeat_body = if types.terminating[last_type] or last_type == "continue"
{ {"do", body} }
else
body
insert repeat_body, {"assign", {continue_name}, {"true"}}
{
{"assign", {continue_name}, {"false"}}
{"repeat", "true", repeat_body}
{"if", {"not", continue_name}, {
{"break"}
}}
}
-- this mutates body searching for assigns
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
for i=start,#body
stm = body[i]
continue if stm == nil
stm = @transform.statement stm
body[i] = stm
switch stm[1]
when "assign", "declare", "declare_constants"
for name in *stm[2]
if ntype(name) == "ref"
insert out, name
elseif type(name) == "string"
-- TODO: don't use string literal as ref
insert out, name
when "group"
extract_declarations @, stm[2], 1, out
out
-- the names a declare statement must list for an assignment's names,
-- destructuring literals are replaced by the names they extract so the
-- declaration works when the assignment happens in a deeper scope
extract_declare_names = (names) ->
out = {}
for name in *names
if ntype(name) == "table"
for tuple in *destructure.extract_assign_names name
insert out, tuple[1]
else
insert out, name
out
expand_elseif_assign = (ifstm) ->
for i = 4, #ifstm
case = ifstm[i]
if ntype(case) == "elseif" and ntype(case[2]) == "assign"
split = { unpack ifstm, 1, i - 1 }
insert split, {
"else", {
{"if", case[2], case[3], unpack ifstm, i + 1}
}
}
return split
ifstm
Transformer {
transform: (tuple) =>
{_, node, fn} = tuple
fn node
root_stms: (body) =>
transform_last_stm body, implicitly_return @
return: (node) =>
ret_val = node[2]
ret_val_type = ntype ret_val
if ret_val_type == "explist" and #ret_val == 2
ret_val = ret_val[2]
ret_val_type = ntype ret_val
if types.cascading[ret_val_type]
return implicitly_return(@) ret_val
-- flatten things that create block exp
if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension"
-- TODO: clean this up
Value = require "moonscript.transform.value"
ret_val = Value\transform_once @, ret_val
if ntype(ret_val) == "block_exp"
return build.group transform_last_stm ret_val[2], (stm)->
{"return", stm}
node[2] = ret_val
node
declare_glob: (node) =>
names = extract_declarations @
if node[2] == "^"
names = for name in *names
str_name = if ntype(name) == "ref"
name[2]
else
name
continue unless str_name\match "^%u"
name
{"declare", names}
assign: (node) =>
names, values = unpack node, 2
num_values = #values
num_names = #names
-- special code simplifications for single assigns
if num_names == 1 and num_values == 1
first_value = values[1]
first_name = names[1]
first_type = ntype first_value
-- reduce colon stub chain to block exp
if first_type == "chain"
-- TODO: clean this up
Value = require "moonscript.transform.value"
first_value = Value\transform_once @, first_value
first_type = ntype first_value
switch ntype first_value
when "block_exp"
block_body = first_value[2]
idx = #block_body
last = block_body[idx]
assign = build.assign_one first_name, last
-- position of the value being assigned, the name is from the line
-- of the enclosing assign
assign[-1] = last[-1] if type(last) == "table"
block_body[idx] = assign
return build.group {
{"declare", extract_declare_names {first_name}}
{"do", block_body}
}
when "comprehension", "tblcomprehension", "foreach", "for", "while"
-- TODO: clean this up
Value = require "moonscript.transform.value"
return build.assign_one first_name, Value\transform_once @, first_value
else
values[1] = first_value
-- bubble cascading assigns
transformed = if num_values == 1
value = values[1]
t = ntype value
if t == "decorated"
value = @transform.statement value
t = ntype value
if types.cascading[t]
ret = (stm) ->
if is_value stm
assign = {"assign", names, {stm}}
-- position of the value being assigned, the name is from the
-- line of the enclosing assign
assign[-1] = stm[-1] if type(stm) == "table"
assign
else
stm
build.group {
{"declare", extract_declare_names names}
@transform.statement value, ret, node
}
-- the assigns bubbled into the value's branches handle their own
-- destructuring when they are transformed
return transformed if transformed
if destructure.has_destructure names
return destructure.split_assign @, node
node
-- apply_continue binds the loop's flag to the node, a continue without one
-- is outside of any loop
continue: (node) =>
continue_name = node[2]
user_error "continue must be inside of a loop", node[-1] unless continue_name
build.group {
build.assign_one continue_name, "true"
{"break"}
}
export: (node) =>
-- assign values if they are included
if #node > 2
if node[2] == "class"
cls = smart_node node[3]
build.group {
{"export", {cls.name}}
cls
}
else
-- pull out vawlues and assign them after the export
build.group {
{ "export", node[2] }
build.assign {
names: node[2]
values: node[3]
}
}
else
nil
update: (node) =>
name, op, exp = unpack node, 2
op_final = op\match "^(.+)=$"
error "Unknown op: "..op if not op_final
local lifted
if ntype(name) == "chain"
lifted = {}
new_chain = for part in *name[3,]
if ntype(part) == "index"
proxy = NameProxy "update"
table.insert lifted, { proxy, part[2] }
{ "index", proxy }
else
part
if next lifted
name = {name[1], name[2], unpack new_chain}
exp = {"parens", exp} unless value_is_singular exp
out = build.assign_one name, {"exp", name, op_final, exp}
if lifted and next lifted
names = [l[1] for l in *lifted]
values = [l[2] for l in *lifted]
out = build.group {
{"assign", names, values}
out
}
out
import: (node) =>
names, source = unpack node, 2
dest_names = {}
table_values = for name in *names
dest_name = if ntype(name) == "colon"
name[2]
else
name
insert dest_names, dest_name
{{"key_literal", name}, dest_name}
dest = { "table", table_values }
build.group {
{ "assign", {dest}, {source}, [-1]: node[-1] }
{ "declare_constants", dest_names, [-1]: node[-1] }
}
comprehension: (node, action) =>
exp, clauses = unpack node, 2
action = action or (exp) -> {exp}
construct_comprehension action(exp), clauses
do: (node, ret) =>
node[2] = transform_last_stm node[2], ret if ret
node
decorated: (node) =>
stm, dec = unpack node, 2
wrapped = switch dec[1]
when "if"
cond, fail = unpack dec, 2
fail = { "else", { fail } } if fail
{ "if", cond, { stm }, fail }
when "unless"
{ "unless", dec[2], { stm } }
when "comprehension"
{ "comprehension", stm, dec[2] }
else
error "Unknown decorator " .. dec[1]
if ntype(stm) == "assign"
wrapped = build.group {
build.declare names: [name for name in *extract_declare_names(stm[2]) when ntype(name) == "ref"]
wrapped
}
wrapped
unless: (node) =>
clause = node[2]
if ntype(clause) == "assign"
if destructure.has_destructure clause[2]
error "destructure not allowed in unless assignment"
build.do {
clause
{ "if", {"not", clause[2][1]}, unpack node, 3 }
}
else
{ "if", {"not", {"parens", clause}}, unpack node, 3 }
if: (node, ret) =>
-- expand assign in cond
if ntype(node[2]) == "assign"
assign, body = unpack node, 2
if destructure.has_destructure assign[2]
name = NameProxy "des"
body = {
destructure.build_assign @, assign[2][1], name
build.group node[3]
}
return build.do {
build.assign_one name, assign[3][1]
{"if", name, body, unpack node, 4}
}
else
name = assign[2][1]
return build.do {
assign
{"if", name, unpack node, 3}
}
node = expand_elseif_assign node
-- apply cascading return decorator
if ret
smart_node node
-- mutate all the bodies
node['then'] = transform_last_stm node['then'], ret
for i = 4, #node
case = node[i]
body_idx = #node[i]
case[body_idx] = transform_last_stm case[body_idx], ret
node
with: (node, ret) =>
exp, block = unpack node, 2
copy_scope = true
local scope_name, named_assign
if last = last_stm block
ret = false if types.terminating[ntype(last)]
if ntype(exp) == "assign"
names, values = unpack exp, 2
first_name = names[1]
if ntype(first_name) == "ref"
scope_name = first_name
named_assign = exp
exp = values[1]
copy_scope = false
else
scope_name = NameProxy "with"
exp = values[1]
values[1] = scope_name
named_assign = {"assign", names, values}
elseif @is_local exp
scope_name = exp
copy_scope = false
scope_name or= NameProxy "with"
out = build.do {
copy_scope and build.assign_one(scope_name, exp) or NOOP
named_assign or NOOP
Run => @set "scope_var", scope_name
unpack block
}
if ret
table.insert out[2], ret scope_name
out
foreach: (node, _) =>
smart_node node
source = unpack node.iter
destructures = {}
node.names = for i, name in ipairs node.names
if ntype(name) == "table"
with proxy = NameProxy "des"
insert destructures, destructure.build_assign @, name, proxy
else
name
if next destructures
insert destructures, build.group node.body
node.body = destructures
if ntype(source) == "unpack"
list = source[2]
index_name = NameProxy "index"
list_name = @is_local(list) and list or NameProxy "list"
slice_var = nil
bounds = if is_slice list
slice = list[#list]
table.remove list
table.remove slice, 1
list_name = list if @is_local list
slice[2] = if slice[2] and slice[2] != ""
max_tmp_name = NameProxy "max"
slice_var = build.assign_one max_tmp_name, slice[2]
{"exp", max_tmp_name, "<", 0
"and", {"length", list_name}, "+", max_tmp_name
"or", max_tmp_name }
else
{"length", list_name}
slice
else
{1, {"length", list_name}}
names = [is_name_proxy(n) and n or LocalName(n) or n for n in *node.names]
return build.group {
list_name != list and build.assign_one(list_name, list) or NOOP
slice_var or NOOP
build["for"] {
name: index_name
bounds: bounds
body: {
{"assign", names, { NameProxy.index list_name, index_name }}
build.group node.body
}
}
}
node.body = apply_continue node.body
while: (node) =>
smart_node node
node.body = apply_continue node.body
for: (node) =>
smart_node node
node.body = apply_continue node.body
switch: (node, ret) =>
exp, conds = unpack node, 2
exp_name = NameProxy "exp"
-- convert switch conds into if statment conds
convert_cond = (cond) ->
t, case_exps, body = unpack cond
out = {}
insert out, t == "case" and "elseif" or "else"
if t != "else"
cond_exp = {}
for i, case in ipairs case_exps
if i == 1
insert cond_exp, "exp"
else
insert cond_exp, "or"
case = {"parens", case} unless value_is_singular case
insert cond_exp, {"exp", case, "==", exp_name}
insert out, cond_exp
else
body = case_exps
if ret
body = transform_last_stm body, ret
insert out, body
out
first = true
if_stm = {"if"}
for cond in *conds
if_cond = convert_cond cond
if first
first = false
insert if_stm, if_cond[2]
insert if_stm, if_cond[3]
else
insert if_stm, if_cond
build.group {
build.assign_one exp_name, exp
if_stm
}
class: require "moonscript.transform.class"
}