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)
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

View File

@ -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

View File

@ -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)

View File

@ -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}

View File

@ -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"

View File

@ -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"