support return statement with no arguments

This commit is contained in:
leaf corcoran 2011-09-30 23:23:22 -07:00
parent ad7c91ad53
commit 6fae81994c
6 changed files with 32 additions and 16 deletions

View File

@ -130,7 +130,11 @@ line_compile = {
}) })
end, end,
["return"] = function(self, node) ["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, end,
["break"] = function(self, node) ["break"] = function(self, node)
return "break" return "break"

View File

@ -64,7 +64,7 @@ line_compile =
@stm {"assign", {name}, {{"exp", name, op_final, exp}}} @stm {"assign", {name}, {{"exp", name, op_final, exp}}}
return: (node) => return: (node) =>
@line "return ", @value node[2] @line "return ", if node[2] != "" then @value node[2]
break: (node) => break: (node) =>
"break" "break"

View File

@ -291,7 +291,7 @@ local build_grammar = wrap(function()
BreakLoop = Ct(key"break"/trim), 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", With = key"with" * Exp * key"do"^-1 * Body / mark"with",

View File

@ -52,3 +52,10 @@ something = (hello=100, world=(x=[[yeah cool]])-> print "eat rice") ->
(@x=1,y,@z="hello world") => (@x=1,y,@z="hello world") =>
x -> return
y -> return 1
z -> return 1, "hello", "world"
k -> if yes then return else return

View File

@ -94,3 +94,19 @@ _ = function(self, x, y, z)
end end
self.x, self.z = x, z self.x, self.z = x, z
end 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)

13
todo
View File

@ -3,20 +3,9 @@
if hello else world if hello else world
-> if hello then 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: - 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 - allow a return without an argument
* maybe with assigns only
my_func if something
"one arg"
else
"other arg"
- class expressions, x = class extends Hello do new: => print "hello" - class expressions, x = class extends Hello do new: => print "hello"