Skip to content

Commit 52655cf

Browse files
committed
continue on all loops
1 parent 0d5abf6 commit 52655cf

File tree

6 files changed

+129
-82
lines changed

6 files changed

+129
-82
lines changed

moonscript/compile/statement.lua

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -231,66 +231,12 @@ line_compile = {
231231
_with_0:append(" do")
232232
loop = _with_0
233233
end
234-
local continue_name = nil
235-
local out
236234
do
237235
local _with_0 = self:block(loop)
238-
_with_0:listen("continue", function()
239-
if not (continue_name) then
240-
continue_name = NameProxy("continue")
241-
_with_0:put_name(continue_name)
242-
end
243-
return continue_name
244-
end)
245236
_with_0:declare(names)
246237
_with_0:stms(block)
247-
out = _with_0
248-
end
249-
if continue_name then
250-
out:put_name(continue_name, nil)
251-
out:splice(function(lines)
252-
return {
253-
{
254-
"assign",
255-
{
256-
continue_name
257-
},
258-
{
259-
"false"
260-
}
261-
},
262-
{
263-
"repeat",
264-
"true",
265-
{
266-
lines,
267-
{
268-
"assign",
269-
{
270-
continue_name
271-
},
272-
{
273-
"true"
274-
}
275-
}
276-
}
277-
},
278-
{
279-
"if",
280-
{
281-
"not",
282-
continue_name
283-
},
284-
{
285-
{
286-
"break"
287-
}
288-
}
289-
}
290-
}
291-
end)
238+
return _with_0
292239
end
293-
return out
294240
end,
295241
export = function(self, node)
296242
local _, names = unpack(node)

moonscript/compile/statement.moon

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,33 +122,10 @@ line_compile =
122122
\append_list [@value exp for exp in *exps], ","
123123
\append " do"
124124

125-
continue_name = nil
126-
out = with @block loop
127-
\listen "continue", ->
128-
unless continue_name
129-
continue_name = NameProxy"continue"
130-
\put_name continue_name
131-
continue_name
132-
125+
with @block loop
133126
\declare names
134127
\stms block
135128

136-
-- todo: figure out how to put this in the transformer
137-
if continue_name
138-
out\put_name continue_name, nil
139-
out\splice (lines) -> {
140-
{"assign", {continue_name}, {"false"}}
141-
{"repeat", "true", {
142-
lines
143-
{"assign", {continue_name}, {"true"}}
144-
}}
145-
{"if", {"not", continue_name}, {
146-
{"break"}
147-
}}
148-
}
149-
150-
out
151-
152129
export: (node) =>
153130
_, names = unpack node
154131
if type(names) == "string"

moonscript/transform.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,69 @@ expand_elseif_assign = function(ifstm)
275275
return ifstm
276276
end
277277
local constructor_name = "new"
278+
local with_continue_listener
279+
with_continue_listener = function(body)
280+
local continue_name = nil
281+
return {
282+
Run(function(self)
283+
return self:listen("continue", function()
284+
if not (continue_name) then
285+
continue_name = NameProxy("continue")
286+
self:put_name(continue_name)
287+
end
288+
return continue_name
289+
end)
290+
end),
291+
build.group(body),
292+
Run(function(self)
293+
if not (continue_name) then
294+
return
295+
end
296+
self:put_name(continue_name, nil)
297+
return self:splice(function(lines)
298+
return {
299+
{
300+
"assign",
301+
{
302+
continue_name
303+
},
304+
{
305+
"false"
306+
}
307+
},
308+
{
309+
"repeat",
310+
"true",
311+
{
312+
lines,
313+
{
314+
"assign",
315+
{
316+
continue_name
317+
},
318+
{
319+
"true"
320+
}
321+
}
322+
}
323+
},
324+
{
325+
"if",
326+
{
327+
"not",
328+
continue_name
329+
},
330+
{
331+
{
332+
"break"
333+
}
334+
}
335+
}
336+
}
337+
end)
338+
end)
339+
}
340+
end
278341
local Transformer
279342
Transformer = (function()
280343
local _parent_0 = nil
@@ -753,6 +816,15 @@ Statement = Transformer({
753816
})
754817
})
755818
end
819+
node.body = with_continue_listener(node.body)
820+
end,
821+
["while"] = function(self, node)
822+
smart_node(node)
823+
node.body = with_continue_listener(node.body)
824+
end,
825+
["for"] = function(self, node)
826+
smart_node(node)
827+
node.body = with_continue_listener(node.body)
756828
end,
757829
switch = function(self, node, ret)
758830
local _, exp, conds = unpack(node)

moonscript/transform.moon

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ expand_elseif_assign = (ifstm) ->
116116

117117
constructor_name = "new"
118118

119+
with_continue_listener = (body) ->
120+
continue_name = nil
121+
{
122+
Run =>
123+
@listen "continue", ->
124+
unless continue_name
125+
continue_name = NameProxy"continue"
126+
@put_name continue_name
127+
continue_name
128+
129+
build.group body
130+
131+
Run =>
132+
return unless continue_name
133+
@put_name continue_name, nil
134+
@splice (lines) -> {
135+
{"assign", {continue_name}, {"false"}}
136+
{"repeat", "true", {
137+
lines
138+
{"assign", {continue_name}, {"true"}}
139+
}}
140+
{"if", {"not", continue_name}, {
141+
{"break"}
142+
}}
143+
}
144+
}
145+
146+
119147
class Transformer
120148
new: (@transformers) =>
121149
@seen_nodes = setmetatable {}, __mode: "k"
@@ -346,7 +374,7 @@ Statement = Transformer {
346374
else
347375
{1, {"length", list_name}}
348376

349-
build.group {
377+
return build.group {
350378
build.assign_one list_name, list
351379
slice_var
352380
build["for"] {
@@ -359,6 +387,16 @@ Statement = Transformer {
359387
}
360388
}
361389

390+
node.body = with_continue_listener node.body
391+
392+
while: (node) =>
393+
smart_node node
394+
node.body = with_continue_listener node.body
395+
396+
for: (node) =>
397+
smart_node node
398+
node.body = with_continue_listener node.body
399+
362400
switch: (node, ret) =>
363401
_, exp, conds = unpack node
364402
exp_name = NameProxy "exp"

moonscript/types.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ local node_types = {
7474
},
7575
{
7676
"body",
77-
{ }
77+
t
7878
}
7979
},
8080
["for"] = {
@@ -90,6 +90,16 @@ local node_types = {
9090
t
9191
}
9292
},
93+
["while"] = {
94+
{
95+
"cond",
96+
t
97+
},
98+
{
99+
"body",
100+
t
101+
}
102+
},
93103
assign = {
94104
{
95105
"names",

moonscript/types.moon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ node_types = {
5151
foreach: {
5252
{"names", t}
5353
{"iter"}
54-
{"body", {}}
54+
{"body", t}
5555
}
5656
for: {
5757
{"name"}
5858
{"bounds", t}
5959
{"body", t}
6060
}
61+
while: {
62+
{"cond", t}
63+
{"body", t}
64+
}
6165
assign: {
6266
{"names", t}
6367
{"values", t}

0 commit comments

Comments
 (0)