refactor transformer names

This commit is contained in:
leaf corcoran 2011-11-06 11:30:26 -08:00
parent 9813071d45
commit 952987f29f
6 changed files with 34 additions and 18 deletions

View File

@ -318,7 +318,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) node = transform.Value(node)
local action local action
if type(node) ~= "table" then if type(node) ~= "table" then
action = "raw_value" action = "raw_value"
@ -354,7 +354,7 @@ Block_ = (function()
if not node then if not node then
return return
end end
node = transform.stm(node) node = transform.Statement(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

View File

@ -226,7 +226,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 node = transform.Value node
action = if type(node) != "table" action = if type(node) != "table"
"raw_value" "raw_value"
else else
@ -244,7 +244,7 @@ class Block_
stm: (node, ...) => stm: (node, ...) =>
return if not node -- slip blank statements return if not node -- slip blank statements
node = transform.stm node node = transform.Statement 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

@ -7,7 +7,7 @@ local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart
local insert = table.insert local insert = table.insert
local is_value local is_value
is_value = function(stm) 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 end
NameProxy = (function() NameProxy = (function()
local _parent_0 = nil local _parent_0 = nil
@ -186,7 +186,7 @@ Transformer = function(transformers)
end end
}) })
end end
stm = Transformer({ Statement = Transformer({
comprehension = function(node, action) comprehension = function(node, action)
local _, exp, clauses = unpack(node) local _, exp, clauses = unpack(node)
action = action or function(exp) action = action or function(exp)
@ -223,6 +223,10 @@ stm = Transformer({
end end
return current_stms[1] return current_stms[1]
end, end,
["if"] = function(node, ret)
print("node:", node, "ret:", ret)
return node
end,
foreach = function(node) foreach = function(node)
smart_node(node) smart_node(node)
if ntype(node.iter) == "unpack" then if ntype(node.iter) == "unpack" then
@ -588,13 +592,13 @@ local default_accumulator
default_accumulator = function(node) default_accumulator = function(node)
return Accumulator():convert(node) return Accumulator():convert(node)
end end
value = Transformer({ Value = Transformer({
["for"] = default_accumulator, ["for"] = default_accumulator,
["while"] = default_accumulator, ["while"] = default_accumulator,
foreach = default_accumulator, foreach = default_accumulator,
comprehension = function(node) comprehension = function(node)
local a = Accumulator() local a = Accumulator()
node = stm(node, function(exp) node = Statement(node, function(exp)
return a:mutate_body({ return a:mutate_body({
exp exp
}, false) }, false)

View File

@ -9,11 +9,11 @@ import reversed from util
import ntype, build, smart_node, is_slice from types import ntype, build, smart_node, is_slice from types
import insert from table import insert from table
export stm, value, NameProxy, Run export Statement, Value, NameProxy, Run
-- TODO refactor -- TODO refactor
is_value = (stm) -> 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 class NameProxy
new: (@prefix) => new: (@prefix) =>
@ -57,7 +57,7 @@ class Run
-- transform the last stm is a list of stms -- transform the last stm is a list of stms
-- will puke on group -- will puke on group
apply_to_last = (stms, fn using nil) -> apply_to_last = (stms, fn) ->
-- find last (real) exp -- find last (real) exp
last_exp_id = 0 last_exp_id = 0
for i = #stms, 1, -1 for i = #stms, 1, -1
@ -105,7 +105,7 @@ Transformer = (transformers) ->
__call: (...) => self.transform ... __call: (...) => self.transform ...
} }
stm = Transformer { Statement = Transformer {
comprehension: (node, action) -> comprehension: (node, action) ->
_, exp, clauses = unpack node _, exp, clauses = unpack node
@ -126,6 +126,11 @@ stm = Transformer {
current_stms[1] current_stms[1]
-- handle cascading return decorator
if: (node, ret) ->
print "node:", node, "ret:", ret
node
foreach: (node) -> foreach: (node) ->
smart_node node smart_node node
if ntype(node.iter) == "unpack" if ntype(node.iter) == "unpack"
@ -166,7 +171,7 @@ stm = Transformer {
} }
} }
class: (node using nil) -> class: (node) ->
_, name, parent_val, tbl = unpack node _, name, parent_val, tbl = unpack node
constructor = nil constructor = nil
@ -328,14 +333,14 @@ class Accumulator
default_accumulator = (node) -> default_accumulator = (node) ->
Accumulator!\convert node Accumulator!\convert node
value = Transformer { Value = Transformer {
for: default_accumulator for: default_accumulator
while: default_accumulator while: default_accumulator
foreach: default_accumulator foreach: default_accumulator
comprehension: (node) -> comprehension: (node) ->
a = Accumulator! a = Accumulator!
node = stm node, (exp) -> node = Statement node, (exp) ->
a\mutate_body {exp}, false a\mutate_body {exp}, false
a\wrap node a\wrap node
@ -344,9 +349,9 @@ value = Transformer {
node.body = apply_to_last node.body, (stm) -> node.body = apply_to_last node.body, (stm) ->
t = ntype stm t = ntype stm
-- TODO okay this needs a refactor
if types.manual_return[t] or not is_value stm if types.manual_return[t] or not is_value stm
stm stm
-- elseif types.cascading[t]
else else
{"return", stm} {"return", stm}

View File

@ -8,6 +8,10 @@ manual_return = data.Set({
"while", "while",
"return" "return"
}) })
cascading = data.Set({
"if",
"with"
})
ntype = function(node) ntype = function(node)
if type(node) ~= "table" then if type(node) ~= "table" then
return "value" return "value"

View File

@ -3,12 +3,16 @@ util = require "moonscript.util"
data = require "moonscript.data" data = require "moonscript.data"
export ntype, smart_node, build export ntype, smart_node, build
export is_slice, manual_return export is_slice, manual_return, cascading
import insert from table import insert from table
-- implicit return does not work on these statements
manual_return = data.Set{"foreach", "for", "while", "return"} 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 -- type of node as string
ntype = (node) -> ntype = (node) ->
if type(node) != "table" if type(node) != "table"
@ -16,7 +20,6 @@ ntype = (node) ->
else else
node[1] node[1]
is_slice = (node) -> is_slice = (node) ->
ntype(node) == "chain" and ntype(node[#node]) == "slice" ntype(node) == "chain" and ntype(node[#node]) == "slice"