hoist name declarations from statements inside of a class to the top of the class body

This commit is contained in:
leaf corcoran 2012-09-07 20:20:34 -07:00
parent 290b73f92b
commit a750523d10
3 changed files with 65 additions and 6 deletions

View File

@ -205,6 +205,42 @@ is_singular = function(body)
return true
end
end
local find_assigns
find_assigns = function(body, out)
if out == nil then
out = { }
end
local _list_0 = body
for _index_0 = 1, #_list_0 do
local thing = _list_0[_index_0]
local _exp_0 = thing[1]
if "group" == _exp_0 then
find_assigns(thing[2], out)
elseif "assign" == _exp_0 then
table.insert(out, thing[2])
end
end
return out
end
local hoist_declarations
hoist_declarations = function(body, rules)
local assigns = { }
local _list_0 = find_assigns(body)
for _index_0 = 1, #_list_0 do
local names = _list_0[_index_0]
local _list_1 = names
for _index_0 = 1, #_list_1 do
local name = _list_1[_index_0]
if type(name) == "string" then
table.insert(assigns, name)
end
end
end
return table.insert(body, 1, {
"declare",
assigns
})
end
local constructor_name = "new"
local Transformer
Transformer = (function()
@ -806,7 +842,7 @@ Statement = Transformer({
local value = nil
do
local _with_0 = build
value = _with_0.block_exp({
local out_body = {
Run(function(self)
return self:set("super", function(block, chain)
if chain then
@ -915,7 +951,8 @@ Statement = Transformer({
end
end)()),
cls_name
})
}
hoist_declarations(out_body)
value = _with_0.group({
_with_0.declare({
names = {
@ -927,7 +964,7 @@ Statement = Transformer({
name
},
values = {
value
_with_0.block_exp(out_body)
}
})
})

View File

@ -81,6 +81,25 @@ is_singular = (body) ->
else
true
find_assigns = (body, out={}) ->
for thing in *body
switch thing[1]
when "group"
find_assigns thing[2], out
when "assign"
table.insert out, thing[2] -- extract names
out
hoist_declarations = (body, rules) ->
assigns = {}
-- hoist the plain old assigns
for names in *find_assigns body
for name in *names
table.insert assigns, name if type(name) == "string"
table.insert body, 1, {"declare", assigns}
constructor_name = "new"
class Transformer
@ -407,7 +426,7 @@ Statement = Transformer {
value = nil
with build
value = .block_exp {
out_body = {
Run =>
@set "super", (block, chain) ->
if chain
@ -470,11 +489,13 @@ Statement = Transformer {
cls_name
}
hoist_declarations out_body
value = .group {
.declare names: {name}
.assign {
names: {name}
values: {value}
values: {.block_exp out_body}
}
}

View File

@ -507,6 +507,7 @@ local _ = hello[self].world
if something then
local Whacko
Whacko = (function()
local hello
local _parent_0 = nil
local _base_0 = { }
_base_0.__index = _base_0
@ -543,7 +544,7 @@ if something then
if something then
print("hello world")
end
local hello = "world"
hello = "world"
self.another = "day"
print("yeah")
return _class_0