mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
syntax for table array iteration
This commit is contained in:
parent
7a85335e0a
commit
9c0fced067
@ -281,16 +281,33 @@ local compiler_index = {
|
|||||||
and { ("table.insert(tmp, %s)"):format(self:value(exp)) }
|
and { ("table.insert(tmp, %s)"):format(self:value(exp)) }
|
||||||
or { self:stm(exp) }
|
or { self:stm(exp) }
|
||||||
|
|
||||||
|
self:push()
|
||||||
for i = #clauses,1,-1 do
|
for i = #clauses,1,-1 do
|
||||||
local c = clauses[i]
|
local c = clauses[i]
|
||||||
|
|
||||||
if "for" == c[1] then
|
if "for" == c[1] then
|
||||||
local _, names, iter = unpack(c)
|
local _, names, iter = unpack(c)
|
||||||
|
if ntype(iter) == "unpack" then
|
||||||
|
iter = iter[2]
|
||||||
|
local items_tmp, index_tmp = self:get_free_name("items"), self:get_free_name("index")
|
||||||
|
|
||||||
|
self:put_name(items_tmp)
|
||||||
|
self:put_name(index_tmp)
|
||||||
|
|
||||||
|
action = {
|
||||||
|
("local %s = %s"):format(items_tmp, self:value(iter)),
|
||||||
|
("for %s=1,#%s do"):format(index_tmp, items_tmp),
|
||||||
|
{("local %s = %s[%s]"):format(self:name_list(names), items_tmp, index_tmp)},
|
||||||
|
action,
|
||||||
|
"end"
|
||||||
|
}
|
||||||
|
else
|
||||||
action = {
|
action = {
|
||||||
("for %s in %s do"):format(self:name_list(names), self:value(iter)),
|
("for %s in %s do"):format(self:name_list(names), self:value(iter)),
|
||||||
action,
|
action,
|
||||||
"end"
|
"end"
|
||||||
}
|
}
|
||||||
|
end
|
||||||
elseif "when" == c[1] then
|
elseif "when" == c[1] then
|
||||||
local _, when = unpack(c)
|
local _, when = unpack(c)
|
||||||
action = {
|
action = {
|
||||||
@ -302,6 +319,7 @@ local compiler_index = {
|
|||||||
error("Unknown clause type :"..tostring(c[1]))
|
error("Unknown clause type :"..tostring(c[1]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
self:pop()
|
||||||
|
|
||||||
if return_value then
|
if return_value then
|
||||||
return self:pretty{
|
return self:pretty{
|
||||||
|
@ -243,12 +243,10 @@ local build_grammar = wrap(function()
|
|||||||
|
|
||||||
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
|
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
|
||||||
|
|
||||||
Comprehension = sym"[" * Exp *
|
Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension",
|
||||||
Ct((key"for" * Ct(NameList) * key"in" * Exp / mark"for") * CompClause^0) *
|
|
||||||
sym"]" / mark"comprehension",
|
|
||||||
|
|
||||||
CompInner = Ct(CompFor * CompClause^0),
|
CompInner = Ct(CompFor * CompClause^0),
|
||||||
CompFor = key"for" * Ct(NameList) * key"in" * Exp / mark"for",
|
CompFor = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"for",
|
||||||
CompClause = CompFor + key"when" * Exp / mark"when",
|
CompClause = CompFor + key"when" * Exp / mark"when",
|
||||||
|
|
||||||
Assign = Ct(AssignableList) * sym"=" * (Ct(TableBlock + ExpListLow) + If) / mark"assign",
|
Assign = Ct(AssignableList) * sym"=" * (Ct(TableBlock + ExpListLow) + If) / mark"assign",
|
||||||
|
@ -39,3 +39,13 @@ x = [x for x in x]
|
|||||||
|
|
||||||
print x,y for x in ipairs{1,2,4} for y in ipairs{1,2,3} when x != 2
|
print x,y for x in ipairs{1,2,4} for y in ipairs{1,2,3} when x != 2
|
||||||
|
|
||||||
|
double = [x*2 for x in *items]
|
||||||
|
|
||||||
|
print x for x in *double
|
||||||
|
|
||||||
|
cut = [x for x in *items when x > 3]
|
||||||
|
|
||||||
|
hello = [x + y for x in *items for y in *items]
|
||||||
|
|
||||||
|
print z for z in *hello
|
||||||
|
|
||||||
|
@ -122,3 +122,46 @@ for x in ipairs({ 1, 2, 4 }) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
local double = (function()
|
||||||
|
local tmp = {}
|
||||||
|
local _items_0 = items
|
||||||
|
for _index_0=1,#_items_0 do
|
||||||
|
local x = _items_0[_index_0]
|
||||||
|
table.insert(tmp, x * 2)
|
||||||
|
end
|
||||||
|
return tmp
|
||||||
|
end)()
|
||||||
|
local _items_0 = double
|
||||||
|
for _index_0=1,#_items_0 do
|
||||||
|
local x = _items_0[_index_0]
|
||||||
|
print(x)
|
||||||
|
end
|
||||||
|
local cut = (function()
|
||||||
|
local tmp = {}
|
||||||
|
local _items_0 = items
|
||||||
|
for _index_0=1,#_items_0 do
|
||||||
|
local x = _items_0[_index_0]
|
||||||
|
if x > 3 then
|
||||||
|
table.insert(tmp, x)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return tmp
|
||||||
|
end)()
|
||||||
|
local hello = (function()
|
||||||
|
local tmp = {}
|
||||||
|
local _items_1 = items
|
||||||
|
for _index_1=1,#_items_1 do
|
||||||
|
local x = _items_1[_index_1]
|
||||||
|
local _items_0 = items
|
||||||
|
for _index_0=1,#_items_0 do
|
||||||
|
local y = _items_0[_index_0]
|
||||||
|
table.insert(tmp, x + y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return tmp
|
||||||
|
end)()
|
||||||
|
local _items_0 = hello
|
||||||
|
for _index_0=1,#_items_0 do
|
||||||
|
local z = _items_0[_index_0]
|
||||||
|
print(z)
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user