remove limitation on max number of operands
authorLaurent Mazet <mazet@softndesign.org>
Tue, 24 Jan 2023 22:48:48 +0000 (23:48 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Tue, 24 Jan 2023 22:48:48 +0000 (23:48 +0100)
parser.c
parser.h

index 20f507d014446d92f63cd2bf73dba65f4fda2930..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);
     }
 }
index 49acc2080005a8c4cef2f635c71010889f71d09a..67dd9ab2a7f9ef1f64f6c5eea95c665294c35af1 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -34,11 +34,10 @@ typedef struct _keyword_t {
 
 /* calculus element type */
 
-#define MAX_OPERANDS 10
 typedef struct _element_t {
     func_t func;
     int nbops;
-    struct _element_t *ops[MAX_OPERANDS];
+    struct _element_t **ops;
     double value;
     int prio;
 } element_t;