mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
moonscript/: remove unneeded variables on stack (#271)
This commit is contained in:
parent
32d650a5ff
commit
28849bc878
@ -57,7 +57,7 @@ return {
|
||||
end
|
||||
end,
|
||||
assign = function(self, node)
|
||||
local _, names, values = unpack(node)
|
||||
local names, values = unpack(node, 2)
|
||||
local undeclared = self:declare(names)
|
||||
local declare = "local " .. concat(undeclared, ", ")
|
||||
local has_fndef = false
|
||||
@ -150,7 +150,7 @@ return {
|
||||
end
|
||||
end,
|
||||
["while"] = function(self, node)
|
||||
local _, cond, block = unpack(node)
|
||||
local cond, block = unpack(node, 2)
|
||||
do
|
||||
local _with_0 = self:block(self:line("while ", self:value(cond), " do"))
|
||||
_with_0:stms(block)
|
||||
@ -158,7 +158,7 @@ return {
|
||||
end
|
||||
end,
|
||||
["for"] = function(self, node)
|
||||
local _, name, bounds, block = unpack(node)
|
||||
local name, bounds, block = unpack(node, 2)
|
||||
local loop = self:line("for ", self:name(name), " = ", self:value({
|
||||
"explist",
|
||||
unpack(bounds)
|
||||
@ -173,7 +173,7 @@ return {
|
||||
end
|
||||
end,
|
||||
foreach = function(self, node)
|
||||
local _, names, exps, block = unpack(node)
|
||||
local names, exps, block = unpack(node, 2)
|
||||
local loop
|
||||
do
|
||||
local _with_0 = self:line()
|
||||
@ -210,7 +210,7 @@ return {
|
||||
end
|
||||
end,
|
||||
export = function(self, node)
|
||||
local _, names = unpack(node)
|
||||
local names = unpack(node, 2)
|
||||
if type(names) == "string" then
|
||||
if names == "*" then
|
||||
self.export_all = true
|
||||
|
@ -26,7 +26,7 @@ import unpack from require "moonscript.util"
|
||||
\append_list [@name name for name in *names], ", "
|
||||
|
||||
assign: (node) =>
|
||||
_, names, values = unpack node
|
||||
names, values = unpack node, 2
|
||||
|
||||
undeclared = @declare names
|
||||
declare = "local " .. concat(undeclared, ", ")
|
||||
@ -83,12 +83,12 @@ import unpack from require "moonscript.util"
|
||||
\stms block
|
||||
|
||||
while: (node) =>
|
||||
_, cond, block = unpack node
|
||||
cond, block = unpack node, 2
|
||||
with @block @line "while ", @value(cond), " do"
|
||||
\stms block
|
||||
|
||||
for: (node) =>
|
||||
_, name, bounds, block = unpack node
|
||||
name, bounds, block = unpack node, 2
|
||||
loop = @line "for ", @name(name), " = ", @value({"explist", unpack bounds}), " do"
|
||||
with @block loop
|
||||
\declare {name}
|
||||
@ -97,7 +97,7 @@ import unpack from require "moonscript.util"
|
||||
-- for x in y ...
|
||||
-- {"foreach", {names...}, {exp...}, body}
|
||||
foreach: (node) =>
|
||||
_, names, exps, block = unpack node
|
||||
names, exps, block = unpack node, 2
|
||||
|
||||
loop = with @line!
|
||||
\append "for "
|
||||
@ -112,7 +112,7 @@ import unpack from require "moonscript.util"
|
||||
\stms block
|
||||
|
||||
export: (node) =>
|
||||
_, names = unpack node
|
||||
names = unpack node, 2
|
||||
if type(names) == "string"
|
||||
if names == "*"
|
||||
@export_all = true
|
||||
|
@ -71,7 +71,7 @@ return {
|
||||
return self:line("(", self:value(node[2]), ")")
|
||||
end,
|
||||
string = function(self, node)
|
||||
local _, delim, inner = unpack(node)
|
||||
local delim, inner = unpack(node, 2)
|
||||
local end_delim = delim:gsub("%[", "]")
|
||||
if delim == "'" or delim == '"' then
|
||||
inner = inner:gsub("[\r\n]", string_chars)
|
||||
@ -133,7 +133,7 @@ return {
|
||||
return self:line(callee_value, actions)
|
||||
end,
|
||||
fndef = function(self, node)
|
||||
local _, args, whitelist, arrow, block = unpack(node)
|
||||
local args, whitelist, arrow, block = unpack(node, 2)
|
||||
local default_args = { }
|
||||
local self_args = { }
|
||||
local arg_names
|
||||
@ -238,7 +238,7 @@ return {
|
||||
end
|
||||
end,
|
||||
table = function(self, node)
|
||||
local _, items = unpack(node)
|
||||
local items = unpack(node, 2)
|
||||
do
|
||||
local _with_0 = self:block("{", "}")
|
||||
local format_line
|
||||
|
@ -40,7 +40,7 @@ string_chars = {
|
||||
@line "(", @value(node[2]), ")"
|
||||
|
||||
string: (node) =>
|
||||
_, delim, inner = unpack node
|
||||
delim, inner = unpack node, 2
|
||||
end_delim = delim\gsub "%[", "]"
|
||||
if delim == "'" or delim == '"'
|
||||
inner = inner\gsub "[\r\n]", string_chars
|
||||
@ -91,7 +91,7 @@ string_chars = {
|
||||
@line callee_value, actions
|
||||
|
||||
fndef: (node) =>
|
||||
_, args, whitelist, arrow, block = unpack node
|
||||
args, whitelist, arrow, block = unpack node, 2
|
||||
|
||||
default_args = {}
|
||||
self_args = {}
|
||||
@ -137,7 +137,7 @@ string_chars = {
|
||||
.header = "function("..concat(arg_names, ", ")..")"
|
||||
|
||||
table: (node) =>
|
||||
_, items = unpack node
|
||||
items = unpack node, 2
|
||||
with @block "{", "}"
|
||||
format_line = (tuple) ->
|
||||
if #tuple == 2
|
||||
|
@ -106,7 +106,7 @@ super_scope = function(value, t, key)
|
||||
}
|
||||
end
|
||||
return function(self, node, ret, parent_assign)
|
||||
local _, name, parent_val, body = unpack(node)
|
||||
local name, parent_val, body = unpack(node, 2)
|
||||
if parent_val == "" then
|
||||
parent_val = nil
|
||||
end
|
||||
|
@ -76,7 +76,7 @@ super_scope = (value, t, key) ->
|
||||
}
|
||||
|
||||
(node, ret, parent_assign) =>
|
||||
_, name, parent_val, body = unpack node
|
||||
name, parent_val, body = unpack node, 2
|
||||
parent_val = nil if parent_val == ""
|
||||
|
||||
parent_cls_name = NameProxy "parent"
|
||||
|
@ -342,7 +342,7 @@ return Transformer({
|
||||
end
|
||||
end,
|
||||
update = function(self, node)
|
||||
local _, name, op, exp = unpack(node)
|
||||
local name, op, exp = unpack(node, 2)
|
||||
local op_final = op:match("^(.+)=$")
|
||||
if not op_final then
|
||||
error("Unknown op: " .. op)
|
||||
@ -361,7 +361,7 @@ return Transformer({
|
||||
})
|
||||
end,
|
||||
import = function(self, node)
|
||||
local _, names, source = unpack(node)
|
||||
local names, source = unpack(node, 2)
|
||||
local table_values
|
||||
do
|
||||
local _accum_0 = { }
|
||||
@ -402,7 +402,7 @@ return Transformer({
|
||||
}
|
||||
end,
|
||||
comprehension = function(self, node, action)
|
||||
local _, exp, clauses = unpack(node)
|
||||
local exp, clauses = unpack(node, 2)
|
||||
action = action or function(exp)
|
||||
return {
|
||||
exp
|
||||
@ -492,7 +492,7 @@ return Transformer({
|
||||
end,
|
||||
["if"] = function(self, node, ret)
|
||||
if ntype(node[2]) == "assign" then
|
||||
local _, assign, body = unpack(node)
|
||||
local assign, body = unpack(node, 2)
|
||||
if destructure.has_destructure(assign[2]) then
|
||||
local name = NameProxy("des")
|
||||
body = {
|
||||
@ -694,7 +694,7 @@ return Transformer({
|
||||
node.body = with_continue_listener(node.body)
|
||||
end,
|
||||
switch = function(self, node, ret)
|
||||
local _, exp, conds = unpack(node)
|
||||
local exp, conds = unpack(node, 2)
|
||||
local exp_name = NameProxy("exp")
|
||||
local convert_cond
|
||||
convert_cond = function(cond)
|
||||
|
@ -222,14 +222,14 @@ Transformer {
|
||||
nil
|
||||
|
||||
update: (node) =>
|
||||
_, name, op, exp = unpack node
|
||||
name, op, exp = unpack node, 2
|
||||
op_final = op\match "^(.+)=$"
|
||||
error "Unknown op: "..op if not op_final
|
||||
exp = {"parens", exp} unless value_is_singular exp
|
||||
build.assign_one name, {"exp", name, op_final, exp}
|
||||
|
||||
import: (node) =>
|
||||
_, names, source = unpack node
|
||||
names, source = unpack node, 2
|
||||
table_values = for name in *names
|
||||
dest_name = if ntype(name) == "colon"
|
||||
name[2]
|
||||
@ -242,7 +242,7 @@ Transformer {
|
||||
{ "assign", {dest}, {source}, [-1]: node[-1] }
|
||||
|
||||
comprehension: (node, action) =>
|
||||
_, exp, clauses = unpack node
|
||||
exp, clauses = unpack node, 2
|
||||
|
||||
action = action or (exp) -> {exp}
|
||||
construct_comprehension action(exp), clauses
|
||||
@ -280,7 +280,7 @@ Transformer {
|
||||
if: (node, ret) =>
|
||||
-- expand assign in cond
|
||||
if ntype(node[2]) == "assign"
|
||||
_, assign, body = unpack node
|
||||
assign, body = unpack node, 2
|
||||
if destructure.has_destructure assign[2]
|
||||
name = NameProxy "des"
|
||||
|
||||
@ -426,7 +426,7 @@ Transformer {
|
||||
node.body = with_continue_listener node.body
|
||||
|
||||
switch: (node, ret) =>
|
||||
_, exp, conds = unpack node
|
||||
exp, conds = unpack node, 2
|
||||
exp_name = NameProxy "exp"
|
||||
|
||||
-- convert switch conds into if statment conds
|
||||
|
@ -89,7 +89,7 @@ return Transformer({
|
||||
return a:wrap(node)
|
||||
end,
|
||||
tblcomprehension = function(self, node)
|
||||
local _, explist, clauses = unpack(node)
|
||||
local explist, clauses = unpack(node, 2)
|
||||
local key_exp, value_exp = unpack(explist)
|
||||
local accum = NameProxy("tbl")
|
||||
local inner
|
||||
@ -234,7 +234,7 @@ return Transformer({
|
||||
end
|
||||
end,
|
||||
block_exp = function(self, node)
|
||||
local _, body = unpack(node)
|
||||
local body = unpack(node, 2)
|
||||
local fn = nil
|
||||
local arg_list = { }
|
||||
fn = smart_node(build.fndef({
|
||||
|
@ -56,7 +56,7 @@ Transformer {
|
||||
a\wrap node
|
||||
|
||||
tblcomprehension: (node) =>
|
||||
_, explist, clauses = unpack node
|
||||
explist, clauses = unpack node, 2
|
||||
key_exp, value_exp = unpack explist
|
||||
|
||||
accum = NameProxy "tbl"
|
||||
@ -144,7 +144,7 @@ Transformer {
|
||||
}
|
||||
|
||||
block_exp: (node) =>
|
||||
_, body = unpack node
|
||||
body = unpack node, 2
|
||||
|
||||
fn = nil
|
||||
arg_list = {}
|
||||
|
Loading…
Reference in New Issue
Block a user