Codegen while-loops

This commit is contained in:
Sofia 2026-04-28 16:19:17 +03:00
parent 4cf5c9791a
commit 52614a7f01

View File

@ -596,13 +596,38 @@ namespace AST {
builder.builder->SetInsertPoint(after_bb); builder.builder->SetInsertPoint(after_bb);
} }
void WhileStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) { void WhileStatement::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) {
if (!builder.block) if (!builder.block)
return; return;
builder.builder->SetInsertPoint(builder.block); builder.builder->SetInsertPoint(builder.block);
throw CompileError("TODO", this->m_meta); auto function = builder.block->getParent();
auto cond_bb = llvm::BasicBlock::Create(*builder.context, "while-cond", function);
auto inner_bb = llvm::BasicBlock::Create(*builder.context, "while-inner", function);
auto after_bb = llvm::BasicBlock::Create(*builder.context, "while-after", function);
builder.builder->CreateBr(cond_bb);
// While condition
builder.builder->SetInsertPoint(cond_bb);
if (this->m_cond) {
auto cond_value = (*this->m_cond)->codegen(builder, scope, allocator);
builder.builder->CreateCondBr(cond_value.value, inner_bb, after_bb);
}
else {
builder.builder->CreateBr(inner_bb);
}
// While loop
builder.builder->SetInsertPoint(inner_bb);
builder.block = inner_bb;
this->m_loop->codegen(builder, scope, allocator);
builder.builder->CreateBr(cond_bb);
// After
builder.builder->SetInsertPoint(after_bb);
builder.block = after_bb;
} }