This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
forked from leafo/moonscript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.lua
1660 lines (1660 loc) · 37.4 KB
/
transform.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module("moonscript.transform", package.seeall)
local types = require("moonscript.types")
local util = require("moonscript.util")
local data = require("moonscript.data")
local reversed = util.reversed
local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular
local insert = table.insert
local mtype = util.moon.type
local implicitly_return
do
local _parent_0 = nil
local _base_0 = {
get_name = function(self)
return self.name
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, name)
self.name = name
self[1] = "temp_name"
end,
__base = _base_0,
__name = "LocalName",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
LocalName = _class_0
end
do
local _parent_0 = nil
local _base_0 = {
get_name = function(self, scope)
if not self.name then
self.name = scope:free_name(self.prefix, true)
end
return self.name
end,
chain = function(self, ...)
local items = {
...
}
items = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = items
for _index_0 = 1, #_list_0 do
local i = _list_0[_index_0]
local _value_0
if type(i) == "string" then
_value_0 = {
"dot",
i
}
else
_value_0 = i
end
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
return build.chain({
base = self,
unpack(items)
})
end,
index = function(self, key)
return build.chain({
base = self,
{
"index",
key
}
})
end,
__tostring = function(self)
if self.name then
return ("name<%s>"):format(self.name)
else
return ("name<prefix(%s)>"):format(self.prefix)
end
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, prefix)
self.prefix = prefix
self[1] = "temp_name"
end,
__base = _base_0,
__name = "NameProxy",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
NameProxy = _class_0
end
do
local _parent_0 = nil
local _base_0 = {
call = function(self, state)
return self.fn(state)
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, fn)
self.fn = fn
self[1] = "run"
end,
__base = _base_0,
__name = "Run",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
Run = _class_0
end
local apply_to_last
apply_to_last = function(stms, fn)
local last_exp_id = 0
for i = #stms, 1, -1 do
local stm = stms[i]
if stm and util.moon.type(stm) ~= Run then
last_exp_id = i
break
end
end
return (function()
local _accum_0 = { }
local _len_0 = 0
for i, stm in ipairs(stms) do
local _value_0
if i == last_exp_id then
_value_0 = fn(stm)
else
_value_0 = stm
end
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
end
local is_singular
is_singular = function(body)
if #body ~= 1 then
return false
end
if "group" == ntype(body) then
return is_singular(body[2])
else
return true
end
end
local find_assigns
find_assigns = function(body, out)
if out == nil then
out = { }
end
local _list_0 = body
for _index_0 = 1, #_list_0 do
local thing = _list_0[_index_0]
local _exp_0 = thing[1]
if "group" == _exp_0 then
find_assigns(thing[2], out)
elseif "assign" == _exp_0 then
table.insert(out, thing[2])
end
end
return out
end
local hoist_declarations
hoist_declarations = function(body)
local assigns = { }
local _list_0 = find_assigns(body)
for _index_0 = 1, #_list_0 do
local names = _list_0[_index_0]
local _list_1 = names
for _index_1 = 1, #_list_1 do
local name = _list_1[_index_1]
if type(name) == "string" then
table.insert(assigns, name)
end
end
end
local idx = 1
while mtype(body[idx]) == Run do
idx = idx + 1
end
return table.insert(body, idx, {
"declare",
assigns
})
end
local expand_elseif_assign
expand_elseif_assign = function(ifstm)
for i = 4, #ifstm do
local case = ifstm[i]
if ntype(case) == "elseif" and ntype(case[2]) == "assign" then
local split = {
unpack(ifstm, 1, i - 1)
}
insert(split, {
"else",
{
{
"if",
case[2],
case[3],
unpack(ifstm, i + 1)
}
}
})
return split
end
end
return ifstm
end
local constructor_name = "new"
local with_continue_listener
with_continue_listener = function(body)
local continue_name = nil
return {
Run(function(self)
return self:listen("continue", function()
if not (continue_name) then
continue_name = NameProxy("continue")
self:put_name(continue_name)
end
return continue_name
end)
end),
build.group(body),
Run(function(self)
if not (continue_name) then
return
end
self:put_name(continue_name, nil)
return self:splice(function(lines)
return {
{
"assign",
{
continue_name
},
{
"false"
}
},
{
"repeat",
"true",
{
lines,
{
"assign",
{
continue_name
},
{
"true"
}
}
}
},
{
"if",
{
"not",
continue_name
},
{
{
"break"
}
}
}
}
end)
end)
}
end
local Transformer
do
local _parent_0 = nil
local _base_0 = {
transform = function(self, scope, node, ...)
if self.seen_nodes[node] then
return node
end
self.seen_nodes[node] = true
while true do
local transformer = self.transformers[ntype(node)]
local res
if transformer then
res = transformer(scope, node, ...) or node
else
res = node
end
if res == node then
return node
end
node = res
end
return node
end,
bind = function(self, scope)
return function(...)
return self:transform(scope, ...)
end
end,
__call = function(self, ...)
return self:transform(...)
end,
can_transform = function(self, node)
return self.transformers[ntype(node)] ~= nil
end
}
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, transformers)
self.transformers = transformers
self.seen_nodes = setmetatable({ }, {
__mode = "k"
})
end,
__base = _base_0,
__name = "Transformer",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
Transformer = _class_0
end
local construct_comprehension
construct_comprehension = function(inner, clauses)
local current_stms = inner
for _, clause in reversed(clauses) do
local t = clause[1]
if t == "for" then
local names, iter
_, names, iter = unpack(clause)
current_stms = {
"foreach",
names,
{
iter
},
current_stms
}
elseif t == "when" then
local cond
_, cond = unpack(clause)
current_stms = {
"if",
cond,
current_stms
}
else
current_stms = error("Unknown comprehension clause: " .. t)
end
current_stms = {
current_stms
}
end
return current_stms[1]
end
Statement = Transformer({
root_stms = function(self, body)
return apply_to_last(body, implicitly_return(self))
end,
assign = function(self, node)
local names, values = unpack(node, 2)
local transformed
if #values == 1 then
local value = values[1]
local t = ntype(value)
if t == "decorated" then
value = self.transform.statement(value)
t = ntype(value)
end
if types.cascading[t] then
local ret
ret = function(stm)
if types.is_value(stm) then
return {
"assign",
names,
{
stm
}
}
else
return stm
end
end
transformed = build.group({
{
"declare",
names
},
self.transform.statement(value, ret, node)
})
end
end
return transformed or node
end,
continue = function(self, node)
local continue_name = self:send("continue")
if not (continue_name) then
error("continue must be inside of a loop")
end
return build.group({
build.assign_one(continue_name, "true"),
{
"break"
}
})
end,
export = function(self, node)
if #node > 2 then
if node[2] == "class" then
local cls = smart_node(node[3])
return build.group({
{
"export",
{
cls.name
}
},
cls
})
else
return build.group({
node,
build.assign({
names = node[2],
values = node[3]
})
})
end
else
return nil
end
end,
update = function(self, node)
local _, name, op, exp = unpack(node)
local op_final = op:match("^(.+)=$")
if not op_final then
error("Unknown op: " .. op)
end
if not (value_is_singular(exp)) then
exp = {
"parens",
exp
}
end
return build.assign_one(name, {
"exp",
name,
op_final,
exp
})
end,
import = function(self, node)
local _, names, source = unpack(node)
local stubs = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
local _value_0
if type(name) == "table" then
_value_0 = name
else
_value_0 = {
"dot",
name
}
end
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
local real_names = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = names
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
local _value_0 = type(name) == "table" and name[2] or name
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
if type(source) == "string" then
return build.assign({
names = real_names,
values = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = stubs
for _index_0 = 1, #_list_0 do
local stub = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = build.chain({
base = source,
stub
})
end
return _accum_0
end)()
})
else
local source_name = NameProxy("table")
return build.group({
{
"declare",
real_names
},
build["do"]({
build.assign_one(source_name, source),
build.assign({
names = real_names,
values = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = stubs
for _index_0 = 1, #_list_0 do
local stub = _list_0[_index_0]
_len_0 = _len_0 + 1
_accum_0[_len_0] = build.chain({
base = source_name,
stub
})
end
return _accum_0
end)()
})
})
})
end
end,
comprehension = function(self, node, action)
local _, exp, clauses = unpack(node)
action = action or function(exp)
return {
exp
}
end
return construct_comprehension(action(exp), clauses)
end,
["do"] = function(self, node, ret)
if ret then
node[2] = apply_to_last(node[2], ret)
end
return node
end,
decorated = function(self, node)
local stm, dec = unpack(node, 2)
local wrapped
local _exp_0 = dec[1]
if "if" == _exp_0 then
local cond, fail = unpack(dec, 2)
if fail then
fail = {
"else",
{
fail
}
}
end
wrapped = {
"if",
cond,
{
stm
},
fail
}
elseif "unless" == _exp_0 then
wrapped = {
"unless",
dec[2],
{
stm
}
}
elseif "comprehension" == _exp_0 then
wrapped = {
"comprehension",
stm,
dec[2]
}
else
wrapped = error("Unknown decorator " .. dec[1])
end
if ntype(stm) == "assign" then
wrapped = build.group({
build.declare({
names = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_0 = stm[2]
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
if type(name) == "string" then
_len_0 = _len_0 + 1
_accum_0[_len_0] = name
end
end
return _accum_0
end)()
}),
wrapped
})
end
return wrapped
end,
unless = function(self, node)
return {
"if",
{
"not",
{
"parens",
node[2]
}
},
unpack(node, 3)
}
end,
["if"] = function(self, node, ret)
if ntype(node[2]) == "assign" then
local _, assign, body = unpack(node)
local name = assign[2][1]
return build["do"]({
assign,
{
"if",
name,
unpack(node, 3)
}
})
end
node = expand_elseif_assign(node)
if ret then
smart_node(node)
node['then'] = apply_to_last(node['then'], ret)
for i = 4, #node do
local case = node[i]
local body_idx = #node[i]
case[body_idx] = apply_to_last(case[body_idx], ret)
end
end
return node
end,
with = function(self, node, ret)
local _, exp, block = unpack(node)
local scope_name = NameProxy("with")
local named_assign
if ntype(exp) == "assign" then
local names, values = unpack(exp, 2)
local assign_name = names[1]
exp = values[1]
values[1] = scope_name
named_assign = {
"assign",
names,
values
}
end
return build["do"]({
Run(function(self)
return self:set("scope_var", scope_name)
end),
build.assign_one(scope_name, exp),
build.group({
named_assign
}),
build.group(block),
(function()
if ret then
return ret(scope_name)
end
end)()
})
end,
foreach = function(self, node)
smart_node(node)
local source = unpack(node.iter)
if ntype(source) == "unpack" then
local list = source[2]
local index_name = NameProxy("index")
local list_name = NameProxy("list")
local slice_var = nil
local bounds
if is_slice(list) then
local slice = list[#list]
table.remove(list)
table.remove(slice, 1)
if slice[2] and slice[2] ~= "" then
local max_tmp_name = NameProxy("max")
slice_var = build.assign_one(max_tmp_name, slice[2])
slice[2] = {
"exp",
max_tmp_name,
"<",
0,
"and",
{
"length",
list_name
},
"+",
max_tmp_name,
"or",
max_tmp_name
}
else
slice[2] = {
"length",
list_name
}
end
bounds = slice
else
bounds = {
1,
{
"length",
list_name
}
}
end
return build.group({
build.assign_one(list_name, list),
slice_var,
build["for"]({
name = index_name,
bounds = bounds,
body = {
{
"assign",
node.names,
{
list_name:index(index_name)
}
},
build.group(node.body)
}
})
})
end
node.body = with_continue_listener(node.body)
end,
["while"] = function(self, node)
smart_node(node)
node.body = with_continue_listener(node.body)
end,
["for"] = function(self, node)
smart_node(node)
node.body = with_continue_listener(node.body)
end,
switch = function(self, node, ret)
local _, exp, conds = unpack(node)
local exp_name = NameProxy("exp")
local convert_cond
convert_cond = function(cond)
local t, case_exp, body = unpack(cond)
local out = { }
insert(out, t == "case" and "elseif" or "else")
if t ~= "else" then
if t ~= "else" then
insert(out, {
"exp",
case_exp,
"==",
exp_name
})
end
else
body = case_exp
end
if ret then
body = apply_to_last(body, ret)
end
insert(out, body)
return out
end
local first = true
local if_stm = {
"if"
}
local _list_0 = conds
for _index_0 = 1, #_list_0 do
local cond = _list_0[_index_0]
local if_cond = convert_cond(cond)
if first then
first = false
insert(if_stm, if_cond[2])
insert(if_stm, if_cond[3])
else
insert(if_stm, if_cond)
end
end
return build.group({
build.assign_one(exp_name, exp),
if_stm
})
end,
class = function(self, node, ret, parent_assign)
local _, name, parent_val, body = unpack(node)
local statements = { }
local properties = { }
local _list_0 = body
for _index_0 = 1, #_list_0 do
local item = _list_0[_index_0]
local _exp_0 = item[1]
if "stm" == _exp_0 then
insert(statements, item[2])
elseif "props" == _exp_0 then
local _list_1 = item
for _index_1 = 2, #_list_1 do
local tuple = _list_1[_index_1]
if ntype(tuple[1]) == "self" then
insert(statements, build.assign_one(unpack(tuple)))
else
insert(properties, tuple)
end
end
end
end
local constructor = nil
properties = (function()
local _accum_0 = { }
local _len_0 = 0
local _list_1 = properties
for _index_0 = 1, #_list_1 do
local tuple = _list_1[_index_0]
local key = tuple[1]
local _value_0
if key[1] == "key_literal" and key[2] == constructor_name then
constructor = tuple[2]
_value_0 = nil
else
_value_0 = tuple
end
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
local parent_cls_name = NameProxy("parent")
local base_name = NameProxy("base")
local self_name = NameProxy("self")
local cls_name = NameProxy("class")
if not constructor then
constructor = build.fndef({
args = {
{
"..."
}
},
arrow = "fat",
body = {
build["if"]({
cond = parent_cls_name,
["then"] = {
build.chain({
base = "super",
{
"call",
{
"..."
}
}
})
}
})
}
})
else
smart_node(constructor)
constructor.arrow = "fat"
end
local real_name = name or parent_assign and parent_assign[2][1]
local _exp_0 = ntype(real_name)
if "chain" == _exp_0 then
local last = real_name[#real_name]
local _exp_1 = ntype(last)
if "dot" == _exp_1 then
real_name = {
"string",
'"',
last[2]
}
elseif "index" == _exp_1 then
real_name = last[2]
else
real_name = "nil"
end
elseif "nil" == _exp_0 then
real_name = "nil"
else
real_name = {
"string",
'"',