rufus-lua-moon 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -178,8 +178,8 @@ wrap_func_arg = function(value)
178
178
  }
179
179
  }
180
180
  end
181
- local flatten_func
182
- flatten_func = function(callee, args)
181
+ local join_chain
182
+ join_chain = function(callee, args)
183
183
  if #args == 0 then
184
184
  return callee
185
185
  end
@@ -188,13 +188,7 @@ flatten_func = function(callee, args)
188
188
  args
189
189
  }
190
190
  if ntype(callee) == "chain" then
191
- local stub = callee[#callee]
192
- if ntype(stub) == "colon_stub" then
193
- stub[1] = "colon"
194
- table.insert(stub, args)
195
- else
196
- table.insert(callee, args)
197
- end
191
+ table.insert(callee, args)
198
192
  return callee
199
193
  end
200
194
  return {
@@ -203,17 +197,6 @@ flatten_func = function(callee, args)
203
197
  args
204
198
  }
205
199
  end
206
- local flatten_string_chain
207
- flatten_string_chain = function(str, chain, args)
208
- if not (chain) then
209
- return str
210
- end
211
- return flatten_func({
212
- "chain",
213
- str,
214
- unpack(chain)
215
- }, args)
216
- end
217
200
  local wrap_decorator
218
201
  wrap_decorator = function(stm, dec)
219
202
  if not (dec) then
@@ -259,8 +242,7 @@ return {
259
242
  symx = symx,
260
243
  simple_string = simple_string,
261
244
  wrap_func_arg = wrap_func_arg,
262
- flatten_func = flatten_func,
263
- flatten_string_chain = flatten_string_chain,
245
+ join_chain = join_chain,
264
246
  wrap_decorator = wrap_decorator,
265
247
  check_lua_string = check_lua_string,
266
248
  self_assign = self_assign
@@ -1,1634 +1,4 @@
1
- local types = require("moonscript.types")
2
- local util = require("moonscript.util")
3
- local data = require("moonscript.data")
4
- local reversed, unpack
5
- reversed, unpack = util.reversed, util.unpack
6
- local ntype, mtype, build, smart_node, is_slice, value_is_singular
7
- ntype, mtype, build, smart_node, is_slice, value_is_singular = types.ntype, types.mtype, types.build, types.smart_node, types.is_slice, types.value_is_singular
8
- local insert
9
- insert = table.insert
10
- local NameProxy, LocalName
11
- do
12
- local _obj_0 = require("moonscript.transform.names")
13
- NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
14
- end
15
- local destructure = require("moonscript.transform.destructure")
16
- local NOOP = {
17
- "noop"
18
- }
19
- local Run, apply_to_last, is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
20
- do
21
- local _base_0 = {
22
- call = function(self, state)
23
- return self.fn(state)
24
- end
25
- }
26
- _base_0.__index = _base_0
27
- local _class_0 = setmetatable({
28
- __init = function(self, fn)
29
- self.fn = fn
30
- self[1] = "run"
31
- end,
32
- __base = _base_0,
33
- __name = "Run"
34
- }, {
35
- __index = _base_0,
36
- __call = function(cls, ...)
37
- local _self_0 = setmetatable({}, _base_0)
38
- cls.__init(_self_0, ...)
39
- return _self_0
40
- end
41
- })
42
- _base_0.__class = _class_0
43
- Run = _class_0
44
- end
45
- apply_to_last = function(stms, fn)
46
- local last_exp_id = 0
47
- for i = #stms, 1, -1 do
48
- local stm = stms[i]
49
- if stm and mtype(stm) ~= Run then
50
- last_exp_id = i
51
- break
52
- end
53
- end
54
- return (function()
55
- local _accum_0 = { }
56
- local _len_0 = 1
57
- for i, stm in ipairs(stms) do
58
- if i == last_exp_id then
59
- _accum_0[_len_0] = {
60
- "transform",
61
- stm,
62
- fn
63
- }
64
- else
65
- _accum_0[_len_0] = stm
66
- end
67
- _len_0 = _len_0 + 1
68
- end
69
- return _accum_0
70
- end)()
71
- end
72
- is_singular = function(body)
73
- if #body ~= 1 then
74
- return false
75
- end
76
- if "group" == ntype(body) then
77
- return is_singular(body[2])
78
- else
79
- return body[1]
80
- end
81
- end
82
- extract_declarations = function(self, body, start, out)
83
- if body == nil then
84
- body = self.current_stms
85
- end
86
- if start == nil then
87
- start = self.current_stm_i + 1
88
- end
89
- if out == nil then
90
- out = { }
91
- end
92
- for i = start, #body do
93
- local _continue_0 = false
94
- repeat
95
- local stm = body[i]
96
- if stm == nil then
97
- _continue_0 = true
98
- break
99
- end
100
- stm = self.transform.statement(stm)
101
- body[i] = stm
102
- local _exp_0 = stm[1]
103
- if "assign" == _exp_0 or "declare" == _exp_0 then
104
- local _list_0 = stm[2]
105
- for _index_0 = 1, #_list_0 do
106
- local name = _list_0[_index_0]
107
- if ntype(name) == "ref" then
108
- insert(out, name)
109
- elseif type(name) == "string" then
110
- insert(out, name)
111
- end
112
- end
113
- elseif "group" == _exp_0 then
114
- extract_declarations(self, stm[2], 1, out)
115
- end
116
- _continue_0 = true
117
- until true
118
- if not _continue_0 then
119
- break
120
- end
121
- end
122
- return out
123
- end
124
- expand_elseif_assign = function(ifstm)
125
- for i = 4, #ifstm do
126
- local case = ifstm[i]
127
- if ntype(case) == "elseif" and ntype(case[2]) == "assign" then
128
- local split = {
129
- unpack(ifstm, 1, i - 1)
130
- }
131
- insert(split, {
132
- "else",
133
- {
134
- {
135
- "if",
136
- case[2],
137
- case[3],
138
- unpack(ifstm, i + 1)
139
- }
140
- }
141
- })
142
- return split
143
- end
144
- end
145
- return ifstm
146
- end
147
- constructor_name = "new"
148
- with_continue_listener = function(body)
149
- local continue_name = nil
150
- return {
151
- Run(function(self)
152
- return self:listen("continue", function()
153
- if not (continue_name) then
154
- continue_name = NameProxy("continue")
155
- self:put_name(continue_name)
156
- end
157
- return continue_name
158
- end)
159
- end),
160
- build.group(body),
161
- Run(function(self)
162
- if not (continue_name) then
163
- return
164
- end
165
- self:put_name(continue_name, nil)
166
- return self:splice(function(lines)
167
- return {
168
- {
169
- "assign",
170
- {
171
- continue_name
172
- },
173
- {
174
- "false"
175
- }
176
- },
177
- {
178
- "repeat",
179
- "true",
180
- {
181
- lines,
182
- {
183
- "assign",
184
- {
185
- continue_name
186
- },
187
- {
188
- "true"
189
- }
190
- }
191
- }
192
- },
193
- {
194
- "if",
195
- {
196
- "not",
197
- continue_name
198
- },
199
- {
200
- {
201
- "break"
202
- }
203
- }
204
- }
205
- }
206
- end)
207
- end)
208
- }
209
- end
210
- do
211
- local _base_0 = {
212
- transform_once = function(self, scope, node, ...)
213
- if self.seen_nodes[node] then
214
- return node
215
- end
216
- self.seen_nodes[node] = true
217
- local transformer = self.transformers[ntype(node)]
218
- if transformer then
219
- return transformer(scope, node, ...) or node
220
- else
221
- return node
222
- end
223
- end,
224
- transform = function(self, scope, node, ...)
225
- if self.seen_nodes[node] then
226
- return node
227
- end
228
- self.seen_nodes[node] = true
229
- while true do
230
- local transformer = self.transformers[ntype(node)]
231
- local res
232
- if transformer then
233
- res = transformer(scope, node, ...) or node
234
- else
235
- res = node
236
- end
237
- if res == node then
238
- return node
239
- end
240
- node = res
241
- end
242
- return node
243
- end,
244
- bind = function(self, scope)
245
- return function(...)
246
- return self:transform(scope, ...)
247
- end
248
- end,
249
- __call = function(self, ...)
250
- return self:transform(...)
251
- end,
252
- can_transform = function(self, node)
253
- return self.transformers[ntype(node)] ~= nil
254
- end
255
- }
256
- _base_0.__index = _base_0
257
- local _class_0 = setmetatable({
258
- __init = function(self, transformers)
259
- self.transformers = transformers
260
- self.seen_nodes = setmetatable({ }, {
261
- __mode = "k"
262
- })
263
- end,
264
- __base = _base_0,
265
- __name = "Transformer"
266
- }, {
267
- __index = _base_0,
268
- __call = function(cls, ...)
269
- local _self_0 = setmetatable({}, _base_0)
270
- cls.__init(_self_0, ...)
271
- return _self_0
272
- end
273
- })
274
- _base_0.__class = _class_0
275
- Transformer = _class_0
276
- end
277
- construct_comprehension = function(inner, clauses)
278
- local current_stms = inner
279
- for _, clause in reversed(clauses) do
280
- local t = clause[1]
281
- local _exp_0 = t
282
- if "for" == _exp_0 then
283
- local name, bounds
284
- _, name, bounds = clause[1], clause[2], clause[3]
285
- current_stms = {
286
- "for",
287
- name,
288
- bounds,
289
- current_stms
290
- }
291
- elseif "foreach" == _exp_0 then
292
- local names, iter
293
- _, names, iter = clause[1], clause[2], clause[3]
294
- current_stms = {
295
- "foreach",
296
- names,
297
- {
298
- iter
299
- },
300
- current_stms
301
- }
302
- elseif "when" == _exp_0 then
303
- local cond
304
- _, cond = clause[1], clause[2]
305
- current_stms = {
306
- "if",
307
- cond,
308
- current_stms
309
- }
310
- else
311
- current_stms = error("Unknown comprehension clause: " .. t)
312
- end
313
- current_stms = {
314
- current_stms
315
- }
316
- end
317
- return current_stms[1]
318
- end
319
- Statement = Transformer({
320
- transform = function(self, tuple)
321
- local _, node, fn
322
- _, node, fn = tuple[1], tuple[2], tuple[3]
323
- return fn(node)
324
- end,
325
- root_stms = function(self, body)
326
- return apply_to_last(body, implicitly_return(self))
327
- end,
328
- ["return"] = function(self, node)
329
- local ret_val = node[2]
330
- local ret_val_type = ntype(ret_val)
331
- if ret_val_type == "explist" and #ret_val == 2 then
332
- ret_val = ret_val[2]
333
- ret_val_type = ntype(ret_val)
334
- end
335
- if types.cascading[ret_val_type] then
336
- return implicitly_return(self)(ret_val)
337
- end
338
- if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then
339
- ret_val = Value:transform_once(self, ret_val)
340
- if ntype(ret_val) == "block_exp" then
341
- return build.group(apply_to_last(ret_val[2], function(stm)
342
- return {
343
- "return",
344
- stm
345
- }
346
- end))
347
- end
348
- end
349
- node[2] = ret_val
350
- return node
351
- end,
352
- declare_glob = function(self, node)
353
- local names = extract_declarations(self)
354
- if node[2] == "^" then
355
- do
356
- local _accum_0 = { }
357
- local _len_0 = 1
358
- for _index_0 = 1, #names do
359
- local _continue_0 = false
360
- repeat
361
- local name = names[_index_0]
362
- if not (name[2]:match("^%u")) then
363
- _continue_0 = true
364
- break
365
- end
366
- local _value_0 = name
367
- _accum_0[_len_0] = _value_0
368
- _len_0 = _len_0 + 1
369
- _continue_0 = true
370
- until true
371
- if not _continue_0 then
372
- break
373
- end
374
- end
375
- names = _accum_0
376
- end
377
- end
378
- return {
379
- "declare",
380
- names
381
- }
382
- end,
383
- assign = function(self, node)
384
- local names, values = unpack(node, 2)
385
- local num_values = #values
386
- local num_names = #values
387
- if num_names == 1 and num_values == 1 then
388
- local first_value = values[1]
389
- local first_name = names[1]
390
- local first_type = ntype(first_value)
391
- if first_type == "chain" then
392
- first_value = Value:transform_once(self, first_value)
393
- first_type = ntype(first_value)
394
- end
395
- local _exp_0 = ntype(first_value)
396
- if "block_exp" == _exp_0 then
397
- local block_body = first_value[2]
398
- local idx = #block_body
399
- block_body[idx] = build.assign_one(first_name, block_body[idx])
400
- return build.group({
401
- {
402
- "declare",
403
- {
404
- first_name
405
- }
406
- },
407
- {
408
- "do",
409
- block_body
410
- }
411
- })
412
- elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then
413
- return build.assign_one(first_name, Value:transform_once(self, first_value))
414
- else
415
- values[1] = first_value
416
- end
417
- end
418
- local transformed
419
- if num_values == 1 then
420
- local value = values[1]
421
- local t = ntype(value)
422
- if t == "decorated" then
423
- value = self.transform.statement(value)
424
- t = ntype(value)
425
- end
426
- if types.cascading[t] then
427
- local ret
428
- ret = function(stm)
429
- if types.is_value(stm) then
430
- return {
431
- "assign",
432
- names,
433
- {
434
- stm
435
- }
436
- }
437
- else
438
- return stm
439
- end
440
- end
441
- transformed = build.group({
442
- {
443
- "declare",
444
- names
445
- },
446
- self.transform.statement(value, ret, node)
447
- })
448
- end
449
- end
450
- node = transformed or node
451
- if destructure.has_destructure(names) then
452
- return destructure.split_assign(self, node)
453
- end
454
- return node
455
- end,
456
- continue = function(self, node)
457
- local continue_name = self:send("continue")
458
- if not (continue_name) then
459
- error("continue must be inside of a loop")
460
- end
461
- return build.group({
462
- build.assign_one(continue_name, "true"),
463
- {
464
- "break"
465
- }
466
- })
467
- end,
468
- export = function(self, node)
469
- if #node > 2 then
470
- if node[2] == "class" then
471
- local cls = smart_node(node[3])
472
- return build.group({
473
- {
474
- "export",
475
- {
476
- cls.name
477
- }
478
- },
479
- cls
480
- })
481
- else
482
- return build.group({
483
- {
484
- "export",
485
- node[2]
486
- },
487
- build.assign({
488
- names = node[2],
489
- values = node[3]
490
- })
491
- })
492
- end
493
- else
494
- return nil
495
- end
496
- end,
497
- update = function(self, node)
498
- local _, name, op, exp = unpack(node)
499
- local op_final = op:match("^(.+)=$")
500
- if not op_final then
501
- error("Unknown op: " .. op)
502
- end
503
- if not (value_is_singular(exp)) then
504
- exp = {
505
- "parens",
506
- exp
507
- }
508
- end
509
- return build.assign_one(name, {
510
- "exp",
511
- name,
512
- op_final,
513
- exp
514
- })
515
- end,
516
- import = function(self, node)
517
- local _, names, source = unpack(node)
518
- local table_values
519
- do
520
- local _accum_0 = { }
521
- local _len_0 = 1
522
- for _index_0 = 1, #names do
523
- local name = names[_index_0]
524
- local dest_val
525
- if ntype(name) == "colon_stub" then
526
- dest_val = name[2]
527
- else
528
- dest_val = name
529
- end
530
- local _value_0 = {
531
- {
532
- "key_literal",
533
- name
534
- },
535
- dest_val
536
- }
537
- _accum_0[_len_0] = _value_0
538
- _len_0 = _len_0 + 1
539
- end
540
- table_values = _accum_0
541
- end
542
- local dest = {
543
- "table",
544
- table_values
545
- }
546
- return {
547
- "assign",
548
- {
549
- dest
550
- },
551
- {
552
- source
553
- },
554
- [-1] = node[-1]
555
- }
556
- end,
557
- comprehension = function(self, node, action)
558
- local _, exp, clauses = unpack(node)
559
- action = action or function(exp)
560
- return {
561
- exp
562
- }
563
- end
564
- return construct_comprehension(action(exp), clauses)
565
- end,
566
- ["do"] = function(self, node, ret)
567
- if ret then
568
- node[2] = apply_to_last(node[2], ret)
569
- end
570
- return node
571
- end,
572
- decorated = function(self, node)
573
- local stm, dec = unpack(node, 2)
574
- local wrapped
575
- local _exp_0 = dec[1]
576
- if "if" == _exp_0 then
577
- local cond, fail = unpack(dec, 2)
578
- if fail then
579
- fail = {
580
- "else",
581
- {
582
- fail
583
- }
584
- }
585
- end
586
- wrapped = {
587
- "if",
588
- cond,
589
- {
590
- stm
591
- },
592
- fail
593
- }
594
- elseif "unless" == _exp_0 then
595
- wrapped = {
596
- "unless",
597
- dec[2],
598
- {
599
- stm
600
- }
601
- }
602
- elseif "comprehension" == _exp_0 then
603
- wrapped = {
604
- "comprehension",
605
- stm,
606
- dec[2]
607
- }
608
- else
609
- wrapped = error("Unknown decorator " .. dec[1])
610
- end
611
- if ntype(stm) == "assign" then
612
- wrapped = build.group({
613
- build.declare({
614
- names = (function()
615
- local _accum_0 = { }
616
- local _len_0 = 1
617
- local _list_0 = stm[2]
618
- for _index_0 = 1, #_list_0 do
619
- local name = _list_0[_index_0]
620
- if ntype(name) == "ref" then
621
- _accum_0[_len_0] = name
622
- _len_0 = _len_0 + 1
623
- end
624
- end
625
- return _accum_0
626
- end)()
627
- }),
628
- wrapped
629
- })
630
- end
631
- return wrapped
632
- end,
633
- unless = function(self, node)
634
- return {
635
- "if",
636
- {
637
- "not",
638
- {
639
- "parens",
640
- node[2]
641
- }
642
- },
643
- unpack(node, 3)
644
- }
645
- end,
646
- ["if"] = function(self, node, ret)
647
- if ntype(node[2]) == "assign" then
648
- local _, assign, body = unpack(node)
649
- if destructure.has_destructure(assign[2]) then
650
- local name = NameProxy("des")
651
- body = {
652
- destructure.build_assign(self, assign[2][1], name),
653
- build.group(node[3])
654
- }
655
- return build["do"]({
656
- build.assign_one(name, assign[3][1]),
657
- {
658
- "if",
659
- name,
660
- body,
661
- unpack(node, 4)
662
- }
663
- })
664
- else
665
- local name = assign[2][1]
666
- return build["do"]({
667
- assign,
668
- {
669
- "if",
670
- name,
671
- unpack(node, 3)
672
- }
673
- })
674
- end
675
- end
676
- node = expand_elseif_assign(node)
677
- if ret then
678
- smart_node(node)
679
- node['then'] = apply_to_last(node['then'], ret)
680
- for i = 4, #node do
681
- local case = node[i]
682
- local body_idx = #node[i]
683
- case[body_idx] = apply_to_last(case[body_idx], ret)
684
- end
685
- end
686
- return node
687
- end,
688
- with = function(self, node, ret)
689
- local exp, block = unpack(node, 2)
690
- local copy_scope = true
691
- local scope_name, named_assign
692
- if ntype(exp) == "assign" then
693
- local names, values = unpack(exp, 2)
694
- local first_name = names[1]
695
- if ntype(first_name) == "ref" then
696
- scope_name = first_name
697
- named_assign = exp
698
- exp = values[1]
699
- copy_scope = false
700
- else
701
- scope_name = NameProxy("with")
702
- exp = values[1]
703
- values[1] = scope_name
704
- named_assign = {
705
- "assign",
706
- names,
707
- values
708
- }
709
- end
710
- elseif self:is_local(exp) then
711
- scope_name = exp
712
- copy_scope = false
713
- end
714
- scope_name = scope_name or NameProxy("with")
715
- return build["do"]({
716
- Run(function(self)
717
- return self:set("scope_var", scope_name)
718
- end),
719
- copy_scope and build.assign_one(scope_name, exp) or NOOP,
720
- named_assign or NOOP,
721
- build.group(block),
722
- (function()
723
- if ret then
724
- return ret(scope_name)
725
- end
726
- end)()
727
- })
728
- end,
729
- foreach = function(self, node, _)
730
- smart_node(node)
731
- local source = unpack(node.iter)
732
- local destructures = { }
733
- do
734
- local _accum_0 = { }
735
- local _len_0 = 1
736
- for i, name in ipairs(node.names) do
737
- if ntype(name) == "table" then
738
- do
739
- local proxy = NameProxy("des")
740
- insert(destructures, destructure.build_assign(self, name, proxy))
741
- _accum_0[_len_0] = proxy
742
- end
743
- else
744
- _accum_0[_len_0] = name
745
- end
746
- _len_0 = _len_0 + 1
747
- end
748
- node.names = _accum_0
749
- end
750
- if next(destructures) then
751
- insert(destructures, build.group(node.body))
752
- node.body = destructures
753
- end
754
- if ntype(source) == "unpack" then
755
- local list = source[2]
756
- local index_name = NameProxy("index")
757
- local list_name = self:is_local(list) and list or NameProxy("list")
758
- local slice_var = nil
759
- local bounds
760
- if is_slice(list) then
761
- local slice = list[#list]
762
- table.remove(list)
763
- table.remove(slice, 1)
764
- if self:is_local(list) then
765
- list_name = list
766
- end
767
- if slice[2] and slice[2] ~= "" then
768
- local max_tmp_name = NameProxy("max")
769
- slice_var = build.assign_one(max_tmp_name, slice[2])
770
- slice[2] = {
771
- "exp",
772
- max_tmp_name,
773
- "<",
774
- 0,
775
- "and",
776
- {
777
- "length",
778
- list_name
779
- },
780
- "+",
781
- max_tmp_name,
782
- "or",
783
- max_tmp_name
784
- }
785
- else
786
- slice[2] = {
787
- "length",
788
- list_name
789
- }
790
- end
791
- bounds = slice
792
- else
793
- bounds = {
794
- 1,
795
- {
796
- "length",
797
- list_name
798
- }
799
- }
800
- end
801
- return build.group({
802
- list_name ~= list and build.assign_one(list_name, list) or NOOP,
803
- slice_var or NOOP,
804
- build["for"]({
805
- name = index_name,
806
- bounds = bounds,
807
- body = {
808
- {
809
- "assign",
810
- node.names,
811
- {
812
- NameProxy.index(list_name, index_name)
813
- }
814
- },
815
- build.group(node.body)
816
- }
817
- })
818
- })
819
- end
820
- node.body = with_continue_listener(node.body)
821
- end,
822
- ["while"] = function(self, node)
823
- smart_node(node)
824
- node.body = with_continue_listener(node.body)
825
- end,
826
- ["for"] = function(self, node)
827
- smart_node(node)
828
- node.body = with_continue_listener(node.body)
829
- end,
830
- switch = function(self, node, ret)
831
- local _, exp, conds = unpack(node)
832
- local exp_name = NameProxy("exp")
833
- local convert_cond
834
- convert_cond = function(cond)
835
- local t, case_exps, body = unpack(cond)
836
- local out = { }
837
- insert(out, t == "case" and "elseif" or "else")
838
- if t ~= "else" then
839
- local cond_exp = { }
840
- for i, case in ipairs(case_exps) do
841
- if i == 1 then
842
- insert(cond_exp, "exp")
843
- else
844
- insert(cond_exp, "or")
845
- end
846
- if not (value_is_singular(case)) then
847
- case = {
848
- "parens",
849
- case
850
- }
851
- end
852
- insert(cond_exp, {
853
- "exp",
854
- case,
855
- "==",
856
- exp_name
857
- })
858
- end
859
- insert(out, cond_exp)
860
- else
861
- body = case_exps
862
- end
863
- if ret then
864
- body = apply_to_last(body, ret)
865
- end
866
- insert(out, body)
867
- return out
868
- end
869
- local first = true
870
- local if_stm = {
871
- "if"
872
- }
873
- for _index_0 = 1, #conds do
874
- local cond = conds[_index_0]
875
- local if_cond = convert_cond(cond)
876
- if first then
877
- first = false
878
- insert(if_stm, if_cond[2])
879
- insert(if_stm, if_cond[3])
880
- else
881
- insert(if_stm, if_cond)
882
- end
883
- end
884
- return build.group({
885
- build.assign_one(exp_name, exp),
886
- if_stm
887
- })
888
- end,
889
- class = function(self, node, ret, parent_assign)
890
- local _, name, parent_val, body = unpack(node)
891
- if parent_val == "" then
892
- parent_val = nil
893
- end
894
- local statements = { }
895
- local properties = { }
896
- for _index_0 = 1, #body do
897
- local item = body[_index_0]
898
- local _exp_0 = item[1]
899
- if "stm" == _exp_0 then
900
- insert(statements, item[2])
901
- elseif "props" == _exp_0 then
902
- for _index_1 = 2, #item do
903
- local tuple = item[_index_1]
904
- if ntype(tuple[1]) == "self" then
905
- insert(statements, build.assign_one(unpack(tuple)))
906
- else
907
- insert(properties, tuple)
908
- end
909
- end
910
- end
911
- end
912
- local constructor
913
- do
914
- local _accum_0 = { }
915
- local _len_0 = 1
916
- for _index_0 = 1, #properties do
917
- local _continue_0 = false
918
- repeat
919
- local tuple = properties[_index_0]
920
- local key = tuple[1]
921
- local _value_0
922
- if key[1] == "key_literal" and key[2] == constructor_name then
923
- constructor = tuple[2]
924
- _continue_0 = true
925
- break
926
- else
927
- _value_0 = tuple
928
- end
929
- _accum_0[_len_0] = _value_0
930
- _len_0 = _len_0 + 1
931
- _continue_0 = true
932
- until true
933
- if not _continue_0 then
934
- break
935
- end
936
- end
937
- properties = _accum_0
938
- end
939
- local parent_cls_name = NameProxy("parent")
940
- local base_name = NameProxy("base")
941
- local self_name = NameProxy("self")
942
- local cls_name = NameProxy("class")
943
- if not (constructor) then
944
- if parent_val then
945
- constructor = build.fndef({
946
- args = {
947
- {
948
- "..."
949
- }
950
- },
951
- arrow = "fat",
952
- body = {
953
- build.chain({
954
- base = "super",
955
- {
956
- "call",
957
- {
958
- "..."
959
- }
960
- }
961
- })
962
- }
963
- })
964
- else
965
- constructor = build.fndef()
966
- end
967
- end
968
- local real_name = name or parent_assign and parent_assign[2][1]
969
- local _exp_0 = ntype(real_name)
970
- if "chain" == _exp_0 then
971
- local last = real_name[#real_name]
972
- local _exp_1 = ntype(last)
973
- if "dot" == _exp_1 then
974
- real_name = {
975
- "string",
976
- '"',
977
- last[2]
978
- }
979
- elseif "index" == _exp_1 then
980
- real_name = last[2]
981
- else
982
- real_name = "nil"
983
- end
984
- elseif "nil" == _exp_0 then
985
- real_name = "nil"
986
- else
987
- local name_t = type(real_name)
988
- local flattened_name
989
- if name_t == "string" then
990
- flattened_name = real_name
991
- elseif name_t == "table" and real_name[1] == "ref" then
992
- flattened_name = real_name[2]
993
- else
994
- flattened_name = error("don't know how to extract name from " .. tostring(name_t))
995
- end
996
- real_name = {
997
- "string",
998
- '"',
999
- flattened_name
1000
- }
1001
- end
1002
- local cls = build.table({
1003
- {
1004
- "__init",
1005
- constructor
1006
- },
1007
- {
1008
- "__base",
1009
- base_name
1010
- },
1011
- {
1012
- "__name",
1013
- real_name
1014
- },
1015
- parent_val and {
1016
- "__parent",
1017
- parent_cls_name
1018
- } or nil
1019
- })
1020
- local class_index
1021
- if parent_val then
1022
- local class_lookup = build["if"]({
1023
- cond = {
1024
- "exp",
1025
- {
1026
- "ref",
1027
- "val"
1028
- },
1029
- "==",
1030
- "nil"
1031
- },
1032
- ["then"] = {
1033
- parent_cls_name:index("name")
1034
- }
1035
- })
1036
- insert(class_lookup, {
1037
- "else",
1038
- {
1039
- "val"
1040
- }
1041
- })
1042
- class_index = build.fndef({
1043
- args = {
1044
- {
1045
- "cls"
1046
- },
1047
- {
1048
- "name"
1049
- }
1050
- },
1051
- body = {
1052
- build.assign_one(LocalName("val"), build.chain({
1053
- base = "rawget",
1054
- {
1055
- "call",
1056
- {
1057
- base_name,
1058
- {
1059
- "ref",
1060
- "name"
1061
- }
1062
- }
1063
- }
1064
- })),
1065
- class_lookup
1066
- }
1067
- })
1068
- else
1069
- class_index = base_name
1070
- end
1071
- local cls_mt = build.table({
1072
- {
1073
- "__index",
1074
- class_index
1075
- },
1076
- {
1077
- "__call",
1078
- build.fndef({
1079
- args = {
1080
- {
1081
- "cls"
1082
- },
1083
- {
1084
- "..."
1085
- }
1086
- },
1087
- body = {
1088
- build.assign_one(self_name, build.chain({
1089
- base = "setmetatable",
1090
- {
1091
- "call",
1092
- {
1093
- "{}",
1094
- base_name
1095
- }
1096
- }
1097
- })),
1098
- build.chain({
1099
- base = "cls.__init",
1100
- {
1101
- "call",
1102
- {
1103
- self_name,
1104
- "..."
1105
- }
1106
- }
1107
- }),
1108
- self_name
1109
- }
1110
- })
1111
- }
1112
- })
1113
- cls = build.chain({
1114
- base = "setmetatable",
1115
- {
1116
- "call",
1117
- {
1118
- cls,
1119
- cls_mt
1120
- }
1121
- }
1122
- })
1123
- local value = nil
1124
- do
1125
- local out_body = {
1126
- Run(function(self)
1127
- if name then
1128
- self:put_name(name)
1129
- end
1130
- return self:set("super", function(block, chain)
1131
- if chain then
1132
- local slice
1133
- do
1134
- local _accum_0 = { }
1135
- local _len_0 = 1
1136
- for _index_0 = 3, #chain do
1137
- local item = chain[_index_0]
1138
- _accum_0[_len_0] = item
1139
- _len_0 = _len_0 + 1
1140
- end
1141
- slice = _accum_0
1142
- end
1143
- local new_chain = {
1144
- "chain",
1145
- parent_cls_name
1146
- }
1147
- local head = slice[1]
1148
- if head == nil then
1149
- return parent_cls_name
1150
- end
1151
- local _exp_1 = head[1]
1152
- if "call" == _exp_1 then
1153
- local calling_name = block:get("current_block")
1154
- slice[1] = {
1155
- "call",
1156
- {
1157
- "self",
1158
- unpack(head[2])
1159
- }
1160
- }
1161
- if ntype(calling_name) == "key_literal" then
1162
- insert(new_chain, {
1163
- "dot",
1164
- calling_name[2]
1165
- })
1166
- else
1167
- insert(new_chain, {
1168
- "index",
1169
- calling_name
1170
- })
1171
- end
1172
- elseif "colon" == _exp_1 then
1173
- local call = head[3]
1174
- insert(new_chain, {
1175
- "dot",
1176
- head[2]
1177
- })
1178
- slice[1] = {
1179
- "call",
1180
- {
1181
- "self",
1182
- unpack(call[2])
1183
- }
1184
- }
1185
- end
1186
- for _index_0 = 1, #slice do
1187
- local item = slice[_index_0]
1188
- insert(new_chain, item)
1189
- end
1190
- return new_chain
1191
- else
1192
- return parent_cls_name
1193
- end
1194
- end)
1195
- end),
1196
- {
1197
- "declare_glob",
1198
- "*"
1199
- },
1200
- parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP,
1201
- build.assign_one(base_name, {
1202
- "table",
1203
- properties
1204
- }),
1205
- build.assign_one(base_name:chain("__index"), base_name),
1206
- parent_val and build.chain({
1207
- base = "setmetatable",
1208
- {
1209
- "call",
1210
- {
1211
- base_name,
1212
- build.chain({
1213
- base = parent_cls_name,
1214
- {
1215
- "dot",
1216
- "__base"
1217
- }
1218
- })
1219
- }
1220
- }
1221
- }) or NOOP,
1222
- build.assign_one(cls_name, cls),
1223
- build.assign_one(base_name:chain("__class"), cls_name),
1224
- build.group((function()
1225
- if #statements > 0 then
1226
- return {
1227
- build.assign_one(LocalName("self"), cls_name),
1228
- build.group(statements)
1229
- }
1230
- end
1231
- end)()),
1232
- parent_val and build["if"]({
1233
- cond = {
1234
- "exp",
1235
- parent_cls_name:chain("__inherited")
1236
- },
1237
- ["then"] = {
1238
- parent_cls_name:chain("__inherited", {
1239
- "call",
1240
- {
1241
- parent_cls_name,
1242
- cls_name
1243
- }
1244
- })
1245
- }
1246
- }) or NOOP,
1247
- build.group((function()
1248
- if name then
1249
- return {
1250
- build.assign_one(name, cls_name)
1251
- }
1252
- end
1253
- end)()),
1254
- (function()
1255
- if ret then
1256
- return ret(cls_name)
1257
- end
1258
- end)()
1259
- }
1260
- value = build.group({
1261
- build.group((function()
1262
- if ntype(name) == "value" then
1263
- return {
1264
- build.declare({
1265
- names = {
1266
- name
1267
- }
1268
- })
1269
- }
1270
- end
1271
- end)()),
1272
- build["do"](out_body)
1273
- })
1274
- end
1275
- return value
1276
- end
1277
- })
1278
- do
1279
- local _base_0 = {
1280
- body_idx = {
1281
- ["for"] = 4,
1282
- ["while"] = 3,
1283
- foreach = 4
1284
- },
1285
- convert = function(self, node)
1286
- local index = self.body_idx[ntype(node)]
1287
- node[index] = self:mutate_body(node[index])
1288
- return self:wrap(node)
1289
- end,
1290
- wrap = function(self, node, group_type)
1291
- if group_type == nil then
1292
- group_type = "block_exp"
1293
- end
1294
- return build[group_type]({
1295
- build.assign_one(self.accum_name, build.table()),
1296
- build.assign_one(self.len_name, 1),
1297
- node,
1298
- group_type == "block_exp" and self.accum_name or NOOP
1299
- })
1300
- end,
1301
- mutate_body = function(self, body)
1302
- local single_stm = is_singular(body)
1303
- local val
1304
- if single_stm and types.is_value(single_stm) then
1305
- body = { }
1306
- val = single_stm
1307
- else
1308
- body = apply_to_last(body, function(n)
1309
- if types.is_value(n) then
1310
- return build.assign_one(self.value_name, n)
1311
- else
1312
- return build.group({
1313
- {
1314
- "declare",
1315
- {
1316
- self.value_name
1317
- }
1318
- },
1319
- n
1320
- })
1321
- end
1322
- end)
1323
- val = self.value_name
1324
- end
1325
- local update = {
1326
- build.assign_one(NameProxy.index(self.accum_name, self.len_name), val),
1327
- {
1328
- "update",
1329
- self.len_name,
1330
- "+=",
1331
- 1
1332
- }
1333
- }
1334
- insert(body, build.group(update))
1335
- return body
1336
- end
1337
- }
1338
- _base_0.__index = _base_0
1339
- local _class_0 = setmetatable({
1340
- __init = function(self, accum_name)
1341
- self.accum_name = NameProxy("accum")
1342
- self.value_name = NameProxy("value")
1343
- self.len_name = NameProxy("len")
1344
- end,
1345
- __base = _base_0,
1346
- __name = "Accumulator"
1347
- }, {
1348
- __index = _base_0,
1349
- __call = function(cls, ...)
1350
- local _self_0 = setmetatable({}, _base_0)
1351
- cls.__init(_self_0, ...)
1352
- return _self_0
1353
- end
1354
- })
1355
- _base_0.__class = _class_0
1356
- Accumulator = _class_0
1357
- end
1358
- default_accumulator = function(self, node)
1359
- return Accumulator():convert(node)
1360
- end
1361
- implicitly_return = function(scope)
1362
- local is_top = true
1363
- local fn
1364
- fn = function(stm)
1365
- local t = ntype(stm)
1366
- if t == "decorated" then
1367
- stm = scope.transform.statement(stm)
1368
- t = ntype(stm)
1369
- end
1370
- if types.cascading[t] then
1371
- is_top = false
1372
- return scope.transform.statement(stm, fn)
1373
- elseif types.manual_return[t] or not types.is_value(stm) then
1374
- if is_top and t == "return" and stm[2] == "" then
1375
- return NOOP
1376
- else
1377
- return stm
1378
- end
1379
- else
1380
- if t == "comprehension" and not types.comprehension_has_value(stm) then
1381
- return stm
1382
- else
1383
- return {
1384
- "return",
1385
- stm
1386
- }
1387
- end
1388
- end
1389
- end
1390
- return fn
1391
- end
1392
- Value = Transformer({
1393
- ["for"] = default_accumulator,
1394
- ["while"] = default_accumulator,
1395
- foreach = default_accumulator,
1396
- ["do"] = function(self, node)
1397
- return build.block_exp(node[2])
1398
- end,
1399
- decorated = function(self, node)
1400
- return self.transform.statement(node)
1401
- end,
1402
- class = function(self, node)
1403
- return build.block_exp({
1404
- node
1405
- })
1406
- end,
1407
- string = function(self, node)
1408
- local delim = node[2]
1409
- local convert_part
1410
- convert_part = function(part)
1411
- if type(part) == "string" or part == nil then
1412
- return {
1413
- "string",
1414
- delim,
1415
- part or ""
1416
- }
1417
- else
1418
- return build.chain({
1419
- base = "tostring",
1420
- {
1421
- "call",
1422
- {
1423
- part[2]
1424
- }
1425
- }
1426
- })
1427
- end
1428
- end
1429
- if #node <= 3 then
1430
- if type(node[3]) == "string" then
1431
- return node
1432
- else
1433
- return convert_part(node[3])
1434
- end
1435
- end
1436
- local e = {
1437
- "exp",
1438
- convert_part(node[3])
1439
- }
1440
- for i = 4, #node do
1441
- insert(e, "..")
1442
- insert(e, convert_part(node[i]))
1443
- end
1444
- return e
1445
- end,
1446
- comprehension = function(self, node)
1447
- local a = Accumulator()
1448
- node = self.transform.statement(node, function(exp)
1449
- return a:mutate_body({
1450
- exp
1451
- })
1452
- end)
1453
- return a:wrap(node)
1454
- end,
1455
- tblcomprehension = function(self, node)
1456
- local _, explist, clauses = unpack(node)
1457
- local key_exp, value_exp = unpack(explist)
1458
- local accum = NameProxy("tbl")
1459
- local inner
1460
- if value_exp then
1461
- local dest = build.chain({
1462
- base = accum,
1463
- {
1464
- "index",
1465
- key_exp
1466
- }
1467
- })
1468
- inner = {
1469
- build.assign_one(dest, value_exp)
1470
- }
1471
- else
1472
- local key_name, val_name = NameProxy("key"), NameProxy("val")
1473
- local dest = build.chain({
1474
- base = accum,
1475
- {
1476
- "index",
1477
- key_name
1478
- }
1479
- })
1480
- inner = {
1481
- build.assign({
1482
- names = {
1483
- key_name,
1484
- val_name
1485
- },
1486
- values = {
1487
- key_exp
1488
- }
1489
- }),
1490
- build.assign_one(dest, val_name)
1491
- }
1492
- end
1493
- return build.block_exp({
1494
- build.assign_one(accum, build.table()),
1495
- construct_comprehension(inner, clauses),
1496
- accum
1497
- })
1498
- end,
1499
- fndef = function(self, node)
1500
- smart_node(node)
1501
- node.body = apply_to_last(node.body, implicitly_return(self))
1502
- node.body = {
1503
- Run(function(self)
1504
- return self:listen("varargs", function() end)
1505
- end),
1506
- unpack(node.body)
1507
- }
1508
- return node
1509
- end,
1510
- ["if"] = function(self, node)
1511
- return build.block_exp({
1512
- node
1513
- })
1514
- end,
1515
- unless = function(self, node)
1516
- return build.block_exp({
1517
- node
1518
- })
1519
- end,
1520
- with = function(self, node)
1521
- return build.block_exp({
1522
- node
1523
- })
1524
- end,
1525
- switch = function(self, node)
1526
- return build.block_exp({
1527
- node
1528
- })
1529
- end,
1530
- chain = function(self, node)
1531
- local stub = node[#node]
1532
- for i = 3, #node do
1533
- local part = node[i]
1534
- if ntype(part) == "dot" and data.lua_keywords[part[2]] then
1535
- node[i] = {
1536
- "index",
1537
- {
1538
- "string",
1539
- '"',
1540
- part[2]
1541
- }
1542
- }
1543
- end
1544
- end
1545
- if ntype(node[2]) == "string" then
1546
- node[2] = {
1547
- "parens",
1548
- node[2]
1549
- }
1550
- elseif type(stub) == "table" and stub[1] == "colon_stub" then
1551
- table.remove(node, #node)
1552
- local base_name = NameProxy("base")
1553
- local fn_name = NameProxy("fn")
1554
- local is_super = ntype(node[2]) == "ref" and node[2][2] == "super"
1555
- return build.block_exp({
1556
- build.assign({
1557
- names = {
1558
- base_name
1559
- },
1560
- values = {
1561
- node
1562
- }
1563
- }),
1564
- build.assign({
1565
- names = {
1566
- fn_name
1567
- },
1568
- values = {
1569
- build.chain({
1570
- base = base_name,
1571
- {
1572
- "dot",
1573
- stub[2]
1574
- }
1575
- })
1576
- }
1577
- }),
1578
- build.fndef({
1579
- args = {
1580
- {
1581
- "..."
1582
- }
1583
- },
1584
- body = {
1585
- build.chain({
1586
- base = fn_name,
1587
- {
1588
- "call",
1589
- {
1590
- is_super and "self" or base_name,
1591
- "..."
1592
- }
1593
- }
1594
- })
1595
- }
1596
- })
1597
- })
1598
- end
1599
- end,
1600
- block_exp = function(self, node)
1601
- local _, body = unpack(node)
1602
- local fn = nil
1603
- local arg_list = { }
1604
- fn = smart_node(build.fndef({
1605
- body = {
1606
- Run(function(self)
1607
- return self:listen("varargs", function()
1608
- insert(arg_list, "...")
1609
- insert(fn.args, {
1610
- "..."
1611
- })
1612
- return self:unlisten("varargs")
1613
- end)
1614
- end),
1615
- unpack(body)
1616
- }
1617
- }))
1618
- return build.chain({
1619
- base = {
1620
- "parens",
1621
- fn
1622
- },
1623
- {
1624
- "call",
1625
- arg_list
1626
- }
1627
- })
1628
- end
1629
- })
1630
1
  return {
1631
- Statement = Statement,
1632
- Value = Value,
1633
- Run = Run
2
+ Statement = require("moonscript.transform.statement"),
3
+ Value = require("moonscript.transform.value")
1634
4
  }