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.
@@ -0,0 +1,54 @@
1
+ local is_value
2
+ is_value = require("moonscript.types").is_value
3
+ local construct_comprehension
4
+ construct_comprehension = function(inner, clauses)
5
+ local current_stms = inner
6
+ for i = #clauses, 1, -1 do
7
+ local clause = clauses[i]
8
+ local t = clause[1]
9
+ local _exp_0 = t
10
+ if "for" == _exp_0 then
11
+ local _, name, bounds
12
+ _, name, bounds = clause[1], clause[2], clause[3]
13
+ current_stms = {
14
+ "for",
15
+ name,
16
+ bounds,
17
+ current_stms
18
+ }
19
+ elseif "foreach" == _exp_0 then
20
+ local _, names, iter
21
+ _, names, iter = clause[1], clause[2], clause[3]
22
+ current_stms = {
23
+ "foreach",
24
+ names,
25
+ {
26
+ iter
27
+ },
28
+ current_stms
29
+ }
30
+ elseif "when" == _exp_0 then
31
+ local _, cond
32
+ _, cond = clause[1], clause[2]
33
+ current_stms = {
34
+ "if",
35
+ cond,
36
+ current_stms
37
+ }
38
+ else
39
+ current_stms = error("Unknown comprehension clause: " .. t)
40
+ end
41
+ current_stms = {
42
+ current_stms
43
+ }
44
+ end
45
+ return current_stms[1]
46
+ end
47
+ local comprehension_has_value
48
+ comprehension_has_value = function(comp)
49
+ return is_value(comp[2])
50
+ end
51
+ return {
52
+ construct_comprehension = construct_comprehension,
53
+ comprehension_has_value = comprehension_has_value
54
+ }
@@ -68,7 +68,7 @@ extract_assign_names = function(name, accum, prefix)
68
68
  local s
69
69
  if ntype(key) == "key_literal" then
70
70
  local key_name = key[2]
71
- if ntype(key_name) == "colon_stub" then
71
+ if ntype(key_name) == "colon" then
72
72
  s = key_name
73
73
  else
