diff --git a/moonscript/compile.lua b/moonscript/compile.lua index f9b1b81..6891c3c 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -586,6 +586,14 @@ RootBlock = (function() __tostring = function(self) return "RootBlock<>" end, + root_stms = function(self, stms) + stms = transform.Statement.transformers.root_stms(self, stms) + local _list_0 = stms + for _index_0 = 1, #_list_0 do + local s = _list_0[_index_0] + self:stm(s) + end + end, render = function(self) local buffer = self._lines:flatten() if buffer[#buffer] == "\n" then @@ -652,11 +660,7 @@ tree = function(tree, scope) end assert(tree, "missing tree") local runner = coroutine.create(function() - local _list_0 = tree - for _index_0 = 1, #_list_0 do - local line = _list_0[_index_0] - scope:stm(line) - end + return scope:root_stms(tree) end) local success, err = coroutine.resume(runner) if not success then diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 70d3df4..c1aae94 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -346,6 +346,10 @@ class RootBlock extends Block __tostring: => "RootBlock<>" + root_stms: (stms) => + stms = transform.Statement.transformers.root_stms self, stms + @stm s for s in *stms + render: => -- print @_lines buffer = @_lines\flatten! @@ -372,7 +376,7 @@ tree = (tree, scope=RootBlock!) -> assert tree, "missing tree" runner = coroutine.create -> - scope\stm line for line in *tree + scope\root_stms tree success, err = coroutine.resume runner if not success diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 6a40991..82b3a5a 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -5,6 +5,7 @@ local data = require("moonscript.data") local reversed = util.reversed local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular local insert = table.insert +local implicitly_return LocalName = (function() local _parent_0 = nil local _base_0 = { @@ -443,6 +444,9 @@ construct_comprehension = function(inner, clauses) return current_stms[1] end Statement = Transformer({ + root_stms = function(self, body) + return apply_to_last(body, implicitly_return(self)) + end, assign = function(self, node) local names, values = unpack(node, 2) local transformed @@ -1329,7 +1333,6 @@ local default_accumulator default_accumulator = function(self, node) return Accumulator():convert(node) end -local implicitly_return implicitly_return = function(scope) local is_top = true local fn diff --git a/moonscript/transform.moon b/moonscript/transform.moon index f99cead..c1c2b22 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -11,6 +11,8 @@ import insert from table export Statement, Value, NameProxy, LocalName, Run +local implicitly_return + -- always declares as local class LocalName new: (@name) => self[1] = "temp_name" @@ -186,6 +188,9 @@ construct_comprehension = (inner, clauses) -> current_stms[1] Statement = Transformer { + root_stms: (body) => + apply_to_last body, implicitly_return @ + assign: (node) => names, values = unpack node, 2 -- bubble cascading assigns @@ -679,7 +684,6 @@ class Accumulator default_accumulator = (node) => Accumulator!\convert node - implicitly_return = (scope) -> is_top = true fn = (stm) -> diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index 1f1aa4f..5af1404 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -589,4 +589,4 @@ Whacko = (function() end return _class_0 end)() -print("hello") \ No newline at end of file +return print("hello") \ No newline at end of file diff --git a/tests/outputs/comprehension.lua b/tests/outputs/comprehension.lua index b1bf218..b0ec910 100644 --- a/tests/outputs/comprehension.lua +++ b/tests/outputs/comprehension.lua @@ -52,7 +52,7 @@ _ = (function() end return _tbl_0 end)() -_ = (function() +return (function() local _tbl_0 = { } local _list_0 = { { diff --git a/tests/outputs/export.lua b/tests/outputs/export.lua index 50e1328..95a0238 100644 --- a/tests/outputs/export.lua +++ b/tests/outputs/export.lua @@ -69,4 +69,5 @@ end do local _with_0 = tmp local j = 2000 + return _with_0 end \ No newline at end of file diff --git a/tests/outputs/funcs.lua b/tests/outputs/funcs.lua index 98a3f5f..35dee92 100644 --- a/tests/outputs/funcs.lua +++ b/tests/outputs/funcs.lua @@ -108,7 +108,7 @@ k(function() return end end) -_ = function() +return function() if something then return real_name end diff --git a/tests/outputs/lists.lua b/tests/outputs/lists.lua index acc09ba..87936ca 100644 --- a/tests/outputs/lists.lua +++ b/tests/outputs/lists.lua @@ -286,7 +286,7 @@ _ = function() _ = x end end -_ = function() +return function() return (function() local _accum_0 = { } local _len_0 = 0 diff --git a/tests/outputs/literals.lua b/tests/outputs/literals.lua index 0f78e4c..69609c5 100644 --- a/tests/outputs/literals.lua +++ b/tests/outputs/literals.lua @@ -11,4 +11,4 @@ _ = [[ hello world ]] _ = [=[ hello world ]=] _ = [====[ hello world ]====] _ = "another world" -_ = 'what world' \ No newline at end of file +return 'what world' \ No newline at end of file diff --git a/tests/outputs/string.lua b/tests/outputs/string.lua index 0d1f8d7..2d751d8 100644 --- a/tests/outputs/string.lua +++ b/tests/outputs/string.lua @@ -34,4 +34,4 @@ local _ = "hello" ("hello"):format().hello(1, 2, 3); ("hello"):format(1, 2, 3) something("hello"):world() -something(("hello"):world()) \ No newline at end of file +return something(("hello"):world()) \ No newline at end of file diff --git a/tests/outputs/switch.lua b/tests/outputs/switch.lua index 9b39d7b..96cc104 100644 --- a/tests/outputs/switch.lua +++ b/tests/outputs/switch.lua @@ -43,7 +43,7 @@ do end end fix(this) -call_func((function() +return call_func((function() local _exp_5 = something if 1 == _exp_5 then return "yes" diff --git a/tests/outputs/using.lua b/tests/outputs/using.lua index 2ab4101..6111d28 100644 --- a/tests/outputs/using.lua +++ b/tests/outputs/using.lua @@ -12,7 +12,7 @@ _ = function(a, b, c) a, b, c = 1, 2, 3 local world = 12321 end -_ = function(a, e, f) +return function(a, e, f) local b, c a, b, c = 1, 2, 3 hello = 12321 diff --git a/tests/outputs/whitespace.lua b/tests/outputs/whitespace.lua index bcc868b..ce2d792 100644 --- a/tests/outputs/whitespace.lua +++ b/tests/outputs/whitespace.lua @@ -72,5 +72,5 @@ if hello(1, 2, 3, world, world) then print("hello") end if hello(1, 2, 3, world, world) then - print("hello") + return print("hello") end \ No newline at end of file diff --git a/tests/outputs/with.lua b/tests/outputs/with.lua index a0af190..7d9ee76 100644 --- a/tests/outputs/with.lua +++ b/tests/outputs/with.lua @@ -45,4 +45,5 @@ do local _ = _with_0:prop("something").hello _with_0.prop:send(one) _with_0.prop:send(one) + return _with_0 end \ No newline at end of file