split transformers

This commit is contained in:
leaf corcoran 2011-10-02 23:15:38 -07:00
parent e6f33c0844
commit d98dabd7bf
6 changed files with 85 additions and 72 deletions

View File

@ -22,8 +22,7 @@ local bubble_names = {
"has_varargs" "has_varargs"
} }
local Line local Line
Line = (function() Line = (function(_parent_0)
local _parent_0 = nil
local _base_0 = { local _base_0 = {
_append_single = function(self, item) _append_single = function(self, item)
if util.moon.type(item) == Line then if util.moon.type(item) == Line then
@ -86,18 +85,17 @@ Line = (function()
end end
}, { }, {
__index = _base_0, __index = _base_0,
__call = function(cls, ...) __call = function(mt, ...)
local _self_0 = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
cls.__init(_self_0, ...) mt.__init(self, ...)
return _self_0 return self
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local Block_ local Block_
Block_ = (function() Block_ = (function(_parent_0)
local _parent_0 = nil
local _base_0 = { local _base_0 = {
header = "do", header = "do",
footer = "end", footer = "end",
@ -335,6 +333,7 @@ Block_ = (function()
return self:value(node) return self:value(node)
end, end,
value = function(self, node, ...) value = function(self, node, ...)
node = transform.value(node)
local action local action
if type(node) ~= "table" then if type(node) ~= "table" then
action = "raw_value" action = "raw_value"
@ -346,7 +345,6 @@ Block_ = (function()
if not fn then if not fn then
error("Failed to compile value: " .. dump.value(node)) error("Failed to compile value: " .. dump.value(node))
end end
node = transform.node(node)
return fn(self, node, ...) return fn(self, node, ...)
end, end,
values = function(self, values, delim) values = function(self, values, delim)
@ -370,7 +368,7 @@ Block_ = (function()
end end
end, end,
stm = function(self, node, ...) stm = function(self, node, ...)
node = transform.node(node) node = transform.stm(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
@ -458,18 +456,17 @@ Block_ = (function()
end end
}, { }, {
__index = _base_0, __index = _base_0,
__call = function(cls, ...) __call = function(mt, ...)
local _self_0 = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
cls.__init(_self_0, ...) mt.__init(self, ...)
return _self_0 return self
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local RootBlock local RootBlock
RootBlock = (function() RootBlock = (function(_parent_0)
local _parent_0 = Block_
local _base_0 = { local _base_0 = {
render = function(self) render = function(self)
self:_insert_breaks() self:_insert_breaks()
@ -488,15 +485,15 @@ RootBlock = (function()
end end
}, { }, {
__index = _base_0, __index = _base_0,
__call = function(cls, ...) __call = function(mt, ...)
local _self_0 = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
cls.__init(_self_0, ...) mt.__init(self, ...)
return _self_0 return self
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)(Block_)
Block = Block_ Block = Block_
format_error = function(msg, pos, file_str) format_error = function(msg, pos, file_str)
local line = pos_to_line(file_str, pos) local line = pos_to_line(file_str, pos)

View File

@ -228,6 +228,7 @@ class Block_
-- line wise compile functions -- line wise compile functions
name: (node) => @value node name: (node) => @value node
value: (node, ...) => value: (node, ...) =>
node = transform.value node
action = if type(node) != "table" action = if type(node) != "table"
"raw_value" "raw_value"
else else
@ -236,7 +237,6 @@ class Block_
fn = value_compile[action] fn = value_compile[action]
error "Failed to compile value: "..dump.value node if not fn error "Failed to compile value: "..dump.value node if not fn
node = transform.node node
fn self, node, ... fn self, node, ...
values: (values, delim) => values: (values, delim) =>
@ -245,7 +245,7 @@ class Block_
\append_list [@value v for v in *values], delim \append_list [@value v for v in *values], delim
stm: (node, ...) => stm: (node, ...) =>
node = transform.node node node = transform.stm 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

View File

@ -4,8 +4,7 @@ local util = require("moonscript.util")
local data = require("moonscript.data") local data = require("moonscript.data")
local ntype, build, smart_node = types.ntype, types.build, types.smart_node local ntype, build, smart_node = types.ntype, types.build, types.smart_node
local insert = table.insert local insert = table.insert
NameProxy = (function() NameProxy = (function(_parent_0)
local _parent_0 = nil
local _base_0 = { local _base_0 = {
get_name = function(self, scope) get_name = function(self, scope)
if not self.name then if not self.name then
@ -65,18 +64,17 @@ NameProxy = (function()
end end
}, { }, {
__index = _base_0, __index = _base_0,
__call = function(cls, ...) __call = function(mt, ...)
local _self_0 = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
cls.__init(_self_0, ...) mt.__init(self, ...)
return _self_0 return self
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local Run local Run
Run = (function() Run = (function(_parent_0)
local _parent_0 = nil
local _base_0 = { local _base_0 = {
call = function(self, state) call = function(self, state)
return self.fn(state) return self.fn(state)
@ -93,17 +91,28 @@ Run = (function()
end end
}, { }, {
__index = _base_0, __index = _base_0,
__call = function(cls, ...) __call = function(mt, ...)
local _self_0 = setmetatable({}, _base_0) local self = setmetatable({}, _base_0)
cls.__init(_self_0, ...) mt.__init(self, ...)
return _self_0 return self
end end
}) })
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local constructor_name = "new" local constructor_name = "new"
local transformers = { local transformer
transformer = function(transformers)
return function(n)
transformer = transformers[ntype(n)]
if transformer then
return transformer(n) or n
else
return n
end
end
end
stm = transformer({
class = function(node) class = function(node)
local _, name, parent_val, tbl = unpack(node) local _, name, parent_val, tbl = unpack(node)
local constructor = nil local constructor = nil
@ -316,7 +325,9 @@ local transformers = {
}) })
end end
return value return value
end, end
})
value = transformer({
chain = function(node) chain = function(node)
local stub = node[#node] local stub = node[#node]
if type(stub) == "table" and stub[1] == "colon_stub" then if type(stub) == "table" and stub[1] == "colon_stub" then
@ -367,13 +378,21 @@ local transformers = {
}) })
}) })
end end
end,
block_exp = function(node)
local _, body = unpack(node)
local fn = build.fndef({
body = body
})
return build.chain({
base = {
"parens",
fn
},
{
"call",
{ }
}
})
end end
} })
node = function(n)
local transformer = transformers[ntype(n)]
if transformer then
return transformer(n) or n
else
return n
end
end

View File

@ -8,7 +8,7 @@ data = require "moonscript.data"
import ntype, build, smart_node from types import ntype, build, smart_node from types
import insert from table import insert from table
export node, NameProxy export stm, value, NameProxy
class NameProxy class NameProxy
new: (@prefix) => new: (@prefix) =>
@ -47,8 +47,16 @@ class Run
constructor_name = "new" constructor_name = "new"
transformers = { transformer = (transformers) ->
class: (node) -> (n) ->
transformer = transformers[ntype n]
if transformer
transformer(n) or n
else
n
stm = transformer {
class: (node using nil) ->
_, name, parent_val, tbl = unpack node _, name, parent_val, tbl = unpack node
constructor = nil constructor = nil
@ -156,7 +164,9 @@ transformers = {
} }
value value
}
value = transformer {
-- pull out colon chain -- pull out colon chain
chain: (node) -> chain: (node) ->
stub = node[#node] stub = node[#node]
@ -188,13 +198,10 @@ transformers = {
} }
} }
} }
block_exp: (node) ->
_, body = unpack node
fn = build.fndef body: body
build.chain { base: {"parens", fn}, {"call", {}} }
} }
node = (n) ->
transformer = transformers[ntype n]
if transformer
transformer(n) or n
else
n

View File

@ -124,19 +124,10 @@ build = setmetatable({
} }
end, end,
block_exp = function(body) block_exp = function(body)
local fn = build.fndef({ return {
body = body "block_exp",
}) body
return build.chain({
base = {
"parens",
fn
},
{
"call",
{ }
} }
})
end, end,
chain = function(parts) chain = function(parts)
local base = parts.base or error("expecting base property for chain") local base = parts.base or error("expecting base property for chain")

View File

@ -72,8 +72,7 @@ build = setmetatable {
table: (tbl) -> table: (tbl) ->
{"table", tbl} {"table", tbl}
block_exp: (body) -> block_exp: (body) ->
fn = build.fndef body: body {"block_exp", body}
build.chain { base: {"parens", fn}, {"call", {}} }
chain: (parts) -> chain: (parts) ->
base = parts.base or error"expecting base property for chain" base = parts.base or error"expecting base property for chain"
node = {"chain", base} node = {"chain", base}