continue on all loops

This commit is contained in:
leaf corcoran 2012-10-27 22:23:06 -07:00
parent 0d5abf6cc4
commit 52655cf199
6 changed files with 129 additions and 82 deletions

View File

@ -231,66 +231,12 @@ line_compile = {
_with_0:append(" do") _with_0:append(" do")
loop = _with_0 loop = _with_0
end end
local continue_name = nil
local out
do do
local _with_0 = self:block(loop) local _with_0 = self:block(loop)
_with_0:listen("continue", function()
if not (continue_name) then
continue_name = NameProxy("continue")
_with_0:put_name(continue_name)
end
return continue_name
end)
_with_0:declare(names) _with_0:declare(names)
_with_0:stms(block) _with_0:stms(block)
out = _with_0 return _with_0
end end
if continue_name then
out:put_name(continue_name, nil)
out:splice(function(lines)
return {
{
"assign",
{
continue_name
},
{
"false"
}
},
{
"repeat",
"true",
{
lines,
{
"assign",
{
continue_name
},
{
"true"
}
}
}
},
{
"if",
{
"not",
continue_name
},
{
{
"break"
}
}
}
}
end)
end
return out
end, end,
export = function(self, node) export = function(self, node)
local _, names = unpack(node) local _, names = unpack(node)

View File

@ -122,33 +122,10 @@ line_compile =
\append_list [@value exp for exp in *exps], "," \append_list [@value exp for exp in *exps], ","
\append " do" \append " do"
continue_name = nil with @block loop
out = with @block loop
\listen "continue", ->
unless continue_name
continue_name = NameProxy"continue"
\put_name continue_name
continue_name
\declare names \declare names
\stms block \stms block
-- todo: figure out how to put this in the transformer
if continue_name
out\put_name continue_name, nil
out\splice (lines) -> {
{"assign", {continue_name}, {"false"}}
{"repeat", "true", {
lines
{"assign", {continue_name}, {"true"}}
}}
{"if", {"not", continue_name}, {
{"break"}
}}
}
out
export: (node) => export: (node) =>
_, names = unpack node _, names = unpack node
if type(names) == "string" if type(names) == "string"

View File

@ -275,6 +275,69 @@ expand_elseif_assign = function(ifstm)
return ifstm return ifstm
end end
local constructor_name = "new" local constructor_name = "new"
local with_continue_listener
with_continue_listener = function(body)
local continue_name = nil
return {
Run(function(self)
return self:listen("continue", function()
if not (continue_name) then
continue_name = NameProxy("continue")
self:put_name(continue_name)
end
return continue_name
end)
end),
build.group(body),
Run(function(self)
if not (continue_name) then
return
end
self:put_name(continue_name, nil)
return self:splice(function(lines)
return {
{
"assign",
{
continue_name
},
{
"false"
}
},
{
"repeat",
"true",
{
lines,
{
"assign",
{
continue_name
},
{
"true"
}
}
}
},
{
"if",
{
"not",
continue_name
},
{
{
"break"
}
}
}
}
end)
end)
}
end
local Transformer local Transformer
Transformer = (function() Transformer = (function()
local _parent_0 = nil local _parent_0 = nil
@ -753,6 +816,15 @@ Statement = Transformer({
}) })
}) })
end end
node.body = with_continue_listener(node.body)
end,
["while"] = function(self, node)
smart_node(node)
node.body = with_continue_listener(node.body)
end,
["for"] = function(self, node)
smart_node(node)
node.body = with_continue_listener(node.body)
end, end,
switch = function(self, node, ret) switch = function(self, node, ret)
local _, exp, conds = unpack(node) local _, exp, conds = unpack(node)

View File

@ -116,6 +116,34 @@ expand_elseif_assign = (ifstm) ->
constructor_name = "new" constructor_name = "new"
with_continue_listener = (body) ->
continue_name = nil
{
Run =>
@listen "continue", ->
unless continue_name
continue_name = NameProxy"continue"
@put_name continue_name
continue_name
build.group body
Run =>
return unless continue_name
@put_name continue_name, nil
@splice (lines) -> {
{"assign", {continue_name}, {"false"}}
{"repeat", "true", {
lines
{"assign", {continue_name}, {"true"}}
}}
{"if", {"not", continue_name}, {
{"break"}
}}
}
}
class Transformer class Transformer
new: (@transformers) => new: (@transformers) =>
@seen_nodes = setmetatable {}, __mode: "k" @seen_nodes = setmetatable {}, __mode: "k"
@ -346,7 +374,7 @@ Statement = Transformer {
else else
{1, {"length", list_name}} {1, {"length", list_name}}
build.group { return build.group {
build.assign_one list_name, list build.assign_one list_name, list
slice_var slice_var
build["for"] { build["for"] {
@ -359,6 +387,16 @@ Statement = Transformer {
} }
} }
node.body = with_continue_listener node.body
while: (node) =>
smart_node node
node.body = with_continue_listener node.body
for: (node) =>
smart_node node
node.body = with_continue_listener node.body
switch: (node, ret) => switch: (node, ret) =>
_, exp, conds = unpack node _, exp, conds = unpack node
exp_name = NameProxy "exp" exp_name = NameProxy "exp"

View File

@ -74,7 +74,7 @@ local node_types = {
}, },
{ {
"body", "body",
{ } t
} }
}, },
["for"] = { ["for"] = {
@ -90,6 +90,16 @@ local node_types = {
t t
} }
}, },
["while"] = {
{
"cond",
t
},
{
"body",
t
}
},
assign = { assign = {
{ {
"names", "names",

View File

@ -51,13 +51,17 @@ node_types = {
foreach: { foreach: {
{"names", t} {"names", t}
{"iter"} {"iter"}
{"body", {}} {"body", t}
} }
for: { for: {
{"name"} {"name"}
{"bounds", t} {"bounds", t}
{"body", t} {"body", t}
} }
while: {
{"cond", t}
{"body", t}
}
assign: { assign: {
{"names", t} {"names", t}
{"values", t} {"values", t}