numeric for loops

This commit is contained in:
leaf corcoran 2011-06-13 08:42:46 -07:00
parent 133c9b058d
commit a873203469
6 changed files with 59 additions and 1 deletions

View File

@ -265,6 +265,15 @@ local line_compile = {
self:add_line(inner:render())
return self:add_line("end")
end,
["for"] = function(self, node)
local _, name, bounds, block = unpack(node)
bounds = self:value({ "explist", unpack(bounds) })
self:add_line("for", self:name(name), "=", bounds, "do")
local inner = self:block()
inner:stms(block)
self:add_line(inner:render())
return self:add_line("end")
end,
comprehension = function(self, node, action)
local _, exp, clauses = unpack(node)
if not action then

View File

@ -189,6 +189,15 @@ line_compile =
@add_line inner:render()
@add_line "end"
["for"]: (node) =>
_, name, bounds, block = unpack node
bounds = @value {"explist", unpack bounds}
@add_line "for", @name(name), "=", bounds, "do"
inner = @block()
inner:stms block
@add_line inner:render()
@add_line "end"
comprehension: (node, action) =>
_, exp, clauses = unpack node

View File

@ -227,7 +227,7 @@ local build_grammar = wrap(function()
Block = Ct(Line * (Break^1 * Line)^0),
Line = Cmt(Indent, check_indent) * Statement + _Space * Comment,
Statement = (Import + While + BreakLoop + Ct(ExpList) / flatten_or_mark"explist" * Space) * (
Statement = (Import + While + For + BreakLoop + Ct(ExpList) / flatten_or_mark"explist" * Space) * (
-- statement decorators
key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" +
CompInner / mark"comprehension"
@ -252,6 +252,9 @@ local build_grammar = wrap(function()
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) *
key"do"^-1 * Body / mark"for",
Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension",
CompInner = Ct(CompFor * CompClause^0),

17
tests/inputs/loops.moon Normal file
View File

@ -0,0 +1,17 @@
for x=1,10
print "yeah"
for x=1,#something
print "yeah"
for y=100,60,-3
print "count down", y
for a=1,10 do print "okay"
for a=1,10
for b = 2,43
print a,b

View File

@ -147,3 +147,6 @@ y /= 100
m %= 2
x = 0
(if ntype(v) == "fndef" then x += 1) for v in *values

17
tests/outputs/loops.lua Normal file
View File

@ -0,0 +1,17 @@
for x = 1, 10 do
print("yeah")
end
for x = 1, #something do
print("yeah")
end
for y = 100, 60, -3 do
print("count down", y)
end
for a = 1, 10 do
print("okay")
end
for a = 1, 10 do
for b = 2, 43 do
print(a, b)
end
end