exit directly when memory allocation failed
[calc.git] / parser.c
index e6fc31eb4cc209c06286245091578033395401d0..9796048af19e5f8d552a682fef0c1afc564fd799 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -43,14 +43,13 @@ 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);
     }
     if (nbops) {
         new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
         if (new->ops == NULL) {
-            free (new);
             VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
-            return NULL;
+            exit (1);
         }
     }
     new->func = function;
@@ -89,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;
 }
@@ -156,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))) {
@@ -171,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;
@@ -212,10 +197,6 @@ element_t *parser (char *str, char **next, int prio)
             }
             element_t **prog = NULL;
             new = newelement (Prog, 0, 5);
-            if (new == NULL) {
-                delelement (root);
-                return ERROR_OP;
-            }
             root = new;
             prog = &root;
 
@@ -279,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);
@@ -335,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;
                 }
@@ -360,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;
@@ -387,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;
@@ -417,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;
@@ -601,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"));