rename tests rule
[calc.git] / parser.c
index 8ddd83ad62e14a80b1f152d00bb11ff7234c723f..02443079228e3385b361b5e2f027152ad48f18ad 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,4 +1,5 @@
 #include <malloc.h>
+#include <math.h>
 #include <stdlib.h>
 
 #include "debug.h"
@@ -30,7 +31,7 @@ int codecmp (char *ref, char *str)
 
 /* allocate new element */
 
-element_t *newelement (func_t function, int nbops)
+element_t *newelement (func_t function, int nbops, int prio)
 {
     element_t *new = (element_t *) calloc (1, sizeof (element_t));
     if (new == NULL) {
@@ -39,6 +40,7 @@ element_t *newelement (func_t function, int nbops)
     }
     new->func = function;
     new->nbops = nbops;
+    new->prio = prio;
 
     return new;
 }
@@ -48,27 +50,27 @@ element_t *newelement (func_t function, int nbops)
 #define NB_OPERATORS 5
 
 keyword_t operators[NB_OPERATORS] = {
-    { "+\t",  Add, 2, 1 },
-    { "-\t", Sub, 2, 1 },
-    { "*",   Mul, 2, 1 },
-    { "/",   Div, 2, 1 },
-    { "^",   Pow, 2, 1 }
+    { "+\t", Add, 2, 1, 1},
+    { "-\t", Sub, 2, 1, 1},
+    { "*",   Mul, 2, 1, 2},
+    { "/",   Div, 2, 1, 2},
+    { "^",   Pow, 2, 1, 3}
 };
 
 #define NB_FUNCTIONS 7
 keyword_t functions[NB_FUNCTIONS] = {
-    { "sqrt", Sqr, 1, 4 },
-    { "pow",  Pow, 2, 3 },
-    { "cos",  Cos, 1, 3 },
-    { "sin",  Sin, 1, 3 },
-    { "atan", Atn, 1, 4 },
-    { "exp",  Exp, 1, 3 },
-    { "log",  Log, 1, 3 }
+    { "sqrt", Sqr, 1, 4, 4},
+    { "pow",  Pow, 2, 3, 4},
+    { "cos",  Cos, 1, 3, 4},
+    { "sin",  Sin, 1, 3, 4},
+    { "atan", Atn, 1, 4, 4},
+    { "exp",  Exp, 1, 3, 4},
+    { "log",  Log, 1, 3, 4}
 };
 
 /* parser function */
 
-element_t *parser (char *str, char **next)
+element_t *parser (char *str, char **next, int prio)
 {
     element_t *root = NULL;
     int i;
@@ -95,7 +97,7 @@ element_t *parser (char *str, char **next)
             if (root) {
                 do {
                     found = 0;
-                    new = parser (str + 1, &str);
+                    new = parser (str + 1, &str, 0);
                     if (new == ERROR_OP) {
                         return ERROR_OP;
                     }
@@ -111,11 +113,11 @@ element_t *parser (char *str, char **next)
                     }
                 } while (*str == ',');
             } else {
-                root = newelement (Val, 1);
+                root = newelement (Val, 1, 4);
                 if (root == NULL) {
                     return ERROR_OP;
                 }
-                new = parser (str + 1, &str);
+                new = parser (str + 1, &str, 0);
                 if ((new == ERROR_OP) || (*str == ',')) {
                     return ERROR_OP;
                 }
@@ -141,20 +143,28 @@ element_t *parser (char *str, char **next)
             keyword_t *operator = operators + i;
             if (codecmp (operator->keyword, str) == 0) {
                 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
-                str += operator->offset;
-                if ((root) && (root->func != Set)) {
+                if (root) {
+                    if ((prio) && (prio > operator->prio)) {
+                        VERBOSE (DEBUG, PRINTOUT ("stop processing operator because operator priority\n"));
+                        *next = str;
+                        return root;
+                    }
+                    str += operator->offset;
                     VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
-                    new = newelement (operator->func, operator->nbops);
+                    new = newelement (operator->func, operator->nbops, operator->prio);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
                     new->ops[0] = root;
-                    root = new;
-                    new = parser (str, &str);
-                    if (new == ERROR_OP) {
+                    new->ops[1] = parser (str, &str, new->prio);
+                    if (new->ops[1] == ERROR_OP) {
                         return ERROR_OP;
                     }
-                    root->ops[1] = new;
+                    root = newelement (Val, 1, 4);
+                    if (root == ERROR_OP) {
+                        return ERROR_OP;
+                    }
+                    root->ops[0] = new;
                 } else {
                     return ERROR_OP;
                 }
@@ -175,7 +185,7 @@ element_t *parser (char *str, char **next)
                 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
                 if (root == NULL) {
                     VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
-                    new = newelement (function->func, function->nbops);
+                    new = newelement (function->func, function->nbops, function->prio);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
@@ -202,7 +212,7 @@ element_t *parser (char *str, char **next)
             float value = strtof (str, &pt);
             VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
             if (str != pt) {
-                new = newelement (Val, 1);
+                new = newelement (Val, 1, 4);
                 if (new == NULL) {
                     return ERROR_OP;
                 }
@@ -211,7 +221,7 @@ element_t *parser (char *str, char **next)
                     root = new;
                 } else if (root->func == Val) {
                     if ((*str == '+') || (*str == '-')) {
-                        element_t *add = newelement (Add, 2);
+                        element_t *add = newelement (Add, 2, 1);
                         if (add == NULL) {
                             return ERROR_OP;
                         }
@@ -261,7 +271,6 @@ void print_element (element_t *root, int level)
 
     switch (root->func) {
     case Val: func = "Value"; break;
-    case Set: func = "Set"; break;
     case Add: func = "Addition"; break;
     case Sub: func = "Subtraction"; break;
     case Mul: func = "Multiplication"; break;
@@ -289,4 +298,67 @@ void print_element (element_t *root, int level)
     }
 }
 
+/* evaluate element tree */
+
+double evaluate_element (element_t *root)
+{
+    double op0 = 0, op1 = 0;
+
+    if ((root == NULL) || (root == ERROR_OP)) {
+        VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
+        return 0;
+    }
+
+    switch (root->func) {
+    case Val:
+        if (root->ops[0]) {
+            return evaluate_element (root->ops[0]);
+        } else {
+            return root->value;
+        }
+        break;
+    case Add:
+    case Sub:
+    case Mul:
+    case Div:
+    case Pow:
+        if (root->ops[1]) {
+            op1 = evaluate_element (root->ops[1]);
+        } else {
+            VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
+            return 0;
+        }
+        /* fallthrough */
+    case Sqr:
+    case Cos:
+    case Sin:
+    case Atn:
+    case Log:
+    case Exp:
+        if (root->ops[0]) {
+            op0 = evaluate_element (root->ops[0]);
+        } else {
+            VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+            return 0;
+        }
+    }
+
+    switch (root->func) {
+    case Add: return op0 + op1;
+    case Sub: return op0 - op1;
+    case Mul: return op0 * op1;
+    case Div: return op0 / op1;
+    case Pow: return pow (op0, op1);
+    case Sqr: return sqrt (op0);
+    case Cos: return cos (op0);
+    case Sin: return sin (op0);
+    case Atn: return atan (op0);
+    case Log: return log (op0);
+    case Exp: return exp (op0);
+    default: break;
+    }
+
+    return 0;
+}
+
 /* vim: set ts=4 sw=4 et: */