extract declarations in transform instead of delayed line

This commit is contained in:
leaf corcoran 2013-01-13 14:05:16 -08:00
parent e075c2f971
commit f56095256b
8 changed files with 177 additions and 108 deletions

View File

@ -582,11 +582,18 @@ do
if ret then if ret then
error("deprecated stms call, use transformer") error("deprecated stms call, use transformer")
end end
local _list_0 = stms local current_stms, current_stm_i
for _index_0 = 1, #_list_0 do do
local stm = _list_0[_index_0] local _obj_0 = self
self:stm(stm) current_stms, current_stm_i = _obj_0.current_stms, _obj_0.current_stm_i
end 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 return nil
end, end,
splice = function(self, fn) splice = function(self, fn)
@ -663,11 +670,7 @@ do
if not (self.options.implicitly_return_root == false) then if not (self.options.implicitly_return_root == false) then
stms = transform.Statement.transformers.root_stms(self, stms) stms = transform.Statement.transformers.root_stms(self, stms)
end end
local _list_0 = stms return self:stms(stms)
for _index_0 = 1, #_list_0 do
local s = _list_0[_index_0]
self:stm(s)
end
end, end,
render = function(self) render = function(self)
local buffer = self._lines:flatten() local buffer = self._lines:flatten()

View File

@ -356,7 +356,16 @@ class Block
stms: (stms, ret) => stms: (stms, ret) =>
error "deprecated stms call, use transformer" if 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 nil
splice: (fn) => splice: (fn) =>
@ -374,7 +383,7 @@ class RootBlock extends Block
root_stms: (stms) => root_stms: (stms) =>
unless @options.implicitly_return_root == false unless @options.implicitly_return_root == false
stms = transform.Statement.transformers.root_stms self, stms stms = transform.Statement.transformers.root_stms self, stms
@stm s for s in *stms @stms stms
render: => render: =>
-- print @_lines -- print @_lines

View File

@ -39,40 +39,6 @@ local statement_compilers = {
end end
end 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) declare_with_shadows = function(self, node)
local names = node[2] local names = node[2]
self:declare(names) self:declare(names)

View File

@ -20,33 +20,6 @@ statement_compilers =
with @line "local " with @line "local "
\append_list [@name name for name in *undeclared], ", " \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 -- this overrides the existing names with new locals, used for local keyword
declare_with_shadows: (node) => declare_with_shadows: (node) =>
names = node[2] names = node[2]

View File

@ -127,6 +127,35 @@ hoist_declarations = function(body)
assigns assigns
}) })
end 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 local expand_elseif_assign
expand_elseif_assign = function(ifstm) expand_elseif_assign = function(ifstm)
for i = 4, #ifstm do for i = 4, #ifstm do
@ -342,20 +371,40 @@ local Statement = Transformer({
root_stms = function(self, body) root_stms = function(self, body)
return apply_to_last(body, implicitly_return(self)) return apply_to_last(body, implicitly_return(self))
end, end,
assign = function(self, node) declare_glob = function(self, node)
local names, values = unpack(node, 2) local names = extract_declarations(self)
do if node[2] == "^" then
local globber = self:get_current("name_glob") names = (function()
if globber then local _accum_0 = { }
local _len_0 = 1
local _list_0 = names local _list_0 = names
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local _continue_0 = false
repeat
local name = _list_0[_index_0] local name = _list_0[_index_0]
if globber(name) then if not (name:match("^%u")) then
self:put_name(name) _continue_0 = true
end 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 end
return _accum_0
end)()
end end
return {
"declare",
names
}
end,
assign = function(self, node)
local names, values = unpack(node, 2)
local transformed local transformed
if #values == 1 then if #values == 1 then
local value = values[1] local value = values[1]

View File

@ -67,6 +67,20 @@ hoist_declarations = (body) ->
table.insert body, idx, {"declare", assigns} 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) -> expand_elseif_assign = (ifstm) ->
for i = 4, #ifstm for i = 4, #ifstm
case = ifstm[i] case = ifstm[i]
@ -161,14 +175,19 @@ Statement = Transformer {
root_stms: (body) => root_stms: (body) =>
apply_to_last body, implicitly_return @ 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) => assign: (node) =>
names, values = unpack node, 2 names, values = unpack node, 2
if globber = @get_current "name_glob"
for name in *names
if globber name
@put_name name
-- bubble cascading assigns -- bubble cascading assigns
transformed = if #values == 1 transformed = if #values == 1
value = values[1] value = values[1]

View File

@ -1,14 +1,17 @@
local a do
local a,b,c local a
local a,b,c
b,g = 23232
x = 1212 do
something = -> x = 1212
something = ->
local x local x
x = 1212 x = 1212
do do
local * local *
y = 2323 y = 2323
@ -39,7 +42,6 @@ do
if something if something
x = 2323 x = 2323
-- this is broken
do do
local * local *
do do
@ -50,3 +52,27 @@ do
do do
x = "two" 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

View File

@ -1,10 +1,16 @@
local a do
local a, b, c local a
local x = 1212 local a, b, c
local something local g
something = function() b, g = 23232
end
do
local x = 1212
local something
something = function()
local x local x
x = 1212 x = 1212
end
end end
do do
local y, z local y, z
@ -12,29 +18,25 @@ do
z = 2323 z = 2323
end end
do do
print("Nothing Here!") print("Nothing Here!")
end end
do do
local X, Y local X, Y
x = 3434 local x = 3434
local y = 3434 local y = 3434
X = 3434 X = 3434
Y = "yeah" Y = "yeah"
end end
do do
local x, y = "a", "b"
local y
x, y = "a", "b"
end end
do do
local x, y local x, y
x, y = "a", "b" x, y = "a", "b"
end end
do do
if something then if something then
x = 2323 local x = 2323
end end
end end
do do
@ -47,3 +49,25 @@ do
x = "two" x = "two"
end 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