-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathvalue.lua
More file actions
468 lines (468 loc) · 10.9 KB
/
Copy pathvalue.lua
File metadata and controls
468 lines (468 loc) · 10.9 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
local util = require("moonscript.util")
local data = require("moonscript.data")
local ntype
ntype = require("moonscript.types").ntype
local user_error
user_error = require("moonscript.errors").user_error
local concat, insert
do
local _obj_0 = table
concat, insert = _obj_0.concat, _obj_0.insert
end
local unpack
unpack = util.unpack
local table_delim = ","
local string_chars = {
["\r"] = "\\r",
["\n"] = "\\n"
}
local binary_op_prec
do
local out = { }
for prec, ops in ipairs({
{
"or"
},
{
"and"
},
{
"<",
">",
"<=",
">=",
"~=",
"!=",
"=="
},
{
"|"
},
{
"&"
},
{
"<<",
">>"
},
{
".."
},
{
"+",
"-"
},
{
"*",
"/",
"//",
"%"
},
{
"^"
}
}) do
for _index_0 = 1, #ops do
local op = ops[_index_0]
out[op] = prec
end
end
binary_op_prec = out
end
local right_assoc_op = {
[".."] = true,
["^"] = true
}
local exp_precedence
exp_precedence = function(node)
local min_prec
for i = 3, #node, 2 do
do
local prec = binary_op_prec[node[i]]
if prec then
if not min_prec or prec < min_prec then
min_prec = prec
end
end
end
end
return min_prec
end
return {
scoped = function(self, node)
local _, before, value, after
_, before, value, after = node[1], node[2], node[3], node[4]
local _scrap_0 = before and before:call(self)
do
local _with_0 = self:value(value)
local _scrap_1 = after and after:call(self)
return _with_0
end
end,
exp = function(self, node)
local needs_parens
needs_parens = function(value, i)
if not (type(value) == "table" and value[1] == "exp") then
return false
end
local inner = exp_precedence(value)
if not (inner) then
return false
end
if i > 2 then
do
local left = binary_op_prec[node[i - 1]]
if left then
if left > inner then
return true
end
if left == inner and not right_assoc_op[node[i - 1]] then
return true
end
end
end
end
if i < #node then
do
local right = binary_op_prec[node[i + 1]]
if right then
if right > inner then
return true
end
if right == inner and right_assoc_op[node[i + 1]] then
return true
end
end
end
end
return false
end
local _comp
_comp = function(i, value)
if i % 2 == 1 and value == "!=" then
value = "~="
end
if type(value) == "table" then
value = self.transform.value(value)
end
if needs_parens(value, i) then
return self:line("(", self:value(value), ")")
else
return self:value(value)
end
end
do
local _with_0 = self:line()
_with_0:append_list((function()
local _accum_0 = { }
local _len_0 = 1
for i, v in ipairs(node) do
if i > 1 then
_accum_0[_len_0] = _comp(i, v)
_len_0 = _len_0 + 1
end
end
return _accum_0
end)(), " ")
return _with_0
end
end,
explist = function(self, node)
do
local _with_0 = self:line()
_with_0:append_list((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 2, #node do
local v = node[_index_0]
_accum_0[_len_0] = self:value(v)
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ", ")
return _with_0
end
end,
parens = function(self, node)
return self:line("(", self:value(node[2]), ")")
end,
string = function(self, node)
local delim, inner = unpack(node, 2)
local end_delim = delim:gsub("%[", "]")
if delim == "'" or delim == '"' then
inner = inner:gsub("[\r\n]", string_chars)
end
return delim .. inner .. end_delim
end,
chain = function(self, node)
local callee = node[2]
local callee_type = ntype(callee)
local item_offset = 3
if callee_type == "dot" or callee_type == "colon" or callee_type == "index" then
callee = self:get("scope_var")
if not (callee) then
user_error("Short-dot syntax must be called within a with block", node[-1])
end
item_offset = 2
end
if callee_type == "ref" and callee[2] == "super" or callee == "super" then
do
local sup = self:get("super")
if sup then
return self:value(sup(self, node))
end
end
end
local chain_item
chain_item = function(node)
local t, arg = unpack(node)
if t == "call" then
return "(", self:values(arg), ")"
elseif t == "index" then
return "[", self:value(arg), "]"
elseif t == "dot" then
return ".", tostring(arg)
elseif t == "colon" then
return ":", tostring(arg)
else
return error("Unknown chain action: " .. tostring(t))
end
end
if (callee_type == "self" or callee_type == "self_class") and node[3] and ntype(node[3]) == "call" then
callee[1] = callee_type .. "_colon"
end
local callee_value = self:value(callee)
local _exp_0 = ntype(callee)
if "exp" == _exp_0 or "table" == _exp_0 then
callee_value = self:line("(", callee_value, ")")
end
local actions
do
local _with_0 = self:line()
for _index_0 = item_offset, #node do
local action = node[_index_0]
_with_0:append(chain_item(action))
end
actions = _with_0
end
return self:line(callee_value, actions)
end,
fndef = function(self, node)
local args, whitelist, arrow, block = unpack(node, 2)
local default_args = { }
local self_args = { }
local fn_block = self:block()
local arg_names
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #args do
local arg = args[_index_0]
local name, default_value = unpack(arg)
if type(name) == "string" then
name = name
elseif name[1] == "temp_name" then
name = name:get_name(fn_block, false)
else
if name[1] == "self" or name[1] == "self_class" then
insert(self_args, name)
end
name = name[2]
end
if default_value then
insert(default_args, arg)
end
local _value_0 = name
_accum_0[_len_0] = _value_0
_len_0 = _len_0 + 1
end
arg_names = _accum_0
end
if arrow == "fat" then
insert(arg_names, 1, "self")
end
do
local _with_0 = fn_block
_with_0.header = "function(" .. concat(arg_names, ", ") .. ")"
if #whitelist > 0 then
_with_0:whitelist_names(whitelist)
end
for _index_0 = 1, #arg_names do
local name = arg_names[_index_0]
_with_0:put_name(name)
end
for _index_0 = 1, #default_args do
local default = default_args[_index_0]
local name, value = unpack(default)
if type(name) == "table" then
if name[1] == "temp_name" then
name = name:get_name(fn_block)
else
name = name[2]
end
end
_with_0:stm({
'if',
{
'exp',
{
"ref",
name
},
'==',
'nil'
},
{
{
'assign',
{
name
},
{
value
}
}
}
})
end
local self_arg_values
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self_args do
local arg = self_args[_index_0]
_accum_0[_len_0] = arg[2]
_len_0 = _len_0 + 1
end
self_arg_values = _accum_0
end
if #self_args > 0 then
_with_0:stm({
"assign",
self_args,
self_arg_values
})
end
_with_0:stms(block)
return _with_0
end
end,
table = function(self, node)
local items = unpack(node, 2)
do
local _with_0 = self:block("{", "}")
local format_line
format_line = function(tuple)
if #tuple == 2 then
local key, value = unpack(tuple)
if ntype(key) == "key_literal" and data.lua_keywords[key[2]] then
key = {
"string",
'"',
key[2]
}
end
local assign
if ntype(key) == "key_literal" then
assign = key[2]
else
assign = self:line("[", _with_0:value(key), "]")
end
local out = self:line(assign, " = ", _with_0:value(value))
return out
else
return self:line(_with_0:value(tuple[1]))
end
end
if items then
local count = #items
for i, tuple in ipairs(items) do
local line = format_line(tuple)
if not (count == i) then
line:append(table_delim)
end
_with_0:add(line)
end
end
return _with_0
end
end,
minus = function(self, node)
return self:line("-", self:value(node[2]))
end,
temp_name = function(self, node, ...)
return node:get_name(self, ...)
end,
number = function(self, node)
return node[2]
end,
bitnot = function(self, node)
return self:line("~", self:value(node[2]))
end,
length = function(self, node)
return self:line("#", self:value(node[2]))
end,
["not"] = function(self, node)
return self:line("not ", self:value(node[2]))
end,
self = function(self, node)
local field_name = self:name(node[2])
if data.lua_keywords[field_name] then
return self:value({
"chain",
"self",
{
"index",
{
"string",
'"',
field_name
}
}
})
else
return "self." .. tostring(field_name)
end
end,
self_class = function(self, node)
local field_name = self:name(node[2])
if data.lua_keywords[field_name] then
return self:value({
"chain",
"self",
{
"dot",
"__class"
},
{
"index",
{
"string",
'"',
field_name
}
}
})
else
return "self.__class." .. tostring(field_name)
end
end,
self_colon = function(self, node)
return "self:" .. tostring(self:name(node[2]))
end,
self_class_colon = function(self, node)
return "self.__class:" .. tostring(self:name(node[2]))
end,
ref = function(self, value)
do
local sup = value[2] == "super" and self:get("super")
if sup then
return self:value(sup(self))
end
end
return tostring(value[2])
end,
raw_value = function(self, value)
return tostring(value)
end
}