implicit return bubbles again

This commit is contained in:
leaf corcoran 2011-11-06 12:27:37 -08:00
parent 952987f29f
commit fd32503224
2 changed files with 44 additions and 20 deletions

View File

@ -225,6 +225,16 @@ Statement = Transformer({
end, end,
["if"] = function(node, ret) ["if"] = function(node, ret)
print("node:", node, "ret:", 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 return node
end, end,
foreach = function(node) foreach = function(node)
@ -592,6 +602,20 @@ local default_accumulator
default_accumulator = function(node) default_accumulator = function(node)
return Accumulator():convert(node) return Accumulator():convert(node)
end 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({ Value = Transformer({
["for"] = default_accumulator, ["for"] = default_accumulator,
["while"] = default_accumulator, ["while"] = default_accumulator,
@ -607,17 +631,7 @@ Value = Transformer({
end, end,
fndef = function(node) fndef = function(node)
smart_node(node) smart_node(node)
node.body = apply_to_last(node.body, function(stm) node.body = apply_to_last(node.body, implicitly_return)
local t = ntype(stm)
if types.manual_return[t] or not is_value(stm) then
return stm
else
return {
"return",
stm
}
end
end)
return node return node
end, end,
chain = function(node) chain = function(node)

View File

@ -129,6 +129,15 @@ Statement = Transformer {
-- handle cascading return decorator -- handle cascading return decorator
if: (node, ret) -> if: (node, ret) ->
print "node:", node, "ret:", 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 node
foreach: (node) -> foreach: (node) ->
@ -333,6 +342,15 @@ class Accumulator
default_accumulator = (node) -> default_accumulator = (node) ->
Accumulator!\convert 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 { Value = Transformer {
for: default_accumulator for: default_accumulator
while: default_accumulator while: default_accumulator
@ -346,15 +364,7 @@ Value = Transformer {
fndef: (node) -> fndef: (node) ->
smart_node node smart_node node
node.body = apply_to_last node.body, implicitly_return
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 node
-- pull out colon chain -- pull out colon chain
chain: (node) -> chain: (node) ->