remove limitation on max number of operands
[calc.git] / parser.c
index 8654fa8a78a61a3c6e90bc0c11de5f48566c8120..7a9f9885318585f31c4cf4a16fd1da186a0c32fb 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,6 +45,12 @@ element_t *newelement (func_t function, int nbops, int prio)
         VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
         return NULL;
     }
+    new->ops = (element_t **) calloc (1, sizeof (element_t *));
+    if (new->ops == NULL) {
+        free (new);
+        VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+        return NULL;
+    }
     new->func = function;
     new->nbops = nbops;
     new->prio = prio;
@@ -56,13 +62,16 @@ element_t *newelement (func_t function, int nbops, int prio)
 
 void delelement (element_t *root)
 {
-    int i;
     if ((root != NULL) && (root != ERROR_OP)) {
+        int i;
         for (i = 0; i < root->nbops; i++) {
             if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
                 delelement (root->ops[i]);
             }
         }
+        if (root->nbops) {
+            free (root->ops);
+        }
         free (root);
     }
 }
@@ -677,7 +686,12 @@ double evaluate_element (element_t *root, char mask)
     case Ans:
     case Pi:
     case E:
+        break;
     case While:
+        if (root->ops[0] == NULL) {
+            VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+            return 0;
+        }
         break;
     }