added parsing for function literals
This commit is contained in:
@@ -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);
|
||||
|
Reference in New Issue
Block a user