Allow returns without an explicit value.

This commit is contained in:
Bob Nystrom
2014-01-12 19:37:11 -08:00
parent cf623844c4
commit 8e8bb4b9b9
5 changed files with 28 additions and 6 deletions

View File

@ -1828,7 +1828,6 @@ void namedSignature(Compiler* compiler, char* name, int* length)
if (match(compiler, TOKEN_EQ))
{
// It's a setter.
// TODO: Allow setters with parameters? Like: foo.bar(1, 2) = "blah"
name[(*length)++] = '=';
name[(*length)++] = ' ';
@ -2326,9 +2325,15 @@ void statement(Compiler* compiler)
if (match(compiler, TOKEN_RETURN))
{
// Compile the return value.
// TODO: Implicitly return null if there is a newline or } after the
// "return".
expression(compiler);
if (peek(compiler) == TOKEN_LINE || peek(compiler) == TOKEN_RIGHT_BRACE)
{
// Implicitly return null if there is no value.
emit(compiler, CODE_NULL);
}
else
{
expression(compiler);
}
emit(compiler, CODE_RETURN);
return;

View File

@ -185,7 +185,7 @@ DEF_NATIVE(fiber_create)
ObjFiber* newFiber = wrenNewFiber(vm, AS_OBJ(args[1]));
// The compiler expect the first slot of a function to hold the receiver.
// The compiler expects the first slot of a function to hold the receiver.
// Since a fiber's stack is invoked directly, it doesn't have one, so put it
// in here.
// TODO: Is there a cleaner solution?

View File

@ -1 +1,6 @@
var a = "ok"; IO.print(a) // expect: ok
var f = fn {
return "ok"
IO.print("bad")
}
IO.print(f.call) // expect: ok

View File

@ -0,0 +1,6 @@
var f = fn {
if (true) { return }
IO.print("bad")
}
IO.print(f.call) // expect: null

View File

@ -0,0 +1,6 @@
var f = fn {
return
IO.print("bad")
}
IO.print(f.call) // expect: null