don't use table.insert to append to table

This commit is contained in:
leaf corcoran 2011-08-13 11:20:10 -07:00
parent d64142c85c
commit a76c251899
5 changed files with 57 additions and 45 deletions

View File

@ -5,6 +5,33 @@ local dump = require("moonscript.dump")
require("moonscript.compile.format")
local ntype = data.ntype
local concat, insert = table.concat, table.insert
local table_append
table_append = function(name, value)
return {
"assign",
{
{
"chain",
name,
{
"index",
{
"exp",
{
"length",
name
},
"+",
1
}
}
}
},
{
value
}
}
end
local create_accumulate_wrapper
create_accumulate_wrapper = function(block_pos)
return function(self, node)
@ -33,17 +60,7 @@ create_accumulate_wrapper = function(block_pos)
"nil"
},
{
{
"chain",
"table.insert",
{
"call",
{
accum_name,
value_name
}
}
}
table_append(accum_name, value_name)
}
})
_with_0:stm(node)
@ -130,17 +147,7 @@ value_compile = {
})
local action
action = function(value)
return {
"chain",
"table.insert",
{
"call",
{
tmp_name,
value
}
}
}
return table_append(tmp_name, value)
end
_with_0:stm(node, action)
_with_0:stm({

View File

@ -12,6 +12,11 @@ import concat, insert from table
export value_compile
table_append = (name, value) ->
{"assign", {
{"chain", name, {"index", {"exp", {"length", name}, "+", 1}}}
}, { value }}
create_accumulate_wrapper = (block_pos) ->
(node) =>
with @block "(function()", "end)()"
@ -22,7 +27,7 @@ create_accumulate_wrapper = (block_pos) ->
inner[#inner] = {"assign", {value_name}, {inner[#inner]}}
insert inner, {
"if", {"exp", value_name, "~=", "nil"}, {
{"chain", "table.insert", {"call", {accum_name, value_name}}}
table_append accum_name, value_name
}
}
@ -70,7 +75,7 @@ value_compile =
tmp_name = \init_free_var "accum", {"table"}
action = (value) ->
{"chain", "table.insert", {"call", {tmp_name, value}}}
table_append tmp_name, value
\stm node, action
\stm {"return", tmp_name}

View File

@ -6,7 +6,7 @@ local hi = (function()
3,
4
}) do
table.insert(_accum_0, x * 2)
_accum_0[#_accum_0 + 1] = x * 2
end
return _accum_0
end)()
@ -21,7 +21,7 @@ local items = {
local mm = (function()
local _accum_0 = { }
for self.x in ipairs(items) do
table.insert(_accum_0, self.x)
_accum_0[#_accum_0 + 1] = self.x
end
return _accum_0
end)()
@ -41,9 +41,9 @@ local rad = (function()
6
}) do
if good_number(a) then
table.insert(_accum_0, {
_accum_0[#_accum_0 + 1] = {
a
})
}
end
end
return _accum_0
@ -73,7 +73,7 @@ end
dump((function()
local _accum_0 = { }
for x in range(10) do
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
return _accum_0
end)())
@ -82,10 +82,10 @@ dump((function()
for x in range(5) do
if x > 2 then
for y in range(5) do
table.insert(_accum_0, {
_accum_0[#_accum_0 + 1] = {
x,
y
})
}
end
end
end
@ -97,7 +97,7 @@ local things = (function()
if x > 5 then
for y in range(10) do
if y > 7 then
table.insert(_accum_0, x + y)
_accum_0[#_accum_0 + 1] = x + y
end
end
end
@ -128,7 +128,7 @@ end
local x = (function()
local _accum_0 = { }
for x in x do
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
return _accum_0
end)()
@ -153,7 +153,7 @@ local double = (function()
local _item_0 = items
for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0]
table.insert(_accum_0, x * 2)
_accum_0[#_accum_0 + 1] = x * 2
end
end
return _accum_0
@ -172,7 +172,7 @@ local cut = (function()
for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0]
if x > 3 then
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
end
end
@ -188,7 +188,7 @@ local hello = (function()
local _item_1 = items
for _index_1 = 1, #_item_1 do
local y = _item_1[_index_1]
table.insert(_accum_0, x + y)
_accum_0[#_accum_0 + 1] = x + y
end
end
end
@ -264,7 +264,7 @@ x = function(...)
for _index_0 = 1, #_item_0 do
local x = _item_0[_index_0]
if f(...) > 4 then
table.insert(_accum_0, x * x)
_accum_0[#_accum_0 + 1] = x * x
end
end
end
@ -276,7 +276,7 @@ normal = function(hello)
return (function()
local _accum_0 = { }
for x in yeah do
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
return _accum_0
end)()
@ -288,7 +288,7 @@ dont_bubble = function()
for x in (function(...)
return print(...)
end)("hello") do
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
return _accum_0
end)()

View File

@ -55,7 +55,7 @@ x = (function()
_value_0 = y
end
if _value_0 ~= nil then
table.insert(_accum_0, _value_0)
_accum_0[#_accum_0 + 1] = _value_0
end
end
end
@ -75,7 +75,7 @@ local t = (function()
for i = 10, 20 do
local _value_0 = i * 2
if _value_0 ~= nil then
table.insert(_accum_0, _value_0)
_accum_0[#_accum_0 + 1] = _value_0
end
end
return _accum_0
@ -87,7 +87,7 @@ local y = (function()
hmm = hmm + 1
local _value_0 = j * hmm
if _value_0 ~= nil then
table.insert(_accum_0, _value_0)
_accum_0[#_accum_0 + 1] = _value_0
end
end
return _accum_0
@ -104,7 +104,7 @@ _ = function()
for k = 10, 40 do
local _value_0 = "okay"
if _value_0 ~= nil then
table.insert(_accum_0, _value_0)
_accum_0[#_accum_0 + 1] = _value_0
end
end
return _accum_0
@ -128,7 +128,7 @@ x = (function()
i = i + 1
local _value_0 = i
if _value_0 ~= nil then
table.insert(_accum_0, _value_0)
_accum_0[#_accum_0 + 1] = _value_0
end
end
return _accum_0

View File

@ -92,7 +92,7 @@ local hi = -"herfef"
x = -(function()
local _accum_0 = { }
for x in x do
table.insert(_accum_0, x)
_accum_0[#_accum_0 + 1] = x
end
return _accum_0
end)()