Removed extra scope around for loop body blocks, resolved #107

This commit is contained in:
2023-08-22 01:11:32 +10:00
parent 7290efe069
commit db52c13613
4 changed files with 17 additions and 18 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -841,12 +841,20 @@ static Toy_Opcode Toy_writeCompilerWithJumps(Toy_Compiler* compiler, Toy_ASTNode
compiler->count += sizeof(unsigned short); //2 bytes
//write the body
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
}
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);

View File

@@ -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)