mirror of
https://github.com/leafo/moonscript.git
synced 2024-10-05 05:34:19 +00:00
handle general case of block_exp single assign
This commit is contained in:
parent
2d345f1c7c
commit
931596d6af
@ -351,9 +351,25 @@ Statement = Transformer({
|
||||
local num_names = #values
|
||||
if num_names == 1 and num_values == 1 then
|
||||
local first_value = values[1]
|
||||
local first_name = names[1]
|
||||
local _exp_0 = ntype(first_value)
|
||||
if "comprehension" == _exp_0 then
|
||||
local first_name = names[1]
|
||||
if "block_exp" == _exp_0 then
|
||||
local block_body = first_value[2]
|
||||
local idx = #block_body
|
||||
block_body[idx] = build.assign_one(first_name, block_body[idx])
|
||||
return build.group({
|
||||
{
|
||||
"declare",
|
||||
{
|
||||
first_name
|
||||
}
|
||||
},
|
||||
{
|
||||
"do",
|
||||
block_body
|
||||
}
|
||||
})
|
||||
elseif "comprehension" == _exp_0 then
|
||||
local a = Accumulator()
|
||||
local action
|
||||
action = function(exp)
|
||||
@ -362,17 +378,8 @@ Statement = Transformer({
|
||||
})
|
||||
end
|
||||
node = self.transform.statement(first_value, action, node)
|
||||
local wrapped = a:wrap(node, "do")
|
||||
insert(wrapped[2], build.assign_one(first_name, a.accum_name))
|
||||
return build.group({
|
||||
{
|
||||
"declare",
|
||||
{
|
||||
first_name
|
||||
}
|
||||
},
|
||||
wrapped
|
||||
})
|
||||
local wrapped = a:wrap(node)
|
||||
return build.assign_one(first_name, wrapped)
|
||||
end
|
||||
end
|
||||
local transformed
|
||||
|
@ -174,23 +174,29 @@ Statement = Transformer {
|
||||
num_values = #values
|
||||
num_names = #values
|
||||
|
||||
-- special code simplifications for single assigns
|
||||
if num_names == 1 and num_values == 1
|
||||
first_value = values[1]
|
||||
switch ntype first_value
|
||||
when "comprehension"
|
||||
first_name = names[1]
|
||||
first_name = names[1]
|
||||
|
||||
switch ntype first_value
|
||||
when "block_exp"
|
||||
block_body = first_value[2]
|
||||
idx = #block_body
|
||||
block_body[idx] = build.assign_one first_name, block_body[idx]
|
||||
|
||||
return build.group {
|
||||
{"declare", {first_name}}
|
||||
{"do", block_body}
|
||||
}
|
||||
|
||||
when "comprehension"
|
||||
a = Accumulator!
|
||||
action = (exp) -> a\mutate_body { exp }
|
||||
node = @transform.statement first_value, action, node
|
||||
|
||||
wrapped = a\wrap node, "do"
|
||||
insert wrapped[2], build.assign_one first_name, a.accum_name
|
||||
|
||||
return build.group {
|
||||
{"declare", {first_name}}
|
||||
wrapped
|
||||
}
|
||||
wrapped = a\wrap node
|
||||
return build.assign_one first_name, wrapped
|
||||
|
||||
-- bubble cascading assigns
|
||||
transformed = if num_values == 1
|
||||
|
Loading…
Reference in New Issue
Block a user