new functions: quit and help
[calc.git] / parser.c
index 2ed8d9d845dd4f6749b021f582703fe9a92a168c..1d35b4c44785dc85eee79b7600b2c62eebb40910 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1,4 +1,5 @@
 #include <malloc.h>
+#include <math.h>
 #include <stdlib.h>
 
 #include "debug.h"
@@ -13,10 +14,16 @@ int codecmp (char *ref, char *str)
     int sig;
 
     while (*ref != '\0') {
-        sig = *str++ - *ref++;
+        if (*ref == '\t') {
+             sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
+        } else {
+            sig = *str - *ref;
+        }
         if (sig != 0) {
             return (sig > 0) ? 1 : -1;
         }
+        str++;
+        ref++;
     }
 
     return 0;
@@ -24,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) {
@@ -33,38 +40,62 @@ element_t *newelement (func_t function, int nbops)
     }
     new->func = function;
     new->nbops = nbops;
+    new->prio = prio;
 
     return new;
 }
 
 /* functions */
 
-#define NB_OPERATORS 7
+#define NB_OPERATORS 6
 
 keyword_t operators[NB_OPERATORS] = {
-    { "+ ",  Add, 2, 1 },
-    { "+\t", Add, 2, 1 },
-    { "- ",  Sub, 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},
+    { "%",   Mod, 2, 1, 3},
+    { "^",   Pow, 2, 1, 4}
 };
 
-#define NB_FUNCTIONS 7
+#define NB_FUNCTIONS 9
 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, 5},
+    { "pow",  Pow, 2, 3, 5},
+    { "cos",  Cos, 1, 3, 5},
+    { "sin",  Sin, 1, 3, 5},
+    { "atan", Atn, 1, 4, 5},
+    { "exp",  Exp, 1, 3, 5},
+    { "log",  Log, 1, 3, 5},
+    { "quit", Qui, 0, 4, 5},
+    { "help", Hel, 0, 4, 5}
 };
 
+/* subparser function */
+
+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] == ERROR_OP) {
+        return ERROR_OP;
+    }
+    *proot = newelement (Val, 1, 5);
+    if (*proot == ERROR_OP) {
+        return ERROR_OP;
+    }
+    (*proot)->ops[0] = new;
+
+    return *proot;
+}
+
 /* parser function */
 
-element_t *parser (char *str, char **next)
+element_t *parser (char *str, char **next, int prio)
 {
     element_t *root = NULL;
     int i;
@@ -91,7 +122,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;
                     }
@@ -107,11 +138,11 @@ element_t *parser (char *str, char **next)
                     }
                 } while (*str == ',');
             } else {
-                root = newelement (Val, 1);
+                root = newelement (Val, 1, 5);
                 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;
                 }
@@ -137,20 +168,23 @@ 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 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);
-                    if (new == NULL) {
+                    if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
                         return ERROR_OP;
                     }
-                    new->ops[0] = root;
-                    root = new;
-                    new = parser (str, &str);
-                    if (new == ERROR_OP) {
+                } else if (*str == '-') {
+                    new = newelement (Sig, 1, 9);
+                    if (new == NULL) {
                         return ERROR_OP;
                     }
-                    root->ops[1] = new;
+                    root = new;
                 } else {
                     return ERROR_OP;
                 }
@@ -171,7 +205,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;
                     }
@@ -189,56 +223,39 @@ element_t *parser (char *str, char **next)
             continue;
         }
 
-        /* last attend to detect addition and substraction */
-
-        if (((*str == '-') || (*str == '+')) &&
-            ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
-            ((root) && (root->func == Val))) {
-            VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
-            new = newelement (Add, 2);
-            if (new == NULL) {
-                return ERROR_OP;
-            }
-            new->ops[0] = root;
-            root = new;
-        }
-
         /* look for number */
 
