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