diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 82b3a5a..e6b5e47 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -877,7 +877,7 @@ Statement = Transformer({ if_stm }) end, - class = function(self, node) + class = function(self, node, ret) local _, name, parent_val, body = unpack(node) local statements = { } local properties = { } @@ -1213,7 +1213,12 @@ Statement = Transformer({ values = { _with_0.block_exp(out_body) } - }) + }), + (function() + if ret then + return ret(name) + end + end)() }) end return value @@ -1342,15 +1347,15 @@ implicitly_return = function(scope) stm = scope.transform.statement(stm) t = ntype(stm) end - if types.manual_return[t] or not types.is_value(stm) then + if types.cascading[t] then + is_top = false + return scope.transform.statement(stm, fn) + elseif types.manual_return[t] or not types.is_value(stm) then if is_top and t == "return" and stm[2] == "" then return nil else return stm end - elseif types.cascading[t] then - is_top = false - return scope.transform.statement(stm, fn) else if t == "comprehension" and not types.comprehension_has_value(stm) then return stm diff --git a/moonscript/transform.moon b/moonscript/transform.moon index c1c2b22..9ab3088 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -439,7 +439,7 @@ Statement = Transformer { if_stm } - class: (node) => + class: (node, ret) => _, name, parent_val, body = unpack node -- split apart properties and statements @@ -620,6 +620,8 @@ Statement = Transformer { names: {name} values: {.block_exp out_body} } + if ret + ret name } value @@ -694,15 +696,15 @@ implicitly_return = (scope) -> stm = scope.transform.statement stm t = ntype stm - if types.manual_return[t] or not types.is_value stm + if types.cascading[t] + is_top = false + scope.transform.statement stm, fn + elseif types.manual_return[t] or not types.is_value stm -- remove blank return statement if is_top and t == "return" and stm[2] == "" nil else stm - elseif types.cascading[t] - is_top = false - scope.transform.statement stm, fn else if t == "comprehension" and not types.comprehension_has_value stm stm diff --git a/moonscript/types.lua b/moonscript/types.lua index fc5ec01..4b917bf 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -12,7 +12,8 @@ cascading = data.Set({ "if", "unless", "with", - "switch" + "switch", + "class" }) is_value = function(stm) local compile, transform = moonscript.compile, moonscript.transform diff --git a/moonscript/types.moon b/moonscript/types.moon index d072adb..70cb135 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -14,7 +14,7 @@ manual_return = data.Set{"foreach", "for", "while", "return"} -- Assigns and returns are bubbled into their bodies. -- All cascading statement transform functions accept a second arugment that -- is the transformation to apply to the last statement in their body -cascading = data.Set{ "if", "unless", "with", "switch" } +cascading = data.Set{ "if", "unless", "with", "switch", "class" } is_value = (stm) -> import compile, transform from moonscript diff --git a/tests/inputs/class.moon b/tests/inputs/class.moon index 92f6e6a..1677468 100644 --- a/tests/inputs/class.moon +++ b/tests/inputs/class.moon @@ -129,4 +129,7 @@ class Whacko print "hello" +yyy = -> + class Cool + nil diff --git a/tests/outputs/class.lua b/tests/outputs/class.lua index 5af1404..2f32e38 100644 --- a/tests/outputs/class.lua +++ b/tests/outputs/class.lua @@ -589,4 +589,48 @@ Whacko = (function() end return _class_0 end)() -return print("hello") \ No newline at end of file +print("hello") +local yyy +yyy = function() + local Cool + Cool = (function() + local _parent_0 = nil + local _base_0 = { } + _base_0.__index = _base_0 + if _parent_0 then + setmetatable(_base_0, _parent_0.__base) + end + local _class_0 = setmetatable({ + __init = function(self, ...) + if _parent_0 then + return _parent_0.__init(self, ...) + end + end, + __base = _base_0, + __name = "Cool", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil and _parent_0 then + return _parent_0[name] + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + _ = nil + if _parent_0 and _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + return _class_0 + end)() + return Cool +end \ No newline at end of file