diff --git a/src/ast.cpp b/src/ast.cpp index 0ee4ee1..dd18fe1 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -49,7 +49,16 @@ namespace AST { std::string Function::formatted() { std::stringstream out{ "" }; out << this->m_name; - out << "() -> "; + out << "("; + + int counter = 0; + for (auto& param : this->m_params) { + if (counter++ > 0) + out << ", "; + out << param.second->formatted() << " " << param.first; + } + + out << ") -> "; out << this->m_return_ty->formatted(); out << " {\n"; for (auto& statement : this->m_statements) { diff --git a/src/codegen.cpp b/src/codegen.cpp index 0f00f4d..6cf21d3 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -115,6 +115,9 @@ namespace AST { auto ret_ty = this->m_return_ty->codegen(builder); std::vector params{}; + for (auto& param : this->m_params) { + params.push_back(param.second->codegen(builder)); + } auto fn_ty = llvm::FunctionType::get(ret_ty, params, false); diff --git a/src/parsing.cpp b/src/parsing.cpp index 7471f07..d6126c8 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -175,6 +175,17 @@ namespace parsing { auto type = parse_type(inner).unwrap(); auto name_token = inner.expect(token::Type::Ident); inner.expect(token::Type::Symbol, "("); + + std::vector>> params; + while (inner.peek().content != ")") { + if (params.size() > 0) { + inner.expect(token::Type::Symbol, ","); + } + auto param_ty = parse_type(inner).unwrap(); + auto param_name = inner.expect(token::Type::Ident); + params.push_back(std::pair(param_name.content, std::move(param_ty))); + } + inner.expect(token::Type::Symbol, ")"); inner.expect(token::Type::Symbol, "{"); @@ -191,7 +202,7 @@ namespace parsing { stream.m_position = inner.m_position; - auto fun = new AST::Function{ std::move(type), {}, name_token.content, std::move(statements) }; + auto fun = new AST::Function{ std::move(type), std::move(params), name_token.content, std::move(statements) }; return new std::unique_ptr{ fun }; } catch (std::runtime_error error) { diff --git a/test.c b/test.c index 7eb00a6..9f182a7 100644 --- a/test.c +++ b/test.c @@ -1,3 +1,7 @@ +int fibonacci(int n, int b) { + return 0; +} + int main() { int a = 5; a = 15 + 20;