diff --git a/source/toy_compiler.c b/source/toy_compiler.c index af6079a..ed61be4 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -1464,7 +1464,7 @@ static void writeBytecodeBody(Toy_Bytecode* mb, Toy_Ast* ast) { writeBytecodeFromAst(&mb, ast); //append an extra return if needed - if (mb->codeCount <= 4 || mb->code[mb->codeCount - 4] != TOY_OPCODE_RETURN) { //if empty or no return statement + if (mb->codeCount < 4 || mb->code[mb->codeCount - 4] != TOY_OPCODE_RETURN) { //if empty or no return statement EMIT_BYTE(&mb, code, TOY_OPCODE_RETURN); //end terminator EMIT_BYTE(&mb, code, 0); //4-byte alignment EMIT_BYTE(&mb, code, 0); diff --git a/tests/scripts/test_keyword_return.toy b/tests/scripts/test_keyword_return.toy index 2b1614a..ad35fa2 100644 --- a/tests/scripts/test_keyword_return.toy +++ b/tests/scripts/test_keyword_return.toy @@ -1,8 +1,12 @@ -fn empty() { //BUG: there's an extra return in the bytecode +fn empty() { + // +} + +fn almostEmpty() { return; } -fn full() { +fn notEmpty() { return 42; }