Skip to content

Commit 1be72f5

Browse files
committed
add implicit return to end of files #46
1 parent 19a8047 commit 1be72f5

File tree

15 files changed

+34
-17
lines changed

15 files changed

+34
-17
lines changed

moonscript/compile.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,14 @@ RootBlock = (function()
586586
__tostring = function(self)
587587
return "RootBlock<>"
588588
end,
589+
root_stms = function(self, stms)
590+
stms = transform.Statement.transformers.root_stms(self, stms)
591+
local _list_0 = stms
592+
for _index_0 = 1, #_list_0 do
593+
local s = _list_0[_index_0]
594+
self:stm(s)
595+
end
596+
end,
589597
render = function(self)
590598
local buffer = self._lines:flatten()
591599
if buffer[#buffer] == "\n" then
@@ -652,11 +660,7 @@ tree = function(tree, scope)
652660
end
653661
assert(tree, "missing tree")
654662
local runner = coroutine.create(function()
655-
local _list_0 = tree
656-
for _index_0 = 1, #_list_0 do
657-
local line = _list_0[_index_0]
658-
scope:stm(line)
659-
end
663+
return scope:root_stms(tree)
660664
end)
661665
local success, err = coroutine.resume(runner)
662666
if not success then

moonscript/compile.moon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ class RootBlock extends Block
346346

347347
__tostring: => "RootBlock<>"
348348

349+
root_stms: (stms) =>
350+
stms = transform.Statement.transformers.root_stms self, stms
351+
@stm s for s in *stms
352+
349353
render: =>
350354
-- print @_lines
351355
buffer = @_lines\flatten!
@@ -372,7 +376,7 @@ tree = (tree, scope=RootBlock!) ->
372376
assert tree, "missing tree"
373377

374378
runner = coroutine.create ->
375-
scope\stm line for line in *tree
379+
scope\root_stms tree
376380

377381
success, err = coroutine.resume runner
378382
if not success

moonscript/transform.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local data = require("moonscript.data")
55
local reversed = util.reversed
66
local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular
77
local insert = table.insert
8+
local implicitly_return
89
LocalName = (function()
910
local _parent_0 = nil
1011
local _base_0 = {
@@ -443,6 +444,9 @@ construct_comprehension = function(inner, clauses)
443444
return current_stms[1]
444445
end
445446
Statement = Transformer({
447+
root_stms = function(self, body)
448+
return apply_to_last(body, implicitly_return(self))
449+
end,
446450
assign = function(self, node)
447451
local names, values = unpack(node, 2)
448452
local transformed
@@ -1329,7 +1333,6 @@ local default_accumulator
13291333
default_accumulator = function(self, node)
13301334
return Accumulator():convert(node)
13311335
end
1332-
local implicitly_return
13331336
implicitly_return = function(scope)
13341337
local is_top = true
13351338
local fn

moonscript/transform.moon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import insert from table
1111

1212
export Statement, Value, NameProxy, LocalName, Run
1313

14+
local implicitly_return
15+
1416
-- always declares as local
1517
class LocalName
1618
new: (@name) => self[1] = "temp_name"
@@ -186,6 +188,9 @@ construct_comprehension = (inner, clauses) ->
186188
current_stms[1]
187189

188190
Statement = Transformer {
191+
root_stms: (body) =>
192+
apply_to_last body, implicitly_return @
193+
189194
assign: (node) =>
190195
names, values = unpack node, 2
191196
-- bubble cascading assigns
@@ -679,7 +684,6 @@ class Accumulator
679684
default_accumulator = (node) =>
680685
Accumulator!\convert node
681686

682-
683687
implicitly_return = (scope) ->
684688
is_top = true
685689
fn = (stm) ->

tests/outputs/class.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,4 @@ Whacko = (function()
589589
end
590590
return _class_0
591591
end)()
592-
print("hello")
592+
return print("hello")

tests/outputs/comprehension.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ _ = (function()
5252
end
5353
return _tbl_0
5454
end)()
55-
_ = (function()
55+
return (function()
5656
local _tbl_0 = { }
5757
local _list_0 = {
5858
{

tests/outputs/export.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ end
6969
do
7070
local _with_0 = tmp
7171
local j = 2000
72+
return _with_0
7273
end

tests/outputs/funcs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ k(function()
108108
return
109109
end
110110
end)
111-
_ = function()
111+
return function()
112112
if something then
113113
return real_name
114114
end

tests/outputs/lists.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ _ = function()
286286
_ = x
287287
end
288288
end
289-
_ = function()
289+
return function()
290290
return (function()
291291
local _accum_0 = { }
292292
local _len_0 = 0

tests/outputs/literals.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ _ = [[ hello world ]]
1111
_ = [=[ hello world ]=]
1212
_ = [====[ hello world ]====]
1313
_ = "another world"
14-
_ = 'what world'
14+
return 'what world'

0 commit comments

Comments
 (0)