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

View File

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

View File

@ -4,8 +4,7 @@ local util = require("moonscript.util")
local data = require("moonscript.data")
local ntype, build, smart_node = types.ntype, types.build, types.smart_node
local insert = table.insert
NameProxy = (function()
local _parent_0 = nil
NameProxy = (function(_parent_0)
local _base_0 = {
get_name = function(self, scope)
if not self.name then
@ -65,18 +64,17 @@ NameProxy = (function()
end
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
__call = function(mt, ...)
local self = setmetatable({}, _base_0)
mt.__init(self, ...)
return self
end
})
_base_0.__class = _class_0
return _class_0
end)()
local Run
Run = (function()
local _parent_0 = nil
Run = (function(_parent_0)
local _base_0 = {
call = function(self, state)
return self.fn(state)
@ -93,17 +91,28 @@ Run = (function()
end
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
__call = function(mt, ...)
local self = setmetatable({}, _base_0)
mt.__init(self, ...)
return self
end
})
_base_0.__class = _class_0
return _class_0
end)()
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)
local _, name, parent_val, tbl = unpack(node)
local constructor = nil
@ -316,7 +325,9 @@ local transformers = {
})
end
return value
end,
end
})
value = transformer({
chain = function(node)
local stub = node[#node]
if type(stub) == "table" and stub[1] == "colon_stub" then
@ -367,13 +378,21 @@ local transformers = {
})
})
end
end,
block_exp = function(node)
local _, body = unpack(node)
local fn = build.fndef({
body = body
})
return build.chain({
base = {
"parens",
fn
},
{
"call",
{ }
}
})
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 insert from table
export node, NameProxy
export stm, value, NameProxy
class NameProxy
new: (@prefix) =>
@ -47,8 +47,16 @@ class Run
constructor_name = "new"
transformers = {
class: (node) ->
transformer = (transformers) ->
(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
constructor = nil
@ -156,7 +164,9 @@ transformers = {
}
value
}
value = transformer {
-- pull out colon chain
chain: (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,
block_exp = function(body)
local fn = build.fndef({
body = body
})
return build.chain({
base = {
"parens",
fn
},
{
"call",
{ }
}
})
return {
"block_exp",
body
}
end,
chain = function(parts)
local base = parts.base or error("expecting base property for chain")

View File

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