refactor transformers to run with context of current block

This commit is contained in:
leaf corcoran 2012-10-27 19:37:39 -07:00
parent 5c409cfc49
commit cae4d4ae7a
4 changed files with 35 additions and 26 deletions

View File

@ -344,7 +344,7 @@ Block = (function()
return self:value(node) return self:value(node)
end, end,
value = function(self, node, ...) value = function(self, node, ...)
node = self.root.transform.value(node) node = self.transform.value(node)
local action local action
if type(node) ~= "table" then if type(node) ~= "table" then
action = "raw_value" action = "raw_value"
@ -380,7 +380,7 @@ Block = (function()
if not node then if not node then
return return
end end
node = self.root.transform.statement(node) node = self.transform.statement(node)
local fn = line_compile[ntype(node)] local fn = line_compile[ntype(node)]
if not fn then if not fn then
if has_value(node) then if has_value(node) then
@ -429,6 +429,13 @@ Block = (function()
self._posmap = { } self._posmap = { }
self._names = { } self._names = { }
self._state = { } self._state = { }
do
local _with_0 = transform
self.transform = {
value = _with_0.Value:bind(self),
statement = _with_0.Statement:bind(self)
}
end
if self.parent then if self.parent then
self.root = self.parent.root self.root = self.parent.root
self.indent = self.parent.indent + 1 self.indent = self.parent.indent + 1
@ -481,10 +488,6 @@ RootBlock = (function()
local _class_0 = setmetatable({ local _class_0 = setmetatable({
__init = function(self, ...) __init = function(self, ...)
self.root = self self.root = self
self.transform = {
value = transform.Value:instance(self),
statement = transform.Statement:instance(self)
}
return _parent_0.__init(self, ...) return _parent_0.__init(self, ...)
end, end,
__base = _base_0, __base = _base_0,

View File

@ -71,6 +71,12 @@ class Block
@_names = {} @_names = {}
@_state = {} @_state = {}
with transform
@transform = {
value: .Value\bind self
statement: .Statement\bind self
}
if @parent if @parent
@root = @parent.root @root = @parent.root
@indent = @parent.indent + 1 @indent = @parent.indent + 1
@ -239,7 +245,7 @@ class Block
-- line wise compile functions -- line wise compile functions
name: (node) => @value node name: (node) => @value node
value: (node, ...) => value: (node, ...) =>
node = @root.transform.value node node = @transform.value node
action = if type(node) != "table" action = if type(node) != "table"
"raw_value" "raw_value"
else else
@ -257,7 +263,7 @@ class Block
stm: (node, ...) => stm: (node, ...) =>
return if not node -- skip blank statements return if not node -- skip blank statements
node = @root.transform.statement node node = @transform.statement node
fn = line_compile[ntype(node)] fn = line_compile[ntype(node)]
if not fn if not fn
-- coerce value into statement -- coerce value into statement
@ -279,10 +285,6 @@ class Block
class RootBlock extends Block class RootBlock extends Block
new: (...) => new: (...) =>
@root = self @root = self
@transform = {
value: transform.Value\instance self
statement: transform.Statement\instance self
}
super ... super ...
__tostring: => "RootBlock<>" __tostring: => "RootBlock<>"

View File

@ -297,12 +297,15 @@ Transformer = (function()
end end
node = res node = res
end end
return node
end, end,
__call = function(self, node, ...) bind = function(self, scope)
return self:transform(self.scope, node, ...) return function(...)
return self:transform(scope, ...)
end
end, end,
instance = function(self, scope) __call = function(self, ...)
return Transformer(self.transformers, scope) return self:transform(...)
end, end,
can_transform = function(self, node) can_transform = function(self, node)
return self.transformers[ntype(node)] ~= nil return self.transformers[ntype(node)] ~= nil
@ -313,9 +316,11 @@ Transformer = (function()
setmetatable(_base_0, _parent_0.__base) setmetatable(_base_0, _parent_0.__base)
end end
local _class_0 = setmetatable({ local _class_0 = setmetatable({
__init = function(self, transformers, scope) __init = function(self, transformers)
self.transformers, self.scope = transformers, scope self.transformers = transformers
self.seen_nodes = { } self.seen_nodes = setmetatable({ }, {
__mode = "k"
})
end, end,
__base = _base_0, __base = _base_0,
__name = "Transformer", __name = "Transformer",

View File

@ -117,11 +117,10 @@ expand_elseif_assign = (ifstm) ->
constructor_name = "new" constructor_name = "new"
class Transformer class Transformer
new: (@transformers, @scope) => new: (@transformers) =>
@seen_nodes = {} @seen_nodes = setmetatable {}, __mode: "k"
transform: (scope, node, ...) => transform: (scope, node, ...) =>
-- print scope, node, ...
return node if @seen_nodes[node] return node if @seen_nodes[node]
@seen_nodes[node] = true @seen_nodes[node] = true
while true while true
@ -132,12 +131,12 @@ class Transformer
node node
return node if res == node return node if res == node
node = res node = res
node
__call: (node, ...) => bind: (scope) =>
@transform @scope, node, ... (...) -> @transform scope, ...
instance: (scope) => __call: (...) => @transform ...
Transformer @transformers, scope
can_transform: (node) => can_transform: (node) =>
@transformers[ntype node] != nil @transformers[ntype node] != nil