Compare commits

...

4 Commits

Author SHA1 Message Date
0fb63ee703 Update test.c 2026-04-28 16:19:53 +03:00
52614a7f01 Codegen while-loops 2026-04-28 16:19:17 +03:00
4cf5c9791a Parse while-statements 2026-04-28 16:13:38 +03:00
9817d9b8b6 Fix bug with typecheckign for-loops 2026-04-28 01:47:49 +03:00
9 changed files with 136 additions and 7 deletions

View File

@ -154,6 +154,17 @@ namespace AST {
return out.str();
}
std::string WhileStatement::formatted() {
std::stringstream out{ "" };
out << "while (";
if (this->m_cond)
out << (*this->m_cond)->formatted();
out << ")";
out << "\n then " << this->m_loop->formatted();
return out.str();
}
std::string ReturnStatement::formatted() {
std::stringstream out{ "" };
out << "return ";

View File

@ -453,6 +453,26 @@ namespace AST {
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class WhileStatement : public Statement {
private:
std::optional<std::unique_ptr<Expression>> m_cond;
std::unique_ptr<Statement> m_loop;
public:
WhileStatement(token::Metadata meta,
std::optional<std::unique_ptr<Expression>> cond,
std::unique_ptr<Statement> loop)
: Statement{ meta }
, m_cond{ std::move(cond) }
, m_loop{ std::move(loop) } {
}
virtual ~WhileStatement() override = default;
virtual std::string formatted() override;
virtual void codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) override;
virtual void codegen_alloca(codegen::StackAllocator& allocator) override;
virtual void typecheck_preprocess(typecheck::Scope& scope) override;
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
};
class TopLevelStatement : public Node {
public:
TopLevelStatement(token::Metadata meta) : Node{ meta } {}

View File

@ -596,6 +596,40 @@ namespace AST {
builder.builder->SetInsertPoint(after_bb);
}
void WhileStatement::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) {
if (!builder.block)
return;
builder.builder->SetInsertPoint(builder.block);
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;
}
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };

View File

@ -598,6 +598,30 @@ namespace parsing {
};
return std::pair{ std::unique_ptr<AST::Statement>{ statement }, false };
}
else if (inner.peek().type == token::Type::WhileKeyword) {
inner.next();
inner.expect(token::Type::Symbol, "(");
std::optional<std::unique_ptr<AST::Expression>> cond;
if (inner.peek().content != ")")
cond = parse_expression(inner, scope).unwrap();
inner.expect(token::Type::Symbol, ")");
auto loop_res = parse_statement(inner, scope).unwrap();
if (loop_res.second)
inner.expect(token::Type::Symbol, ";");
auto loop = std::move(loop_res.first);
stream.m_position = inner.m_position;
auto statement = new AST::WhileStatement{
before_meta + stream.metadata(),
std::move(cond),
std::move(loop),
};
return std::pair{ std::unique_ptr<AST::Statement>{ statement }, false };
}
else if (auto init = parse_init_statement(inner, scope); init.ok()) {
stream.m_position = inner.m_position;
return std::pair{ std::unique_ptr<AST::Statement>{ init.unwrap() }, true };

View File

@ -103,4 +103,10 @@ namespace AST {
(*this->m_after)->codegen_alloca(allocator);
this->m_loop->codegen_alloca(allocator);
}
void WhileStatement::codegen_alloca(codegen::StackAllocator& allocator) {
if (this->m_cond)
(*this->m_cond)->codegen_alloca(allocator);
this->m_loop->codegen_alloca(allocator);
}
}

View File

@ -61,6 +61,8 @@ namespace token {
return "Else";
case token::Type::ForKeyword:
return "For";
case token::Type::WhileKeyword:
return "While";
case token::Type::Whitespace:
return "Whitespace";
@ -227,6 +229,9 @@ namespace token {
else if (content == "for") {
type = token::Type::ForKeyword;
}
else if (content == "while") {
type = token::Type::WhileKeyword;
}
tokens.push_back(token::Token{ type, content, meta + content.size() });
}
else if (iswhitespace(c)) {

View File

@ -17,6 +17,7 @@ namespace token {
IfKeyword,
ElseKeyword,
ForKeyword,
WhileKeyword,
Whitespace,

View File

@ -645,20 +645,44 @@ namespace AST {
auto bool_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
typecheck::Scope inner_scope{ scope };
if (this->m_init)
(*this->m_init)->typecheck(state, scope);
(*this->m_init)->typecheck(state, inner_scope);
if (this->m_cond) {
auto cond_ty = (*this->m_cond)->typecheck(state, scope, bool_ty).type;
auto cond_ty = (*this->m_cond)->typecheck(state, inner_scope, bool_ty).type;
auto check_res = check_type(state, cond_ty, bool_ty);
this->m_cond = handle_res(std::move(*this->m_cond), check_res, state);
}
if (this->m_after)
(*this->m_after)->typecheck(state, scope, {});
(*this->m_after)->typecheck(state, inner_scope, {});
this->m_loop->typecheck(state, scope);
this->m_loop->typecheck(state, inner_scope);
}
void WhileStatement::typecheck_preprocess(typecheck::Scope& scope) {
if (this->m_cond)
(*this->m_cond)->typecheck_preprocess(scope);
this->m_loop->typecheck_preprocess(scope);
}
void WhileStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
auto bool_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ false, types::FundamentalTypeKind::Bool } };
typecheck::Scope inner_scope{ scope };
if (this->m_cond) {
auto cond_ty = (*this->m_cond)->typecheck(state, inner_scope, bool_ty).type;
auto check_res = check_type(state, cond_ty, bool_ty);
this->m_cond = handle_res(std::move(*this->m_cond), check_res, state);
}
this->m_loop->typecheck(state, inner_scope);
}
void Function::typecheck_preprocess(typecheck::Scope& scope) {

6
test.c
View File

@ -52,7 +52,11 @@ int main() {
printf("2d array: %d!\n", twod_array[0][0]);
for (int counter = 0; counter < 10; counter++)
printf("counter: %d\n", counter);
printf("for-counter: %d\n", counter);
int counter = 0;
while (counter < 10)
printf("while-counter: %d\n", counter++);
return 0;
}