From 952987f29f79563a5467727d8026c4e7bc79eadf Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Nov 2011 11:30:26 -0800 Subject: [PATCH] refactor transformer names --- moonscript/compile.lua | 4 ++-- moonscript/compile.moon | 4 ++-- moonscript/transform.lua | 12 ++++++++---- moonscript/transform.moon | 21 +++++++++++++-------- moonscript/types.lua | 4 ++++ moonscript/types.moon | 7 +++++-- 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 073aecd..e3216e1 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -318,7 +318,7 @@ Block_ = (function() return self:value(node) end, value = function(self, node, ...) - node = transform.value(node) + node = transform.Value(node) local action if type(node) ~= "table" then action = "raw_value" @@ -354,7 +354,7 @@ Block_ = (function() if not node then return end - node = transform.stm(node) + node = transform.Statement(node) local fn = line_compile[ntype(node)] if not fn then if has_value(node) then diff --git a/moonscript/compile.moon b/moonscript/compile.moon index f4c4b16..0d8f97f 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -226,7 +226,7 @@ class Block_ -- line wise compile functions name: (node) => @value node value: (node, ...) => - node = transform.value node + node = transform.Value node action = if type(node) != "table" "raw_value" else @@ -244,7 +244,7 @@ class Block_ stm: (node, ...) => return if not node -- slip blank statements - node = transform.stm node + node = transform.Statement node fn = line_compile[ntype(node)] if not fn -- coerce value into statement diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 3f4ffbb..c59846c 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -7,7 +7,7 @@ local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart local insert = table.insert local is_value is_value = function(stm) - return moonscript.compile.Block:is_value(stm) or value.can_transform(stm) + return moonscript.compile.Block:is_value(stm) or Value.can_transform(stm) end NameProxy = (function() local _parent_0 = nil @@ -186,7 +186,7 @@ Transformer = function(transformers) end }) end -stm = Transformer({ +Statement = Transformer({ comprehension = function(node, action) local _, exp, clauses = unpack(node) action = action or function(exp) @@ -223,6 +223,10 @@ stm = Transformer({ end return current_stms[1] end, + ["if"] = function(node, ret) + print("node:", node, "ret:", ret) + return node + end, foreach = function(node) smart_node(node) if ntype(node.iter) == "unpack" then @@ -588,13 +592,13 @@ local default_accumulator default_accumulator = function(node) return Accumulator():convert(node) end -value = Transformer({ +Value = Transformer({ ["for"] = default_accumulator, ["while"] = default_accumulator, foreach = default_accumulator, comprehension = function(node) local a = Accumulator() - node = stm(node, function(exp) + node = Statement(node, function(exp) return a:mutate_body({ exp }, false) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index c7e9c57..fed4616 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -9,11 +9,11 @@ import reversed from util import ntype, build, smart_node, is_slice from types import insert from table -export stm, value, NameProxy, Run +export Statement, Value, NameProxy, Run -- TODO refactor is_value = (stm) -> - moonscript.compile.Block\is_value(stm) or value.can_transform stm + moonscript.compile.Block\is_value(stm) or Value.can_transform stm class NameProxy new: (@prefix) => @@ -57,7 +57,7 @@ class Run -- transform the last stm is a list of stms -- will puke on group -apply_to_last = (stms, fn using nil) -> +apply_to_last = (stms, fn) -> -- find last (real) exp last_exp_id = 0 for i = #stms, 1, -1 @@ -105,7 +105,7 @@ Transformer = (transformers) -> __call: (...) => self.transform ... } -stm = Transformer { +Statement = Transformer { comprehension: (node, action) -> _, exp, clauses = unpack node @@ -126,6 +126,11 @@ stm = Transformer { current_stms[1] + -- handle cascading return decorator + if: (node, ret) -> + print "node:", node, "ret:", ret + node + foreach: (node) -> smart_node node if ntype(node.iter) == "unpack" @@ -166,7 +171,7 @@ stm = Transformer { } } - class: (node using nil) -> + class: (node) -> _, name, parent_val, tbl = unpack node constructor = nil @@ -328,14 +333,14 @@ class Accumulator default_accumulator = (node) -> Accumulator!\convert node -value = Transformer { +Value = Transformer { for: default_accumulator while: default_accumulator foreach: default_accumulator comprehension: (node) -> a = Accumulator! - node = stm node, (exp) -> + node = Statement node, (exp) -> a\mutate_body {exp}, false a\wrap node @@ -344,9 +349,9 @@ value = Transformer { node.body = apply_to_last node.body, (stm) -> t = ntype stm - -- TODO okay this needs a refactor if types.manual_return[t] or not is_value stm stm + -- elseif types.cascading[t] else {"return", stm} diff --git a/moonscript/types.lua b/moonscript/types.lua index 705414f..4dbf221 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -8,6 +8,10 @@ manual_return = data.Set({ "while", "return" }) +cascading = data.Set({ + "if", + "with" +}) ntype = function(node) if type(node) ~= "table" then return "value" diff --git a/moonscript/types.moon b/moonscript/types.moon index f600ec0..3aa6063 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -3,12 +3,16 @@ util = require "moonscript.util" data = require "moonscript.data" export ntype, smart_node, build -export is_slice, manual_return +export is_slice, manual_return, cascading import insert from table +-- implicit return does not work on these statements manual_return = data.Set{"foreach", "for", "while", "return"} +-- assigns and returns are bubbled into their bodies +cascading = data.Set{ "if", "with" } + -- type of node as string ntype = (node) -> if type(node) != "table" @@ -16,7 +20,6 @@ ntype = (node) -> else node[1] - is_slice = (node) -> ntype(node) == "chain" and ntype(node[#node]) == "slice"