While is working but untested, read more

* TODO: break and continue keywords need to be implemented
* TODO: while-then needs testing
* Fixed the parser not liking zero-length strings
This commit is contained in:
2024-11-26 14:48:24 +11:00
parent 0947430c29
commit 6cc331d462
7 changed files with 139 additions and 25 deletions

View File

@@ -319,7 +319,7 @@ static unsigned int writeInstructionIfThenElse(Toy_Routine** rt, Toy_AstIfThenEl
unsigned int thenEndAddr = SKIP_INT(rt, code); //parameter to be written later
//emit then branch
//emit then-branch
writeRoutineCode(rt, ast.thenBranch);
if (ast.elseBranch != NULL) {
@@ -349,6 +349,37 @@ static unsigned int writeInstructionIfThenElse(Toy_Routine** rt, Toy_AstIfThenEl
return 0;
}
static unsigned int writeInstructionWhileThen(Toy_Routine** rt, Toy_AstWhileThen ast) {
//TODO: begin
unsigned int beginAddr = CURRENT_ADDRESS(rt, code);
//cond-branch
writeRoutineCode(rt, ast.condBranch);
//emit the jump word (opcode, type, condition, padding)
EMIT_BYTE(rt, code, TOY_OPCODE_JUMP);
EMIT_BYTE(rt, code, TOY_OP_PARAM_JUMP_RELATIVE);
EMIT_BYTE(rt, code, TOY_OP_PARAM_JUMP_IF_FALSE);
EMIT_BYTE(rt, code, 0);
unsigned int endAddr = SKIP_INT(rt, code); //parameter to be written later
//emit then-branch
writeRoutineCode(rt, ast.thenBranch);
//jump to begin to repeat the conditional test
EMIT_BYTE(rt, code, TOY_OPCODE_JUMP);
EMIT_BYTE(rt, code, TOY_OP_PARAM_JUMP_RELATIVE);
EMIT_BYTE(rt, code, TOY_OP_PARAM_JUMP_ALWAYS);
EMIT_BYTE(rt, code, 0);
EMIT_INT(rt, code, beginAddr - (CURRENT_ADDRESS(rt, code) + 4)); //this sets a negative value
OVERWRITE_INT(rt, code, endAddr, CURRENT_ADDRESS(rt, code) - (endAddr + 4));
return 0;
}
static unsigned int writeInstructionPrint(Toy_Routine** rt, Toy_AstPrint ast) {
//the thing to print
writeRoutineCode(rt, ast.child);
@@ -603,6 +634,10 @@ static unsigned int writeRoutineCode(Toy_Routine** rt, Toy_Ast* ast) {
result += writeInstructionIfThenElse(rt, ast->ifThenElse);
break;
case TOY_AST_WHILE_THEN:
result += writeInstructionWhileThen(rt, ast->whileThen);
break;
case TOY_AST_PRINT:
result += writeInstructionPrint(rt, ast->print);
break;