From 7cf752f67b1dde81d724a51e46e41656cc180564 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 11 Apr 2026 21:34:44 +0300 Subject: [PATCH] Add forward declarations --- src/ast.cpp | 11 ++++++----- src/ast.h | 4 ++-- src/codegen.cpp | 32 +++++++++++++++++--------------- src/parsing.cpp | 25 ++++++++++++++++--------- test.c | 2 ++ 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/ast.cpp b/src/ast.cpp index 194a02d..6c78787 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -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(); } } \ No newline at end of file diff --git a/src/ast.h b/src/ast.h index ad40435..b1081d9 100644 --- a/src/ast.h +++ b/src/ast.h @@ -167,14 +167,14 @@ namespace AST { std::unique_ptr m_return_ty; std::vector>> m_params; std::string m_name; - std::vector> m_statements; + std::optional>> m_statements; public: Function( token::Metadata meta, std::unique_ptr return_ty, std::vector>> params, std::string name, - std::vector> statements) + std::optional>> statements) : TopLevelStatement{ meta } , m_return_ty{ std::move(return_ty) } , m_params{ std::move(params) } diff --git a/src/codegen.cpp b/src/codegen.cpp index 8eadfad..1ba2c69 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -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); diff --git a/src/parsing.cpp b/src/parsing.cpp index e41fea9..f026de3 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -252,18 +252,25 @@ namespace parsing { } inner.expect(token::Type::Symbol, ")"); - inner.expect(token::Type::Symbol, "{"); - std::vector> statements{}; + std::optional>> 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> 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; diff --git a/test.c b/test.c index 7d5e563..106fdcb 100644 --- a/test.c +++ b/test.c @@ -1,3 +1,5 @@ +void printf(int b); + int fibonacci(int n) { if (n < 2) return 1;