mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Allow returns without an explicit value.
This commit is contained in:
@ -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;
|
||||
|
||||
@ -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?
|
||||
|
||||
@ -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
|
||||
|
||||
6
test/return/return_null_if_brace.wren
Normal file
6
test/return/return_null_if_brace.wren
Normal file
@ -0,0 +1,6 @@
|
||||
var f = fn {
|
||||
if (true) { return }
|
||||
IO.print("bad")
|
||||
}
|
||||
|
||||
IO.print(f.call) // expect: null
|
||||
6
test/return/return_null_if_newline.wren
Normal file
6
test/return/return_null_if_newline.wren
Normal file
@ -0,0 +1,6 @@
|
||||
var f = fn {
|
||||
return
|
||||
IO.print("bad")
|
||||
}
|
||||
|
||||
IO.print(f.call) // expect: null
|
||||
Reference in New Issue
Block a user