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();
out << " {\n";
for (auto& statement : this->m_statements) {
out << " " << statement->formatted() << "\n";
if (this->m_statements) {
out << " {\n";
for (auto& statement : *this->m_statements) {
out << " " << statement->formatted() << "\n";
}
out << "}";
}
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,24 +187,26 @@ namespace AST {
scope.values[this->m_name] = codegen::StackValue{ function, fn_ty_ptr };
auto BB = llvm::BasicBlock::Create(*builder.context, "entry", function, nullptr);
builder.block = BB;
if (this->m_statements) {
auto BB = llvm::BasicBlock::Create(*builder.context, "entry", function, nullptr);
builder.block = BB;
codegen::Scope inner_scope{ scope };
codegen::Scope inner_scope{ scope };
int counter = 0;
for (auto& param : this->m_params) {
auto param_ty_ptr = param_ty_ptrs[counter];
auto arg = function->getArg(counter++);
arg->setName(param.first);
inner_scope.values[param.first] = codegen::StackValue{
arg,
param_ty_ptr,
};
}
int counter = 0;
for (auto& param : this->m_params) {
auto param_ty_ptr = param_ty_ptrs[counter];
auto arg = function->getArg(counter++);
arg->setName(param.first);
inner_scope.values[param.first] = codegen::StackValue{
arg,
param_ty_ptr,
};
}
for (auto& statement : this->m_statements) {
statement->codegen(builder, inner_scope);
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, ")");
inner.expect(token::Type::Symbol, "{");
std::vector<std::unique_ptr<AST::Statement>> statements{};
std::optional<std::vector<std::unique_ptr<AST::Statement>>> statements{};
if (inner.peek().content == "{") {
inner.expect(token::Type::Symbol, "{");
auto statement = parse_statement(inner);
while (statement.ok()) {
statements.push_back(statement.unwrap());
statement = parse_statement(inner);
std::vector<std::unique_ptr<AST::Statement>> statement_list{};
auto statement = parse_statement(inner);
while (statement.ok()) {
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, ";");
}
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;