From d98dabd7bf93036ffcb932f3324f2a06b9d6d8f0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 2 Oct 2011 23:15:38 -0700 Subject: [PATCH] split transformers --- moonscript/compile.lua | 39 +++++++++++------------ moonscript/compile.moon | 4 +-- moonscript/transform.lua | 65 +++++++++++++++++++++++++-------------- moonscript/transform.moon | 29 ++++++++++------- moonscript/types.lua | 17 +++------- moonscript/types.moon | 3 +- 6 files changed, 85 insertions(+), 72 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 475361e..54bf39e 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -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) diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 69abe88..aab11bb 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -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 diff --git a/moonscript/transform.lua b/moonscript/transform.lua index a2029ff..85e654c 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -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 +}) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index c210285..f21e814 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -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 - - diff --git a/moonscript/types.lua b/moonscript/types.lua index 1f96b46..6f100ef 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -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") diff --git a/moonscript/types.moon b/moonscript/types.moon index 4e546f1..5835c0f 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -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}