From 1ae3e4e4cc2700db488921e8fbfba0db362bfa69 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 14 Jul 2011 23:46:16 -0700 Subject: [PATCH] standalone for in loop --- moonscript/parse.lua | 5 ++++- tests/inputs/loops.moon | 14 ++++++++++++++ tests/outputs/loops.lua | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 2b4321b..0dbb959 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -240,7 +240,8 @@ local build_grammar = wrap(function() Block = Ct(Line * (Break^1 * Line)^0), Line = Cmt(Indent, check_indent) * Statement + _Space * Comment, - Statement = (Import + While + With + For + ClassDecl + Export + BreakLoop + Ct(ExpList) / flatten_or_mark"explist" * Space) * ( + Statement = (Import + While + With + For + Foreach + + ClassDecl + Export + BreakLoop + Ct(ExpList) / flatten_or_mark"explist" * Space) * ( -- statement decorators key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" + CompInner / mark"comprehension" @@ -270,6 +271,8 @@ local build_grammar = wrap(function() For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) * key"do"^-1 * Body / mark"for", + Foreach = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) * key"do"^-1 * Body / mark"foreach", + Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension", CompInner = Ct(CompFor * CompClause^0), diff --git a/tests/inputs/loops.moon b/tests/inputs/loops.moon index 28f3d3b..cce532a 100644 --- a/tests/inputs/loops.moon +++ b/tests/inputs/loops.moon @@ -14,4 +14,18 @@ for a=1,10 for b = 2,43 print a,b +for i in iter + for j in yeah + x = 343 + i + j + print i, j + +for x in *something + print x + +for k,v in pairs hello do print k,v + +x = -> + for x in y + y + diff --git a/tests/outputs/loops.lua b/tests/outputs/loops.lua index e888c1b..1ff4280 100644 --- a/tests/outputs/loops.lua +++ b/tests/outputs/loops.lua @@ -14,4 +14,26 @@ for a = 1, 10 do for b = 2, 43 do print(a, b) end +end +for i in iter do + for j in yeah do + local x = 343 + i + j + print(i, j) + end +end +do + local _item_0 = something + for _index_0 = 1, #_item_0 do + local x = _item_0[_index_0] + print(x) + end +end +for k, v in pairs(hello) do + print(k, v) +end +local x +x = function() + for x in y do + local _ = y + end end \ No newline at end of file