From a8732034698a0737146f3f4adafda95212889c15 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 13 Jun 2011 08:42:46 -0700 Subject: [PATCH] numeric for loops --- moonscript/compile2.lua | 9 +++++++++ moonscript/compile2.moon | 9 +++++++++ moonscript/parse.lua | 5 ++++- tests/inputs/loops.moon | 17 +++++++++++++++++ tests/inputs/syntax.moon | 3 +++ tests/outputs/loops.lua | 17 +++++++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/inputs/loops.moon create mode 100644 tests/outputs/loops.lua diff --git a/moonscript/compile2.lua b/moonscript/compile2.lua index cc2dffd..0ea8bac 100644 --- a/moonscript/compile2.lua +++ b/moonscript/compile2.lua @@ -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 diff --git a/moonscript/compile2.moon b/moonscript/compile2.moon index ef73dcd..9dc5c71 100644 --- a/moonscript/compile2.moon +++ b/moonscript/compile2.moon @@ -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 diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 6e9c2f0..e1399c5 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -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), diff --git a/tests/inputs/loops.moon b/tests/inputs/loops.moon new file mode 100644 index 0000000..28f3d3b --- /dev/null +++ b/tests/inputs/loops.moon @@ -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 + + diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index 69210ad..e92c4bc 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -147,3 +147,6 @@ y /= 100 m %= 2 +x = 0 +(if ntype(v) == "fndef" then x += 1) for v in *values + diff --git a/tests/outputs/loops.lua b/tests/outputs/loops.lua new file mode 100644 index 0000000..e888c1b --- /dev/null +++ b/tests/outputs/loops.lua @@ -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 \ No newline at end of file