reuse locals for unpack loops

This commit is contained in:
leaf corcoran 2013-06-10 19:18:49 -07:00
parent 1311671cdc
commit 4978054a8f
6 changed files with 73 additions and 65 deletions

View File

@ -754,7 +754,7 @@ local Statement = Transformer({
if ntype(source) == "unpack" then
local list = source[2]
local index_name = NameProxy("index")
local list_name = NameProxy("list")
local list_name = self:is_local(list) and list or NameProxy("list")
local slice_var = nil
local bounds
if is_slice(list) then
@ -796,8 +796,8 @@ local Statement = Transformer({
}
end
return build.group({
build.assign_one(list_name, list),
slice_var,
list_name ~= list and build.assign_one(list_name, list) or NOOP,
slice_var or NOOP,
build["for"]({
name = index_name,
bounds = bounds,
@ -806,7 +806,7 @@ local Statement = Transformer({
"assign",
node.names,
{
list_name:index(index_name)
NameProxy.index(list_name, index_name)
}
},
build.group(node.body)

View File

@ -396,7 +396,7 @@ Statement = Transformer {
list = source[2]
index_name = NameProxy "index"
list_name = NameProxy "list"
list_name = @is_local(list) and list or NameProxy "list"
slice_var = nil
bounds = if is_slice list
@ -418,13 +418,13 @@ Statement = Transformer {
{1, {"length", list_name}}
return build.group {
build.assign_one list_name, list
slice_var
list_name != list and build.assign_one(list_name, list) or NOOP
slice_var or NOOP
build["for"] {
name: index_name
bounds: bounds
body: {
{"assign", node.names, {list_name\index index_name}}
{"assign", node.names, { NameProxy.index list_name, index_name }}
build.group node.body
}
}

View File

@ -114,3 +114,11 @@ for x=1,10
for y = 2,12
continue if y % 3 == 0
--
do
xxx = {1,2,3,4}
for thing in *xxx
print thing

View File

@ -101,9 +101,8 @@ do
4
}
}
local _list_0 = thing
for _index_0 = 1, #_list_0 do
local _des_0 = _list_0[_index_0]
for _index_0 = 1, #thing do
local _des_0 = thing[_index_0]
local x, y
x, y = _des_0[1], _des_0[2]
print(x, y)

View File

@ -164,25 +164,22 @@ end
local double = (function()
local _accum_0 = { }
local _len_0 = 1
local _list_0 = items
for _index_0 = 1, #_list_0 do
x = _list_0[_index_0]
for _index_0 = 1, #items do
x = items[_index_0]
_accum_0[_len_0] = x * 2
_len_0 = _len_0 + 1
end
return _accum_0
end)()
local _list_0 = double
for _index_0 = 1, #_list_0 do
x = _list_0[_index_0]
for _index_0 = 1, #double do
x = double[_index_0]
print(x)
end
local cut = (function()
local _accum_0 = { }
local _len_0 = 1
local _list_1 = items
for _index_0 = 1, #_list_1 do
x = _list_1[_index_0]
for _index_0 = 1, #items do
x = items[_index_0]
if x > 3 then
_accum_0[_len_0] = x
_len_0 = _len_0 + 1
@ -193,21 +190,18 @@ end)()
local hello = (function()
local _accum_0 = { }
local _len_0 = 1
local _list_1 = items
for _index_0 = 1, #_list_1 do
x = _list_1[_index_0]
local _list_2 = items
for _index_1 = 1, #_list_2 do
local y = _list_2[_index_1]
for _index_0 = 1, #items do
x = items[_index_0]
for _index_1 = 1, #items do
local y = items[_index_1]
_accum_0[_len_0] = x + y
_len_0 = _len_0 + 1
end
end
return _accum_0
end)()
local _list_1 = hello
for _index_0 = 1, #_list_1 do
local z = _list_1[_index_0]
for _index_0 = 1, #hello do
local z = hello[_index_0]
print(z)
end
x = {
@ -219,38 +213,38 @@ x = {
6,
7
}
local _list_2 = x
local _list_0 = x
local _max_0 = -5
for _index_0 = 2, _max_0 < 0 and #_list_2 + _max_0 or _max_0, 2 do
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0, 2 do
local y = _list_0[_index_0]
print(y)
end
local _list_1 = x
local _max_1 = 3
for _index_0 = 1, _max_1 < 0 and #_list_1 + _max_1 or _max_1 do
local y = _list_1[_index_0]
print(y)
end
local _list_2 = x
for _index_0 = 2, #_list_2 do
local y = _list_2[_index_0]
print(y)
end
local _list_3 = x
local _max_1 = 3
for _index_0 = 1, _max_1 < 0 and #_list_3 + _max_1 or _max_1 do
for _index_0 = 1, #_list_3, 2 do
local y = _list_3[_index_0]
print(y)
end
local _list_4 = x
for _index_0 = 2, #_list_4 do
for _index_0 = 2, #_list_4, 2 do
local y = _list_4[_index_0]
print(y)
end
local _list_5 = x
for _index_0 = 1, #_list_5, 2 do
local y = _list_5[_index_0]
print(y)
end
local _list_6 = x
for _index_0 = 2, #_list_6, 2 do
local y = _list_6[_index_0]
print(y)
end
local a, b, c = 1, 5, 2
local _list_7 = x
local _list_5 = x
local _max_2 = b
for _index_0 = a, _max_2 < 0 and #_list_7 + _max_2 or _max_2, c do
local y = _list_7[_index_0]
for _index_0 = a, _max_2 < 0 and #_list_5 + _max_2 or _max_2, c do
local y = _list_5[_index_0]
print(y)
end
local normal
@ -266,23 +260,21 @@ normal = function(hello)
end)()
end
local test = x(1, 2, 3, 4, 5)
local _list_8 = test
for _index_0 = 1, #_list_8 do
local thing = _list_8[_index_0]
for _index_0 = 1, #test do
local thing = test[_index_0]
print(thing)
end
local _
_ = function()
local _list_9 = rows
for _index_0 = 1, #_list_9 do
local row = _list_9[_index_0]
local _list_6 = rows
for _index_0 = 1, #_list_6 do
local row = _list_6[_index_0]
a = b
end
end
_ = function()
local _list_9 = things
for _index_0 = 1, #_list_9 do
x = _list_9[_index_0]
for _index_0 = 1, #things do
x = things[_index_0]
_ = x
end
end
@ -290,9 +282,8 @@ return function()
return (function()
local _accum_0 = { }
local _len_0 = 1
local _list_9 = things
for _index_0 = 1, #_list_9 do
x = _list_9[_index_0]
for _index_0 = 1, #things do
x = things[_index_0]
_accum_0[_len_0] = x
_len_0 = _len_0 + 1
end

View File

@ -51,9 +51,8 @@ local hello = {
x = (function()
local _accum_0 = { }
local _len_0 = 1
local _list_1 = hello
for _index_0 = 1, #_list_1 do
local y = _list_1[_index_0]
for _index_0 = 1, #hello do
local y = hello[_index_0]
if y % 2 == 0 then
_accum_0[_len_0] = y
end
@ -62,9 +61,8 @@ x = (function()
return _accum_0
end)()
x = function()
local _list_1 = hello
for _index_0 = 1, #_list_1 do
x = _list_1[_index_0]
for _index_0 = 1, #hello do
x = hello[_index_0]
local _ = y
end
end
@ -258,4 +256,16 @@ for x = 1, 10 do
if not _continue_0 then
break
end
end
do
local xxx = {
1,
2,
3,
4
}
for _index_0 = 1, #xxx do
local thing = xxx[_index_0]
print(thing)
end
end