mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
extract declarations in transform instead of delayed line
This commit is contained in:
parent
e075c2f971
commit
f56095256b
@ -582,11 +582,18 @@ do
|
||||
if ret then
|
||||
error("deprecated stms call, use transformer")
|
||||
end
|
||||
local _list_0 = stms
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local stm = _list_0[_index_0]
|
||||
self:stm(stm)
|
||||
local current_stms, current_stm_i
|
||||
do
|
||||
local _obj_0 = self
|
||||
current_stms, current_stm_i = _obj_0.current_stms, _obj_0.current_stm_i
|
||||
end
|
||||
self.current_stms = stms
|
||||
for i = 1, #stms do
|
||||
self.current_stm_i = i
|
||||
self:stm(stms[i])
|
||||
end
|
||||
self.current_stms = current_stms
|
||||
self.current_stm_i = current_stm_i
|
||||
return nil
|
||||
end,
|
||||
splice = function(self, fn)
|
||||
@ -663,11 +670,7 @@ do
|
||||
if not (self.options.implicitly_return_root == false) then
|
||||
stms = transform.Statement.transformers.root_stms(self, stms)
|
||||
end
|
||||
local _list_0 = stms
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local s = _list_0[_index_0]
|
||||
self:stm(s)
|
||||
end
|
||||
return self:stms(stms)
|
||||
end,
|
||||
render = function(self)
|
||||
local buffer = self._lines:flatten()
|
||||
|
@ -356,7 +356,16 @@ class Block
|
||||
|
||||
stms: (stms, ret) =>
|
||||
error "deprecated stms call, use transformer" if ret
|
||||
@stm stm for stm in *stms
|
||||
{:current_stms, :current_stm_i} = @
|
||||
|
||||
@current_stms = stms
|
||||
for i=1,#stms
|
||||
@current_stm_i = i
|
||||
@stm stms[i]
|
||||
|
||||
@current_stms = current_stms
|
||||
@current_stm_i = current_stm_i
|
||||
|
||||
nil
|
||||
|
||||
splice: (fn) =>
|
||||
@ -374,7 +383,7 @@ class RootBlock extends Block
|
||||
root_stms: (stms) =>
|
||||
unless @options.implicitly_return_root == false
|
||||
stms = transform.Statement.transformers.root_stms self, stms
|
||||
@stm s for s in *stms
|
||||
@stms stms
|
||||
|
||||
render: =>
|
||||
-- print @_lines
|
||||
|
@ -39,40 +39,6 @@ local statement_compilers = {
|
||||
end
|
||||
end
|
||||
end,
|
||||
declare_glob = function(self, node)
|
||||
local kind = node[2]
|
||||
local names = { }
|
||||
local fn
|
||||
local _exp_0 = node[2]
|
||||
if "*" == _exp_0 then
|
||||
fn = function(name)
|
||||
if type(name) == "string" then
|
||||
insert(names, name)
|
||||
return true
|
||||
end
|
||||
end
|
||||
elseif "^" == _exp_0 then
|
||||
fn = function(name)
|
||||
if type(name) == "string" and name:match("^%u") then
|
||||
insert(names, name)
|
||||
return true
|
||||
end
|
||||
end
|
||||
else
|
||||
fn = error("unknown glob")
|
||||
end
|
||||
self:set("name_glob", fn)
|
||||
return data.DelayedLine(function(buff)
|
||||
insert(buff, "local ")
|
||||
local _list_0 = names
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local name = _list_0[_index_0]
|
||||
insert(buff, self:name(name))
|
||||
insert(buff, ", ")
|
||||
end
|
||||
buff[#buff] = nil
|
||||
end)
|
||||
end,
|
||||
declare_with_shadows = function(self, node)
|
||||
local names = node[2]
|
||||
self:declare(names)
|
||||
|
@ -20,33 +20,6 @@ statement_compilers =
|
||||
with @line "local "
|
||||
\append_list [@name name for name in *undeclared], ", "
|
||||
|
||||
declare_glob: (node) =>
|
||||
kind = node[2]
|
||||
|
||||
names = {}
|
||||
fn = switch node[2]
|
||||
when "*"
|
||||
(name) ->
|
||||
if type(name) == "string"
|
||||
insert names, name
|
||||
true
|
||||
when "^"
|
||||
(name) ->
|
||||
if type(name) == "string" and name\match "^%u"
|
||||
insert names, name
|
||||
true
|
||||
else
|
||||
error "unknown glob"
|
||||
|
||||
@set "name_glob", fn
|
||||
|
||||
data.DelayedLine (buff) ->
|
||||
insert buff, "local "
|
||||
for name in *names
|
||||
insert buff, @name name
|
||||
insert buff, ", "
|
||||
buff[#buff] = nil -- strips local if no names
|
||||
|
||||
-- this overrides the existing names with new locals, used for local keyword
|
||||
declare_with_shadows: (node) =>
|
||||
names = node[2]
|
||||
|
@ -127,6 +127,35 @@ hoist_declarations = function(body)
|
||||
assigns
|
||||
})
|
||||
end
|
||||
local extract_declarations
|
||||
extract_declarations = function(self, body, start, out)
|
||||
if body == nil then
|
||||
body = self.current_stms
|
||||
end
|
||||
if start == nil then
|
||||
start = self.current_stm_i + 1
|
||||
end
|
||||
if out == nil then
|
||||
out = { }
|
||||
end
|
||||
for i = start, #body do
|
||||
local stm = self.transform.statement(body[i])
|
||||
body[i] = stm
|
||||
local _exp_0 = stm[1]
|
||||
if "assign" == _exp_0 or "declare" == _exp_0 then
|
||||
local _list_0 = stm[2]
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local name = _list_0[_index_0]
|
||||
if type(name) == "string" then
|
||||
insert(out, name)
|
||||
end
|
||||
end
|
||||
elseif "group" == _exp_0 then
|
||||
extract_declarations(self, stm[2], 1, out)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
local expand_elseif_assign
|
||||
expand_elseif_assign = function(ifstm)
|
||||
for i = 4, #ifstm do
|
||||
@ -342,20 +371,40 @@ local Statement = Transformer({
|
||||
root_stms = function(self, body)
|
||||
return apply_to_last(body, implicitly_return(self))
|
||||
end,
|
||||
assign = function(self, node)
|
||||
local names, values = unpack(node, 2)
|
||||
do
|
||||
local globber = self:get_current("name_glob")
|
||||
if globber then
|
||||
declare_glob = function(self, node)
|
||||
local names = extract_declarations(self)
|
||||
if node[2] == "^" then
|
||||
names = (function()
|
||||
local _accum_0 = { }
|
||||
local _len_0 = 1
|
||||
local _list_0 = names
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local name = _list_0[_index_0]
|
||||
if globber(name) then
|
||||
self:put_name(name)
|
||||
local _continue_0 = false
|
||||
repeat
|
||||
local name = _list_0[_index_0]
|
||||
if not (name:match("^%u")) then
|
||||
_continue_0 = true
|
||||
break
|
||||
end
|
||||
local _value_0 = name
|
||||
_accum_0[_len_0] = _value_0
|
||||
_len_0 = _len_0 + 1
|
||||
_continue_0 = true
|
||||
until true
|
||||
if not _continue_0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return _accum_0
|
||||
end)()
|
||||
end
|
||||
return {
|
||||
"declare",
|
||||
names
|
||||
}
|
||||
end,
|
||||
assign = function(self, node)
|
||||
local names, values = unpack(node, 2)
|
||||
local transformed
|
||||
if #values == 1 then
|
||||
local value = values[1]
|
||||
|
@ -67,6 +67,20 @@ hoist_declarations = (body) ->
|
||||
|
||||
table.insert body, idx, {"declare", assigns}
|
||||
|
||||
|
||||
-- this mutates body searching for assigns
|
||||
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
|
||||
for i=start,#body
|
||||
stm = @transform.statement body[i]
|
||||
body[i] = stm
|
||||
switch stm[1]
|
||||
when "assign", "declare"
|
||||
for name in *stm[2]
|
||||
insert out, name if type(name) == "string"
|
||||
when "group"
|
||||
extract_declarations @, stm[2], 1, out
|
||||
out
|
||||
|
||||
expand_elseif_assign = (ifstm) ->
|
||||
for i = 4, #ifstm
|
||||
case = ifstm[i]
|
||||
@ -161,14 +175,19 @@ Statement = Transformer {
|
||||
root_stms: (body) =>
|
||||
apply_to_last body, implicitly_return @
|
||||
|
||||
declare_glob: (node) =>
|
||||
names = extract_declarations @
|
||||
|
||||
if node[2] == "^"
|
||||
names = for name in *names
|
||||
continue unless name\match "^%u"
|
||||
name
|
||||
|
||||
{"declare", names}
|
||||
|
||||
assign: (node) =>
|
||||
names, values = unpack node, 2
|
||||
|
||||
if globber = @get_current "name_glob"
|
||||
for name in *names
|
||||
if globber name
|
||||
@put_name name
|
||||
|
||||
-- bubble cascading assigns
|
||||
transformed = if #values == 1
|
||||
value = values[1]
|
||||
|
@ -1,13 +1,16 @@
|
||||
|
||||
local a
|
||||
local a,b,c
|
||||
do
|
||||
local a
|
||||
local a,b,c
|
||||
|
||||
b,g = 23232
|
||||
|
||||
|
||||
x = 1212
|
||||
something = ->
|
||||
local x
|
||||
do
|
||||
x = 1212
|
||||
|
||||
something = ->
|
||||
local x
|
||||
x = 1212
|
||||
|
||||
do
|
||||
local *
|
||||
@ -39,7 +42,6 @@ do
|
||||
if something
|
||||
x = 2323
|
||||
|
||||
-- this is broken
|
||||
do
|
||||
local *
|
||||
do
|
||||
@ -50,3 +52,27 @@ do
|
||||
do
|
||||
x = "two"
|
||||
|
||||
do
|
||||
local *
|
||||
k = if what
|
||||
10
|
||||
x = 100
|
||||
|
||||
{:a,:b, :c} = y
|
||||
|
||||
|
||||
do
|
||||
local *
|
||||
|
||||
a = 100
|
||||
print "hi"
|
||||
b = 200
|
||||
|
||||
local *
|
||||
c = 100
|
||||
print "hi"
|
||||
d = 200
|
||||
d = 2323
|
||||
|
||||
|
||||
g = 2323 -- test if anything leaked
|
||||
|
@ -1,10 +1,16 @@
|
||||
local a
|
||||
local a, b, c
|
||||
local x = 1212
|
||||
local something
|
||||
something = function()
|
||||
local x
|
||||
x = 1212
|
||||
do
|
||||
local a
|
||||
local a, b, c
|
||||
local g
|
||||
b, g = 23232
|
||||
end
|
||||
do
|
||||
local x = 1212
|
||||
local something
|
||||
something = function()
|
||||
local x
|
||||
x = 1212
|
||||
end
|
||||
end
|
||||
do
|
||||
local y, z
|
||||
@ -12,29 +18,25 @@ do
|
||||
z = 2323
|
||||
end
|
||||
do
|
||||
|
||||
print("Nothing Here!")
|
||||
end
|
||||
do
|
||||
local X, Y
|
||||
x = 3434
|
||||
local x = 3434
|
||||
local y = 3434
|
||||
X = 3434
|
||||
Y = "yeah"
|
||||
end
|
||||
do
|
||||
|
||||
local y
|
||||
x, y = "a", "b"
|
||||
local x, y = "a", "b"
|
||||
end
|
||||
do
|
||||
local x, y
|
||||
x, y = "a", "b"
|
||||
end
|
||||
do
|
||||
|
||||
if something then
|
||||
x = 2323
|
||||
local x = 2323
|
||||
end
|
||||
end
|
||||
do
|
||||
@ -46,4 +48,26 @@ do
|
||||
do
|
||||
x = "two"
|
||||
end
|
||||
end
|
||||
end
|
||||
do
|
||||
local k, x, a, b, c
|
||||
if what then
|
||||
k = 10
|
||||
end
|
||||
x = 100
|
||||
do
|
||||
local _obj_0 = y
|
||||
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
|
||||
end
|
||||
end
|
||||
do
|
||||
local a, b, c, d
|
||||
a = 100
|
||||
print("hi")
|
||||
b = 200
|
||||
c = 100
|
||||
print("hi")
|
||||
d = 200
|
||||
d = 2323
|
||||
end
|
||||
local g = 2323
|
Loading…
Reference in New Issue
Block a user