From c8fe27d6c49e46351df572b0902926a19dc9eb07 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sun, 29 Dec 2013 10:46:21 -0800 Subject: [PATCH] Allow empty blocks. --- src/wren_compiler.c | 3 +++ src/wren_vm.c | 4 ++-- test/empty_block.wren | 7 +++++++ test/function/empty_block.wren | 2 ++ test/method/empty_block.wren | 5 +++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/empty_block.wren create mode 100644 test/function/empty_block.wren create mode 100644 test/method/empty_block.wren diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 6085fd02..47bbeb6f 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -1192,6 +1192,9 @@ static void patchJump(Compiler* compiler, int offset) // Parses a block body, after the initial "{" has been consumed. static void finishBlock(Compiler* compiler) { + // Empty blocks do nothing. + if (match(compiler, TOKEN_RIGHT_BRACE)) return; + for (;;) { definition(compiler); diff --git a/src/wren_vm.c b/src/wren_vm.c index a2e4d128..e101c19e 100644 --- a/src/wren_vm.c +++ b/src/wren_vm.c @@ -1288,9 +1288,9 @@ void wrenDefineMethod(WrenVM* vm, const char* className, double wrenGetArgumentDouble(WrenVM* vm, int index) { - ASSERT(vm->nativeCallSlot != NULL, "Must be in foreign call."); + ASSERT(vm->foreignCallSlot != NULL, "Must be in foreign call."); ASSERT(index >= 0, "index cannot be negative."); - ASSERT(index < vm->nativeCallNumArgs, "Not that many arguments."); + ASSERT(index < vm->foreignCallNumArgs, "Not that many arguments."); // + 1 to shift past the receiver. // TODO: Check actual value type first. diff --git a/test/empty_block.wren b/test/empty_block.wren new file mode 100644 index 00000000..71591ff7 --- /dev/null +++ b/test/empty_block.wren @@ -0,0 +1,7 @@ +{} // By itself. + +// In a statement. +if (true) {} +if (false) {} else {} + +IO.write("ok") // expect: ok \ No newline at end of file diff --git a/test/function/empty_block.wren b/test/function/empty_block.wren new file mode 100644 index 00000000..632e7ba2 --- /dev/null +++ b/test/function/empty_block.wren @@ -0,0 +1,2 @@ +var f = fn {} +IO.write(f.call) // expect: null diff --git a/test/method/empty_block.wren b/test/method/empty_block.wren new file mode 100644 index 00000000..da578d24 --- /dev/null +++ b/test/method/empty_block.wren @@ -0,0 +1,5 @@ +class Foo { + bar {} +} + +IO.write((new Foo).bar) // expect: null