From 45ba49f16b064414b46f3afd221f874b174e3bb3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Nov 2011 12:59:27 -0800 Subject: [PATCH] with moved to transformer --- moonscript/compile/line.lua | 13 ------------- moonscript/compile/line.moon | 9 --------- moonscript/compile/value.lua | 14 -------------- moonscript/compile/value.moon | 8 -------- moonscript/transform.lua | 28 ++++++++++++++++++++++++++-- moonscript/transform.moon | 17 +++++++++++++++-- 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index 73a63d5..07e9c0f 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -305,19 +305,6 @@ line_compile = { end return nil end, - with = function(self, node, ret) - local _, exp, block = unpack(node) - do - local _with_0 = self:block() - local var = _with_0:init_free_var("with", exp) - self:set("scope_var", var) - _with_0:stms(block) - if ret then - _with_0:stm(ret(var)) - end - return _with_0 - end - end, run = function(self, code) code:call(self) return nil diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index 773afce..793dff8 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -167,15 +167,6 @@ line_compile = @declare names nil - with: (node, ret) => - _, exp, block = unpack node - - with @block! - var = \init_free_var "with", exp - @set "scope_var", var - \stms block - \stm ret var if ret - run: (code) => code\call self nil diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 7adebbc..8c0e102 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -89,20 +89,6 @@ value_compile = { local _, delim, inner, delim_end = unpack(node) return delim .. inner .. (delim_end or delim) end, - with = function(self, node) - do - local _with_0 = self:block("(function()", "end)()") - _with_0:stm(node, default_return) - return _with_0 - end - end, - ["if"] = function(self, node) - do - local _with_0 = self:block("(function()", "end)()") - _with_0:stm(node, default_return) - return _with_0 - end - end, chain = function(self, node) local callee = node[2] if callee == -1 then diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index a02e0d6..2dcece9 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -44,14 +44,6 @@ value_compile = _, delim, inner, delim_end = unpack node delim..inner..(delim_end or delim) - with: (node) => - with @block "(function()", "end)()" - \stm node, default_return - - if: (node) => - with @block "(function()", "end)()" - \stm node, default_return - chain: (node) => callee = node[2] diff --git a/moonscript/transform.lua b/moonscript/transform.lua index ca45d31..e2fe72f 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -224,8 +224,6 @@ Statement = Transformer({ return current_stms[1] end, ["if"] = function(node, ret) - print("node:", node, "ret:", ret) - print(util.dump(node)) if ret then smart_node(node) node['then'] = apply_to_last(node['then'], ret) @@ -237,6 +235,22 @@ Statement = Transformer({ end return node end, + with = function(node, ret) + local _, exp, block = unpack(node) + local scope_name = NameProxy("with") + return build.group({ + build.assign_one(scope_name, exp), + Run(function(self) + return self:set("scope_var", scope_name) + end), + build.group(block), + (function() + if ret then + return ret(scope_name) + end + end)() + }) + end, foreach = function(node) smart_node(node) if ntype(node.iter) == "unpack" then @@ -634,6 +648,16 @@ Value = Transformer({ node.body = apply_to_last(node.body, implicitly_return) return node end, + ["if"] = function(node) + return build.block_exp({ + node + }) + end, + with = function(node) + return build.block_exp({ + node + }) + end, chain = function(node) local stub = node[#node] if type(stub) == "table" and stub[1] == "colon_stub" then diff --git a/moonscript/transform.moon b/moonscript/transform.moon index e6e276e..6bcddc4 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -128,8 +128,6 @@ Statement = Transformer { -- handle cascading return decorator if: (node, ret) -> - print "node:", node, "ret:", ret - print util.dump node if ret smart_node node -- mutate all the bodies @@ -140,6 +138,17 @@ Statement = Transformer { case[body_idx] = apply_to_last case[body_idx], ret node + with: (node, ret) -> + _, exp, block = unpack node + scope_name = NameProxy "with" + build.group { + build.assign_one scope_name, exp + Run => @set "scope_var", scope_name + build.group block + if ret + ret scope_name + } + foreach: (node) -> smart_node node if ntype(node.iter) == "unpack" @@ -366,6 +375,10 @@ Value = Transformer { smart_node node node.body = apply_to_last node.body, implicitly_return node + + if: (node) -> build.block_exp { node } + with: (node) -> build.block_exp { node } + -- pull out colon chain chain: (node) -> stub = node[#node]