WIP: Started on break & continue, needs Toy_Array in Toy_Value first

This commit is contained in:
2024-11-28 13:43:26 +11:00
parent 6cc331d462
commit 431893bf60
6 changed files with 99 additions and 2 deletions

View File

@@ -126,6 +126,22 @@ void Toy_private_emitAstWhileThen(Toy_Bucket** bucketHandle, Toy_Ast** astHandle
(*astHandle) = tmp;
}
void Toy_private_emitAstBreak(Toy_Bucket** bucketHandle, Toy_Ast** astHandle) {
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Ast));
tmp->type = TOY_AST_BREAK;
(*astHandle) = tmp;
}
void Toy_private_emitAstContinue(Toy_Bucket** bucketHandle, Toy_Ast** astHandle) {
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Ast));
tmp->type = TOY_AST_CONTINUE;
(*astHandle) = tmp;
}
void Toy_private_emitAstPrint(Toy_Bucket** bucketHandle, Toy_Ast** astHandle) {
Toy_Ast* tmp = (Toy_Ast*)Toy_partitionBucket(bucketHandle, sizeof(Toy_Ast));

View File

@@ -20,6 +20,8 @@ typedef enum Toy_AstType {
TOY_AST_ASSERT,
TOY_AST_IF_THEN_ELSE,
TOY_AST_WHILE_THEN,
TOY_AST_BREAK,
TOY_AST_CONTINUE,
TOY_AST_PRINT,
TOY_AST_VAR_DECLARE,
@@ -138,6 +140,14 @@ typedef struct Toy_AstWhileThen {
Toy_Ast* thenBranch;
} Toy_AstWhileThen;
typedef struct Toy_AstBreak {
Toy_AstType type;
} Toy_AstBreak;
typedef struct Toy_AstContinue {
Toy_AstType type;
} Toy_AstContinue;
typedef struct Toy_AstPrint {
Toy_AstType type;
Toy_Ast* child;
@@ -185,6 +195,8 @@ union Toy_Ast { //32 | 64 BITNESS
Toy_AstAssert assert; //16 | 24
Toy_AstIfThenElse ifThenElse; //16 | 32
Toy_AstWhileThen whileThen; //16 | 24
Toy_AstBreak breakPoint; //4 | 4
Toy_AstContinue continuePoint; //4 | 4
Toy_AstPrint print; //8 | 16
Toy_AstVarDeclare varDeclare; //16 | 24
Toy_AstVarAssign varAssign; //16 | 24
@@ -207,6 +219,8 @@ void Toy_private_emitAstCompound(Toy_Bucket** bucketHandle, Toy_Ast** astHandle,
void Toy_private_emitAstAssert(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, Toy_Ast* child, Toy_Ast* msg);
void Toy_private_emitAstIfThenElse(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, Toy_Ast* condBranch, Toy_Ast* thenBranch, Toy_Ast* elseBranch);
void Toy_private_emitAstWhileThen(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, Toy_Ast* condBranch, Toy_Ast* thenBranch);
void Toy_private_emitAstBreak(Toy_Bucket** bucketHandle, Toy_Ast** rootHandle);
void Toy_private_emitAstContinue(Toy_Bucket** bucketHandle, Toy_Ast** rootHandle);
void Toy_private_emitAstPrint(Toy_Bucket** bucketHandle, Toy_Ast** astHandle);
void Toy_private_emitAstVariableDeclaration(Toy_Bucket** bucketHandle, Toy_Ast** astHandle, Toy_String* name, Toy_Ast* expr);

View File

@@ -716,6 +716,16 @@ static void makeWhileStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast
Toy_private_emitAstWhileThen(bucketHandle, rootHandle, condBranch, thenBranch);
}
static void makeBreakStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
Toy_private_emitAstBreak(bucketHandle, rootHandle);
consume(parser, TOY_TOKEN_OPERATOR_SEMICOLON, "Expected ';' at the end of break statement");
}
static void makeContinueStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
Toy_private_emitAstContinue(bucketHandle, rootHandle);
consume(parser, TOY_TOKEN_OPERATOR_SEMICOLON, "Expected ';' at the end of continue statement");
}
static void makePrintStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** rootHandle) {
makeExpr(bucketHandle, parser, rootHandle);
Toy_private_emitAstPrint(bucketHandle, rootHandle);
@@ -788,8 +798,6 @@ static void makeStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** ro
return;
}
//TODO: break & continue
//while-then
else if (match(parser, TOY_TOKEN_KEYWORD_WHILE)) {
makeWhileStmt(bucketHandle, parser, rootHandle);
@@ -800,6 +808,18 @@ static void makeStmt(Toy_Bucket** bucketHandle, Toy_Parser* parser, Toy_Ast** ro
//return
//import
//break
else if (match(parser, TOY_TOKEN_KEYWORD_BREAK)) {
makeBreakStmt(bucketHandle, parser, rootHandle);
return;
}
//continue
else if (match(parser, TOY_TOKEN_KEYWORD_CONTINUE)) {
makeContinueStmt(bucketHandle, parser, rootHandle);
return;
}
//print
else if (match(parser, TOY_TOKEN_KEYWORD_PRINT)) {
makePrintStmt(bucketHandle, parser, rootHandle);

View File

@@ -380,6 +380,22 @@ static unsigned int writeInstructionWhileThen(Toy_Routine** rt, Toy_AstWhileThen
return 0;
}
static unsigned int writeInstructionBreak(Toy_Routine** rt, Toy_AstBreak ast) {
//TODO: implement break
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Keyword 'break' not yet implemented\n" TOY_CC_RESET);
(*rt)->panic = true;
return 0;
}
static unsigned int writeInstructionContinue(Toy_Routine** rt, Toy_AstContinue ast) {
//TODO: implement continue
fprintf(stderr, TOY_CC_ERROR "COMPILER ERROR: Keyword 'continue' not yet implemented\n" TOY_CC_RESET);
(*rt)->panic = true;
return 0;
}
static unsigned int writeInstructionPrint(Toy_Routine** rt, Toy_AstPrint ast) {
//the thing to print
writeRoutineCode(rt, ast.child);
@@ -638,6 +654,14 @@ static unsigned int writeRoutineCode(Toy_Routine** rt, Toy_Ast* ast) {
result += writeInstructionWhileThen(rt, ast->whileThen);
break;
case TOY_AST_BREAK:
result += writeInstructionBreak(rt, ast->breakPoint);
break;
case TOY_AST_CONTINUE:
result += writeInstructionContinue(rt, ast->continuePoint);
break;
case TOY_AST_PRINT:
result += writeInstructionPrint(rt, ast->print);
break;