add Fibonacci sequence
[calc.git] / parser.c
index 798abb8545e30b443da0983ee2541f9fa4aa746e..9796048af19e5f8d552a682fef0c1afc564fd799 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -43,13 +43,14 @@ element_t *newelement (func_t function, int nbops, int prio)
     element_t *new = (element_t *) calloc (1, sizeof (element_t));
     if (new == NULL) {
         VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
-        return NULL;
+        exit (1);
     }
-    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;
+    if (nbops) {
+        new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
+        if (new->ops == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
     }
     new->func = function;
     new->nbops = nbops;
@@ -87,16 +88,9 @@ element_t *dupelement (element_t *root)
         return root;
     }
     tmp = newelement (root->func, root->nbops, root->prio);
-    if (tmp == NULL) {
-        return ERROR_OP;
-    }
     tmp->value = root->value;
     for (i = 0; i < root->nbops; i++) {
         tmp->ops[i] = dupelement (root->ops[i]);
-        if (tmp->ops[i] == ERROR_OP) {
-            delelement (tmp);
-            return ERROR_OP;
-        }
     }
     return tmp;
 }
@@ -154,9 +148,6 @@ keyword_t constants[NB_CONSTANTS] = {
 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] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
@@ -169,10 +160,6 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
         return ERROR_OP;
     }
     *proot = newelement (Val, 1, 5);
-    if (*proot == NULL) {
-        delelement (new);
-        return ERROR_OP;
-    }
     (*proot)->ops[0] = new;
 
     return *proot;
@@ -204,45 +191,33 @@ element_t *parser (char *str, char **next, int prio)
 
         if (*str == '{') {
             VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
-            element_t *prog = newelement (Prog, 0, 5);
-            if (prog == NULL) {
+            if (root != NULL) {
                 delelement (root);
                 return ERROR_OP;
             }
-            if (root == NULL) {
-                root = prog;
-            } else {
-                for (i = 0; i < root->nbops; i++) {
-                    if (root->ops[i] == NULL) {
-                        root->ops[i] = prog;
-                        found = 1;
-                    }
-                }
-                if (!found) {
-                    delelement (prog);
-                    delelement (root);
-                    return ERROR_OP;
-                }
-            }
+            element_t **prog = NULL;
+            new = newelement (Prog, 0, 5);
+            root = new;
+            prog = &root;
 
             do {
-                found = 0;
                 new = parser (str + 1, &str, 0);
                 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                     delelement (new);
                     new = ERROR_OP;
                 }
-                if ((new == NULL) || (new == ERROR_OP)) {
+                if (new == ERROR_OP) {
                     delelement (root);
-                return ERROR_OP;
+                    return ERROR_OP;
                 }
-                element_t *prognew = newelement (Prog, prog->nbops + 1, 5);
-                for (i = 0; i < prog->nbops; i++) {
-                    prognew->ops[i] = prog->ops[i];
+                element_t *newprog = newelement (Prog, (*prog)->nbops + 1, 5);
+                for (i = 0; i < (*prog)->nbops; i++) {
+                    newprog->ops[i] = (*prog)->ops[i];
+                    (*prog)->ops[i] = NULL;
                 }
-                prog->ops[prog->nbops] = new;
-                delelement (prog);
-                prog = prognew;
+                newprog->ops[(*prog)->nbops] = new;
+                delelement (*prog);
+                (*prog) = newprog;
             } while (*str == ',');
 
             if (*str != '}') {
@@ -285,9 +260,6 @@ element_t *parser (char *str, char **next, int prio)
                 } while (*str == ',');
             } else {
                 root = newelement (Val, 1, 5);
-                if (root == NULL) {
-                    return ERROR_OP;
-                }
                 new = parser (str + 1, &str, 0);
                 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                     delelement (new);
@@ -312,6 +284,10 @@ element_t *parser (char *str, char **next, int prio)
         /* check for closing bracket, closing brace or koma */
 
         if ((*str == ')') || (*str == '}') || (*str == ',')) {
+            if (prio == -9) {
+                delelement (root);
+                return ERROR_OP;
+            }
             if (next != NULL) {
                 *next = str;
             }
@@ -337,11 +313,7 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                 } else if (*str == '-') {
-                    new = newelement (Sig, 1, 9);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (Sig, 1, 9);
                 } else {
                     return ERROR_OP;
                 }
@@ -362,11 +334,7 @@ element_t *parser (char *str, char **next, int prio)
                 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
                 if (root == NULL) {
                     VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
-                    new = newelement (function->func, function->nbops, function->prio);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (function->func, function->nbops, function->prio);
                 } else {
                     delelement (root);
                     return ERROR_OP;
@@ -389,11 +357,7 @@ element_t *parser (char *str, char **next, int prio)
                 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
                 if (root == NULL) {
                     VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
-                    new = newelement (constant->func, constant->nbops, constant->prio);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
-                    root = new;
+                    root = newelement (constant->func, constant->nbops, constant->prio);
                 } else {
                     delelement (root);
                     return ERROR_OP;
@@ -419,9 +383,6 @@ element_t *parser (char *str, char **next, int prio)
             if (str != pt) {
                 if ((root == NULL) || (root->prio == 6)) {
                     new = newelement (Val, 1, 5);
-                    if (new == NULL) {
-                        return ERROR_OP;
-                    }
                     new->value = value;
                     if (root == NULL) {
                         root = new;
@@ -603,9 +564,6 @@ double while_do (element_t *cond, element_t *action)
     element_t *temp = NULL;
 
     VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
-    if (cond == NULL) {
-        return ret;
-    }
     while (1) {
         VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));