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
return _class_0
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({
assign = function(self, node)
local _, names, values = unpack(node)
@ -367,34 +396,8 @@ Statement = Transformer({
exp
}
end
local current_stms = action(exp)
for _, clause in reversed(clauses) do
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]
local out = construct_comprehension(action(exp, clauses))
return print(util.dump(out))
end,
["if"] = function(self, node, ret)
if ret then
@ -820,6 +823,11 @@ Value = Transformer({
end)
return a:wrap(node)
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)
smart_node(node)
node.body = apply_to_last(node.body, implicitly_return(self))

View File

@ -108,6 +108,22 @@ class Transformer
can_transform: (node) =>
@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 {
assign: (node) =>
_, names, values = unpack node
@ -185,23 +201,9 @@ Statement = Transformer {
comprehension: (node, action) =>
_, exp, clauses = unpack node
action = action or (exp) -> {exp}
current_stms = action exp
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]
out = construct_comprehension action exp, clauses
print util.dump out
-- handle cascading return decorator
if: (node, ret) =>
@ -428,6 +430,7 @@ class Accumulator
default_accumulator = (node) =>
Accumulator!\convert node
implicitly_return = (scope) ->
fn = (stm) ->
t = ntype stm
@ -451,6 +454,11 @@ Value = Transformer {
a\mutate_body {exp}, false
a\wrap node
tblcomprehension: (node) =>
key_exp, value_exp, clauses = unpack node
print util.dump node
"hello_world"
fndef: (node) =>
smart_node node
node.body = apply_to_last node.body, implicitly_return self