This commit is contained in:
leaf corcoran 2011-11-14 19:49:12 -08:00
parent 652e59c96c
commit b279983134
2 changed files with 60 additions and 44 deletions

View File

@ -206,6 +206,35 @@ Transformer = (function()
_base_0.__class = _class_0 _base_0.__class = _class_0
return _class_0 return _class_0
end)() end)()
local construct_comprehension
construct_comprehension = function(inner, clauses)
local current_stms = inner
for _, clause in reversed(clauses) do
local t = clause[1]
if t == "for" then
local _, names, iter = unpack(clause)
current_stms = {
"foreach",
names,
iter,
current_stms
}
elseif t == "when" then
local _, cond = unpack(clause)
current_stms = {
"if",
cond,
current_stms
}
else
current_stms = error("Unknown comprehension clause: " .. t)
end
current_stms = {
current_stms
}
end
return current_stms[1]
end
Statement = Transformer({ Statement = Transformer({
assign = function(self, node) assign = function(self, node)
local _, names, values = unpack(node) local _, names, values = unpack(node)
@ -367,34 +396,8 @@ Statement = Transformer({
exp exp
} }
end end
local current_stms = action(exp) local out = construct_comprehension(action(exp, clauses))
for _, clause in reversed(clauses) do return print(util.dump(out))
local t = clause[1]
if t == "for" then
local names, iter
_, names, iter = unpack(clause)
current_stms = {
"foreach",
names,
iter,
current_stms
}
elseif t == "when" then
local cond
_, cond = unpack(clause)
current_stms = {
"if",
cond,
current_stms
}
else
current_stms = error("Unknown comprehension clause: " .. t)
end
current_stms = {
current_stms
}
end
return current_stms[1]
end, end,
["if"] = function(self, node, ret) ["if"] = function(self, node, ret)
if ret then if ret then
@ -820,6 +823,11 @@ Value = Transformer({
end) end)
return a:wrap(node) return a:wrap(node)
end, end,
tblcomprehension = function(self, node)
local key_exp, value_exp, clauses = unpack(node)
print(util.dump(node))
return "hello_world"
end,
fndef = function(self, node) fndef = function(self, node)
smart_node(node) smart_node(node)
node.body = apply_to_last(node.body, implicitly_return(self)) node.body = apply_to_last(node.body, implicitly_return(self))

View File

@ -108,6 +108,22 @@ class Transformer
can_transform: (node) => can_transform: (node) =>
@transformers[ntype node] != nil @transformers[ntype node] != nil
construct_comprehension = (inner, clauses) ->
current_stms = inner
for _, clause in reversed clauses
t = clause[1]
current_stms = if t == "for"
_, names, iter = unpack clause
{"foreach", names, iter, current_stms}
elseif t == "when"
_, cond = unpack clause
{"if", cond, current_stms}
else
error "Unknown comprehension clause: "..t
current_stms = {current_stms}
current_stms[1]
Statement = Transformer { Statement = Transformer {
assign: (node) => assign: (node) =>
_, names, values = unpack node _, names, values = unpack node
@ -185,23 +201,9 @@ Statement = Transformer {
comprehension: (node, action) => comprehension: (node, action) =>
_, exp, clauses = unpack node _, exp, clauses = unpack node
action = action or (exp) -> {exp} action = action or (exp) -> {exp}
out = construct_comprehension action exp, clauses
current_stms = action exp print util.dump out
for _, clause in reversed clauses
t = clause[1]
current_stms = if t == "for"
_, names, iter = unpack clause
{"foreach", names, iter, current_stms}
elseif t == "when"
_, cond = unpack clause
{"if", cond, current_stms}
else
error "Unknown comprehension clause: "..t
current_stms = {current_stms}
current_stms[1]
-- handle cascading return decorator -- handle cascading return decorator
if: (node, ret) => if: (node, ret) =>
@ -428,6 +430,7 @@ class Accumulator
default_accumulator = (node) => default_accumulator = (node) =>
Accumulator!\convert node Accumulator!\convert node
implicitly_return = (scope) -> implicitly_return = (scope) ->
fn = (stm) -> fn = (stm) ->
t = ntype stm t = ntype stm
@ -451,6 +454,11 @@ Value = Transformer {
a\mutate_body {exp}, false a\mutate_body {exp}, false
a\wrap node a\wrap node
tblcomprehension: (node) =>
key_exp, value_exp, clauses = unpack node
print util.dump node
"hello_world"
fndef: (node) => fndef: (node) =>
smart_node node smart_node node
node.body = apply_to_last node.body, implicitly_return self node.body = apply_to_last node.body, implicitly_return self