Add forward declarations

This commit is contained in:
Sofia 2026-04-11 21:34:44 +03:00
parent 07c0059c5c
commit 7cf752f67b
5 changed files with 43 additions and 31 deletions

View File

@ -85,12 +85,13 @@ namespace AST {
out << ") -> ";
out << this->m_return_ty->formatted();
if (this->m_statements) {
out << " {\n";
for (auto& statement : this->m_statements) {
for (auto& statement : *this->m_statements) {
out << " " << statement->formatted() << "\n";
}
out << "}";
}
return out.str();
}
}

View File

@ -167,14 +167,14 @@ namespace AST {
std::unique_ptr<types::Type> m_return_ty;
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> m_params;
std::string m_name;
std::vector<std::unique_ptr<Statement>> m_statements;
std::optional<std::vector<std::unique_ptr<Statement>>> m_statements;
public:
Function(
token::Metadata meta,
std::unique_ptr<types::Type> return_ty,
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> params,
std::string name,
std::vector<std::unique_ptr<Statement>> statements)
std::optional<std::vector<std::unique_ptr<Statement>>> statements)
: TopLevelStatement{ meta }
, m_return_ty{ std::move(return_ty) }
, m_params{ std::move(params) }

View File

@ -187,6 +187,7 @@ namespace AST {
scope.values[this->m_name] = codegen::StackValue{ function, fn_ty_ptr };
if (this->m_statements) {
auto BB = llvm::BasicBlock::Create(*builder.context, "entry", function, nullptr);
builder.block = BB;
@ -203,9 +204,10 @@ namespace AST {
};
}
for (auto& statement : this->m_statements) {
for (auto& statement : *this->m_statements) {
statement->codegen(builder, inner_scope);
}
}
llvm::verifyFunction(*function);

View File

@ -252,18 +252,25 @@ namespace parsing {
}
inner.expect(token::Type::Symbol, ")");
std::optional<std::vector<std::unique_ptr<AST::Statement>>> statements{};
if (inner.peek().content == "{") {
inner.expect(token::Type::Symbol, "{");
std::vector<std::unique_ptr<AST::Statement>> statements{};
std::vector<std::unique_ptr<AST::Statement>> statement_list{};
auto statement = parse_statement(inner);
while (statement.ok()) {
statements.push_back(statement.unwrap());
statement_list.push_back(statement.unwrap());
statement = parse_statement(inner);
}
statements = std::optional{ std::move(statement_list) };
inner.expect(token::Type::Symbol, "}");
}
else {
inner.expect(token::Type::Symbol, ";");
}
stream.m_position = inner.m_position;

2
test.c
View File

@ -1,3 +1,5 @@
void printf(int b);
int fibonacci(int n) {
if (n < 2)
return 1;