feat: allow empty blocks in parser and fix native call slot assertion
Add support for empty block bodies in the Wren compiler by early-returning
from `finishBlock` when a right brace is immediately matched. This permits
syntactically valid constructs like `{}`, `if (true) {}`, and empty function
or method bodies.
Fix a copy-paste error in `wrenGetArgumentDouble` where the assertion
checked `nativeCallSlot` and `nativeCallNumArgs` instead of the correct
`foreignCallSlot` and `foreignCallNumArgs` fields.
Add test cases covering standalone empty blocks, empty blocks in if/else
statements, and empty function and method bodies returning null.
This commit is contained in:
@@ -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);
|
||||
|
||||
+2
-2
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user