rufus-lua-moon 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ local types = require("moonscript.types")
2
+ local ntype, mtype, is_value, NOOP
3
+ ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.NOOP
4
+ local comprehension_has_value
5
+ comprehension_has_value = require("moonscript.transform.comprehension").comprehension_has_value
6
+ local Run
7
+ do
8
+ local _class_0
9
+ local _base_0 = {
10
+ call = function(self, state)
11
+ return self.fn(state)
12
+ end
13
+ }
14
+ _base_0.__index = _base_0
15
+ _class_0 = setmetatable({
16
+ __init = function(self, fn)
17
+ self.fn = fn
18
+ self[1] = "run"
19
+ end,
20
+ __base = _base_0,
21
+ __name = "Run"
22
+ }, {
23
+ __index = _base_0,
24
+ __call = function(cls, ...)
25
+ local _self_0 = setmetatable({}, _base_0)
26
+ cls.__init(_self_0, ...)
27
+ return _self_0
28
+ end
29
+ })
30
+ _base_0.__class = _class_0
31
+ Run = _class_0
32
+ end
33
+ local last_stm
34
+ last_stm = function(stms)
35
+ local last_exp_id = 0
36
+ for i = #stms, 1, -1 do
37
+ local stm = stms[i]
38
+ if stm and mtype(stm) ~= Run then
39
+ if ntype(stm) == "group" then
40
+ return last_stm(stm[2])
41
+ end
42
+ last_exp_id = i
43
+ break
44
+ end
45
+ end
46
+ return stms[last_exp_id], last_exp_id, stms
47
+ end
48
+ local transform_last_stm
49
+ transform_last_stm = function(stms, fn)
50
+ local _, last_idx, _stms = last_stm(stms)
51
+ if _stms ~= stms then
52
+ error("cannot transform last node in group")
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_idx 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
+ local chain_is_stub
73
+ chain_is_stub = function(chain)
74
+ local stub = chain[#chain]
75
+ return stub and ntype(stub) == "colon"
76
+ end
77
+ local implicitly_return
78
+ implicitly_return = function(scope)
79
+ local is_top = true
80
+ local fn
81
+ fn = function(stm)
82
+ local t = ntype(stm)
83
+ if t == "decorated" then
84
+ stm = scope.transform.statement(stm)
85
+ t = ntype(stm)
86
+ end
87
+ if types.cascading[t] then
88
+ is_top = false
89
+ return scope.transform.statement(stm, fn)
90
+ elseif types.manual_return[t] or not is_value(stm) then
91
+ if is_top and t == "return" and stm[2] == "" then
92
+ return NOOP
93
+ else
94
+ return stm
95
+ end
96
+ else
97
+ if t == "comprehension" and not comprehension_has_value(stm) then
98
+ return stm
99
+ else
100
+ return {
101
+ "return",
102
+ stm
103
+ }
104
+ end
105
+ end
106
+ end
107
+ return fn
108
+ end
109
+ return {
110
+ Run = Run,
111
+ last_stm = last_stm,
112
+ transform_last_stm = transform_last_stm,
113
+ chain_is_stub = chain_is_stub,
114
+ implicitly_return = implicitly_return
115
+ }
@@ -0,0 +1,74 @@
1
+ local ntype
2
+ ntype = require("moonscript.types").ntype
3
+ local Transformer
4
+ do
5
+ local _class_0
6
+ local _base_0 = {
7
+ transform_once = function(self, scope, node, ...)
8
+ if self.seen_nodes[node] then
9
+ return node
10
+ end
11
+ self.seen_nodes[node] = true
12
+ local transformer = self.transformers[ntype(node)]
13
+ if transformer then
14
+ return transformer(scope, node, ...) or node
15
+ else
16
+ return node
17
+ end
18
+ end,
19
+ transform = function(self, scope, node, ...)
20
+ if self.seen_nodes[node] then
21
+ return node
22
+ end
23
+ self.seen_nodes[node] = true
24
+ while true do
25
+ local transformer = self.transformers[ntype(node)]
26
+ local res
27
+ if transformer then
28
+ res = transformer(scope, node, ...) or node
29
+ else
30
+ res = node
31
+ end
32
+ if res == node then
33
+ return node
34
+ end
35
+ node = res
36
+ end
37
+ return node
38
+ end,
39
+ bind = function(self, scope)
40
+ return function(...)
41
+ return self:transform(scope, ...)
42
+ end
43
+ end,
44
+ __call = function(self, ...)
45
+ return self:transform(...)
46
+ end,
47
+ can_transform = function(self, node)
48
+ return self.transformers[ntype(node)] ~= nil
49
+ end
50
+ }
51
+ _base_0.__index = _base_0
52
+ _class_0 = setmetatable({
53
+ __init = function(self, transformers)
54
+ self.transformers = transformers
55
+ self.seen_nodes = setmetatable({ }, {
56
+ __mode = "k"
57
+ })
58
+ end,
59
+ __base = _base_0,
60
+ __name = "Transformer"
61
+ }, {
62
+ __index = _base_0,
63
+ __call = function(cls, ...)
64
+ local _self_0 = setmetatable({}, _base_0)
65
+ cls.__init(_self_0, ...)
66
+ return _self_0
67
+ end
68
+ })
69
+ _base_0.__class = _class_0
70
+ Transformer = _class_0
71
+ end
72
+ return {
73
+ Transformer = Transformer
74
+ }
@@ -0,0 +1,265 @@
1
+ local Transformer
2
+ Transformer = require("moonscript.transform.transformer").Transformer
3
+ local build, ntype, smart_node
4
+ do
5
+ local _obj_0 = require("moonscript.types")
6
+ build, ntype, smart_node = _obj_0.build, _obj_0.ntype, _obj_0.smart_node
7
+ end
8
+ local NameProxy
9
+ NameProxy = require("moonscript.transform.names").NameProxy
10
+ local Accumulator, default_accumulator
11
+ do
12
+ local _obj_0 = require("moonscript.transform.accumulator")
13
+ Accumulator, default_accumulator = _obj_0.Accumulator, _obj_0.default_accumulator
14
+ end
15
+ local lua_keywords
16
+ lua_keywords = require("moonscript.data").lua_keywords
17
+ local Run, transform_last_stm, implicitly_return, chain_is_stub
18
+ do
19
+ local _obj_0 = require("moonscript.transform.statements")
20
+ Run, transform_last_stm, implicitly_return, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub
21
+ end
22
+ local construct_comprehension
23
+ construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
24
+ local insert
25
+ insert = table.insert
26
+ local unpack
27
+ unpack = require("moonscript.util").unpack
28
+ return Transformer({
29
+ ["for"] = default_accumulator,
30
+ ["while"] = default_accumulator,
31
+ foreach = default_accumulator,
32
+ ["do"] = function(self, node)
33
+ return build.block_exp(node[2])
34
+ end,
35
+ decorated = function(self, node)
36
+ return self.transform.statement(node)
37
+ end,
38
+ class = function(self, node)
39
+ return build.block_exp({
40
+ node
41
+ })
42
+ end,
43
+ string = function(self, node)
44
+ local delim = node[2]
45
+ local convert_part
46
+ convert_part = function(part)
47
+ if type(part) == "string" or part == nil then
48
+ return {
49
+ "string",
50
+ delim,
51
+ part or ""
52
+ }
53
+ else
54
+ return build.chain({
55
+ base = "tostring",
56
+ {
57
+ "call",
58
+ {
59
+ part[2]
60
+ }
61
+ }
62
+ })
63
+ end
64
+ end
65
+ if #node <= 3 then
66
+ if type(node[3]) == "string" then
67
+ return node
68
+ else
69
+ return convert_part(node[3])
70
+ end
71
+ end
72
+ local e = {
73
+ "exp",
74
+ convert_part(node[3])
75
+ }
76
+ for i = 4, #node do
77
+ insert(e, "..")
78
+ insert(e, convert_part(node[i]))
79
+ end
80
+ return e
81
+ end,
82
+ comprehension = function(self, node)
83
+ local a = Accumulator()
84
+ node = self.transform.statement(node, function(exp)
85
+ return a:mutate_body({
86
+ exp
87
+ })
88
+ end)
89
+ return a:wrap(node)
90
+ end,
91
+ tblcomprehension = function(self, node)
92
+ local _, explist, clauses = unpack(node)
93
+ local key_exp, value_exp = unpack(explist)
94
+ local accum = NameProxy("tbl")
95
+ local inner
96
+ if value_exp then
97
+ local dest = build.chain({
98
+ base = accum,
99
+ {
100
+ "index",
101
+ key_exp
102
+ }
103
+ })
104
+ inner = {
105
+ build.assign_one(dest, value_exp)
106
+ }
107
+ else
108
+ local key_name, val_name = NameProxy("key"), NameProxy("val")
109
+ local dest = build.chain({
110
+ base = accum,
111
+ {
112
+ "index",
113
+ key_name
114
+ }
115
+ })
116
+ inner = {
117
+ build.assign({
118
+ names = {
119
+ key_name,
120
+ val_name
121
+ },
122
+ values = {
123
+ key_exp
124
+ }
125
+ }),
126
+ build.assign_one(dest, val_name)
127
+ }
128
+ end
129
+ return build.block_exp({
130
+ build.assign_one(accum, build.table()),
131
+ construct_comprehension(inner, clauses),
132
+ accum
133
+ })
134
+ end,
135
+ fndef = function(self, node)
136
+ smart_node(node)
137
+ node.body = transform_last_stm(node.body, implicitly_return(self))
138
+ node.body = {
139
+ Run(function(self)
140
+ return self:listen("varargs", function() end)
141
+ end),
142
+ unpack(node.body)
143
+ }
144
+ return node
145
+ end,
146
+ ["if"] = function(self, node)
147
+ return build.block_exp({
148
+ node
149
+ })
150
+ end,
151
+ unless = function(self, node)
152
+ return build.block_exp({
153
+ node
154
+ })
155
+ end,
156
+ with = function(self, node)
157
+ return build.block_exp({
158
+ node
159
+ })
160
+ end,
161
+ switch = function(self, node)
162
+ return build.block_exp({
163
+ node
164
+ })
165
+ end,
166
+ chain = function(self, node)
167
+ for i = 2, #node do
168
+ local part = node[i]
169
+ if ntype(part) == "dot" and lua_keywords[part[2]] then
170
+ node[i] = {
171
+ "index",
172
+ {
173
+ "string",
174
+ '"',
175
+ part[2]
176
+ }
177
+ }
178
+ end
179
+ end
180
+ if ntype(node[2]) == "string" then
181
+ node[2] = {
182
+ "parens",
183
+ node[2]
184
+ }
185
+ end
186
+ if chain_is_stub(node) then
187
+ local base_name = NameProxy("base")
188
+ local fn_name = NameProxy("fn")
189
+ local colon = table.remove(node)
190
+ local is_super = ntype(node[2]) == "ref" and node[2][2] == "super"
191
+ return build.block_exp({
192
+ build.assign({
193
+ names = {
194
+ base_name
195
+ },
196
+ values = {
197
+ node
198
+ }
199
+ }),
200
+ build.assign({
201
+ names = {
202
+ fn_name
203
+ },
204
+ values = {
205
+ build.chain({
206
+ base = base_name,
207
+ {
208
+ "dot",
209
+ colon[2]
210
+ }
211
+ })
212
+ }
213
+ }),
214
+ build.fndef({
215
+ args = {
216
+ {
217
+ "..."
218
+ }
219
+ },
220
+ body = {
221
+ build.chain({
222
+ base = fn_name,
223
+ {
224
+ "call",
225
+ {
226
+ is_super and "self" or base_name,
227
+ "..."
228
+ }
229
+ }
230
+ })
231
+ }
232
+ })
233
+ })
234
+ end
235
+ end,
236
+ block_exp = function(self, node)
237
+ local _, body = unpack(node)
238
+ local fn = nil
239
+ local arg_list = { }
240
+ fn = smart_node(build.fndef({
241
+ body = {
242
+ Run(function(self)
243
+ return self:listen("varargs", function()
244
+ insert(arg_list, "...")
245
+ insert(fn.args, {
246
+ "..."
247
+ })
248
+ return self:unlisten("varargs")
249
+ end)
250
+ end),
251
+ unpack(body)
252
+ }
253
+ }))
254
+ return build.chain({
255
+ base = {
256
+ "parens",
257
+ fn
258
+ },
259
+ {
260
+ "call",
261
+ arg_list
262
+ }
263
+ })
264
+ end
265
+ })