mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
numeric for loops
This commit is contained in:
parent
133c9b058d
commit
a873203469
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
17
tests/inputs/loops.moon
Normal 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
|
||||
|
||||
|
@ -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
17
tests/outputs/loops.lua
Normal 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
|
Loading…
Reference in New Issue
Block a user