Parse varargs
This commit is contained in:
parent
3f61e3749e
commit
8646d5c0d0
@ -91,6 +91,11 @@ namespace AST {
|
|||||||
out << " " << *param.first;
|
out << " " << *param.first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (this->m_is_vararg) {
|
||||||
|
if (counter > 0)
|
||||||
|
out << ", ";
|
||||||
|
out << "...";
|
||||||
|
}
|
||||||
|
|
||||||
out << ") -> ";
|
out << ") -> ";
|
||||||
out << this->m_return_ty->formatted();
|
out << this->m_return_ty->formatted();
|
||||||
|
|||||||
@ -176,6 +176,7 @@ namespace AST {
|
|||||||
private:
|
private:
|
||||||
std::unique_ptr<types::Type> m_return_ty;
|
std::unique_ptr<types::Type> m_return_ty;
|
||||||
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> m_params;
|
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> m_params;
|
||||||
|
bool m_is_vararg;
|
||||||
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:
|
||||||
@ -183,11 +184,13 @@ namespace AST {
|
|||||||
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::optional<std::string>, std::unique_ptr<types::Type>>> params,
|
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params,
|
||||||
|
bool is_vararg,
|
||||||
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 }
|
||||||
, m_return_ty{ std::move(return_ty) }
|
, m_return_ty{ std::move(return_ty) }
|
||||||
, m_params{ std::move(params) }
|
, m_params{ std::move(params) }
|
||||||
|
, m_is_vararg{ is_vararg }
|
||||||
, m_name{ name }
|
, m_name{ name }
|
||||||
, m_statements{ std::move(statements) } {
|
, m_statements{ std::move(statements) } {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -275,10 +275,20 @@ namespace parsing {
|
|||||||
inner.expect(token::Type::Symbol, "(");
|
inner.expect(token::Type::Symbol, "(");
|
||||||
|
|
||||||
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params;
|
std::vector<std::pair<std::optional<std::string>, std::unique_ptr<types::Type>>> params;
|
||||||
|
bool is_vararg = false;
|
||||||
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, ",");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (inner.peek().content == ".") {
|
||||||
|
inner.next();
|
||||||
|
inner.expect(token::Type::Symbol, ".");
|
||||||
|
inner.expect(token::Type::Symbol, ".");
|
||||||
|
is_vararg = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
auto param_ty = parse_type(inner).unwrap();
|
auto param_ty = parse_type(inner).unwrap();
|
||||||
std::optional<std::string> param_name{};
|
std::optional<std::string> param_name{};
|
||||||
if (inner.peek().type == token::Type::Ident) {
|
if (inner.peek().type == token::Type::Ident) {
|
||||||
@ -310,7 +320,14 @@ namespace parsing {
|
|||||||
|
|
||||||
stream.m_position = inner.m_position;
|
stream.m_position = inner.m_position;
|
||||||
|
|
||||||
auto fun = new AST::Function{ before_meta + stream.metadata(), std::move(type), std::move(params), name_token.content, std::move(statements) };
|
auto fun = new AST::Function{
|
||||||
|
before_meta + stream.metadata(),
|
||||||
|
std::move(type),
|
||||||
|
std::move(params),
|
||||||
|
is_vararg,
|
||||||
|
name_token.content,
|
||||||
|
std::move(statements)
|
||||||
|
};
|
||||||
return std::unique_ptr<AST::TopLevelStatement>{ fun };
|
return std::unique_ptr<AST::TopLevelStatement>{ fun };
|
||||||
}
|
}
|
||||||
catch (std::runtime_error& error) {
|
catch (std::runtime_error& error) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user