diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index a822772..1cf2a8f 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -130,7 +130,11 @@ line_compile = { }) end, ["return"] = function(self, node) - return self:line("return ", self:value(node[2])) + return self:line("return ", (function() + if node[2] ~= "" then + return self:value(node[2]) + end + end)()) end, ["break"] = function(self, node) return "break" diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index 2e59afc..8998d94 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -64,7 +64,7 @@ line_compile = @stm {"assign", {name}, {{"exp", name, op_final, exp}}} return: (node) => - @line "return ", @value node[2] + @line "return ", if node[2] != "" then @value node[2] break: (node) => "break" diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 41884f5..137b271 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -291,7 +291,7 @@ local build_grammar = wrap(function() BreakLoop = Ct(key"break"/trim), - Return = key"return" * (ExpListLow/ mark"explist") / mark"return", + Return = key"return" * (ExpListLow/mark"explist" + C"") / mark"return", With = key"with" * Exp * key"do"^-1 * Body / mark"with", diff --git a/tests/inputs/funcs.moon b/tests/inputs/funcs.moon index e274af3..3837e18 100644 --- a/tests/inputs/funcs.moon +++ b/tests/inputs/funcs.moon @@ -52,3 +52,10 @@ something = (hello=100, world=(x=[[yeah cool]])-> print "eat rice") -> (@x=1,y,@z="hello world") => +x -> return +y -> return 1 +z -> return 1, "hello", "world" +k -> if yes then return else return + + + diff --git a/tests/outputs/funcs.lua b/tests/outputs/funcs.lua index 37df2f2..f2322cc 100644 --- a/tests/outputs/funcs.lua +++ b/tests/outputs/funcs.lua @@ -93,4 +93,20 @@ _ = function(self, x, y, z) z = "hello world" end self.x, self.z = x, z -end \ No newline at end of file +end +x(function() + return +end) +y(function() + return 1 +end) +z(function() + return 1, "hello", "world" +end) +k(function() + if yes then + return + else + return + end +end) \ No newline at end of file diff --git a/todo b/todo index 4f75988..abe8440 100644 --- a/todo +++ b/todo @@ -3,20 +3,9 @@ if hello else world -> if hello then hello else world - -- enhance with documentation -- fix variable names starting with and, not, or, etc.. - - varargs that get put in a nested generated function aren't valid anymore: -- cascading expressions in function call? this looks too much like line decorator -* maybe with assigns only - - my_func if something - "one arg" - else - "other arg" - +- allow a return without an argument - class expressions, x = class extends Hello do new: => print "hello"