Skip to content

Commit fd32503

Browse files
committed
implicit return bubbles again
1 parent 952987f commit fd32503

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

moonscript/transform.lua

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ Statement = Transformer({
225225
end,
226226
["if"] = function(node, ret)
227227
print("node:", node, "ret:", ret)
228+
print(util.dump(node))
229+
if ret then
230+
smart_node(node)
231+
node['then'] = apply_to_last(node['then'], ret)
232+
for i = 4, #node do
233+
local case = node[i]
234+
local body_idx = #node[i]
235+
case[body_idx] = apply_to_last(case[body_idx], ret)
236+
end
237+
end
228238
return node
229239
end,
230240
foreach = function(node)
@@ -592,6 +602,20 @@ local default_accumulator
592602
default_accumulator = function(node)
593603
return Accumulator():convert(node)
594604
end
605+
local implicitly_return
606+
implicitly_return = function(stm)
607+
local t = ntype(stm)
608+
if types.manual_return[t] or not is_value(stm) then
609+
return stm
610+
elseif types.cascading[t] then
611+
return Statement(stm, implicitly_return)
612+
else
613+
return {
614+
"return",
615+
stm
616+
}
617+
end
618+
end
595619
Value = Transformer({
596620
["for"] = default_accumulator,
597621
["while"] = default_accumulator,
@@ -607,17 +631,7 @@ Value = Transformer({
607631
end,
608632
fndef = function(node)
609633
smart_node(node)
610-
node.body = apply_to_last(node.body, function(stm)
611-
local t = ntype(stm)
612-
if types.manual_return[t] or not is_value(stm) then
613-
return stm
614-
else
615-
return {
616-
"return",
617-
stm
618-
}
619-
end
620-
end)
634+
node.body = apply_to_last(node.body, implicitly_return)
621635
return node
622636
end,
623637
chain = function(node)

moonscript/transform.moon

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ Statement = Transformer {
129129
-- handle cascading return decorator
130130
if: (node, ret) ->
131131
print "node:", node, "ret:", ret
132+
print util.dump node
133+
if ret
134+
smart_node node
135+
-- mutate all the bodies
136+
node['then'] = apply_to_last node['then'], ret
137+
for i = 4, #node
138+
case = node[i]
139+
body_idx = #node[i]
140+
case[body_idx] = apply_to_last case[body_idx], ret
132141
node
133142

134143
foreach: (node) ->
@@ -333,6 +342,15 @@ class Accumulator
333342
default_accumulator = (node) ->
334343
Accumulator!\convert node
335344

345+
implicitly_return = (stm) ->
346+
t = ntype stm
347+
if types.manual_return[t] or not is_value stm
348+
stm
349+
elseif types.cascading[t]
350+
Statement stm, implicitly_return
351+
else
352+
{"return", stm}
353+
336354
Value = Transformer {
337355
for: default_accumulator
338356
while: default_accumulator
@@ -346,15 +364,7 @@ Value = Transformer {
346364

347365
fndef: (node) ->
348366
smart_node node
349-
350-
node.body = apply_to_last node.body, (stm) ->
351-
t = ntype stm
352-
if types.manual_return[t] or not is_value stm
353-
stm
354-
-- elseif types.cascading[t]
355-
else
356-
{"return", stm}
357-
367+
node.body = apply_to_last node.body, implicitly_return
358368
node
359369
-- pull out colon chain
360370
chain: (node) ->

0 commit comments

Comments
 (0)