rufus-lua-moon 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,142 +1,17 @@
1
- module("moonscript.transform", package.seeall)
2
1
  local types = require("moonscript.types")
3
2
  local util = require("moonscript.util")
4
3
  local data = require("moonscript.data")
5
- local reversed = util.reversed
6
- local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular
4
+ local reversed, unpack = util.reversed, util.unpack
5
+ local 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
7
6
  local insert = table.insert
8
- local mtype = util.moon.type
9
- local implicitly_return
7
+ local NameProxy, LocalName
10
8
  do
11
- local _parent_0 = nil
12
- local _base_0 = {
13
- get_name = function(self)
14
- return self.name
15
- end
16
- }
17
- _base_0.__index = _base_0
18
- if _parent_0 then
19
- setmetatable(_base_0, _parent_0.__base)
20
- end
21
- local _class_0 = setmetatable({
22
- __init = function(self, name)
23
- self.name = name
24
- self[1] = "temp_name"
25
- end,
26
- __base = _base_0,
27
- __name = "LocalName",
28
- __parent = _parent_0
29
- }, {
30
- __index = function(cls, name)
31
- local val = rawget(_base_0, name)
32
- if val == nil and _parent_0 then
33
- return _parent_0[name]
34
- else
35
- return val
36
- end
37
- end,
38
- __call = function(cls, ...)
39
- local _self_0 = setmetatable({}, _base_0)
40
- cls.__init(_self_0, ...)
41
- return _self_0
42
- end
43
- })
44
- _base_0.__class = _class_0
45
- if _parent_0 and _parent_0.__inherited then
46
- _parent_0.__inherited(_parent_0, _class_0)
47
- end
48
- LocalName = _class_0
49
- end
50
- do
51
- local _parent_0 = nil
52
- local _base_0 = {
53
- get_name = function(self, scope)
54
- if not self.name then
55
- self.name = scope:free_name(self.prefix, true)
56
- end
57
- return self.name
58
- end,
59
- chain = function(self, ...)
60
- local items = {
61
- ...
62
- }
63
- items = (function()
64
- local _accum_0 = { }
65
- local _len_0 = 0
66
- local _list_0 = items
67
- for _index_0 = 1, #_list_0 do
68
- local i = _list_0[_index_0]
69
- local _value_0
70
- if type(i) == "string" then
71
- _value_0 = {
72
- "dot",
73
- i
74
- }
75
- else
76
- _value_0 = i
77
- end
78
- if _value_0 ~= nil then
79
- _len_0 = _len_0 + 1
80
- _accum_0[_len_0] = _value_0
81
- end
82
- end
83
- return _accum_0
84
- end)()
85
- return build.chain({
86
- base = self,
87
- unpack(items)
88
- })
89
- end,
90
- index = function(self, key)
91
- return build.chain({
92
- base = self,
93
- {
94
- "index",
95
- key
96
- }
97
- })
98
- end,
99
- __tostring = function(self)
100
- if self.name then
101
- return ("name<%s>"):format(self.name)
102
- else
103
- return ("name<prefix(%s)>"):format(self.prefix)
104
- end
105
- end
106
- }
107
- _base_0.__index = _base_0
108
- if _parent_0 then
109
- setmetatable(_base_0, _parent_0.__base)
110
- end
111
- local _class_0 = setmetatable({
112
- __init = function(self, prefix)
113
- self.prefix = prefix
114
- self[1] = "temp_name"
115
- end,
116
- __base = _base_0,
117
- __name = "NameProxy",
118
- __parent = _parent_0
119
- }, {
120
- __index = function(cls, name)
121
- local val = rawget(_base_0, name)
122
- if val == nil and _parent_0 then
123
- return _parent_0[name]
124
- else
125
- return val
126
- end
127
- end,
128
- __call = function(cls, ...)
129
- local _self_0 = setmetatable({}, _base_0)
130
- cls.__init(_self_0, ...)
131
- return _self_0
132
- end
133
- })
134
- _base_0.__class = _class_0
135
- if _parent_0 and _parent_0.__inherited then
136
- _parent_0.__inherited(_parent_0, _class_0)
137
- end
138
- NameProxy = _class_0
9
+ local _table_0 = require("moonscript.transform.names")
10
+ NameProxy, LocalName = _table_0.NameProxy, _table_0.LocalName
139
11
  end
