Parse and process function parameters

This commit is contained in:
Sofia 2026-04-10 17:25:01 +03:00
parent 7d4190d9a1
commit f59b5db29b
4 changed files with 29 additions and 2 deletions

View File

@ -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) {

View File

@ -115,6 +115,9 @@ namespace AST {
auto ret_ty = this->m_return_ty->codegen(builder);
std::vector<llvm::Type*> params{};
for (auto& param : this->m_params) {
params.push_back(param.second->codegen(builder));
}
auto fn_ty = llvm::FunctionType::get(ret_ty, params, false);

View File

@ -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<std::pair<std::string, std::unique_ptr<types::Type>>> 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<AST::TopLevelStatement>{ fun };
}
catch (std::runtime_error error) {

4
test.c
View File

@ -1,3 +1,7 @@
int fibonacci(int n, int b) {
return 0;
}
int main() {
int a = 5;
a = 15 + 20;