Parse and process function parameters
This commit is contained in:
parent
7d4190d9a1
commit
f59b5db29b
11
src/ast.cpp
11
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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user