aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-03 15:43:05 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit350a116e5a9a167b229cefae6e5927f604d7fa85 (patch)
tree57b7a9dd3886b13252cb73db493c1c6cb0843788
parent465c544d4a593a135c86390d61385cfbd2f93dd5 (diff)
Simplify (cleanup) binop parsing
-rw-r--r--src/parser.c62
1 files changed, 17 insertions, 45 deletions
diff --git a/src/parser.c b/src/parser.c
index 52c2916..c94d416 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -84,7 +84,8 @@ static THROWABLE parser_parse_body_loop(Parser *self, Buffer *body_list, Token e
}
/*
-LHS = 'const'|'var' TOK_IDENTIFIER
+LHS = ('const' TOK_IDENTIFIER '=') |
+ ('var' TOK_IDENTIFIER '='|';')
*/
static THROWABLE parser_parse_lhs(Parser *self, LhsExpr **result, bool *assignment) {
bool isConst;
@@ -280,51 +281,36 @@ static THROWABLE parser_parse_rhs_single_expr(Parser *self, Ast *rhs_expr) {
static THROWABLE parser_parse_rhs_binop(Parser *self, Ast *expr);
/*
-RHS_BINOP_PAREN = '(' RHS_BINOP ')' (TOK_BINOP RHS_BINOP)
+RHS_BINOP_OPT_PAREN = RHS_S | '(' RHS_BINOP ')'
*/
-static THROWABLE parser_parse_rhs_binop_paren(Parser *self, bool *match, Ast *expr) {
- bool binop_match;
- Ast lhs;
- Ast rhs;
- BinopType binop_type;
-
- throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_OPEN_PAREN, match));
- if(!*match)
+static THROWABLE parser_parse_rhs_binop_opt_paren(Parser *self, Ast *expr) {
+ bool match;
+ throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_OPEN_PAREN, &match));
+ if(!match) {
+ try(parser_parse_rhs_single_expr(self, expr));
return PARSER_OK;
+ }
- try(parser_parse_rhs_binop(self, &lhs));
+ try(parser_parse_rhs_binop(self, expr));
throw_if_error(tokenizer_accept(&self->tokenizer, TOK_CLOSING_PAREN));
+ if(expr->type == AST_BINOP)
+ expr->value.binop->grouped = bool_true;
- throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_BINOP, &binop_match));
- if(!binop_match) {
- if(lhs.type == AST_BINOP)
- lhs.value.binop->grouped = bool_true;
- *expr = lhs;
- return PARSER_OK;
- }
- binop_type = self->tokenizer.value.binop_type;
-
- try(parser_parse_rhs_binop(self, &rhs));
- throw_if_error(scoped_allocator_alloc(self->allocator, sizeof(Binop), (void**)&expr->value.binop));
- binop_init(expr->value.binop);
- expr->value.binop->type = binop_type;
- expr->value.binop->grouped = bool_true;
- expr->value.binop->lhs = lhs;
- expr->value.binop->rhs = rhs;
- expr->type = AST_BINOP;
return PARSER_OK;
}
/*
-RHS_BINOP_WO_PAREN = RHS_S | (RHS_S TOK_BINOP RHS_BINOP)
+RHS_BINOP = RHS_BINOP_OPT_PAREN (TOK_BINOP RHS_BINOP_OPT_PAREN)?
+
+Note: Parantheses count has to match for the beginning paranthesis and the ending parenthesis.
*/
-static THROWABLE parser_parse_rhs_binop_without_paren(Parser *self, Ast *expr) {
+int parser_parse_rhs_binop(Parser *self, Ast *expr) {
bool match;
Ast lhs;
Ast rhs;
BinopType binop_type;
- try(parser_parse_rhs_single_expr(self, &lhs));
+ try(parser_parse_rhs_binop_opt_paren(self, &lhs));
throw_if_error(tokenizer_consume_if(&self->tokenizer, TOK_BINOP, &match));
if(!match) {
*expr = lhs;
@@ -343,20 +329,6 @@ static THROWABLE parser_parse_rhs_binop_without_paren(Parser *self, Ast *expr) {
}
/*
-RHS_BINOP = RHS_BINOP_PAREN | RHS_BINOP_WO_PAREN
-
-Note: Parantheses count has to match for the beginning paranthesis and the ending parenthesis.
-*/
-int parser_parse_rhs_binop(Parser *self, Ast *expr) {
- bool match;
- try(parser_parse_rhs_binop_paren(self, &match, expr));
- if(match)
- return PARSER_OK;
- try(parser_parse_rhs_binop_without_paren(self, expr));
- return PARSER_OK;
-}
-
-/*
RHS = RHS_BINOP ';'
Note: Parantheses count has to match for the beginning paranthesis and the ending parenthesis.