Skip to content

Commit f8925a4

Browse files
committed
classes can implicitly return #46
1 parent 1be72f5 commit f8925a4

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

moonscript/transform.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ Statement = Transformer({
877877
if_stm
878878
})
879879
end,
880-
class = function(self, node)
880+
class = function(self, node, ret)
881881
local _, name, parent_val, body = unpack(node)
882882
local statements = { }
883883
local properties = { }
@@ -1213,7 +1213,12 @@ Statement = Transformer({
12131213
values = {
12141214
_with_0.block_exp(out_body)
12151215
}
1216-
})
1216+
}),
1217+
(function()
1218+
if ret then
1219+
return ret(name)
1220+
end
1221+
end)()
12171222
})
12181223
end
12191224
return value
@@ -1342,15 +1347,15 @@ implicitly_return = function(scope)
13421347
stm = scope.transform.statement(stm)
13431348
t = ntype(stm)
13441349
end
1345-
if types.manual_return[t] or not types.is_value(stm) then
1350+
if types.cascading[t] then
1351+
is_top = false
1352+
return scope.transform.statement(stm, fn)
1353+
elseif types.manual_return[t] or not types.is_value(stm) then
13461354
if is_top and t == "return" and stm[2] == "" then
13471355
return nil
13481356
else
13491357
return stm
13501358
end
1351-
elseif types.cascading[t] then
1352-
is_top = false
1353-
return scope.transform.statement(stm, fn)
13541359
else
13551360
if t == "comprehension" and not types.comprehension_has_value(stm) then
13561361
return stm

moonscript/transform.moon

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ Statement = Transformer {
439439
if_stm
440440
}
441441

442-
class: (node) =>
442+
class: (node, ret) =>
443443
_, name, parent_val, body = unpack node
444444

445445
-- split apart properties and statements
@@ -620,6 +620,8 @@ Statement = Transformer {
620620
names: {name}
621621
values: {.block_exp out_body}
622622
}
623+
if ret
624+
ret name
623625
}
624626

625627
value
@@ -694,15 +696,15 @@ implicitly_return = (scope) ->
694696
stm = scope.transform.statement stm
695697
t = ntype stm
696698

697-
if types.manual_return[t] or not types.is_value stm
699+
if types.cascading[t]
700+
is_top = false
701+
scope.transform.statement stm, fn
702+
elseif types.manual_return[t] or not types.is_value stm
698703
-- remove blank return statement
699704
if is_top and t == "return" and stm[2] == ""
700705
nil
701706
else
702707
stm
703-
elseif types.cascading[t]
704-
is_top = false
705-
scope.transform.statement stm, fn
706708
else
707709
if t == "comprehension" and not types.comprehension_has_value stm
708710
stm

moonscript/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ cascading = data.Set({
1212
"if",
1313
"unless",
1414
"with",
15-
"switch"
15+
"switch",
16+
"class"
1617
})
1718
is_value = function(stm)
1819
local compile, transform = moonscript.compile, moonscript.transform

moonscript/types.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ manual_return = data.Set{"foreach", "for", "while", "return"}
1414
-- Assigns and returns are bubbled into their bodies.
1515
-- All cascading statement transform functions accept a second arugment that
1616
-- is the transformation to apply to the last statement in their body
17-
cascading = data.Set{ "if", "unless", "with", "switch" }
17+
cascading = data.Set{ "if", "unless", "with", "switch", "class" }
1818

1919
is_value = (stm) ->
2020
import compile, transform from moonscript

tests/inputs/class.moon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,7 @@ class Whacko
129129

130130
print "hello"
131131

132+
yyy = ->
133+
class Cool
134+
nil
132135

tests/outputs/class.lua

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,48 @@ Whacko = (function()
589589
end
590590
return _class_0
591591
end)()
592-
return print("hello")
592+
print("hello")
593+
local yyy
594+
yyy = function()
595+
local Cool
596+
Cool = (function()
597+
local _parent_0 = nil
598+
local _base_0 = { }
599+
_base_0.__index = _base_0
600+
if _parent_0 then
601+
setmetatable(_base_0, _parent_0.__base)
602+
end
603+
local _class_0 = setmetatable({
604+
__init = function(self, ...)
605+
if _parent_0 then
606+
return _parent_0.__init(self, ...)
607+
end
608+
end,
609+
__base = _base_0,
610+
__name = "Cool",
611+
__parent = _parent_0
612+
}, {
613+
__index = function(cls, name)
614+
local val = rawget(_base_0, name)
615+
if val == nil and _parent_0 then
616+
return _parent_0[name]
617+
else
618+
return val
619+
end
620+
end,
621+
__call = function(cls, ...)
622+
local _self_0 = setmetatable({}, _base_0)
623+
cls.__init(_self_0, ...)
624+
return _self_0
625+
end
626+
})
627+
_base_0.__class = _class_0
628+
local self = _class_0
629+
_ = nil
630+
if _parent_0 and _parent_0.__inherited then
631+
_parent_0.__inherited(_parent_0, _class_0)
632+
end
633+
return _class_0
634+
end)()
635+
return Cool
636+
end

0 commit comments

Comments
 (0)