This commit is contained in:
leaf corcoran 2015-09-26 21:16:02 -07:00
parent 9640694708
commit ce396acb15
6 changed files with 55 additions and 22 deletions

View File

@ -116,8 +116,7 @@ with_continue_listener = function(body)
return
end
local last = last_stm(body)
local t = last and ntype(last)
local enclose_lines = t == "return" or t == "break"
local enclose_lines = types.terminating[last and ntype(last)]
self:put_name(continue_name, nil)
return self:splice(function(lines)
if enclose_lines then
@ -653,6 +652,14 @@ Statement = Transformer({
local exp, block = unpack(node, 2)
local copy_scope = true
local scope_name, named_assign
do
local last = last_stm(block)
if last then
if types.terminating[ntype(last)] then
ret = false
end
end
end
if ntype(exp) == "assign" then
local names, values = unpack(exp, 2)
local first_name = names[1]
@ -676,19 +683,18 @@ Statement = Transformer({
copy_scope = false
end
scope_name = scope_name or NameProxy("with")
return build["do"]({
local out = build["do"]({
copy_scope and build.assign_one(scope_name, exp) or NOOP,
named_assign or NOOP,
Run(function(self)
return self:set("scope_var", scope_name)
end),
build.group(block),
(function()
if ret then
return ret(scope_name)
end
end)()
unpack(block)
})
if ret then
table.insert(out[2], ret(scope_name))
end
return out
end,
foreach = function(self, node, _)
smart_node(node)

View File

@ -74,8 +74,7 @@ with_continue_listener = (body) ->
Run =>
return unless continue_name
last = last_stm body
t = last and ntype(last)
enclose_lines = t == "return" or t == "break"
enclose_lines = types.terminating[last and ntype(last)]
@put_name continue_name, nil
@splice (lines) ->
@ -381,6 +380,8 @@ Statement = Transformer {
copy_scope = true
local scope_name, named_assign
if last = last_stm block
ret = false if types.terminating[ntype(last)]
if ntype(exp) == "assign"
names, values = unpack exp, 2
@ -403,16 +404,18 @@ Statement = Transformer {
scope_name or= NameProxy "with"
build.do {
out = build.do {
copy_scope and build.assign_one(scope_name, exp) or NOOP
named_assign or NOOP
Run => @set "scope_var", scope_name
build.group block
if ret
ret scope_name
unpack block
}
if ret
table.insert out[2], ret scope_name
out
foreach: (node, _) =>
smart_node node
source = unpack node.iter

View File

@ -19,6 +19,10 @@ local cascading = Set({
"class",
"do"
})
local terminating = Set({
"return",
"break"
})
local ntype
ntype = function(node)
local _exp_0 = type(node)
@ -317,5 +321,6 @@ return {
value_is_singular = value_is_singular,
comprehension_has_value = comprehension_has_value,
has_value = has_value,
mtype = mtype
mtype = mtype,
terminating = terminating
}

View File

@ -6,12 +6,20 @@ import insert from table
import unpack from util
-- implicit return does not work on these statements
manual_return = Set{"foreach", "for", "while", "return"}
manual_return = Set {
"foreach", "for", "while", "return"
}
-- Assigns and returns are bubbled into their bodies.
-- All cascading statement transform functions accept a second arugment that
-- is the transformation to apply to the last statement in their body
cascading = Set{ "if", "unless", "with", "switch", "class", "do" }
cascading = Set {
"if", "unless", "with", "switch", "class", "do"
}
terminating = Set {
"return", "break"
}
-- type of node as string
ntype = (node) ->
@ -185,7 +193,6 @@ smart_node = (node) ->
{
:ntype, :smart_node, :build, :is_value, :is_slice, :manual_return,
:cascading, :value_is_singular, :comprehension_has_value, :has_value,
:mtype
:cascading, :value_is_singular, :comprehension_has_value, :has_value, :mtype, :terminating
}

View File

@ -106,3 +106,8 @@ do
with .b = 2
print .c
do
->
with hi
return .a, .b

View File

@ -151,6 +151,13 @@ do
_with_0.b = _with_1
print(_with_1.c)
end
return _with_0
end
end
do
return function()
do
local _with_0 = hi
return _with_0.a, _with_0.b
end
end
end