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:
|
local:
|
||||||
luarocks make --local moonscript-dev-1.rockspec
|
luarocks make --local moonscript-dev-1.rockspec
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ line_compile = {
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
foreach = function(self, node)
|
foreach = function(self, node)
|
||||||
local _, names, exp, block = unpack(node)
|
local _, names, exps, block = unpack(node)
|
||||||
local loop
|
local loop
|
||||||
do
|
do
|
||||||
local _with_0 = self:line()
|
local _with_0 = self:line()
|
||||||
@ -178,7 +178,19 @@ line_compile = {
|
|||||||
end
|
end
|
||||||
return _accum_0
|
return _accum_0
|
||||||
end)(), ", ")
|
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
|
loop = _with_0
|
||||||
end
|
end
|
||||||
do
|
do
|
||||||
|
@ -94,14 +94,16 @@ line_compile =
|
|||||||
\stms block
|
\stms block
|
||||||
|
|
||||||
-- for x in y ...
|
-- for x in y ...
|
||||||
-- {"foreach", {names...}, exp, body}
|
-- {"foreach", {names...}, {exp...}, body}
|
||||||
foreach: (node) =>
|
foreach: (node) =>
|
||||||
_, names, exp, block = unpack node
|
_, names, exps, block = unpack node
|
||||||
|
|
||||||
loop = with @line!
|
loop = with @line!
|
||||||
\append "for "
|
\append "for "
|
||||||
\append_list [@name name for name in *names], ", "
|
\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
|
with @block loop
|
||||||
\stms block
|
\stms block
|
||||||
|
@ -333,7 +333,7 @@ local build_grammar = wrap_env(function()
|
|||||||
For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) *
|
For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) *
|
||||||
key"do"^-1 * Body / mark"for",
|
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",
|
Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension",
|
||||||
|
|
||||||
|
@ -279,7 +279,9 @@ construct_comprehension = function(inner, clauses)
|
|||||||
current_stms = {
|
current_stms = {
|
||||||
"foreach",
|
"foreach",
|
||||||
names,
|
names,
|
||||||
iter,
|
{
|
||||||
|
iter
|
||||||
|
},
|
||||||
current_stms
|
current_stms
|
||||||
}
|
}
|
||||||
elseif t == "when" then
|
elseif t == "when" then
|
||||||
@ -491,8 +493,9 @@ Statement = Transformer({
|
|||||||
end,
|
end,
|
||||||
foreach = function(self, node)
|
foreach = function(self, node)
|
||||||
smart_node(node)
|
smart_node(node)
|
||||||
if ntype(node.iter) == "unpack" then
|
local source = unpack(node.iter)
|
||||||
local list = node.iter[2]
|
if ntype(source) == "unpack" then
|
||||||
|
local list = source[2]
|
||||||
local index_name = NameProxy("index")
|
local index_name = NameProxy("index")
|
||||||
local list_name = NameProxy("list")
|
local list_name = NameProxy("list")
|
||||||
local slice_var = nil
|
local slice_var = nil
|
||||||
|
@ -115,7 +115,7 @@ construct_comprehension = (inner, clauses) ->
|
|||||||
t = clause[1]
|
t = clause[1]
|
||||||
current_stms = if t == "for"
|
current_stms = if t == "for"
|
||||||
_, names, iter = unpack clause
|
_, names, iter = unpack clause
|
||||||
{"foreach", names, iter, current_stms}
|
{"foreach", names, {iter}, current_stms}
|
||||||
elseif t == "when"
|
elseif t == "when"
|
||||||
_, cond = unpack clause
|
_, cond = unpack clause
|
||||||
{"if", cond, current_stms}
|
{"if", cond, current_stms}
|
||||||
@ -231,8 +231,10 @@ Statement = Transformer {
|
|||||||
|
|
||||||
foreach: (node) =>
|
foreach: (node) =>
|
||||||
smart_node node
|
smart_node node
|
||||||
if ntype(node.iter) == "unpack"
|
source = unpack node.iter
|
||||||
list = node.iter[2]
|
|
||||||
|
if ntype(source) == "unpack"
|
||||||
|
list = source[2]
|
||||||
|
|
||||||
index_name = NameProxy "index"
|
index_name = NameProxy "index"
|
||||||
list_name = NameProxy "list"
|
list_name = NameProxy "list"
|
||||||
|
@ -24,6 +24,13 @@ for x in *something
|
|||||||
|
|
||||||
for k,v in pairs hello do print k,v
|
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 = ->
|
x = ->
|
||||||
for x in y
|
for x in y
|
||||||
y
|
y
|
||||||
|
@ -29,6 +29,12 @@ end
|
|||||||
for k, v in pairs(hello) do
|
for k, v in pairs(hello) do
|
||||||
print(k, v)
|
print(k, v)
|
||||||
end
|
end
|
||||||
|
for x in y,z do
|
||||||
|
print(x)
|
||||||
|
end
|
||||||
|
for x in y,z,k do
|
||||||
|
print(x)
|
||||||
|
end
|
||||||
local x
|
local x
|
||||||
x = function()
|
x = function()
|
||||||
for x in y do
|
for x in y do
|
||||||
|
Loading…
Reference in New Issue
Block a user