-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathdestructure.lua
More file actions
246 lines (246 loc) · 5.26 KB
/
Copy pathdestructure.lua
File metadata and controls
246 lines (246 loc) · 5.26 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
local ntype, mtype, build
do
local _table_0 = require("moonscript.types")
ntype, mtype, build = _table_0.ntype, _table_0.mtype, _table_0.build
end
local NameProxy
do
local _table_0 = require("moonscript.transform.names")
NameProxy = _table_0.NameProxy
end
local insert = table.insert
local unpack
do
local _table_0 = require("moonscript.util")
unpack = _table_0.unpack
end
local user_error
do
local _table_0 = require("moonscript.errors")
user_error = _table_0.user_error
end
local join
join = function(...)
do
local _with_0 = { }
local out = _with_0
local i = 1
local _list_0 = {
...
}
for _index_0 = 1, #_list_0 do
local tbl = _list_0[_index_0]
local _list_1 = tbl
for _index_1 = 1, #_list_1 do
local v = _list_1[_index_1]
out[i] = v
i = i + 1
end
end
return _with_0
end
end
local has_destructure
has_destructure = function(names)
local _list_0 = names
for _index_0 = 1, #_list_0 do
local n = _list_0[_index_0]
if ntype(n) == "table" then
return true
end
end
return false
end
local extract_assign_names
extract_assign_names = function(name, accum, prefix)
if accum == nil then
accum = { }
end
if prefix == nil then
prefix = { }
end
local i = 1
local _list_0 = name[2]
for _index_0 = 1, #_list_0 do
local tuple = _list_0[_index_0]
local value, suffix
if #tuple == 1 then
local s = {
"index",
{
"number",
i
}
}
i = i + 1
value, suffix = tuple[1], s
else
local key = tuple[1]
local s
if ntype(key) == "key_literal" then
s = {
"dot",
key[2]
}
else
s = {
"index",
key
}
end
value, suffix = tuple[2], s
end
suffix = join(prefix, {
suffix
})
local t = ntype(value)
if t == "value" or t == "chain" or t == "self" then
insert(accum, {
value,
suffix
})
elseif t == "table" then
extract_assign_names(value, accum, suffix)
else
user_error("Can't destructure value of type: " .. tostring(ntype(value)))
end
end
return accum
end
local build_assign
build_assign = function(destruct_literal, receiver)
local extracted_names = extract_assign_names(destruct_literal)
local names = { }
local values = { }
local inner = {
"assign",
names,
values
}
local obj
if mtype(receiver) == NameProxy then
obj = receiver
else
do
local _with_0 = NameProxy("obj")
obj = _with_0
inner = build["do"]({
build.assign_one(obj, receiver),
{
"assign",
names,
values
}
})
obj = _with_0
end
end
local _list_0 = extracted_names
for _index_0 = 1, #_list_0 do
local tuple = _list_0[_index_0]
insert(names, tuple[1])
insert(values, obj:chain(unpack(tuple[2])))
end
return build.group({
{
"declare",
names
},
inner
})
end
local split_assign
split_assign = function(assign)
local names, values = unpack(assign, 2)
local g = { }
local total_names = #names
local total_values = #values
local start = 1
for i, n in ipairs(names) do
if ntype(n) == "table" then
if i > start then
local stop = i - 1
insert(g, {
"assign",
(function()
local _accum_0 = { }
local _len_0 = 0
for i = start, stop do
local _value_0 = names[i]
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)(),
(function()
local _accum_0 = { }
local _len_0 = 0
for i = start, stop do
local _value_0 = values[i]
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
})
end
insert(g, build_assign(n, values[i]))
start = i + 1
end
end
if total_names >= start or total_values >= start then
local name_slice
if total_names < start then
name_slice = {
"_"
}
else
name_slice = (function()
local _accum_0 = { }
local _len_0 = 0
for i = start, total_names do
local _value_0 = names[i]
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
end
local value_slice
if total_values < start then
value_slice = {
"nil"
}
else
value_slice = (function()
local _accum_0 = { }
local _len_0 = 0
for i = start, total_values do
local _value_0 = values[i]
if _value_0 ~= nil then
_len_0 = _len_0 + 1
_accum_0[_len_0] = _value_0
end
end
return _accum_0
end)()
end
insert(g, {
"assign",
name_slice,
value_slice
})
end
return build.group(g)
end
return {
has_destructure = has_destructure,
split_assign = split_assign,
build_assign = build_assign
}