diff --git a/moon/init.lua b/moon/init.lua index 5d80e52..0ca497c 100644 --- a/moon/init.lua +++ b/moon/init.lua @@ -108,13 +108,11 @@ extend = function(...) return tbls[1] end copy = function(self) - return (function() - local _tbl_0 = { } - for key, val in pairs(self) do - _tbl_0[key] = val - end - return _tbl_0 - end)() + local _tbl_0 = { } + for key, val in pairs(self) do + _tbl_0[key] = val + end + return _tbl_0 end mixin = function(self, cls, ...) for key, val in pairs(cls.__base) do diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 5892c20..83f95cd 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -119,16 +119,14 @@ do local strip strip = function(t) if "table" == type(t) then - return (function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #t do - local v = t[_index_0] - _accum_0[_len_0] = strip(v) - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #t do + local v = t[_index_0] + _accum_0[_len_0] = strip(v) + _len_0 = _len_0 + 1 + end + return _accum_0 else return t end diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 09a8aa2..4287c1d 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -210,6 +210,18 @@ with_continue_listener = function(body) end do local _base_0 = { + transform_once = function(self, scope, node, ...) + if self.seen_nodes[node] then + return node + end + self.seen_nodes[node] = true + local transformer = self.transformers[ntype(node)] + if transformer then + return transformer(scope, node, ...) or node + else + return node + end + end, transform = function(self, scope, node, ...) if self.seen_nodes[node] then return node @@ -314,6 +326,18 @@ Statement = Transformer({ root_stms = function(self, body) return apply_to_last(body, implicitly_return(self)) end, + ["return"] = function(self, node) + node[2] = Value:transform_once(self, node[2]) + if "block_exp" == ntype(node[2]) then + local block_exp = node[2] + local block_body = block_exp[2] + local idx = #block_body + node[2] = block_body[idx] + block_body[idx] = node + return build.group(block_body) + end + return node + end, declare_glob = function(self, node) local names = extract_declarations(self) if node[2] == "^" then @@ -370,7 +394,7 @@ Statement = Transformer({ } }) elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then - return build.assign_one(first_name, Value.transformers[first_value[1]](self, first_value)) + return build.assign_one(first_name, Value:transform_once(self, first_value)) end end local transformed diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 0667197..a7fcab6 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -108,6 +108,16 @@ class Transformer new: (@transformers) => @seen_nodes = setmetatable {}, __mode: "k" + transform_once: (scope, node, ...) => + return node if @seen_nodes[node] + @seen_nodes[node] = true + + transformer = @transformers[ntype node] + if transformer + transformer(scope, node, ...) or node + else + node + transform: (scope, node, ...) => return node if @seen_nodes[node] @seen_nodes[node] = true @@ -158,6 +168,20 @@ Statement = Transformer { root_stms: (body) => apply_to_last body, implicitly_return @ + return: (node) => + node[2] = Value\transform_once @, node[2] + + if "block_exp" == ntype node[2] + block_exp = node[2] + block_body = block_exp[2] + + idx = #block_body + node[2] = block_body[idx] + block_body[idx] = node + return build.group block_body + + node + declare_glob: (node) => names = extract_declarations @ @@ -191,8 +215,7 @@ Statement = Transformer { } when "comprehension", "tblcomprehension", "foreach", "for", "while" - return build.assign_one first_name, - Value.transformers[first_value[1]] @, first_value + return build.assign_one first_name, Value\transform_once @, first_value -- bubble cascading assigns transformed = if num_values == 1 diff --git a/moonscript/util.lua b/moonscript/util.lua index 7e5631d..dfb4355 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -77,15 +77,13 @@ split = function(str, delim) return { } end str = str .. delim - return (function() - local _accum_0 = { } - local _len_0 = 1 - for m in str:gmatch("(.-)" .. delim) do - _accum_0[_len_0] = m - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() + local _accum_0 = { } + local _len_0 = 1 + for m in str:gmatch("(.-)" .. delim) do + _accum_0[_len_0] = m + _len_0 = _len_0 + 1 + end + return _accum_0 end local dump dump = function(what) diff --git a/spec/outputs/bubbling.lua b/spec/outputs/bubbling.lua index c0bc5fe..247ff1c 100644 --- a/spec/outputs/bubbling.lua +++ b/spec/outputs/bubbling.lua @@ -6,17 +6,15 @@ f = function(...) end local dont_bubble dont_bubble = function() - return (function() - local _accum_0 = { } - local _len_0 = 1 - for x in (function(...) - return print(...) - end)("hello") do - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() + local _accum_0 = { } + local _len_0 = 1 + for x in (function(...) + return print(...) + end)("hello") do + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 end local k do @@ -44,21 +42,19 @@ do end local m m = function(...) - return (function(...) - local _accum_0 = { } - local _len_0 = 1 - local _list_0 = { - ... - } - for _index_0 = 1, #_list_0 do - local x = _list_0[_index_0] - if f(...) > 4 then - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - end + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = { + ... + } + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + if f(...) > 4 then + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 end - return _accum_0 - end)(...) + end + return _accum_0 end local x do diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua index 7428af4..2846150 100644 --- a/spec/outputs/lists.lua +++ b/spec/outputs/lists.lua @@ -251,15 +251,13 @@ for _index_0 = a, _max_2 < 0 and #x + _max_2 or _max_2, c do end local normal normal = function(hello) - return (function() - local _accum_0 = { } - local _len_0 = 1 - for x in yeah do - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() + local _accum_0 = { } + local _len_0 = 1 + for x in yeah do + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 end local test = x(1, 2, 3, 4, 5) for _index_0 = 1, #test do @@ -281,14 +279,12 @@ _ = function() end end return function() - return (function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #things do - x = things[_index_0] - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #things do + x = things[_index_0] + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 end \ No newline at end of file