diff --git a/Makefile b/Makefile index 2b02375..9c2b8ab 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ +test:: + lua test.lua + local: luarocks make --local moonscript-dev-1.rockspec diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 2d97d74..4c42a58 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -162,7 +162,7 @@ line_compile = { end end, foreach = function(self, node) - local _, names, exp, block = unpack(node) + local _, names, exps, block = unpack(node) local loop do local _with_0 = self:line() @@ -178,7 +178,19 @@ line_compile = { end return _accum_0 end)(), ", ") - _with_0:append(" in ", self:value(exp), " do") + _with_0:append(" in ") + _with_0:append_list((function() + local _accum_0 = { } + local _len_0 = 0 + local _list_0 = exps + for _index_0 = 1, #_list_0 do + local exp = _list_0[_index_0] + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:value(exp) + end + return _accum_0 + end)(), ",") + _with_0:append(" do") loop = _with_0 end do diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 45736b2..0381afc 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -94,14 +94,16 @@ line_compile = \stms block -- for x in y ... - -- {"foreach", {names...}, exp, body} + -- {"foreach", {names...}, {exp...}, body} foreach: (node) => - _, names, exp, block = unpack node + _, names, exps, block = unpack node loop = with @line! \append "for " \append_list [@name name for name in *names], ", " - \append " in ", @value(exp), " do" + \append " in " + \append_list [@value exp for exp in *exps], "," + \append " do" with @block loop \stms block diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 7398813..0ebe092 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -333,7 +333,7 @@ local build_grammar = wrap_env(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", + ForEach = key"for" * Ct(NameList) * key"in" * Ct(sym"*" * Exp / mark"unpack" + ExpList) * key"do"^-1 * Body / mark"foreach", Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension", diff --git a/moonscript/transform.lua b/moonscript/transform.lua index cffae14..1a589fa 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -279,7 +279,9 @@ construct_comprehension = function(inner, clauses) current_stms = { "foreach", names, - iter, + { + iter + }, current_stms } elseif t == "when" then @@ -491,8 +493,9 @@ Statement = Transformer({ end, foreach = function(self, node) smart_node(node) - if ntype(node.iter) == "unpack" then - local list = node.iter[2] + local source = unpack(node.iter) + if ntype(source) == "unpack" then + local list = source[2] local index_name = NameProxy("index") local list_name = NameProxy("list") local slice_var = nil diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 98c4f5a..3264306 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -115,7 +115,7 @@ construct_comprehension = (inner, clauses) -> t = clause[1] current_stms = if t == "for" _, names, iter = unpack clause - {"foreach", names, iter, current_stms} + {"foreach", names, {iter}, current_stms} elseif t == "when" _, cond = unpack clause {"if", cond, current_stms} @@ -231,8 +231,10 @@ Statement = Transformer { foreach: (node) => smart_node node - if ntype(node.iter) == "unpack" - list = node.iter[2] + source = unpack node.iter + + if ntype(source) == "unpack" + list = source[2] index_name = NameProxy "index" list_name = NameProxy "list" diff --git a/tests/inputs/loops.moon b/tests/inputs/loops.moon index b2a43bc..fe0f0cd 100644 --- a/tests/inputs/loops.moon +++ b/tests/inputs/loops.moon @@ -24,6 +24,13 @@ for x in *something for k,v in pairs hello do print k,v +for x in y, z + print x + +for x in y, z, k + print x + + x = -> for x in y y diff --git a/tests/outputs/loops.lua b/tests/outputs/loops.lua index 2231235..fa3cdf6 100644 --- a/tests/outputs/loops.lua +++ b/tests/outputs/loops.lua @@ -29,6 +29,12 @@ end for k, v in pairs(hello) do print(k, v) end +for x in y,z do + print(x) +end +for x in y,z,k do + print(x) +end local x x = function() for x in y do