diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 90e1b4b..ee0e8b1 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -101,12 +101,14 @@ Block_ = (function(_parent_0) declare = function(self, names) local undeclared = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = names for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] if type(name) == "string" and not self:has_name(name) then - table.insert(_accum_0, name) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = name end end end @@ -316,11 +318,13 @@ Block_ = (function(_parent_0) local _with_0 = Line() _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = values for _index_0 = 1, #_item_0 do local v = _item_0[_index_0] - table.insert(_accum_0, self:value(v)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:value(v) end end return _accum_0 diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index 5262968..77ecc1a 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -21,11 +21,13 @@ line_compile = { local _with_0 = self:line("local ") _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = names for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] - table.insert(_accum_0, self:name(name)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:name(name) end end return _accum_0 @@ -83,11 +85,13 @@ line_compile = { end _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = names for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] - table.insert(_accum_0, self:value(name)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:value(name) end end return _accum_0 @@ -96,11 +100,13 @@ line_compile = { _with_0:append(" = ") _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = values for _index_0 = 1, #_item_0 do local v = _item_0[_index_0] - table.insert(_accum_0, self:value(v)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:value(v) end end return _accum_0 @@ -166,11 +172,13 @@ line_compile = { if type(source) == "string" then local values = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = final_names for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] - table.insert(_accum_0, get_value(name)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = get_value(name) end end return _accum_0 @@ -334,11 +342,13 @@ line_compile = { }) block = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = block for _index_0 = 1, #_item_0 do local s = _item_0[_index_0] - table.insert(_accum_0, s) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = s end end return _accum_0 @@ -380,11 +390,13 @@ line_compile = { _with_0:append("for ") _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = names for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] - table.insert(_accum_0, self:name(name)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:name(name) end end return _accum_0 @@ -460,11 +472,13 @@ line_compile = { end constructor.args = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = constructor.args for _index_0 = 1, #_item_0 do local arg = _item_0[_index_0] - table.insert(_accum_0, get_initializers(arg)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = get_initializers(arg) end end return _accum_0 @@ -472,14 +486,16 @@ line_compile = { constructor.arrow = "fat" local dests = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = self_args for _index_0 = 1, #_item_0 do local name = _item_0[_index_0] - table.insert(_accum_0, { + _len_0 = _len_0 + 1 + _accum_0[_len_0] = { "self", name - }) + } end end return _accum_0 @@ -504,11 +520,13 @@ line_compile = { local calling_name = block:get("current_block") local slice = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = chain for _index_0 = 3, #_item_0 do local item = _item_0[_index_0] - table.insert(_accum_0, item) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = item end end return _accum_0 @@ -676,38 +694,39 @@ line_compile = { local _, exp, clauses = unpack(node) if not action then action = function(exp) - return exp + return { + exp + } end end - local statement = action(exp) + local current_stms = action(exp) for _, clause in reversed(clauses) do local t = clause[1] if t == "for" then local names, iter _, names, iter = unpack(clause) - statement = { + current_stms = { "foreach", names, iter, - { - statement - } + current_stms } elseif t == "when" then local cond _, cond = unpack(clause) - statement = { + current_stms = { "if", cond, - { - statement - } + current_stms } else - statement = error("Unknown comprehension clause: " .. t) + current_stms = error("Unknown comprehension clause: " .. t) end + current_stms = { + current_stms + } end - return self:stm(statement) + return self:stms(current_stms) end, with = function(self, node, ret) local _, exp, block = unpack(node) diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index d2238f0..6ab61e9 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -307,21 +307,22 @@ line_compile = _, exp, clauses = unpack node if not action - action = (exp) -> exp + action = (exp) -> {exp} - statement = action exp + current_stms = action exp for _, clause in reversed clauses t = clause[1] - statement = if t == "for" + current_stms = if t == "for" _, names, iter = unpack clause - {"foreach", names, iter, {statement}} + {"foreach", names, iter, current_stms} elseif t == "when" _, cond = unpack clause - {"if", cond, {statement}} + {"if", cond, current_stms} else error "Unknown comprehension clause: "..t + current_stms = {current_stms} - @stm statement + @stms current_stms with: (node, ret) => diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 8db96f2..2303513 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -6,29 +6,29 @@ require("moonscript.compile.format") local ntype = data.ntype local concat, insert = table.concat, table.insert local table_append -table_append = function(name, value) +table_append = function(name, len, value) return { - "assign", { - { - "chain", - name, - { - "index", - { - "exp", - { - "length", - name - }, - "+", - 1 - } - } - } + "update", + len, + "+=", + 1 }, { - value + "assign", + { + { + "chain", + name, + { + "index", + len + } + } + }, + { + value + } } } end @@ -40,6 +40,7 @@ create_accumulate_wrapper = function(block_pos) local accum_name = _with_0:init_free_var("accum", { "table" }) + local count_name = _with_0:init_free_var("len", 0) local value_name = _with_0:free_name("value", true) local inner = node[block_pos] inner[#inner] = { @@ -59,9 +60,7 @@ create_accumulate_wrapper = function(block_pos) "~=", "nil" }, - { - table_append(accum_name, value_name) - } + table_append(accum_name, count_name, value_name) }) _with_0:stm(node) _with_0:stm({ @@ -85,9 +84,11 @@ value_compile = { local _with_0 = self:line() _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 for i, v in ipairs(node) do if i > 1 then - table.insert(_accum_0, _comp(i, v)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _comp(i, v) end end return _accum_0 @@ -105,11 +106,13 @@ value_compile = { local _with_0 = self:line() _with_0:append_list((function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = node for _index_0 = 2, #_item_0 do local v = _item_0[_index_0] - table.insert(_accum_0, self:value(v)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self:value(v) end end return _accum_0 @@ -145,9 +148,10 @@ value_compile = { local tmp_name = _with_0:init_free_var("accum", { "table" }) + local len_name = _with_0:init_free_var("len", 0) local action action = function(value) - return table_append(tmp_name, value) + return table_append(tmp_name, len_name, value) end _with_0:stm(node, action) _with_0:stm({ @@ -229,11 +233,13 @@ value_compile = { end args = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = args for _index_0 = 1, #_item_0 do local arg = _item_0[_index_0] - table.insert(_accum_0, format_names(arg)) + _len_0 = _len_0 + 1 + _accum_0[_len_0] = format_names(arg) end end return _accum_0 diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 606dd8b..136192d 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -12,23 +12,25 @@ import concat, insert from table export value_compile -table_append = (name, value) -> - {"assign", { - {"chain", name, {"index", {"exp", {"length", name}, "+", 1}}} - }, { value }} +table_append = (name, len, value) -> + { + {"update", len, "+=", 1} + {"assign", { + {"chain", name, {"index", len}} }, { value }} + } create_accumulate_wrapper = (block_pos) -> (node) => with @block "(function()", "end)()" accum_name = \init_free_var "accum", {"table"} + count_name = \init_free_var "len", 0 value_name = \free_name "value", true inner = node[block_pos] inner[#inner] = {"assign", {value_name}, {inner[#inner]}} insert inner, { - "if", {"exp", value_name, "~=", "nil"}, { - table_append accum_name, value_name - } + "if", {"exp", value_name, "~=", "nil"}, + table_append accum_name, count_name, value_name } \stm node @@ -73,9 +75,10 @@ value_compile = with @block! tmp_name = \init_free_var "accum", {"table"} + len_name = \init_free_var "len", 0 action = (value) -> - table_append tmp_name, value + table_append tmp_name, len_name, value \stm node, action \stm {"return", tmp_name} diff --git a/tests/outputs/lists.lua b/tests/outputs/lists.lua index 71b2fff..63a3aa3 100644 --- a/tests/outputs/lists.lua +++ b/tests/outputs/lists.lua @@ -1,12 +1,14 @@ local hi = (function() local _accum_0 = { } + local _len_0 = 0 for _, x in ipairs({ 1, 2, 3, 4 }) do - _accum_0[#_accum_0 + 1] = x * 2 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x * 2 end return _accum_0 end)() @@ -20,8 +22,10 @@ local items = { } local mm = (function() local _accum_0 = { } + local _len_0 = 0 for self.x in ipairs(items) do - _accum_0[#_accum_0 + 1] = self.x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = self.x end return _accum_0 end)() @@ -32,6 +36,7 @@ for z in ipairs(items) do end local rad = (function() local _accum_0 = { } + local _len_0 = 0 for a in ipairs({ 1, 2, @@ -41,7 +46,8 @@ local rad = (function() 6 }) do if good_number(a) then - _accum_0[#_accum_0 + 1] = { + _len_0 = _len_0 + 1 + _accum_0[_len_0] = { a } end @@ -72,17 +78,21 @@ range = function(count) end dump((function() local _accum_0 = { } + local _len_0 = 0 for x in range(10) do - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end return _accum_0 end)()) dump((function() local _accum_0 = { } + local _len_0 = 0 for x in range(5) do if x > 2 then for y in range(5) do - _accum_0[#_accum_0 + 1] = { + _len_0 = _len_0 + 1 + _accum_0[_len_0] = { x, y } @@ -93,11 +103,13 @@ dump((function() end)()) local things = (function() local _accum_0 = { } + local _len_0 = 0 for x in range(10) do if x > 5 then for y in range(10) do if y > 7 then - _accum_0[#_accum_0 + 1] = x + y + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x + y end end end @@ -127,8 +139,10 @@ for x in x do end local x = (function() local _accum_0 = { } + local _len_0 = 0 for x in x do - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end return _accum_0 end)() @@ -149,11 +163,13 @@ for x in ipairs({ end local double = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = items for _index_0 = 1, #_item_0 do local x = _item_0[_index_0] - _accum_0[#_accum_0 + 1] = x * 2 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x * 2 end end return _accum_0 @@ -167,12 +183,14 @@ do end local cut = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = items for _index_0 = 1, #_item_0 do local x = _item_0[_index_0] if x > 3 then - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end end end @@ -180,6 +198,7 @@ local cut = (function() end)() local hello = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = items for _index_0 = 1, #_item_0 do @@ -188,7 +207,8 @@ local hello = (function() local _item_1 = items for _index_1 = 1, #_item_1 do local y = _item_1[_index_1] - _accum_0[#_accum_0 + 1] = x + y + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x + y end end end @@ -257,6 +277,7 @@ end x = function(...) return (function(...) local _accum_0 = { } + local _len_0 = 0 do local _item_0 = { ... @@ -264,7 +285,8 @@ x = function(...) for _index_0 = 1, #_item_0 do local x = _item_0[_index_0] if f(...) > 4 then - _accum_0[#_accum_0 + 1] = x * x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x * x end end end @@ -275,8 +297,10 @@ local normal normal = function(hello) return (function() local _accum_0 = { } + local _len_0 = 0 for x in yeah do - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end return _accum_0 end)() @@ -285,10 +309,12 @@ local dont_bubble dont_bubble = function() return (function() local _accum_0 = { } + local _len_0 = 0 for x in (function(...) return print(...) end)("hello") do - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end return _accum_0 end)() diff --git a/tests/outputs/loops.lua b/tests/outputs/loops.lua index d774f42..4375ee4 100644 --- a/tests/outputs/loops.lua +++ b/tests/outputs/loops.lua @@ -46,6 +46,7 @@ local hello = { } x = (function() local _accum_0 = { } + local _len_0 = 0 do local _item_0 = hello for _index_0 = 1, #_item_0 do @@ -55,7 +56,8 @@ x = (function() _value_0 = y end if _value_0 ~= nil then - _accum_0[#_accum_0 + 1] = _value_0 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 end end end @@ -72,10 +74,12 @@ x = function() end local t = (function() local _accum_0 = { } + local _len_0 = 0 for i = 10, 20 do local _value_0 = i * 2 if _value_0 ~= nil then - _accum_0[#_accum_0 + 1] = _value_0 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 end end return _accum_0 @@ -83,11 +87,13 @@ end)() local hmm = 0 local y = (function() local _accum_0 = { } + local _len_0 = 0 for j = 3, 30, 8 do hmm = hmm + 1 local _value_0 = j * hmm if _value_0 ~= nil then - _accum_0[#_accum_0 + 1] = _value_0 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 end end return _accum_0 @@ -101,10 +107,12 @@ end _ = function() return (function() local _accum_0 = { } + local _len_0 = 0 for k = 10, 40 do local _value_0 = "okay" if _value_0 ~= nil then - _accum_0[#_accum_0 + 1] = _value_0 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 end end return _accum_0 @@ -124,11 +132,13 @@ end local i = 0 x = (function() local _accum_0 = { } + local _len_0 = 0 while i < 10 do i = i + 1 local _value_0 = i if _value_0 ~= nil then - _accum_0[#_accum_0 + 1] = _value_0 + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 end end return _accum_0 diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index 4969af9..9d49f37 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -91,8 +91,10 @@ x = -hello(world(one(two))) local hi = -"herfef" x = -(function() local _accum_0 = { } + local _len_0 = 0 for x in x do - _accum_0[#_accum_0 + 1] = x + _len_0 = _len_0 + 1 + _accum_0[_len_0] = x end return _accum_0 end)()