added parsing for function literals

This commit is contained in:
Karma Riuk
2025-07-15 00:56:12 +02:00
parent 85f530f5f9
commit 0b2a12cef7
4 changed files with 228 additions and 0 deletions

View File

@@ -59,6 +59,11 @@ namespace parser {
std::bind(&parser::parse_if_then_else, this)
);
register_prefix(
token::type::FUNCTION,
std::bind(&parser::parse_function, this)
);
using namespace std::placeholders;
register_infix(
{token::type::PLUS,
@@ -243,6 +248,39 @@ namespace parser {
);
};
ast::function_literal* parser::parse_function() {
TRACE_FUNCTION;
ast::function_literal* ret = new ast::function_literal(current);
if (!expect_next(token::type::LPAREN)) {
delete ret;
return nullptr;
}
while (next.type != token::type::RPAREN
&& expect_next(token::type::IDENTIFIER)) {
ret->parameters.push_back(parse_identifier());
if (next.type == token::type::RPAREN)
break;
if (!expect_next(token::type::COMMA)) {
// delete ret;
return nullptr;
}
}
if (!expect_next(token::type::RPAREN)) {
// delete ret;
return nullptr;
}
if (!expect_next(token::type::LBRACE)) {
// delete ret;
return nullptr;
}
ret->block = parse_block();
return ret;
};
ast::prefix_expr* parser::parse_prefix_expr() {
TRACE_FUNCTION;
ast::prefix_expr* ret = new ast::prefix_expr(current, current.literal);