Add AST for compound statement, break and continue
This commit is contained in:
parent
745447265e
commit
a90259eccc
18
src/ast.cpp
18
src/ast.cpp
@ -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;
|
||||||
|
|||||||
46
src/ast.h
46
src/ast.h
@ -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 } {}
|
||||||
|
|||||||
@ -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 };
|
||||||
|
|||||||
@ -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&) {}
|
||||||
}
|
}
|
||||||
@ -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) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user