From 2e8c33ad2369430141e86ec051ea39af5a8c4991 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 29 May 2011 16:25:52 -0700 Subject: [PATCH] new assign compile --- moonscript/compile.lua | 46 +++++++++++++++------------------------- tests/outputs/assign.lua | 9 ++++---- tests/outputs/import.lua | 4 ++-- tests/outputs/syntax.lua | 12 +++++------ 4 files changed, 29 insertions(+), 42 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 01cb404..bd30bef 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -326,8 +326,8 @@ local compiler_index = { local ln = node[i] local value = self:stm(ln, return_value and i == len) if type(value) == "table" then - for _, v in value do - table.insert(lines, value) + for _, v in ipairs(value) do + table.insert(lines, v) end else table.insert(lines, value) @@ -386,40 +386,28 @@ local compiler_index = { local _, names, values = unpack(node) local assigns, current = {}, nil - local function append(t, name, value) - if not current or t ~= current[1] then - current = {t, {name}, {value}} - table.insert(assigns, current) - else - table.insert(current[2], name) - table.insert(current[3], value) - end - end - - for i, assignee in ipairs(names) do - local name_value = self:value(assignee) - local value = self:value(values[i]) - - if ntype(assignee) == "chain" or self:has_name(assignee) then - append("non-local", name_value, value) - else - append("local", name_value, value) - end - - if type(assignee) == "string" then - self:put_name(assignee) + -- declare undeclared names + local undeclared_names = {} + for _, name in ipairs(names) do + if type(name) == "string" and not self:has_name(name) then + table.insert(undeclared_names, name) + self:put_name(name) end end local lines = {} - for _, group in ipairs(assigns) do - local t, names, values = unpack(group) - if #values == 0 then values = {"nil"} end - local line = table.concat(names, ", ").." = "..table.concat(values, ", ") - table.insert(lines, t == "local" and "local "..line or line) + local num_undeclared = #undeclared_names + if num_undeclared > 0 and num_undeclared ~= #names then + table.insert(lines, "local "..table.concat(undeclared_names, ", ")) end + local ln = self:name_list(names).. " = "..table.concat(self:values(values), ", ") + if num_undeclared == #names then + ln = "local "..ln + end + + table.insert(lines, ln) return self:pretty(lines) end, diff --git a/tests/outputs/assign.lua b/tests/outputs/assign.lua index 4b5645b..701fe96 100644 --- a/tests/outputs/assign.lua +++ b/tests/outputs/assign.lua @@ -3,11 +3,10 @@ local _ = function() return function(hi) local d = 100 hi = 1021 - local a, b, c = 1, 2, 3 - d = 4 - hello[232], (5 + 5)[121] = 100, 200 - local hello = 300 - x[99] = nil + local a, b, c + a, b, c, d = 1, 2, 3, 4 + local hello + hello[232], (5 + 5)[121], hello, x[99] = 100, 200, 300 joop = 12 end end diff --git a/tests/outputs/import.lua b/tests/outputs/import.lua index 06633d8..4bc015a 100644 --- a/tests/outputs/import.lua +++ b/tests/outputs/import.lua @@ -12,8 +12,8 @@ do master = _table_0.master ghost = moon.bind(_table_0.ghost, _table_0) end -a = 3434 -local yumm = "hello" +local yumm +a, yumm = 3434, "hello" local _table_0 = 232 local something do diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index 155e045..16979a3 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -12,11 +12,11 @@ end) local h = function() return hi end eat(function() end, world) local a = 1 + 2 * 3 / 6 -a = another -local bunch, go, here = world +local bunch, go, here +a, bunch, go, here = another, world func(arg1, arg2, another, arg3) -here = function() end -local we = yeah +local we +here, we = function() end, yeah local the, different = function() return approach end, yeah dad() dad(lord) @@ -29,8 +29,8 @@ hello(world(what(are(you(doing(here)))))) what(the)[3243](world, yeck(heck)) hairy[hands][are](gross)(okay(okay[world])); (get[something] + 5)[years] -local i = 200 -x = 300 +local i +i, x = 200, 300 local yeah = (1 + 5) * 3 yeah = ((1 + 5) * 3) / 2 yeah = ((1 + 5) * 3) / 2 + i % 100