74
74
  s = {
@@ -228,5 +228,6 @@ end
228
228
  return {
229
229
  has_destructure = has_destructure,
230
230
  split_assign = split_assign,
231
- build_assign = build_assign
231
+ build_assign = build_assign,
232
+ extract_assign_names = extract_assign_names
232
233
  }
@@ -4,13 +4,14 @@ local unpack
4
4
  unpack = require("moonscript.util").unpack
5
5
  local LocalName
6
6
  do
7
+ local _class_0
7
8
  local _base_0 = {
8
9
  get_name = function(self)
9
10
  return self.name
10
11
  end
11
12
  }
12
13
  _base_0.__index = _base_0
13
- local _class_0 = setmetatable({
14
+ _class_0 = setmetatable({
14
15
  __init = function(self, name)
15
16
  self.name = name
16
17
  self[1] = "temp_name"
@@ -30,6 +31,7 @@ do
30
31
  end
31
32
  local NameProxy
32
33
  do
34
+ local _class_0
33
35
  local _base_0 = {
34
36
  get_name = function(self, scope, dont_put)
35
37
  if dont_put == nil then
@@ -81,7 +83,7 @@ do
81
83
  end
82
84
  }
83
85
  _base_0.__index = _base_0
84
- local _class_0 = setmetatable({
86
+ _class_0 = setmetatable({
85
87
  __init = function(self, prefix)
86
88
  self.prefix = prefix
87
89
  self[1] = "temp_name"
@@ -0,0 +1,741 @@
1
+ local Transformer
2
+ Transformer = require("moonscript.transform.transformer").Transformer
3
+ local NameProxy
4
+ NameProxy = require("moonscript.transform.names").NameProxy
5
+ local Run, transform_last_stm, implicitly_return, last_stm
6
+ do
7
+ local _obj_0 = require("moonscript.transform.statements")
8
+ Run, transform_last_stm, implicitly_return, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.last_stm
9
+ end
10
+ local types = require("moonscript.types")
11
+ local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
12
+ build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP = types.build, types.ntype, types.is_value, types.smart_node, types.value_is_singular, types.is_slice, types.NOOP
13
+ local insert
14
+ insert = table.insert
15
+ local destructure = require("moonscript.transform.destructure")
16
+ local construct_comprehension
17
+ construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
18
+ local unpack
19
+ unpack = require("moonscript.util").unpack
20
+ local with_continue_listener
21
+ with_continue_listener = function(body)
22
+ local continue_name = nil
23
+ return {
24
+ Run(function(self)
25
+ return self:listen("continue", function()
26
+ if not (continue_name) then
27
+ continue_name = NameProxy("continue")
28
+ self:put_name(continue_name)
29
+ end
30
+ return continue_name
31
+ end)
32
+ end),
33
+ build.group(body),
34
+ Run(function(self)
35
+ if not (continue_name) then
36
+ return
37
+ end
38
+ local last = last_stm(body)
39
+ local enclose_lines = types.terminating[last and ntype(last)]
40
+ self:put_name(continue_name, nil)
41
+ return self:splice(function(lines)
42
+ if enclose_lines then
43
+ lines = {
44
+ "do",
45
+ {
46
+ lines
47
+ }
48
+ }
49
+ end
50
+ return {
51
+ {
52
+ "assign",
53
+ {
54
+ continue_name
55
+ },
56
+ {
57
+ "false"
58
+ }
59
+ },
60
+ {
61
+ "repeat",
62
+ "true",
63
+ {
64
+ lines,
65
+ {
66
+ "assign",
67
+ {
68
+ continue_name
69
+ },
70
+ {
71
+ "true"
72
+ }
73
+ }
74
+ }
75
+ },
76
+ {
77
+ "if",
78
+ {
79
+ "not",
80
+ continue_name
81
+ },
82
+ {
83
+ {
84
+ "break"
85
+ }
86
+ }
87
+ }
88
+ }
89
+ end)
90
+ end)
91
+ }
92
+ end
93
+ local extract_declarations
94
+ extract_declarations = function(self, body, start, out)
95
+ if body == nil then
96
+ body = self.current_stms
97
+ end
98
+ if start == nil then
99
+ start = self.current_stm_i + 1
100
+ end
101
+ if out == nil then
102
+ out = { }
103
+ end
104
+ for i = start, #body do
105
+ local _continue_0 = false
106
+ repeat
107
+ local stm = body[i]
108
+ if stm == nil then
109
+ _continue_0 = true
110
+ break
111
+ end
112
+ stm = self.transform.statement(stm)
113
+ body[i] = stm
114
+ local _exp_0 = stm[1]
115
+ if "assign" == _exp_0 or "declare" == _exp_0 then
116
+ local _list_0 = stm[2]
117
+ for _index_0 = 1, #_list_0 do
118
+ local name = _list_0[_index_0]
119
+ if ntype(name) == "ref" then
120
+ insert(out, name)
121
+ elseif type(name) == "string" then
122
+ insert(out, name)
123
+ end
124
+ end
125
+ elseif "group" == _exp_0 then
126
+ extract_declarations(self, stm[2], 1, out)
127
+ end
128
+ _continue_0 = true
129
+ until true
130
+ if not _continue_0 then
131
+ break
132
+ end
133
+ end
134
+ return out
135
+ end
136
+ local expand_elseif_assign
137
+ expand_elseif_assign = function(ifstm)
138
+ for i = 4, #ifstm do
139
+ local case = ifstm[i]
140
+ if ntype(case) == "elseif" and ntype(case[2]) == "assign" then
141
+ local split = {
142
+ unpack(ifstm, 1, i - 1)
143
+ }
144
+ insert(split, {
145
+ "else",
146
+ {
147
+ {
148
+ "if",
149
+ case[2],
150
+ case[3],
151
+ unpack(ifstm, i + 1)
152
+ }
153
+ }
154
+ })
155
+ return split
156
+ end
157
+ end
158
+ return ifstm
159
+ end
160
+ return Transformer({
161
+ transform = function(self, tuple)
162
+ local _, node, fn
163
+ _, node, fn = tuple[1], tuple[2], tuple[3]
164
+ return fn(node)
165
+ end,
166
+ root_stms = function(self, body)
167
+ return transform_last_stm(body, implicitly_return(self))
168
+ end,
169
+ ["return"] = function(self, node)
170
+ local ret_val = node[2]
171
+ local ret_val_type = ntype(ret_val)
172
+ if ret_val_type == "explist" and #ret_val == 2 then
173
+ ret_val = ret_val[2]
174
+ ret_val_type = ntype(ret_val)
175
+ end
176
+ if types.cascading[ret_val_type] then
177
+ return implicitly_return(self)(ret_val)
178
+ end
179
+ if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then
180
+ local Value = require("moonscript.transform.value")
181
+ ret_val = Value:transform_once(self, ret_val)
182
+ if ntype(ret_val) == "block_exp" then
183
+ return build.group(transform_last_stm(ret_val[2], function(stm)
184
+ return {
185
+ "return",
186
+ stm
187
+ }
188
+ end))
189
+ end
190
+ end
191
+ node[2] = ret_val
192
+ return node
193
+ end,
194
+ declare_glob = function(self, node)
195
+ local names = extract_declarations(self)
196
+ if node[2] == "^" then
197
+ do
198
+ local _accum_0 = { }
199
+ local _len_0 = 1
200
+ for _index_0 = 1, #names do
201
+ local _continue_0 = false
202
+ repeat
203
+ local name = names[_index_0]
204
+ if not (name[2]:match("^%u")) then
205
+ _continue_0 = true
206
+ break
207
+ end
208
+ local _value_0 = name
209
+ _accum_0[_len_0] = _value_0
210
+ _len_0 = _len_0 + 1
211
+ _continue_0 = true
212
+ until true
213
+ if not _continue_0 then
214
+ break
215
+ end
216
+ end
217
+ names = _accum_0
218
+ end
219
+ end
220
+ return {
221
+ "declare",
222
+ names
223
+ }
224
+ end,
225
+ assign = function(self, node)
226
+ local names, values = unpack(node, 2)
227
+ local num_values = #values
228
+ local num_names = #values
229
+ if num_names == 1 and num_values == 1 then
230
+ local first_value = values[1]
231
+ local first_name = names[1]
232
+ local first_type = ntype(first_value)
233
+ if first_type == "chain" then
234
+ local Value = require("moonscript.transform.value")
235
+ first_value = Value:transform_once(self, first_value)
236
+ first_type = ntype(first_value)
237
+ end
238
+ local _exp_0 = ntype(first_value)
239
+ if "block_exp" == _exp_0 then
240
+ local block_body = first_value[2]
241
+ local idx = #block_body
242
+ block_body[idx] = build.assign_one(first_name, block_body[idx])
243
+ return build.group({
244
+ {
245
+ "declare",
246
+ {
247
+ first_name
248
+ }
249
+ },
250
+ {
251
+ "do",
252
+ block_body
253
+ }
254
+ })
255
+ elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then
256
+ local Value = require("moonscript.transform.value")
257
+ return build.assign_one(first_name, Value:transform_once(self, first_value))
258
+ else
259
+ values[1] = first_value
260
+ end
261
+ end
262
+ local transformed
263
+ if num_values == 1 then
264
+ local value = values[1]
265
+ local t = ntype(value)
266
+ if t == "decorated" then
267
+ value = self.transform.statement(value)
268
+ t = ntype(value)
269
+ end
270
+ if types.cascading[t] then
271
+ local ret
272
+ ret = function(stm)
273
+ if is_value(stm) then
274
+ return {
275
+ "assign",
276
+ names,
277
+ {
278
+ stm
279
+ }
280
+ }
281
+ else
282
+ return stm
283
+ end
284
+ end
285
+ transformed = build.group({
286
+ {
287
+ "declare",
288
+ names
289
+ },
290
+ self.transform.statement(value, ret, node)
291
+ })
292
+ end
293
+ end
294
+ node = transformed or node
295
+ if destructure.has_destructure(names) then
296
+ return destructure.split_assign(self, node)
297
+ end
298
+ return node
299
+ end,
300
+ continue = function(self, node)
301
+ local continue_name = self:send("continue")
302
+ if not (continue_name) then
303
+ error("continue must be inside of a loop")
304
+ end
305
+ return build.group({
306
+ build.assign_one(continue_name, "true"),
307
+ {
308
+ "break"
309
+ }
310
+ })
311
+ end,
312
+ export = function(self, node)
313
+ if #node > 2 then
314
+ if node[2] == "class" then
315
+ local cls = smart_node(node[3])
316
+ return build.group({
317
+ {
318
+ "export",
319
+ {
320
+ cls.name
321
+ }
322
+ },
323
+ cls
324
+ })
325
+ else
326
+ return build.group({
327
+ {
328
+ "export",
329
+ node[2]
330
+ },
331
+ build.assign({
332
+ names = node[2],
333
+ values = node[3]
334
+ })
335
+ })
336
+ end
337
+ else
338
+ return nil
339
+ end
340
+ end,
341
+ update = function(self, node)
342
+ local _, name, op, exp = unpack(node)
343
+ local op_final = op:match("^(.+)=$")
344
+ if not op_final then
345
+ error("Unknown op: " .. op)
346
+ end
347
+ if not (value_is_singular(exp)) then
348
+ exp = {
349
+ "parens",
350
+ exp
351
+ }
352
+ end
353
+ return build.assign_one(name, {
354
+ "exp",
355
+ name,
356
+ op_final,
357
+ exp
358
+ })
359
+ end,
360
+ import = function(self, node)
361
+ local _, names, source = unpack(node)
362
+ local table_values
363
+ do
364
+ local _accum_0 = { }
365
+ local _len_0 = 1
366
+ for _index_0 = 1, #names do
367
+ local name = names[_index_0]
368
+ local dest_name
369
+ if ntype(name) == "colon" then
370
+ dest_name = name[2]
371
+ else
372
+ dest_name = name
373
+ end
374
+ local _value_0 = {
375
+ {
376
+ "key_literal",
377
+ name
378
+ },
379
+ dest_name
380
+ }
381
+ _accum_0[_len_0] = _value_0
382
+ _len_0 = _len_0 + 1
383
+ end
384
+ table_values = _accum_0
385
+ end
386
+ local dest = {
387
+ "table",
388
+ table_values
389
+ }
390
+ return {
391
+ "assign",
392
+ {
393
+ dest
394
+ },
395
+ {
396
+ source
397
+ },
398
+ [-1] = node[-1]
399
+ }
400
+ end,
401
+ comprehension = function(self, node, action)
402
+ local _, exp, clauses = unpack(node)
403
+ action = action or function(exp)
404
+ return {
405
+ exp
406
+ }
407
+ end
408
+ return construct_comprehension(action(exp), clauses)
409
+ end,
410
+ ["do"] = function(self, node, ret)
411
+ if ret then
412
+ node[2] = transform_last_stm(node[2], ret)
413
+ end
414
+ return node
415
+ end,
416
+ decorated = function(self, node)
417
+ local stm, dec = unpack(node, 2)
418
+ local wrapped
419
+ local _exp_0 = dec[1]
420
+ if "if" == _exp_0 then
421
+ local cond, fail = unpack(dec, 2)
422
+ if fail then
423
+ fail = {
424
+ "else",
425
+ {
426
+ fail
427
+ }
428
+ }
429
+ end
430
+ wrapped = {
431
+ "if",
432
+ cond,
433
+ {
434
+ stm
435
+ },
436
+ fail
437
+ }
438
+ elseif "unless" == _exp_0 then
439
+ wrapped = {
440
+ "unless",
441
+ dec[2],
442
+ {
443
+ stm
444
+ }
445
+ }
446
+ elseif "comprehension" == _exp_0 then
447
+ wrapped = {
448
+ "comprehension",
449
+ stm,
450
+ dec[2]
451
+ }
452
+ else
453
+ wrapped = error("Unknown decorator " .. dec[1])
454
+ end
455
+ if ntype(stm) == "assign" then
456
+ wrapped = build.group({
457
+ build.declare({
458
+ names = (function()
459
+ local _accum_0 = { }
460
+ local _len_0 = 1
461
+ local _list_0 = stm[2]
462
+ for _index_0 = 1, #_list_0 do
463
+ local name = _list_0[_index_0]
464
+ if ntype(name) == "ref" then
465
+ _accum_0[_len_0] = name
466
+ _len_0 = _len_0 + 1
467
+ end
468
+ end
469
+ return _accum_0
470
+ end)()
471
+ }),
472
+ wrapped
473
+ })
474
+ end
475
+ return wrapped
476
+ end,
477
+ unless = function(self, node)
478
+ return {
479
+ "if",
480
+ {
481
+ "not",
482
+ {
483
+ "parens",
484
+ node[2]
485
+ }
486
+ },
487
+ unpack(node, 3)
488
+ }
489
+ end,
490
+ ["if"] = function(self, node, ret)
491
+ if ntype(node[2]) == "assign" then
492
+ local _, assign, body = unpack(node)
493
+ if destructure.has_destructure(assign[2]) then
494
+ local name = NameProxy("des")
495
+ body = {
496
+ destructure.build_assign(self, assign[2][1], name),
497
+ build.group(node[3])
498
+ }
499
+ return build["do"]({
500
+ build.assign_one(name, assign[3][1]),
501
+ {
502
+ "if",
503
+ name,
504
+ body,
505
+ unpack(node, 4)
506
+ }
507
+ })
508
+ else
509
+ local name = assign[2][1]
510
+ return build["do"]({
511
+ assign,
512
+ {
513
+ "if",
514
+ name,
515
+ unpack(node, 3)
516
+ }
517
+ })
518
+ end
519
+ end
520
+ node = expand_elseif_assign(node)
521
+ if ret then
522
+ smart_node(node)
523
+ node['then'] = transform_last_stm(node['then'], ret)
524
+ for i = 4, #node do
525
+ local case = node[i]
526
+ local body_idx = #node[i]
527
+ case[body_idx] = transform_last_stm(case[body_idx], ret)
528
+ end
529
+ end
530
+ return node
531
+ end,
532
+ with = function(self, node, ret)
533
+ local exp, block = unpack(node, 2)
534
+ local copy_scope = true
535
+ local scope_name, named_assign
536
+ do
537
+ local last = last_stm(block)
538
+ if last then
539
+ if types.terminating[ntype(last)] then
540
+ ret = false
541
+ end
542
+ end
543
+ end
544
+ if ntype(exp) == "assign" then
545
+ local names, values = unpack(exp, 2)
546
+ local first_name = names[1]
547
+ if ntype(first_name) == "ref" then
548
+ scope_name = first_name
549
+ named_assign = exp
550
+ exp = values[1]
551
+ copy_scope = false
552
+ else
553
+ scope_name = NameProxy("with")
554
+ exp = values[1]
555
+ values[1] = scope_name
556
+ named_assign = {
557
+ "assign",
558
+ names,
559
+ values
560
+ }
561
+ end
562
+ elseif self:is_local(exp) then
563
+ scope_name = exp
564
+ copy_scope = false
565
+ end
566
+ scope_name = scope_name or NameProxy("with")
567
+ local out = build["do"]({
568
+ copy_scope and build.assign_one(scope_name, exp) or NOOP,
569
+ named_assign or NOOP,
570
+ Run(function(self)
571
+ return self:set("scope_var", scope_name)
572
+ end),
573
+ unpack(block)
574
+ })
575
+ if ret then
576
+ table.insert(out[2], ret(scope_name))
577
+ end
578
+ return out
579
+ end,
580
+ foreach = function(self, node, _)
581
+ smart_node(node)
582
+ local source = unpack(node.iter)
583
+ local destructures = { }
584
+ do
585
+ local _accum_0 = { }
586
+ local _len_0 = 1
587
+ for i, name in ipairs(node.names) do
588
+ if ntype(name) == "table" then
589
+ do
590
+ local proxy = NameProxy("des")
591
+ insert(destructures, destructure.build_assign(self, name, proxy))
592
+ _accum_0[_len_0] = proxy
593
+ end
594
+ else
595
+ _accum_0[_len_0] = name
596
+ end
597
+ _len_0 = _len_0 + 1
598
+ end
599
+ node.names = _accum_0
600
+ end
601
+ if next(destructures) then
602
+ insert(destructures, build.group(node.body))
603
+ node.body = destructures
604
+ end
605
+ if ntype(source) == "unpack" then
606
+ local list = source[2]
607
+ local index_name = NameProxy("index")
608
+ local list_name = self:is_local(list) and list or NameProxy("list")
609
+ local slice_var = nil
610
+ local bounds
611
+ if is_slice(list) then
612
+ local slice = list[#list]
613
+ table.remove(list)
614
+ table.remove(slice, 1)
615
+ if self:is_local(list) then
616
+ list_name = list
617
+ end
618
+ if slice[2] and slice[2] ~= "" then
619
+ local max_tmp_name = NameProxy("max")
620
+ slice_var = build.assign_one(max_tmp_name, slice[2])
621
+ slice[2] = {
622
+ "exp",
623
+ max_tmp_name,
624
+ "<",
625
+ 0,
626
+ "and",
627
+ {
628
+ "length",
629
+ list_name
630
+ },
631
+ "+",
632
+ max_tmp_name,
633
+ "or",
634
+ max_tmp_name
635
+ }
636
+ else
637
+ slice[2] = {
638
+ "length",
639
+ list_name
640
+ }
641
+ end
642
+ bounds = slice
643
+ else
644
+ bounds = {
645
+ 1,
646
+ {
647
+ "length",
648
+ list_name
649
+ }
650
+ }
651
+ end
652
+ return build.group({
653
+ list_name ~= list and build.assign_one(list_name, list) or NOOP,
654
+ slice_var or NOOP,
655
+ build["for"]({
656
+ name = index_name,
657
+ bounds = bounds,
658
+ body = {
659
+ {
660
+ "assign",
661
+ node.names,
662
+ {
663
+ NameProxy.index(list_name, index_name)
664
+ }
665
+ },
666
+ build.group(node.body)
667
+ }
668
+ })
669
+ })
670
+ end
671
+ node.body = with_continue_listener(node.body)
672
+ end,
673
+ ["while"] = function(self, node)
674
+ smart_node(node)
675
+ node.body = with_continue_listener(node.body)
676
+ end,
677
+ ["for"] = function(self, node)
678
+ smart_node(node)
679
+ node.body = with_continue_listener(node.body)
680
+ end,
681
+ switch = function(self, node, ret)
682
+ local _, exp, conds = unpack(node)
683
+ local exp_name = NameProxy("exp")
684
+ local convert_cond
685
+ convert_cond = function(cond)
686
+ local t, case_exps, body = unpack(cond)
687
+ local out = { }
688
+ insert(out, t == "case" and "elseif" or "else")
689
+ if t ~= "else" then
690
+ local cond_exp = { }
691
+ for i, case in ipairs(case_exps) do
692
+ if i == 1 then
693
+ insert(cond_exp, "exp")
694
+ else
695
+ insert(cond_exp, "or")
696
+ end
697
+ if not (value_is_singular(case)) then
698
+ case = {
699
+ "parens",
700
+ case
701
+ }
702
+ end
703
+ insert(cond_exp, {
704
+ "exp",
705
+ case,
706
+ "==",
707
+ exp_name
708
+ })
709
+ end
710
+ insert(out, cond_exp)
711
+ else
712
+ body = case_exps
713
+ end
714
+ if ret then
715
+ body = transform_last_stm(body, ret)
716
+ end
717
+ insert(out, body)
718
+ return out
719
+ end
720
+ local first = true
721
+ local if_stm = {
722
+ "if"
723
+ }
724
+ for _index_0 = 1, #conds do
725
+ local cond = conds[_index_0]
726
+ local if_cond = convert_cond(cond)
727
+ if first then
728
+ first = false
729
+ insert(if_stm, if_cond[2])
730
+ insert(if_stm, if_cond[3])
731
+ else
732
+ insert(if_stm, if_cond)
733
+ end
734
+ end
735
+ return build.group({
736
+ build.assign_one(exp_name, exp),
737
+ if_stm
738
+ })
739
+ end,
740
+ class = require("moonscript.transform.class")
741
+ })