change whl to while
[calc.git] / parser.c
index ebb56a9c678a35aa2fae9404394598a4375406a4..c023a6e959a5fe6fb3f86f76fba752bb97a0745b 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;
 }
@@ -123,15 +115,21 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 17
+#define NB_FUNCTIONS 23
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
     { "cos",  Cos, 1, 3, 5},
     { "sin",  Sin, 1, 3, 5},
+    { "tan",  Tan, 1, 3, 5},
+    { "acos", Acos, 1, 4, 5},
+    { "asin", Asin, 1, 4, 5},
     { "atan", Atan, 1, 4, 5},
-    { "exp",  Exp, 1, 3, 5},
     { "log",  Log, 1, 3, 5},
+    { "exp",  Exp, 1, 3, 5},
+    { "abs",  Abs, 1, 3, 5},
+    { "floor", Floor, 1, 5, 5},
+    { "ceil", Ceil, 1, 4, 5},
     { "sto",  Store, 2, 3, 5},
     { "rcl",  Recall, 1, 3, 5},
     { "inc",  Inc, 1, 3, 5},
@@ -141,7 +139,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "help", Help, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
     { "cond", Cond, 3, 4, 5},
-    { "whl",  While, 2, 3, 5}
+    { "while", While, 2, 5, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -156,9 +154,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 +166,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 +203,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;
 
@@ -225,7 +212,7 @@ element_t *parser (char *str, char **next, int prio)
                     delelement (new);
                     new = ERROR_OP;
                 }
-                if ((new == NULL) || (new == ERROR_OP)) {
+                if (new == ERROR_OP) {
                     delelement (root);
                     return ERROR_OP;
                 }
@@ -279,9 +266,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);
@@ -306,6 +290,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;
             }
@@ -331,11 +319,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;
                 }
@@ -356,11 +340,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;
@@ -383,11 +363,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;
@@ -413,9 +389,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;
@@ -496,9 +469,15 @@ void print_element (element_t *root, int level)
     case Sqr: func = "Square Root"; break;
     case Cos: func = "Cosine"; break;
     case Sin: func = "Sine"; break;
+    case Tan: func = "Tangent"; break;
+    case Acos: func = "Arc Cosine"; break;
+    case Asin: func = "Arc Sine"; break;
     case Atan: func = "Arc Tangent"; break;
     case Log: func = "Logarithm"; break;
     case Exp: func = "Exponantial"; break;
+    case Abs: func = "Absolute value"; break;
+    case Ceil: func = "Ceil value"; break;
+    case Floor: func = "Floor value"; break;
     case Store: func = "Store"; break;
     case Recall: func = "Recall"; break;
     case Inc: func = "Increase"; break;
@@ -597,9 +576,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"));
 
@@ -650,12 +626,14 @@ void help (void)
     fprintf (stdout, " == != >= <= > <\n");
     fprintf (stdout, "logical operators:");
     fprintf (stdout, " & | !\n");
+    fprintf (stdout, "mathematic functions:");
+    fprintf (stdout, " pow sqrt cos sin tan acos asin atan log exp\n");
     fprintf (stdout, "supported functions:");
-    fprintf (stdout, " pow sqrt cos sin atan log exp\n");
+    fprintf (stdout, " abs ceil floor\n");
     fprintf (stdout, "storage functions:");
     fprintf (stdout, " sto rcl inc dec\n");
     fprintf (stdout, "conditional functions:");
-    fprintf (stdout, " cond\n");
+    fprintf (stdout, " cond while\n");
     fprintf (stdout, "miscellaneous functions:");
     fprintf (stdout, " quit help\n");
     fprintf (stdout, "supported constants:");
@@ -727,9 +705,15 @@ double evaluate_element (element_t *root, char mask)
     case Sqr:
     case Cos:
     case Sin:
+    case Tan:
+    case Acos:
+    case Asin:
     case Atan:
     case Log:
     case Exp:
+    case Abs:
+    case Ceil:
+    case Floor:
     case Recall:
     case Inc:
     case Dec:
@@ -770,9 +754,15 @@ double evaluate_element (element_t *root, char mask)
     case Sqr: return sqrt (op0);
     case Cos: return cos (op0);
     case Sin: return sin (op0);
+    case Tan: return tan (op0);
+    case Acos: return acos (op0);
+    case Asin: return asin (op0);
     case Atan: return atan (op0);
     case Log: return log (op0);
     case Exp: return exp (op0);
+    case Abs: return fabs (op0);
+    case Ceil: return ceil (op0);
+    case Floor: return floor (op0);
     case Store: return store ((int)op0, (op1) ? op1 : answer);
     case Recall: return recall ((int)op0);
     case Inc: return increase ((int)op0);