mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
support full syntax of generic for loop #41
This commit is contained in:
parent
97d8bc2e66
commit
d3170a018d
3
Makefile
3
Makefile
@ -1,4 +1,7 @@
|
||||
|
||||
test::
|
||||
lua test.lua
|
||||
|
||||
local:
|
||||
luarocks make --local moonscript-dev-1.rockspec
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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",
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user