12
+ local destructure = require("moonscript.transform.destructure")
13
+ local implicitly_return
14
+ local Run
140
15
  do
141
16
  local _parent_0 = nil
142
17
  local _base_0 = {
@@ -182,25 +57,21 @@ apply_to_last = function(stms, fn)
182
57
  local last_exp_id = 0
183
58
  for i = #stms, 1, -1 do
184
59
  local stm = stms[i]
185
- if stm and util.moon.type(stm) ~= Run then
60
+ if stm and mtype(stm) ~= Run then
186
61
  last_exp_id = i
187
62
  break
188
63
  end
189
64
  end
190
65
  return (function()
191
66
  local _accum_0 = { }
192
- local _len_0 = 0
67
+ local _len_0 = 1
193
68
  for i, stm in ipairs(stms) do
194
- local _value_0
195
69
  if i == last_exp_id then
196
- _value_0 = fn(stm)
70
+ _accum_0[_len_0] = fn(stm)
197
71
  else
198
- _value_0 = stm
199
- end
200
- if _value_0 ~= nil then
201
- _len_0 = _len_0 + 1
202
- _accum_0[_len_0] = _value_0
72
+ _accum_0[_len_0] = stm
203
73
  end
74
+ _len_0 = _len_0 + 1
204
75
  end
205
76
  return _accum_0
206
77
  end)()
@@ -213,48 +84,49 @@ is_singular = function(body)
213
84
  if "group" == ntype(body) then
214
85
  return is_singular(body[2])
215
86
  else
216
- return true
87
+ return body[1]
217
88
  end
218
89
  end
219
- local find_assigns
220
- find_assigns = function(body, out)
90
+ local extract_declarations
91
+ extract_declarations = function(self, body, start, out)
92
+ if body == nil then
93
+ body = self.current_stms
94
+ end
95
+ if start == nil then
96
+ start = self.current_stm_i + 1
97
+ end
221
98
  if out == nil then
222
99
  out = { }
223
100
  end
224
- local _list_0 = body
225
- for _index_0 = 1, #_list_0 do
226
- local thing = _list_0[_index_0]
227
- local _exp_0 = thing[1]
228
- if "group" == _exp_0 then
229
- find_assigns(thing[2], out)
230
- elseif "assign" == _exp_0 then
231
- table.insert(out, thing[2])
232
- end
233
- end
234
- return out
235
- end
236
- local hoist_declarations
237
- hoist_declarations = function(body)
238
- local assigns = { }
239
- local _list_0 = find_assigns(body)
240
- for _index_0 = 1, #_list_0 do
241
- local names = _list_0[_index_0]
242
- local _list_1 = names
243
- for _index_1 = 1, #_list_1 do
244
- local name = _list_1[_index_1]
245
- if type(name) == "string" then
246
- table.insert(assigns, name)
101
+ for i = start, #body do
102
+ local _continue_0 = false
103
+ repeat
104
+ local stm = body[i]
105
+ if stm == nil then
106
+ _continue_0 = true
107
+ break
108
+ end
109
+ stm = self.transform.statement(stm)
110
+ body[i] = stm
111
+ local _exp_0 = stm[1]
112
+ if "assign" == _exp_0 or "declare" == _exp_0 then
113
+ local _list_0 = stm[2]
114
+ for _index_0 = 1, #_list_0 do
115
+ local name = _list_0[_index_0]
116
+ if type(name) == "string" then
117
+ insert(out, name)
118
+ end
119
+ end
120
+ elseif "group" == _exp_0 then
121
+ extract_declarations(self, stm[2], 1, out)
247
122
  end
123
+ _continue_0 = true
124
+ until true
125
+ if not _continue_0 then
126
+ break
248
127
  end
249
128
  end
250
- local idx = 1
251
- while mtype(body[idx]) == Run do
252
- idx = idx + 1
253
- end
254
- return table.insert(body, idx, {
255
- "declare",
256
- assigns
257
- })
129
+ return out
258
130
  end
259
131
  local expand_elseif_assign
260
132
  expand_elseif_assign = function(ifstm)
@@ -420,9 +292,25 @@ construct_comprehension = function(inner, clauses)
420
292
  local current_stms = inner
421
293
  for _, clause in reversed(clauses) do
422
294
  local t = clause[1]
423
- if t == "for" then
295
+ local _exp_0 = t
296
+ if "for" == _exp_0 then
297
+ local name, bounds
298
+ do
299
+ local _obj_0 = clause
300
+ _, name, bounds = _obj_0[1], _obj_0[2], _obj_0[3]
301
+ end
302
+ current_stms = {
303
+ "for",
304
+ name,
305
+ bounds,
306
+ current_stms
307
+ }
308
+ elseif "foreach" == _exp_0 then
424
309
  local names, iter
425
- _, names, iter = unpack(clause)
310
+ do
311
+ local _obj_0 = clause
312
+ _, names, iter = _obj_0[1], _obj_0[2], _obj_0[3]
313
+ end
426
314
  current_stms = {
427
315
  "foreach",
428
316
  names,
@@ -431,9 +319,12 @@ construct_comprehension = function(inner, clauses)
431
319
  },
432
320
  current_stms
433
321
  }
