free memories
[calc.git] / parser.c
index 1d35b4c44785dc85eee79b7600b2c62eebb40910..2660b43365569ceab2a284b811e3d59c99effeca 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,6 +45,21 @@ element_t *newelement (func_t function, int nbops, int prio)
     return new;
 }
 
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+    int i;
+    if ((root != NULL) && (root != ERROR_OP)) {
+        for (i = 0; i < root->nbops; i++) {
+            if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+                delelement (root->ops[i]);
+            }
+        }
+        free (root);
+    }
+}
+
 /* functions */
 
 #define NB_OPERATORS 6
@@ -82,10 +97,13 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
     new->ops[0] = *proot;
     new->ops[1] = parser (*pstr, pstr, new->prio);
     if (new->ops[1] == ERROR_OP) {
+        delelement (new);
+        *proot = NULL;
         return ERROR_OP;
     }
     *proot = newelement (Val, 1, 5);
-    if (*proot == ERROR_OP) {
+    if (*proot == NULL) {
+        delelement (new);
         return ERROR_OP;
     }
     (*proot)->ops[0] = new;
@@ -124,6 +142,7 @@ element_t *parser (char *str, char **next, int prio)
                     found = 0;
                     new = parser (str + 1, &str, 0);
                     if (new == ERROR_OP) {
+                        delelement (root);
                         return ERROR_OP;
                     }
                     for (i = 0; i < root->nbops; i++) {
@@ -134,6 +153,8 @@ element_t *parser (char *str, char **next, int prio)
                         }
                     }
                     if (!found) {
+                        delelement (new);
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } while (*str == ',');
@@ -144,6 +165,8 @@ element_t *parser (char *str, char **next, int prio)
                 }
                 new = parser (str + 1, &str, 0);
                 if ((new == ERROR_OP) || (*str == ',')) {
+                    delelement (new);
+                    delelement (root);
                     return ERROR_OP;
                 }
                 root->ops[0] = new;
@@ -177,6 +200,7 @@ element_t *parser (char *str, char **next, int prio)
                     str += operator->offset;
                     VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
                     if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } else if (*str == '-') {
@@ -210,7 +234,8 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                     root = new;
-                 } else {
+                } else {
+                    delelement (root);
                     return ERROR_OP;
                 }
                 str += function->offset;
@@ -248,12 +273,15 @@ element_t *parser (char *str, char **next, int prio)
                             return root;
                         }
                         if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+                            delelement (root);
                             return ERROR_OP;
                         }
                     } else {
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } else {
+                    delelement (root);
                     return ERROR_OP;
                 }
                 found = 1;
@@ -264,6 +292,7 @@ element_t *parser (char *str, char **next, int prio)
         /* error */
 
         if (!found) {
+            delelement (root);
             return ERROR_OP;
         }
 
@@ -272,6 +301,7 @@ element_t *parser (char *str, char **next, int prio)
     if (next != NULL) {
         *next = str;
     }
+
     return root;
 }