From db52c136137e693dfba99c9ef33c0cd8d3e2a30b Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Tue, 22 Aug 2023 01:11:32 +1000 Subject: [PATCH] Removed extra scope around for loop body blocks, resolved #107 --- scripts/test.toy | 11 +---------- source/toy_common.h | 2 +- source/toy_compiler.c | 18 +++++++++++++----- tools/disassembler/makefile | 4 ++-- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/scripts/test.toy b/scripts/test.toy index 762cfff..43f28d1 100644 --- a/scripts/test.toy +++ b/scripts/test.toy @@ -1,12 +1,3 @@ -import standard; -var array = [42]; - -var result = null; - -//problematic line -result = max(0, array[0]); - -assert result == 42, "Indexing in argument list failed"; -print "All good"; +for (var i: int = 0; i < 10; i++) print i; \ No newline at end of file diff --git a/source/toy_common.h b/source/toy_common.h index e67704c..7b01fc0 100644 --- a/source/toy_common.h +++ b/source/toy_common.h @@ -64,7 +64,7 @@ The current patch version of Toy. This value is embedded into the bytecode. This value MUST fit into an unsigned char. !*/ -#define TOY_VERSION_PATCH 0 +#define TOY_VERSION_PATCH 1 /*! ### TOY_VERSION_BUILD diff --git a/source/toy_compiler.c b/source/toy_compiler.c index 7797f10..6a08fd6 100644 --- a/source/toy_compiler.c +++ b/source/toy_compiler.c @@ -841,12 +841,20 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode compiler->count += sizeof(unsigned short); //2 bytes //write the body - compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_BEGIN; //1 byte + bool closeScope = false; + if (node->pathFor.thenPath->type != TOY_AST_NODE_BLOCK) { + compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_BEGIN; //1 byte + closeScope = true; + } + override = Toy_writeCompilerWithJumps(compiler, node->pathFor.thenPath, &breakAddresses, &continueAddresses, jumpOffsets, rootNode); if (override != TOY_OP_EOF) {//compensate for indexing & dot notation being screwy compiler->bytecode[compiler->count++] = (unsigned char)override; //1 byte } - compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_END; //1 byte + + if (closeScope) { + compiler->bytecode[compiler->count++] = TOY_OP_SCOPE_END; //1 byte + } //for-breaks actually jump to the bottom int jumpToIncrement = compiler->count; @@ -857,6 +865,9 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode compiler->bytecode[compiler->count++] = (unsigned char)override; //1 byte } + //BUGFIX: clear the stack after each loop + compiler->bytecode[compiler->count++] = TOY_OP_POP_STACK; //1 byte + compiler->bytecode[compiler->count++] = TOY_OP_JUMP; //1 byte unsigned short tmpVal = jumpToStart + jumpOffsets; memcpy(compiler->bytecode + compiler->count, &tmpVal, sizeof(tmpVal)); @@ -880,9 +891,6 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode memcpy(compiler->bytecode + point, &tmpVal, sizeof(tmpVal)); } - //clear the stack after use - compiler->bytecode[compiler->count++] = TOY_OP_POP_STACK; //1 byte - //cleanup Toy_freeLiteralArray(&breakAddresses); Toy_freeLiteralArray(&continueAddresses); diff --git a/tools/disassembler/makefile b/tools/disassembler/makefile index 06e6b95..bc68d7a 100644 --- a/tools/disassembler/makefile +++ b/tools/disassembler/makefile @@ -7,8 +7,8 @@ LIBS+= ODIR = obj SRC = $(wildcard *.c) OBJ = $(addprefix $(ODIR)/,$(SRC:.c=.o)) -OUTNAME= -OUT=disassembler +OUTDIR=../../out +OUT=$(OUTDIR)/disassembler all: $(OBJ) $(CC) $(CFLAGS) -o $(OUT) $(OBJ) $(LIBS)