mirror of
https://github.com/leafo/moonscript.git
synced 2024-10-05 05:34:19 +00:00
refactor transformers to run with context of current block
This commit is contained in:
parent
5c409cfc49
commit
cae4d4ae7a
@ -344,7 +344,7 @@ Block = (function()
|
||||
return self:value(node)
|
||||
end,
|
||||
value = function(self, node, ...)
|
||||
node = self.root.transform.value(node)
|
||||
node = self.transform.value(node)
|
||||
local action
|
||||
if type(node) ~= "table" then
|
||||
action = "raw_value"
|
||||
@ -380,7 +380,7 @@ Block = (function()
|
||||
if not node then
|
||||
return
|
||||
end
|
||||
node = self.root.transform.statement(node)
|
||||
node = self.transform.statement(node)
|
||||
local fn = line_compile[ntype(node)]
|
||||
if not fn then
|
||||
if has_value(node) then
|
||||
@ -429,6 +429,13 @@ Block = (function()
|
||||
self._posmap = { }
|
||||
self._names = { }
|
||||
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
|
||||
self.root = self.parent.root
|
||||
self.indent = self.parent.indent + 1
|
||||
@ -481,10 +488,6 @@ RootBlock = (function()
|
||||
local _class_0 = setmetatable({
|
||||
__init = function(self, ...)
|
||||
self.root = self
|
||||
self.transform = {
|
||||
value = transform.Value:instance(self),
|
||||
statement = transform.Statement:instance(self)
|
||||
}
|
||||
return _parent_0.__init(self, ...)
|
||||
end,
|
||||
__base = _base_0,
|
||||
|
@ -71,6 +71,12 @@ class Block
|
||||
@_names = {}
|
||||
@_state = {}
|
||||
|
||||
with transform
|
||||
@transform = {
|
||||
value: .Value\bind self
|
||||
statement: .Statement\bind self
|
||||
}
|
||||
|
||||
if @parent
|
||||
@root = @parent.root
|
||||
@indent = @parent.indent + 1
|
||||
@ -239,7 +245,7 @@ class Block
|
||||
-- line wise compile functions
|
||||
name: (node) => @value node
|
||||
value: (node, ...) =>
|
||||
node = @root.transform.value node
|
||||
node = @transform.value node
|
||||
action = if type(node) != "table"
|
||||
"raw_value"
|
||||
else
|
||||
@ -257,7 +263,7 @@ class Block
|
||||
|
||||
stm: (node, ...) =>
|
||||
return if not node -- skip blank statements
|
||||
node = @root.transform.statement node
|
||||
node = @transform.statement node
|
||||
fn = line_compile[ntype(node)]
|
||||
if not fn
|
||||
-- coerce value into statement
|
||||
@ -279,10 +285,6 @@ class Block
|
||||
class RootBlock extends Block
|
||||
new: (...) =>
|
||||
@root = self
|
||||
@transform = {
|
||||
value: transform.Value\instance self
|
||||
statement: transform.Statement\instance self
|
||||
}
|
||||
super ...
|
||||
|
||||
__tostring: => "RootBlock<>"
|
||||
|
@ -297,12 +297,15 @@ Transformer = (function()
|
||||
end
|
||||
node = res
|
||||
end
|
||||
return node
|
||||
end,
|
||||
__call = function(self, node, ...)
|
||||
return self:transform(self.scope, node, ...)
|
||||
bind = function(self, scope)
|
||||
return function(...)
|
||||
return self:transform(scope, ...)
|
||||
end
|
||||
end,
|
||||
instance = function(self, scope)
|
||||
return Transformer(self.transformers, scope)
|
||||
__call = function(self, ...)
|
||||
return self:transform(...)
|
||||
end,
|
||||
can_transform = function(self, node)
|
||||
return self.transformers[ntype(node)] ~= nil
|
||||
@ -313,9 +316,11 @@ Transformer = (function()
|
||||
setmetatable(_base_0, _parent_0.__base)
|
||||
end
|
||||
local _class_0 = setmetatable({
|
||||
__init = function(self, transformers, scope)
|
||||
self.transformers, self.scope = transformers, scope
|
||||
self.seen_nodes = { }
|
||||
__init = function(self, transformers)
|
||||
self.transformers = transformers
|
||||
self.seen_nodes = setmetatable({ }, {
|
||||
__mode = "k"
|
||||
})
|
||||
end,
|
||||
__base = _base_0,
|
||||
__name = "Transformer",
|
||||
|
@ -117,11 +117,10 @@ expand_elseif_assign = (ifstm) ->
|
||||
constructor_name = "new"
|
||||
|
||||
class Transformer
|
||||
new: (@transformers, @scope) =>
|
||||
@seen_nodes = {}
|
||||
new: (@transformers) =>
|
||||
@seen_nodes = setmetatable {}, __mode: "k"
|
||||
|
||||
transform: (scope, node, ...) =>
|
||||
-- print scope, node, ...
|
||||
return node if @seen_nodes[node]
|
||||
@seen_nodes[node] = true
|
||||
while true
|
||||
@ -132,12 +131,12 @@ class Transformer
|
||||
node
|
||||
return node if res == node
|
||||
node = res
|
||||
node
|
||||
|
||||
__call: (node, ...) =>
|
||||
@transform @scope, node, ...
|
||||
bind: (scope) =>
|
||||
(...) -> @transform scope, ...
|
||||
|
||||
instance: (scope) =>
|
||||
Transformer @transformers, scope
|
||||
__call: (...) => @transform ...
|
||||
|
||||
can_transform: (node) =>
|
||||
@transformers[ntype node] != nil
|
||||
|
Loading…
Reference in New Issue
Block a user