From b27998313485be5c8d3ea4d405789bde8610551b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 14 Nov 2011 19:49:12 -0800 Subject: [PATCH] fixme --- moonscript/transform.lua | 64 ++++++++++++++++++++++----------------- moonscript/transform.moon | 40 ++++++++++++++---------- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 2c6ddf1..b34fc7a 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -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)) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 7bac6c3..f4e9eac 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -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