From 350a116e5a9a167b229cefae6e5927f604d7fa85 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sun, 3 Mar 2019 15:43:05 +0100
Subject: Simplify (cleanup) binop parsing

---
 src/parser.c | 62 +++++++++++++++++-------------------------------------------
 1 file changed, 17 insertions(+), 45 deletions(-)

(limited to 'src')

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;
@@ -342,20 +328,6 @@ static THROWABLE parser_parse_rhs_binop_without_paren(Parser *self, Ast *expr) {
     return PARSER_OK;
 }
 
-/*
-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 ';'
 
-- 
cgit v1.2.3-70-g09d2