Skip to content

Commit 931596d

Browse files
committed
handle general case of block_exp single assign
1 parent 2d345f1 commit 931596d

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

moonscript/transform.lua

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,35 @@ Statement = Transformer({
351351
local num_names = #values
352352
if num_names == 1 and num_values == 1 then
353353
local first_value = values[1]
354+
local first_name = names[1]
354355
local _exp_0 = ntype(first_value)
355-
if "comprehension" == _exp_0 then
356-
local first_name = names[1]
357-
local a = Accumulator()
358-
local action
359-
action = function(exp)
360-
return a:mutate_body({
361-
exp
362-
})
363-
end
364-
node = self.transform.statement(first_value, action, node)
365-
local wrapped = a:wrap(node, "do")
366-
insert(wrapped[2], build.assign_one(first_name, a.accum_name))
356+
if "block_exp" == _exp_0 then
357+
local block_body = first_value[2]
358+
local idx = #block_body
359+
block_body[idx] = build.assign_one(first_name, block_body[idx])
367360
return build.group({
368361
{
369362
"declare",
370363
{
371364
first_name
372365
}
373366
},
374-
wrapped
367+
{
368+
"do",
369+
block_body
370+
}
375371
})
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+
local wrapped = a:wrap(node)
382+
return build.assign_one(first_name, wrapped)
376383
end
377384
end
378385
local transformed

moonscript/transform.moon

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,29 @@ Statement = Transformer {
174174
num_values = #values
175175
num_names = #values
176176

177+
-- special code simplifications for single assigns
177178
if num_names == 1 and num_values == 1
178179
first_value = values[1]
180+
first_name = names[1]
181+
179182
switch ntype first_value
180-
when "comprehension"
181-
first_name = names[1]
183+
when "block_exp"
184+
block_body = first_value[2]
185+
idx = #block_body
186+
block_body[idx] = build.assign_one first_name, block_body[idx]
187+
188+
return build.group {
189+
{"declare", {first_name}}
190+
{"do", block_body}
191+
}
182192

193+
when "comprehension"
183194
a = Accumulator!
184195
action = (exp) -> a\mutate_body { exp }
185196
node = @transform.statement first_value, action, node
186197

187-
wrapped = a\wrap node, "do"
188-
insert wrapped[2], build.assign_one first_name, a.accum_name
189-
190-
return build.group {
191-
{"declare", {first_name}}
192-
wrapped
193-
}
198+
wrapped = a\wrap node
199+
return build.assign_one first_name, wrapped
194200

195201
-- bubble cascading assigns
196202
transformed = if num_values == 1

0 commit comments

Comments
 (0)