Allow anonymous parameters
This commit is contained in:
parent
5dc6e8ca16
commit
3f61e3749e
@ -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 << ") -> ";
|
||||||
|
|||||||
@ -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 }
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
@ -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, ")");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user