Allow anonymous parameters

This commit is contained in:
Sofia 2026-04-11 22:11:52 +03:00
parent 5dc6e8ca16
commit 3f61e3749e
5 changed files with 20 additions and 12 deletions

View File

@ -86,7 +86,10 @@ namespace AST {
for (auto& param : this->m_params) { for (auto& param : this->m_params) {
if (counter++ > 0) if (counter++ > 0)
out << ", "; out << ", ";
out << param.second->formatted() << " " << param.first; out << param.second->formatted();
if (param.first) {
out << " " << *param.first;
}
} }
out << ") -> "; out << ") -> ";

View File

@ -175,14 +175,14 @@ namespace AST {
class Function : public TopLevelStatement { class Function : public TopLevelStatement {
private: private:
std::unique_ptr<types::Type> m_return_ty; std::unique_ptr<types::Type> m_return_ty;
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> m_params; std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> m_params;
std::string m_name; std::string m_name;
std::optional<std::vector<std::unique_ptr<Statement>>> m_statements; std::optional<std::vector<std::unique_ptr<Statement>>> m_statements;
public: public:
Function( Function(
token::Metadata meta, token::Metadata meta,
std::unique_ptr<types::Type> return_ty, std::unique_ptr<types::Type> return_ty,
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> params, std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params,
std::string name, std::string name,
std::optional<std::vector<std::unique_ptr<Statement>>> statements) std::optional<std::vector<std::unique_ptr<Statement>>> statements)
: TopLevelStatement{ meta } : TopLevelStatement{ meta }

View File

@ -209,11 +209,13 @@ namespace AST {
for (auto& param : this->m_params) { for (auto& param : this->m_params) {
auto param_ty_ptr = param_ty_ptrs[counter]; auto param_ty_ptr = param_ty_ptrs[counter];
auto arg = function->getArg(counter++); auto arg = function->getArg(counter++);
arg->setName(param.first); if (param.first) {
inner_scope.values[param.first] = codegen::StackValue{ arg->setName(*param.first);
arg, inner_scope.values[*param.first] = codegen::StackValue{
param_ty_ptr, arg,
}; param_ty_ptr,
};
}
} }
for (auto& statement : *this->m_statements) { for (auto& statement : *this->m_statements) {

View File

@ -274,14 +274,17 @@ namespace parsing {
auto name_token = inner.expect(token::Type::Ident); auto name_token = inner.expect(token::Type::Ident);
inner.expect(token::Type::Symbol, "("); inner.expect(token::Type::Symbol, "(");
std::vector<std::pair<std::string, std::unique_ptr<types::Type>>> params; std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params;
while (inner.peek().content != ")") { while (inner.peek().content != ")") {
if (params.size() > 0) { if (params.size() > 0) {
inner.expect(token::Type::Symbol, ","); inner.expect(token::Type::Symbol, ",");
} }
auto param_ty = parse_type(inner).unwrap(); auto param_ty = parse_type(inner).unwrap();
auto param_name = inner.expect(token::Type::Ident); std::optional<std::string> param_name{};
params.push_back(std::pair(param_name.content, std::move(param_ty))); if (inner.peek().type == token::Type::Ident) {
param_name = inner.expect(token::Type::Ident).content;
}
params.push_back(std::pair(param_name, std::move(param_ty)));
} }
inner.expect(token::Type::Symbol, ")"); inner.expect(token::Type::Symbol, ")");

2
test.c
View File

@ -1,4 +1,4 @@
void printf(char* b, int num); void printf(char*, int);
int fibonacci(int n) { int fibonacci(int n) {
if (n < 2) if (n < 2)