remove duplicated code
authorLaurent Mazet <mazet@softndesign.org>
Thu, 29 Dec 2022 20:21:50 +0000 (21:21 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 29 Dec 2022 20:21:50 +0000 (21:21 +0100)
parser.c

index 75cc2944c679c79967da02cff100a79b26964b38..fdabef2bf15e6c3848c87824b6a55b44fbc18169 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -69,6 +69,28 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "log",  Log, 1, 3, 5}
 };
 
+/* subparser function */
+
+element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
+{
+    element_t *new = newelement (func, nbops, prio);
+    if (new == NULL) {
+        return ERROR_OP;
+    }
+    new->ops[0] = *proot;
+    new->ops[1] = parser (*pstr, pstr, new->prio);
+    if (new->ops[1] == ERROR_OP) {
+        return ERROR_OP;
+    }
+    *proot = newelement (Val, 1, 4);
+    if (*proot == ERROR_OP) {
+        return ERROR_OP;
+    }
+    (*proot)->ops[0] = new;
+
+    return *proot;
+}
+
 /* parser function */
 
 element_t *parser (char *str, char **next, int prio)
@@ -152,20 +174,9 @@ element_t *parser (char *str, char **next, int prio)
                     }
                     str += operator->offset;
                     VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
-                    new = newelement (operator->func, operator->nbops, operator->prio);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    new->ops[0] = root;
-                    new->ops[1] = parser (str, &str, new->prio);
-                    if (new->ops[1] == ERROR_OP) {
-                        return ERROR_OP;
-                    }
-                    root = newelement (Val, 1, 4);
-                    if (root == ERROR_OP) {
+                    if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
                         return ERROR_OP;
                     }
-                    root->ops[0] = new;
                 } else {
                     return ERROR_OP;
                 }
@@ -228,20 +239,9 @@ element_t *parser (char *str, char **next, int prio)
                             *next = str;
                             return root;
                         }
-                        new = newelement (Add, 2, 1);
-                        if (new == NULL) {
-                            return ERROR_OP;
-                        }
-                        new->ops[0] = root;
-                        new->ops[1] = parser (str, &str, new->prio);
-                        if (new->ops[1] == ERROR_OP) {
-                            return ERROR_OP;
-                        }
-                        root = newelement (Val, 1, 4);
-                        if (root == ERROR_OP) {
+                        if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
                             return ERROR_OP;
                         }
-                        root->ops[0] = new;
                     } else {
                         return ERROR_OP;
                     }