Add AST for compound statement, break and continue

This commit is contained in:
Sofia 2026-04-30 19:50:27 +03:00
parent 745447265e
commit a90259eccc
6 changed files with 121 additions and 1 deletions

View File

@ -173,6 +173,24 @@ namespace AST {
return out.str(); return out.str();
} }
std::string CompoundStatement::formatted() {
std::stringstream out{ "" };
out << "{\n";
for (auto& statement : this->m_statements) {
out << " " << statement->formatted() << "\n";
}
out << "}";
return out.str();
}
std::string BreakStatement::formatted() {
return "break;";
}
std::string ContinueStatement::formatted() {
return "continue;";
}
std::string InitializationStatement::formatted() { std::string InitializationStatement::formatted() {
std::stringstream out{ "" }; std::stringstream out{ "" };
out << this->m_type->formatted() << " " << this->m_name; out << this->m_type->formatted() << " " << this->m_name;

View File

@ -473,6 +473,52 @@ namespace AST {
virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override; virtual void typecheck(typecheck::State& state, typecheck::Scope& scope) override;
}; };
/** @brief a block e.g. {statement; statement;} */
class CompoundStatement : public Statement {
private:
std::vector<std::unique_ptr<Statement>> m_statements;
public:
CompoundStatement(token::Metadata meta,
std::vector<std::unique_ptr<Statement>> statements)
: Statement{ meta }
, m_statements{ std::move(statements) } {
}
virtual ~CompoundStatement() 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;
};
/** @brief break-keyword */
class BreakStatement : public Statement {
public:
BreakStatement(token::Metadata meta)
: Statement{ meta } {
}
virtual ~BreakStatement() 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;
};
/** @brief continue-keyword */
class ContinueStatement : public Statement {
public:
ContinueStatement(token::Metadata meta)
: Statement{ meta } {
}
virtual ~ContinueStatement() 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 { class TopLevelStatement : public Node {
public: public:
TopLevelStatement(token::Metadata meta) : Node{ meta } {} TopLevelStatement(token::Metadata meta) : Node{ meta } {}

View File

@ -630,6 +630,32 @@ namespace AST {
builder.block = after_bb; builder.block = after_bb;
} }
void CompoundStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
if (!builder.block)
return;
builder.builder->SetInsertPoint(builder.block);
throw CompileError("TODO", this->m_meta);
}
void BreakStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
if (!builder.block)
return;
builder.builder->SetInsertPoint(builder.block);
throw CompileError("TODO", this->m_meta);
}
void ContinueStatement::codegen(codegen::Builder& builder, codegen::Scope&, codegen::StackAllocator&) {
if (!builder.block)
return;
builder.builder->SetInsertPoint(builder.block);
throw CompileError("TODO", this->m_meta);
}
void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) { void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) {
std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty }; std::shared_ptr<types::Type> ret_ty_ptr{ this->m_return_ty };

View File

@ -109,4 +109,13 @@ namespace AST {
(*this->m_cond)->codegen_alloca(allocator); (*this->m_cond)->codegen_alloca(allocator);
this->m_loop->codegen_alloca(allocator); this->m_loop->codegen_alloca(allocator);
} }
void CompoundStatement::codegen_alloca(codegen::StackAllocator& allocator) {
for (auto& statement : this->m_statements)
statement->codegen_alloca(allocator);
}
void BreakStatement::codegen_alloca(codegen::StackAllocator&) {}
void ContinueStatement::codegen_alloca(codegen::StackAllocator&) {}
} }

View File

@ -685,6 +685,26 @@ namespace AST {
this->m_loop->typecheck(state, inner_scope); this->m_loop->typecheck(state, inner_scope);
} }
void CompoundStatement::typecheck_preprocess(typecheck::Scope& scope) {
typecheck::Scope inner{ scope };
for (auto& statement : this->m_statements) {
statement->typecheck_preprocess(inner);
}
}
void CompoundStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
typecheck::Scope inner{ scope };
for (auto& statement : this->m_statements) {
statement->typecheck(state, inner);
}
}
void BreakStatement::typecheck_preprocess(typecheck::Scope&) {}
void BreakStatement::typecheck(typecheck::State&, typecheck::Scope&) {}
void ContinueStatement::typecheck_preprocess(typecheck::Scope&) {}
void ContinueStatement::typecheck(typecheck::State&, typecheck::Scope&) {}
void Function::typecheck_preprocess(typecheck::Scope& scope) { void Function::typecheck_preprocess(typecheck::Scope& scope) {
this->m_return_ty = refresh_type(scope, this->m_return_ty); this->m_return_ty = refresh_type(scope, this->m_return_ty);
for (auto& param : this->m_params) { for (auto& param : this->m_params) {

3
test.c
View File

@ -62,8 +62,9 @@ int main() {
// Test while-loops // Test while-loops
int counter = 0; int counter = 0;
while (counter < 10) while (counter < 10) {
printf("while-counter: %d\n", counter++); printf("while-counter: %d\n", counter++);
}
return 0; return 0;
} }