434
- elseif t == "when" then
322
+ elseif "when" == _exp_0 then
435
323
  local cond
436
- _, cond = unpack(clause)
324
+ do
325
+ local _obj_0 = clause
326
+ _, cond = _obj_0[1], _obj_0[2]
327
+ end
437
328
  current_stms = {
438
329
  "if",
439
330
  cond,
@@ -448,10 +339,42 @@ construct_comprehension = function(inner, clauses)
448
339
  end
449
340
  return current_stms[1]
450
341
  end
451
- Statement = Transformer({
342
+ local Statement = Transformer({
452
343
  root_stms = function(self, body)
453
344
  return apply_to_last(body, implicitly_return(self))
454
345
  end,
346
+ declare_glob = function(self, node)
347
+ local names = extract_declarations(self)
348
+ if node[2] == "^" then
349
+ names = (function()
350
+ local _accum_0 = { }
351
+ local _len_0 = 1
352
+ local _list_0 = names
353
+ for _index_0 = 1, #_list_0 do
354
+ local _continue_0 = false
355
+ repeat
356
+ local name = _list_0[_index_0]
357
+ if not (name:match("^%u")) then
358
+ _continue_0 = true
359
+ break
360
+ end
361
+ local _value_0 = name
362
+ _accum_0[_len_0] = _value_0
363
+ _len_0 = _len_0 + 1
364
+ _continue_0 = true
365
+ until true
366
+ if not _continue_0 then
367
+ break
368
+ end
369
+ end
370
+ return _accum_0
371
+ end)()
372
+ end
373
+ return {
374
+ "declare",
375
+ names
376
+ }
377
+ end,
455
378
  assign = function(self, node)
456
379
  local names, values = unpack(node, 2)
457
380
  local transformed
@@ -486,7 +409,11 @@ Statement = Transformer({
486
409
  })
487
410
  end
488
411
  end
489
- return transformed or node
412
+ node = transformed or node
413
+ if destructure.has_destructure(names) then
414
+ return destructure.split_assign(node)
415
+ end
416
+ return node
490
417
  end,
491
418
  continue = function(self, node)
492
419
  local continue_name = self:send("continue")
@@ -549,37 +476,30 @@ Statement = Transformer({
549
476
  local _, names, source = unpack(node)
550
477
  local stubs = (function()
551
478
  local _accum_0 = { }
552
- local _len_0 = 0
479
+ local _len_0 = 1
553
480
  local _list_0 = names
554
481
  for _index_0 = 1, #_list_0 do
555
482
  local name = _list_0[_index_0]
556
- local _value_0
557
483
  if type(name) == "table" then
558
- _value_0 = name
484
+ _accum_0[_len_0] = name
559
485
  else
560
- _value_0 = {
486
+ _accum_0[_len_0] = {
561
487
  "dot",
562
488
  name
563
489
  }
564
490
  end
565
- if _value_0 ~= nil then
566
- _len_0 = _len_0 + 1
567
- _accum_0[_len_0] = _value_0
568
- end
491
+ _len_0 = _len_0 + 1
569
492
  end
570
493
  return _accum_0
571
494
  end)()
572
495
  local real_names = (function()
573
496
  local _accum_0 = { }
574
- local _len_0 = 0
497
+ local _len_0 = 1
575
498
  local _list_0 = names
576
499
  for _index_0 = 1, #_list_0 do
577
500
  local name = _list_0[_index_0]
578
- local _value_0 = type(name) == "table" and name[2] or name
579
- if _value_0 ~= nil then
580
- _len_0 = _len_0 + 1
581
- _accum_0[_len_0] = _value_0
582
- end
501
+ _accum_0[_len_0] = type(name) == "table" and name[2] or name
502
+ _len_0 = _len_0 + 1
583
503
  end
584
504
  return _accum_0
585
505
  end)()
@@ -588,15 +508,15 @@ Statement = Transformer({
588
508
  names = real_names,
589
509
  values = (function()
590
510
  local _accum_0 = { }
591
- local _len_0 = 0
511
+ local _len_0 = 1
592
512
  local _list_0 = stubs
593
513
  for _index_0 = 1, #_list_0 do
594
514
  local stub = _list_0[_index_0]
595
- _len_0 = _len_0 + 1
596
515
  _accum_0[_len_0] = build.chain({
597
516
  base = source,
598
517
  stub
599
518
  })
519
+ _len_0 = _len_0 + 1
600
520
  end
601
521
  return _accum_0
602
522
  end)()
@@ -614,15 +534,15 @@ Statement = Transformer({
614
534
  names = real_names,
615
535
  values = (function()
616
536
  local _accum_0 = { }
617
- local _len_0 = 0
537
+ local _len_0 = 1
618
538
  local _list_0 = stubs
619
539
  for _index_0 = 1, #_list_0 do
620
540
  local stub = _list_0[_index_0]
621
- _len_0 = _len_0 + 1
622
541
  _accum_0[_len_0] = build.chain({
623
542
  base = source_name,
624
543
  stub
625
544
  })
545
+ _len_0 = _len_0 + 1
626
546
  end
627
547
  return _accum_0
628
548
  end)()
@@ -690,13 +610,13 @@ Statement = Transformer({
690
610
  build.declare({
691
611
  names = (function()
692
612
  local _accum_0 = { }
693
- local _len_0 = 0
613
+ local _len_0 = 1
694
614
  local _list_0 = stm[2]
695
615
  for _index_0 = 1, #_list_0 do
696
616
  local name = _list_0[_index_0]
697
617
  if type(name) == "string" then
698
- _len_0 = _len_0 + 1
699
618
  _accum_0[_len_0] = name
619
+ _len_0 = _len_0 + 1
700
620
  end
701
621
  end
702
622
  return _accum_0
@@ -723,15 +643,32 @@ Statement = Transformer({
723
643
  ["if"] = function(self, node, ret)
724
644
  if ntype(node[2]) == "assign" then
725
645
  local _, assign, body = unpack(node)
726
- local name = assign[2][1]
727
- return build["do"]({
728
- assign,
729
- {
730
- "if",
731
- name,
732
- unpack(node, 3)
646
+ if destructure.has_destructure(assign[2]) then
647
+ local name = NameProxy("des")
648
+ body = {
649
+ destructure.build_assign(assign[2][1], name),
650
+ build.group(node[3])
733
651
  }
734
- })
652
+ return build["do"]({
653
+ build.assign_one(name, assign[3][1]),
654
+ {
655
+ "if",
656
+ name,
657
+ body,
658
+ unpack(node, 4)
659
+ }
660
+ })
661
+ else
662
+ local name = assign[2][1]
663
+ return build["do"]({
664
+ assign,
665
+ {
666
+ "if",
667
+ name,
668
+ unpack(node, 3)
669
+ }
670
+ })
671
+ end
735
672
  end
736
673
  node = expand_elseif_assign(node)
737
674
  if ret then
@@ -779,6 +716,29 @@ Statement = Transformer({
779
716
  foreach = function(self, node)
780
717
  smart_node(node)
781
718
  local source = unpack(node.iter)
719
+ local destructures = { }
720
+ node.names = (function()
721
+ local _accum_0 = { }
722
+ local _len_0 = 1
723
+ for i, name in ipairs(node.names) do
724
+ if ntype(name) == "table" then
725
+ do
726
+ local _with_0 = NameProxy("des")
727
+ local proxy = _with_0
728
+ insert(destructures, destructure.build_assign(name, proxy))
729
+ _accum_0[_len_0] = _with_0
730
+ end
731
+ else
732
+ _accum_0[_len_0] = name
733
+ end
734
+ _len_0 = _len_0 + 1
735
+ end
736
+ return _accum_0
737
+ end)()
738
+ if next(destructures) then
739
+ insert(destructures, build.group(node.body))
740
+ node.body = destructures
741
+ end
782
742
  if ntype(source) == "unpack" then
783
743
  local list = source[2]
784
744
  local index_name = NameProxy("index")
@@ -857,20 +817,33 @@ Statement = Transformer({
857
817
  local exp_name = NameProxy("exp")
858
818
  local convert_cond
859
819
  convert_cond = function(cond)
860
- local t, case_exp, body = unpack(cond)
820
+ local t, case_exps, body = unpack(cond)
861
821
  local out = { }
862
822
  insert(out, t == "case" and "elseif" or "else")
863
823
  if t ~= "else" then
864
- if t ~= "else" then
865
- insert(out, {
824
+ local cond_exp = { }
825
+ for i, case in ipairs(case_exps) do
826
+ if i == 1 then
827
+ insert(cond_exp, "exp")
828
+ else
829
+ insert(cond_exp, "or")
830
+ end
831
+ if not (value_is_singular(case)) then
832
+ case = {
833
+ "parens",
834
+ case
835
+ }
836
+ end
837
+ insert(cond_exp, {
866
838
  "exp",
867
- case_exp,
839
+ case,
868
840
  "==",
869
841
  exp_name
870
842
  })
871
843
  end
844
+ insert(out, cond_exp)
872
845
  else
873
- body = case_exp
846
+ body = case_exps
874
847
  end
875
848
  if ret then
876
849
  body = apply_to_last(body, ret)
@@ -921,24 +894,30 @@ Statement = Transformer({
921
894
  end
922
895
  end
923
896
  end
924
- local constructor = nil
897
+ local constructor
925
898
  properties = (function()
926
899
  local _accum_0 = { }
927
- local _len_0 = 0
900
+ local _len_0 = 1
928
901
  local _list_1 = properties
929
902
  for _index_0 = 1, #_list_1 do
930
- local tuple = _list_1[_index_0]
931
- local key = tuple[1]
932
- local _value_0
933
- if key[1] == "key_literal" and key[2] == constructor_name then
934
- constructor = tuple[2]
935
- _value_0 = nil
936
- else
937
- _value_0 = tuple
938
- end
939
- if _value_0 ~= nil then
940
- _len_0 = _len_0 + 1
903
+ local _continue_0 = false
904
+ repeat
905
+ local tuple = _list_1[_index_0]
906
+ local key = tuple[1]
907
+ local _value_0
908
+ if key[1] == "key_literal" and key[2] == constructor_name then
909
+ constructor = tuple[2]
910
+ _continue_0 = true
911
+ break
912
+ else
913
+ _value_0 = tuple
914
+ end
941
915
  _accum_0[_len_0] = _value_0
916
+ _len_0 = _len_0 + 1
917
+ _continue_0 = true
918
+ until true
919
+ if not _continue_0 then
920
+ break
942
921
  end
943
922
  end
944
923
  return _accum_0
@@ -947,7 +926,7 @@ Statement = Transformer({
947
926
  local base_name = NameProxy("base")
948
927
  local self_name = NameProxy("self")
949
928
  local cls_name = NameProxy("class")
950
- if not constructor then
929
+ if not (constructor) then
951
930
  constructor = build.fndef({
952
931
  args = {
953
932
  {
@@ -972,9 +951,6 @@ Statement = Transformer({
972
951
  })
973
952
  }
974
953
  })
975
- else
976
- smart_node(constructor)
977
- constructor.arrow = "fat"
978
954
  end
979
955
  local real_name = name or parent_assign and parent_assign[2][1]
980
956
  local _exp_0 = ntype(real_name)
@@ -1124,12 +1100,12 @@ Statement = Transformer({
1124
1100
  if chain then
1125
1101
  local slice = (function()
1126
1102
  local _accum_0 = { }
1127
- local _len_0 = 0
1103
+ local _len_0 = 1
1128
1104
  local _list_1 = chain
1129
1105
  for _index_0 = 3, #_list_1 do
1130
1106
  local item = _list_1[_index_0]
1131
- _len_0 = _len_0 + 1
1132
1107
  _accum_0[_len_0] = item
1108
+ _len_0 = _len_0 + 1
1133
1109
  end
1134
1110
  return _accum_0
1135
1111
  end)()
@@ -1187,6 +1163,10 @@ Statement = Transformer({
1187
1163
  end
1188
1164
  end)
1189
1165
  end),
1166
+ {
1167
+ "declare_glob",
1168
+ "*"
1169
+ },
1190
1170
  _with_0.assign_one(parent_cls_name, parent_val == "" and "nil" or parent_val),
1191
1171
  _with_0.assign_one(base_name, {
1192
1172
  "table",
@@ -1254,7 +1234,6 @@ Statement = Transformer({
1254
1234
  end
1255
1235
  end)()
1256
1236
  }
1257
- hoist_declarations(out_body)
1258
1237
  value = _with_0.group({
1259
1238
  _with_0.group((function()
1260
1239
  if ntype(name) == "value" then
@@ -1290,22 +1269,17 @@ do
1290
1269
  wrap = function(self, node)
1291
1270
  return build.block_exp({
1292
1271
  build.assign_one(self.accum_name, build.table()),
1293
- build.assign_one(self.len_name, 0),
1272
+ build.assign_one(self.len_name, 1),
1294
1273
  node,
1295
1274
  self.accum_name
1296
1275
  })
1297
1276
  end,
1298
- mutate_body = function(self, body, skip_nil)
1299
- if skip_nil == nil then
1300
- skip_nil = true
1301
- end
1277
+ mutate_body = function(self, body)
1278
+ local single_stm = is_singular(body)
1302
1279
  local val
1303
- if not skip_nil and is_singular(body) then
1304
- do
1305
- local _with_0 = body[1]
1306
- body = { }
1307
- val = _with_0
1308
- end
1280
+ if single_stm and types.is_value(single_stm) then
1281
+ body = { }
1282
+ val = single_stm
1309
1283
  else
1310
1284
  body = apply_to_last(body, function(n)
1311
1285
  if types.is_value(n) then
@@ -1325,27 +1299,15 @@ do
1325
1299
  val = self.value_name
1326
1300
  end
1327
1301
  local update = {
1302
+ build.assign_one(self.accum_name:index(self.len_name), val),
1328
1303
  {
1329
1304
  "update",
1330
1305
  self.len_name,
1331
1306
  "+=",
1332
1307
  1
1333
- },
1334
- build.assign_one(self.accum_name:index(self.len_name), val)
1308
+ }
1335
1309
  }
1336
- if skip_nil then
1337
- table.insert(body, build["if"]({
1338
- cond = {
1339
- "exp",
1340
- self.value_name,
1341
- "!=",
1342
- "nil"
1343
- },
1344
- ["then"] = update
1345
- }))
1346
- else
1347
- table.insert(body, build.group(update))
1348
- end
1310
+ insert(body, build.group(update))
1349
1311
  return body
1350
1312
  end
1351
1313
  }
@@ -1418,7 +1380,7 @@ implicitly_return = function(scope)
1418
1380
  end
1419
1381
  return fn
1420
1382
  end
1421
- Value = Transformer({
1383
+ local Value = Transformer({
1422
1384
  ["for"] = default_accumulator,
1423
1385
  ["while"] = default_accumulator,
1424
1386
  foreach = default_accumulator,
@@ -1479,7 +1441,7 @@ Value = Transformer({
1479
1441
  node = self.transform.statement(node, function(exp)
1480
1442
  return a:mutate_body({
1481
1443
  exp
1482
- }, false)
1444
+ })
1483
1445
  end)
1484
1446
  return a:wrap(node)
1485
1447
  end,
@@ -1658,3 +1620,8 @@ Value = Transformer({
1658
1620
  })
1659
1621
  end
1660
1622
  })
1623
+ return {
1624
+ Statement = Statement,
1625
+ Value = Value,
1626
+ Run = Run
1627
+ }