mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
implicit return bubbles again
This commit is contained in:
parent
952987f29f
commit
fd32503224
@ -225,6 +225,16 @@ Statement = Transformer({
|
||||
end,
|
||||
["if"] = function(node, ret)
|
||||
print("node:", node, "ret:", ret)
|
||||
print(util.dump(node))
|
||||
if ret then
|
||||
smart_node(node)
|
||||
node['then'] = apply_to_last(node['then'], ret)
|
||||
for i = 4, #node do
|
||||
local case = node[i]
|
||||
local body_idx = #node[i]
|
||||
case[body_idx] = apply_to_last(case[body_idx], ret)
|
||||
end
|
||||
end
|
||||
return node
|
||||
end,
|
||||
foreach = function(node)
|
||||
@ -592,6 +602,20 @@ local default_accumulator
|
||||
default_accumulator = function(node)
|
||||
return Accumulator():convert(node)
|
||||
end
|
||||
local implicitly_return
|
||||
implicitly_return = function(stm)
|
||||
local t = ntype(stm)
|
||||
if types.manual_return[t] or not is_value(stm) then
|
||||
return stm
|
||||
elseif types.cascading[t] then
|
||||
return Statement(stm, implicitly_return)
|
||||
else
|
||||
return {
|
||||
"return",
|
||||
stm
|
||||
}
|
||||
end
|
||||
end
|
||||
Value = Transformer({
|
||||
["for"] = default_accumulator,
|
||||
["while"] = default_accumulator,
|
||||
@ -607,17 +631,7 @@ Value = Transformer({
|
||||
end,
|
||||
fndef = function(node)
|
||||
smart_node(node)
|
||||
node.body = apply_to_last(node.body, function(stm)
|
||||
local t = ntype(stm)
|
||||
if types.manual_return[t] or not is_value(stm) then
|
||||
return stm
|
||||
else
|
||||
return {
|
||||
"return",
|
||||
stm
|
||||
}
|
||||
end
|
||||
end)
|
||||
node.body = apply_to_last(node.body, implicitly_return)
|
||||
return node
|
||||
end,
|
||||
chain = function(node)
|
||||
|
@ -129,6 +129,15 @@ Statement = Transformer {
|
||||
-- handle cascading return decorator
|
||||
if: (node, ret) ->
|
||||
print "node:", node, "ret:", ret
|
||||
print util.dump node
|
||||
if ret
|
||||
smart_node node
|
||||
-- mutate all the bodies
|
||||
node['then'] = apply_to_last node['then'], ret
|
||||
for i = 4, #node
|
||||
case = node[i]
|
||||
body_idx = #node[i]
|
||||
case[body_idx] = apply_to_last case[body_idx], ret
|
||||
node
|
||||
|
||||
foreach: (node) ->
|
||||
@ -333,6 +342,15 @@ class Accumulator
|
||||
default_accumulator = (node) ->
|
||||
Accumulator!\convert node
|
||||
|
||||
implicitly_return = (stm) ->
|
||||
t = ntype stm
|
||||
if types.manual_return[t] or not is_value stm
|
||||
stm
|
||||
elseif types.cascading[t]
|
||||
Statement stm, implicitly_return
|
||||
else
|
||||
{"return", stm}
|
||||
|
||||
Value = Transformer {
|
||||
for: default_accumulator
|
||||
while: default_accumulator
|
||||
@ -346,15 +364,7 @@ Value = Transformer {
|
||||
|
||||
fndef: (node) ->
|
||||
smart_node node
|
||||
|
||||
node.body = apply_to_last node.body, (stm) ->
|
||||
t = ntype stm
|
||||
if types.manual_return[t] or not is_value stm
|
||||
stm
|
||||
-- elseif types.cascading[t]
|
||||
else
|
||||
{"return", stm}
|
||||
|
||||
node.body = apply_to_last node.body, implicitly_return
|
||||
node
|
||||
-- pull out colon chain
|
||||
chain: (node) ->
|
||||
|
Loading…
Reference in New Issue
Block a user