syntax for table array iteration

This commit is contained in:
leaf corcoran 2011-06-01 20:58:17 -07:00
parent 7a85335e0a
commit 9c0fced067
4 changed files with 78 additions and 9 deletions

View File

@ -281,16 +281,33 @@ local compiler_index = {
and { ("table.insert(tmp, %s)"):format(self:value(exp)) }
or { self:stm(exp) }
self:push()
for i = #clauses,1,-1 do
local c = clauses[i]
if "for" == c[1] then
local _, names, iter = unpack(c)
action = {
("for %s in %s do"):format(self:name_list(names), self:value(iter)),
action,
"end"
}
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 = {
("for %s in %s do"):format(self:name_list(names), self:value(iter)),
action,
"end"
}
end
elseif "when" == c[1] then
local _, when = unpack(c)
action = {
@ -302,6 +319,7 @@ local compiler_index = {
error("Unknown clause type :"..tostring(c[1]))
end
end
self:pop()
if return_value then
return self:pretty{

View File

@ -243,12 +243,10 @@ local build_grammar = wrap(function()
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
Comprehension = sym"[" * Exp *
Ct((key"for" * Ct(NameList) * key"in" * Exp / mark"for") * CompClause^0) *
sym"]" / mark"comprehension",
Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension",
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",
Assign = Ct(AssignableList) * sym"=" * (Ct(TableBlock + ExpListLow) + If) / mark"assign",

View File

@ -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
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

View File

@ -121,4 +121,47 @@ for x in ipairs({ 1, 2, 4 }) do
print(x, y)
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