Skip to content

Commit 82f7384

Browse files
committed
don't create unecessary function with tblcomp and assign single
1 parent 7735804 commit 82f7384

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

moonscript/transform.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,8 @@ Statement = Transformer({
369369
block_body
370370
}
371371
})
372-
elseif "comprehension" == _exp_0 then
373-
local a = Accumulator()
374-
local action
375-
action = function(exp)
376-
return a:mutate_body({
377-
exp
378-
})
379-
end
380-
node = self.transform.statement(first_value, action, node)
381-
return build.assign_one(first_name, a:wrap(node))
372+
elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 then
373+
return build.assign_one(first_name, Value.transformers[first_value[1]](self, first_value))
382374
end
383375
end
384376
local transformed

moonscript/transform.moon

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,9 @@ Statement = Transformer {
190190
{"do", block_body}
191191
}
192192

193-
when "comprehension"
194-
a = Accumulator!
195-
action = (exp) -> a\mutate_body { exp }
196-
node = @transform.statement first_value, action, node
197-
return build.assign_one first_name, a\wrap node
193+
when "comprehension", "tblcomprehension"
194+
return build.assign_one first_name,
195+
Value.transformers[first_value[1]] @, first_value
198196

199197
-- bubble cascading assigns
200198
transformed = if num_values == 1

spec/outputs/comprehension.lua

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,56 @@ local items = {
66
5,
77
6
88
}
9-
local out = (function()
9+
local out
10+
do
1011
local _tbl_0 = { }
1112
for k in items do
1213
_tbl_0[k] = k * 2
1314
end
14-
return _tbl_0
15-
end)()
15+
out = _tbl_0
16+
end
1617
local x = {
1718
hello = "world",
1819
okay = 2323
1920
}
20-
local copy = (function()
21+
local copy
22+
do
2123
local _tbl_0 = { }
2224
for k, v in pairs(x) do
2325
if k ~= "okay" then
2426
_tbl_0[k] = v
2527
end
2628
end
27-
return _tbl_0
28-
end)()
29-
local _ = (function()
29+
copy = _tbl_0
30+
end
31+
local _
32+
do
3033
local _tbl_0 = { }
3134
for x in yes do
3235
local _key_0, _val_0 = unpack(x)
3336
_tbl_0[_key_0] = _val_0
3437
end
35-
return _tbl_0
36-
end)()
37-
_ = (function()
38+
_ = _tbl_0
39+
end
40+
do
3841
local _tbl_0 = { }
3942
local _list_0 = yes
4043
for _index_0 = 1, #_list_0 do
4144
x = _list_0[_index_0]
4245
local _key_0, _val_0 = unpack(x)
4346
_tbl_0[_key_0] = _val_0
4447
end
45-
return _tbl_0
46-
end)()
47-
_ = (function()
48+
_ = _tbl_0
49+
end
50+
do
4851
local _tbl_0 = { }
4952
for x in yes do
5053
local _key_0, _val_0 = xxxx
5154
_tbl_0[_key_0] = _val_0
5255
end
53-
return _tbl_0
54-
end)()
55-
_ = (function()
56+
_ = _tbl_0
57+
end
58+
do
5659
local _tbl_0 = { }
5760
local _list_0 = {
5861
{
@@ -77,8 +80,8 @@ _ = (function()
7780
end)())
7881
_tbl_0[_key_0] = _val_0
7982
end
80-
return _tbl_0
81-
end)()
83+
_ = _tbl_0
84+
end
8285
local n1
8386
do
8487
local _accum_0 = { }
@@ -158,11 +161,11 @@ do
158161
end
159162
dd = _accum_0
160163
end
161-
_ = (function()
164+
do
162165
local _tbl_0 = { }
163166
for i = 1, 10 do
164167
_tbl_0["hello"] = "world"
165168
end
166-
return _tbl_0
167-
end)()
169+
_ = _tbl_0
170+
end
168171
return nil

0 commit comments

Comments
 (0)