-        if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+        if (((*str >= '0') && (*str <= '9')) ||
+            (*str == '.') || (*str == '+') || (*str == '-')) {
             VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
             char *pt;
             float value = strtof (str, &pt);
             VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
             if (str != pt) {
-                new = newelement (Val, 1);
-                if (new == NULL) {
-                    return ERROR_OP;
-                }
-                new->value = value;
                 if (root == NULL) {
+                    new = newelement (Val, 1, 5);
+                    if (new == NULL) {
+                        return ERROR_OP;
+                    }
+                    new->value = value;
                     root = new;
-                } else {
-                    if (root->func == Val) {
-                        element_t *set = newelement (Set, MAX_OPERANDS);
-                        if (set == NULL) {
-                            return ERROR_OP;
+                    str = pt;
+                } else if (root->func == Val) {
+                    if ((*str == '+') || (*str == '-')) {
+                        if ((prio) && (prio > 1)) {
+                            VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+                            *next = str;
+                            return root;
                         }
-                        set->ops[0] = root;
-                        root = set;
-                    }
-                    for (i = 0; i < root->nbops; i++) {
-                        if (root->ops[i] == NULL) {
-                            root->ops[i] = new;
-                            found = 1;
-                            break;
+                        if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+                            return ERROR_OP;
                         }
-                    }
-                    if (!found) {
+                    } else {
                         return ERROR_OP;
                     }
+                } else {
+                    return ERROR_OP;
                 }
-                str = pt;
                 found = 1;
             }
             VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
@@ -275,11 +292,12 @@ void print_element (element_t *root, int level)
 
     switch (root->func) {
     case Val: func = "Value"; break;
-    case Set: func = "Set"; break;
+    case Sig: func = "Sign"; break;
     case Add: func = "Addition"; break;
     case Sub: func = "Subtraction"; break;
     case Mul: func = "Multiplication"; break;
     case Div: func = "Division"; break;
+    case Mod: func = "Modulo"; break;
     case Pow: func = "Power"; break;
     case Sqr: func = "Square Root"; break;
     case Cos: func = "Cosine"; break;
@@ -287,6 +305,8 @@ void print_element (element_t *root, int level)
     case Atn: func = "Arc Tangent"; break;
     case Log: func = "Logarithm"; break;
     case Exp: func = "Exponantial"; break;
+    case Qui: func = "Quit"; break;
+    case Hel: func = "Help"; break;
     }
 
     PRINTOUT ("Function: %s\n", func);
@@ -303,4 +323,118 @@ void print_element (element_t *root, int level)
     }
 }
 
+/* quit function */
+
+void quit (void)
+{
+    PRINTOUT ("bye\n");
+    exit (0);
+}
+
+/* help message */
+
+void help (void)
+{
+    PRINTOUT ("calc is a simple calculator\n\n");
+    PRINTOUT ("supported operators:\n");
+    PRINTOUT (" + - * / % ^\n\n");
+    PRINTOUT ("supported functions:\n");
+    PRINTOUT (" pow sqrt cos sin atan log exp\n\n");
+    PRINTOUT ("miscellaneous functions:\n");
+    PRINTOUT (" quit help\n");
+}
+
+/* evaluate element tree */
+
+#define MASK_SUB 0x1
+#define MASK_DIV 0x2
+
+double evaluate_element (element_t *root, char mask)
+{
+    double op0 = 0, op1 = 0;
+    char nextmask = mask;
+
+    if ((root == NULL) || (root == ERROR_OP)) {
+        VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
+        return 0;
+    }
+
+    /* mask to manage sub operator sub and div */
+    switch (root->func) {
+    case Add:
+        nextmask &= ~MASK_SUB;
+        nextmask &= ~MASK_DIV;
+        break;
+    case Sub:
+        nextmask |= MASK_SUB;
+        nextmask &= ~MASK_DIV;
+        break;
+    case Mul:
+        nextmask &= ~MASK_DIV;
+        break;
+    case Div:
+        nextmask |= MASK_DIV;
+        break;
+    default:
+        nextmask = mask;
+    }
+
+    switch (root->func) {
+    case Val:
+    case Sig:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
+        break;
+    case Add:
+    case Sub:
+    case Mul:
+    case Div:
+    case Mod:
+    case Pow:
+        if (root->ops[1]) {
+            op1 = evaluate_element (root->ops[1], nextmask);
+        } 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], 0);
+        } else {
+            VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+            return 0;
+        }
+        break;
+    case Qui:
+    case Hel:
+        break;
+    }
+
+    switch (root->func) {
+    case Val: return op0;
+    case Sig: return -op0;
+    case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
+    case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
+    case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
+    case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
+    case Mod: return fmod (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);
+    case Qui: quit (); break;
+    case Hel: help (); break;
+    }
+
+    return 0;
+}
+
 /* vim: set ts=4 sw=4 et: */