condition feature
[calc.git] / parser.c
index 43595e53845ade3e2476755fb8d791a620416ecf..8007f862c973707194388139c9092cd63ec0dd67 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -69,18 +69,25 @@ void delelement (element_t *root)
 
 /* functions */
 
-#define NB_OPERATORS 6
-
+#define NB_OPERATORS 14
 keyword_t operators[NB_OPERATORS] = {
     { "+\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}
+    { "^",   Pow, 2, 1, 4},
+    { "==",  Equal, 2, 2, -1},
+    { "!=",  Diff, 2, 2, -1},
+    { ">=",  Ge, 2, 2, -1},
+    { "<=",  Le, 2, 2, -1},
+    { ">",   Gt, 2, 1, -1},
+    { "<",   Lt, 2, 1, -1},
+    { "&",   And, 2, 1, -2},
+    { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 12
+#define NB_FUNCTIONS 14
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -89,11 +96,13 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "atan", Atan, 1, 4, 5},
     { "exp",  Exp, 1, 3, 5},
     { "log",  Log, 1, 3, 5},
-    { "sto",  Store, 2, 3, 5},
+    { "sto",  Store, 2, 3, 9},
     { "rcl",  Recall, 1, 3, 5},
-    { "disp",  Disp, 0, 4, 5},
-    { "quit", Quit, 0, 4, 5},
-    { "help", Help, 0, 4, 5}
+    { "disp", Disp, 0, 4, 9},
+    { "quit", Quit, 0, 4, 9},
+    { "help", Help, 0, 4, 9},
+    { "!",    Not, 1, 1, 6},
+    { "cond", Cond, 3, 4, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -113,6 +122,10 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
     }
     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))) {
+        delelement (new->ops[1]);
+        new->ops[1] = ERROR_OP;
+    }
     if (new->ops[1] == ERROR_OP) {
         delelement (new);
         *proot = NULL;
@@ -158,7 +171,11 @@ element_t *parser (char *str, char **next, int prio)
                 do {
                     found = 0;
                     new = parser (str + 1, &str, 0);
-                    if (new == ERROR_OP) {
+                    if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+                        delelement (new);
+                        new = ERROR_OP;
+                    }
+                    if ((new == NULL) || (new == ERROR_OP)) {
                         delelement (root);
                         return ERROR_OP;
                     }
@@ -181,13 +198,21 @@ element_t *parser (char *str, char **next, int prio)
                     return ERROR_OP;
                 }
                 new = parser (str + 1, &str, 0);
-                if ((new == ERROR_OP) || (*str == ',')) {
+                if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+                    delelement (new);
+                    new = ERROR_OP;
+                }
+                if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
                     delelement (new);
                     delelement (root);
                     return ERROR_OP;
                 }
                 root->ops[0] = new;
             }
+            if (*str != ')') {
+                delelement (root);
+                return ERROR_OP;
+            }
             str++;
             VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
             continue;
@@ -301,13 +326,28 @@ element_t *parser (char *str, char **next, int prio)
             double value = strtod (str, &pt);
             VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
             if (str != pt) {
-                if (root == NULL) {
+                if ((root == NULL) || (root->prio == 6)) {
                     new = newelement (Val, 1, 5);
                     if (new == NULL) {
                         return ERROR_OP;
                     }
                     new->value = value;
-                    root = new;
+                    if (root == NULL) {
+                        root = new;
+                    } else {
+                        for (i = 0; i < root->nbops; i++) {
+                            if (root->ops[i] == NULL) {
+                                root->ops[i] = new;
+                                found = 1;
+                                break;
+                            }
+                        }
+                        if (!found) {
+                            delelement (new);
+                            delelement (root);
+                            return ERROR_OP;
+                        }
+                    }
                     str = pt;
                 } else if ((*str == '+') || (*str == '-')) {
                     if ((prio) && (prio > 1)) {
@@ -382,6 +422,16 @@ void print_element (element_t *root, int level)
     case Ans: func = "Ans"; break;
     case Pi: func = "Pi"; break;
     case E: func = "E"; break;
+    case Equal: func = "Equal"; break;
+    case Diff: func = "Different"; break;
+    case Ge: func = "Greater or equal"; break;
+    case Le: func = "Lesser or equal"; break;
+    case Gt: func = "Greater"; break;
+    case Lt: func = "Lesser"; break;
+    case And: func = "And"; break;
+    case Or: func = "Or"; break;
+    case Not: func = "Not"; break;
+    case Cond: func = "Condition"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -443,15 +493,21 @@ void quit (void)
 void help (void)
 {
     fprintf (stdout, "calc is a simple calculator\n\n");
-    fprintf (stdout, "supported operators:\n");
-    fprintf (stdout, " + - * / %% ^\n\n");
-    fprintf (stdout, "supported functions:\n");
-    fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
-    fprintf (stdout, "storage functions:\n");
-    fprintf (stdout, " sto rcl\n\n");
-    fprintf (stdout, "miscellaneous functions:\n");
-    fprintf (stdout, " quit help\n\n");
-    fprintf (stdout, "supported constants:\n");
+    fprintf (stdout, "supported operators:");
+    fprintf (stdout, " + - * / %% ^\n");
+    fprintf (stdout, "camparison operators:");
+    fprintf (stdout, " == != >= <= > <\n");
+    fprintf (stdout, "logical operators:");
+    fprintf (stdout, " & | !\n");
+    fprintf (stdout, "supported functions:");
+    fprintf (stdout, " pow sqrt cos sin atan log exp\n");
+    fprintf (stdout, "storage functions:");
+    fprintf (stdout, " sto rcl\n");
+    fprintf (stdout, "conditional functions:");
+    fprintf (stdout, " cond\n");
+    fprintf (stdout, "miscellaneous functions:");
+    fprintf (stdout, " quit help\n");
+    fprintf (stdout, "supported constants:");
     fprintf (stdout, " e pi\n");
 }
 
@@ -502,6 +558,14 @@ double evaluate_element (element_t *root, char mask)
     case Mod:
     case Pow:
     case Store:
+    case Equal:
+    case Diff:
+    case Ge:
+    case Le:
+    case Gt:
+    case Lt:
+    case And:
+    case Or:
         if (root->ops[1]) {
             op1 = evaluate_element (root->ops[1], nextmask);
         } else if (root->func != Store) {
@@ -516,6 +580,8 @@ double evaluate_element (element_t *root, char mask)
     case Log:
     case Exp:
     case Recall:
+    case Not:
+    case Cond:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -555,6 +621,23 @@ double evaluate_element (element_t *root, char mask)
     case Ans: return answer;
     case Pi: return M_PI;
     case E: return M_E;
+    case Equal: return op0 == op1;
+    case Diff: return op0 != op1;
+    case Ge: return op0 >= op1;
+    case Le: return op0 <= op1;
+    case Gt: return op0 > op1;
+    case Lt: return op0 < op1;
+    case And: return (op0 != 0) && (op1 != 0);
+    case Or: return (op0 != 0) || (op1 != 0);
+    case Not: return (op0 == 0);
+    case Cond:
+        if ((op0) && (root->ops[1])) {
+            return evaluate_element (root->ops[1], 0);
+        } else if ((!op0) && (root->ops[2])) {
+            return evaluate_element (root->ops[2], 0);
+        } else {
+            return 0;
+        }
     }
 
     return 0;