mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
new assign compile
This commit is contained in:
parent
d8ff74ccba
commit
2e8c33ad23
@ -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,
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user