add else blocks

pull/1/head
cosmonaut 2021-04-29 13:59:00 -07:00
parent 7ca87d6b13
commit 708d4de0ae
1 changed files with 37 additions and 1 deletions

View File

@ -642,9 +642,41 @@ static void CompileIfStatement(LLVMBuilderRef builder, LLVMValueRef function, No
for (i = 0; i < ifStatement->children[1]->childCount; i += 1) for (i = 0; i < ifStatement->children[1]->childCount; i += 1)
{ {
CompileStatement(builder, function, ifStatement->children[1]->children[i]); CompileStatement(builder, function, ifStatement->children[1]->children[i]);
LLVMBuildBr(builder, afterCond);
} }
LLVMBuildBr(builder, afterCond);
LLVMPositionBuilderAtEnd(builder, afterCond);
}
static void CompileIfElseStatement(LLVMBuilderRef builder, LLVMValueRef function, Node *ifElseStatement)
{
uint32_t i;
LLVMValueRef conditional = CompileExpression(builder, ifElseStatement->children[0]->children[0]);
LLVMBasicBlockRef ifBlock = LLVMAppendBasicBlock(function, "ifBlock");
LLVMBasicBlockRef elseBlock = LLVMAppendBasicBlock(function, "elseBlock");
LLVMBasicBlockRef afterCond = LLVMAppendBasicBlock(function, "afterCond");
LLVMBuildCondBr(builder, conditional, ifBlock, elseBlock);
LLVMPositionBuilderAtEnd(builder, ifBlock);
for (i = 0; i < ifElseStatement->children[0]->children[1]->childCount; i += 1)
{
CompileStatement(builder, function, ifElseStatement->children[0]->children[1]->children[i]);
}
LLVMBuildBr(builder, afterCond);
LLVMPositionBuilderAtEnd(builder, elseBlock);
for (i = 0; i < ifElseStatement->children[1]->childCount; i += 1)
{
CompileStatement(builder, function, ifElseStatement->children[1]->children[i]);
}
LLVMBuildBr(builder, afterCond);
LLVMPositionBuilderAtEnd(builder, afterCond); LLVMPositionBuilderAtEnd(builder, afterCond);
} }
@ -668,6 +700,10 @@ static uint8_t CompileStatement(LLVMBuilderRef builder, LLVMValueRef function, N
CompileIfStatement(builder, function, statement); CompileIfStatement(builder, function, statement);
return 0; return 0;
case IfElseStatement:
CompileIfElseStatement(builder, function, statement);
return 0;
case Return: case Return:
CompileReturn(builder, statement); CompileReturn(builder, statement);
return 1; return 1;