-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathvalue.lua
More file actions
401 lines (401 loc) · 9.82 KB
/
Copy pathvalue.lua
File metadata and controls
401 lines (401 loc) · 9.82 KB
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
local Transformer
Transformer = require("moonscript.transform.transformer").Transformer
local build, ntype, smart_node
do
local _obj_0 = require("moonscript.types")
build, ntype, smart_node = _obj_0.build, _obj_0.ntype, _obj_0.smart_node
end
local NameProxy
NameProxy = require("moonscript.transform.names").NameProxy
local Accumulator, default_accumulator
do
local _obj_0 = require("moonscript.transform.accumulator")
Accumulator, default_accumulator = _obj_0.Accumulator, _obj_0.default_accumulator
end
local lua_keywords
lua_keywords = require("moonscript.data").lua_keywords
local user_error
user_error = require("moonscript.errors").user_error
local transform_last_stm, implicitly_return, chain_is_stub, has_varargs
do
local _obj_0 = require("moonscript.transform.statements")
transform_last_stm, implicitly_return, chain_is_stub, has_varargs = _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub, _obj_0.has_varargs
end
local construct_comprehension
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
local destructure = require("moonscript.transform.destructure")
local insert
insert = table.insert
local unpack
unpack = require("moonscript.util").unpack
return Transformer({
["for"] = default_accumulator,
["while"] = default_accumulator,
foreach = default_accumulator,
["do"] = function(self, node)
return build.block_exp(node[2])
end,
decorated = function(self, node)
return self.transform.statement(node)
end,
class = function(self, node)
return build.block_exp({
node
})
end,
string = function(self, node)
local delim = node[2]
local convert_part
convert_part = function(part)
if type(part) == "string" or part == nil then
return {
"string",
delim,
part or ""
}
else
return build.chain({
base = "tostring",
{
"call",
{
part[2]
}
}
})
end
end
if #node <= 3 then
if type(node[3]) == "string" then
return node
else
return convert_part(node[3])
end
end
local e = {
"exp",
convert_part(node[3])
}
for i = 4, #node do
insert(e, "..")
insert(e, convert_part(node[i]))
end
return e
end,
comprehension = function(self, node)
local a = Accumulator()
node = self.transform.statement(node, function(exp)
return a:mutate_body({
exp
})
end)
return a:wrap(node)
end,
tblcomprehension = function(self, node)
local explist, clauses = unpack(node, 2)
local key_exp, value_exp = unpack(explist)
local accum = NameProxy("tbl")
local inner
if value_exp then
local dest = build.chain({
base = accum,
{
"index",
key_exp
}
})
inner = {
build.assign_one(dest, value_exp)
}
else
local key_name, val_name = NameProxy("key"), NameProxy("val")
local dest = build.chain({
base = accum,
{
"index",
key_name
}
})
inner = {
build.assign({
names = {
key_name,
val_name
},
values = {
key_exp
}
}),
build.assign_one(dest, val_name)
}
end
return build.block_exp({
build.assign_one(accum, build.table()),
construct_comprehension(inner, clauses),
accum
})
end,
fndef = function(self, node)
smart_node(node)
node.body = transform_last_stm(node.body, implicitly_return(self))
local first_destructure
for i, arg in ipairs(node.args) do
if ntype(arg[1]) == "table" then
first_destructure = i
break
end
end
if first_destructure then
local bound_names = { }
if node.arrow == "fat" then
bound_names.self = true
end
local _list_0 = node.args
for _index_0 = 1, #_list_0 do
local arg = _list_0[_index_0]
local _exp_0 = ntype(arg[1])
if "self" == _exp_0 or "self_class" == _exp_0 then
bound_names[arg[1][2]] = true
elseif "table" == _exp_0 then
local _scrap_0 = nil
else
if type(arg[1]) == "string" then
bound_names[arg[1]] = true
end
end
end
local seen_targets = { }
local _list_1 = node.args
for _index_0 = 1, #_list_1 do
local _continue_0 = false
repeat
local arg = _list_1[_index_0]
if not (ntype(arg[1]) == "table") then
_continue_0 = true
break
end
local targets
do
local _accum_0 = { }
local _len_0 = 1
local _list_2 = destructure.extract_assign_names(arg[1])
for _index_1 = 1, #_list_2 do
local _des_0 = _list_2[_index_1]
local t
t = _des_0[1]
if ntype(t) == "ref" then
_accum_0[_len_0] = t
_len_0 = _len_0 + 1
end
end
targets = _accum_0
end
for _index_1 = 1, #targets do
local target = targets[_index_1]
local name = target[2]
if bound_names[name] or seen_targets[name] then
user_error("Can't destructure into '" .. tostring(name) .. "': name is bound by another parameter", target[-1])
end
end
for _index_1 = 1, #targets do
local target = targets[_index_1]
seen_targets[target[2]] = true
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local default_check
default_check = function(name, value)
return {
"if",
{
"exp",
name,
"==",
"nil"
},
{
{
"assign",
{
name
},
{
value
}
}
}
}
end
local prelude = { }
for i = first_destructure, #node.args do
local arg = node.args[i]
local name, default_value = arg[1], arg[2]
local _exp_0 = ntype(name)
if "table" == _exp_0 then
local proxy = NameProxy("arg")
if default_value then
insert(prelude, default_check(proxy, default_value))
end
insert(prelude, destructure.build_assign(self, name, proxy, {
shadow = true
}))
node.args[i] = {
proxy
}
elseif "self" == _exp_0 or "self_class" == _exp_0 then
local raw_name = name[2]
if default_value then
insert(prelude, default_check({
"ref",
raw_name
}, default_value))
end
insert(prelude, build.assign_one(name, {
"ref",
raw_name
}))
node.args[i] = {
raw_name
}
else
if default_value then
insert(prelude, default_check({
"ref",
name
}, default_value))
node.args[i] = {
name
}
end
end
end
insert(prelude, build.group(node.body))
node.body = prelude
end
return node
end,
["if"] = function(self, node)
return build.block_exp({
node
})
end,
unless = function(self, node)
return build.block_exp({
node
})
end,
with = function(self, node)
return build.block_exp({
node
})
end,
switch = function(self, node)
return build.block_exp({
node
})
end,
chain = function(self, node)
for i = 2, #node do
local part = node[i]
if ntype(part) == "dot" and lua_keywords[part[2]] then
node[i] = {
"index",
{
"string",
'"',
part[2]
}
}
end
end
if ntype(node[2]) == "string" then
node[2] = {
"parens",
node[2]
}
end
if chain_is_stub(node) then
local base_name = NameProxy("base")
local fn_name = NameProxy("fn")
local colon = table.remove(node)
if not (node[2]) then
local scope_var = self:get("scope_var")
if not (scope_var) then
user_error("Short-colon syntax must be called within a with block", node[-1])
end
node[2] = scope_var
end
local is_super = ntype(node[2]) == "ref" and node[2][2] == "super"
return build.block_exp({
build.assign({
names = {
base_name
},
values = {
node
}
}),
build.assign({
names = {
fn_name
},
values = {
build.chain({
base = base_name,
{
"dot",
colon[2]
}
})
}
}),
build.fndef({
args = {
{
"..."
}
},
body = {
build.chain({
base = fn_name,
{
"call",
{
is_super and "self" or base_name,
"..."
}
}
})
}
})
})
end
end,
block_exp = function(self, node)
local body = unpack(node, 2)
local arg_list = { }
local fn = smart_node(build.fndef({
body = body
}))
if has_varargs(body) then
insert(arg_list, "...")
insert(fn.args, {
"..."
})
end
return build.chain({
base = {
"parens",
fn
},
{
"call",
arg_list
}
})
end
})