condition feature
[calc.git] / parser.c
index 53d6e6c78efdea14be71bc4ddc216ddbcc005687..8007f862c973707194388139c9092cd63ec0dd67 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -87,7 +87,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 13
+#define NB_FUNCTIONS 14
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -101,7 +101,8 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "disp", Disp, 0, 4, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
-    { "!",    Not, 1, 1, 6}
+    { "!",    Not, 1, 1, 6},
+    { "cond", Cond, 3, 4, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -430,6 +431,7 @@ void print_element (element_t *root, int level)
     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);
@@ -491,19 +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, "camparison operators:\n");
-    fprintf (stdout, " == != >= <= > <\n\n");
-    fprintf (stdout, "logical 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");
 }
 
@@ -577,6 +581,7 @@ double evaluate_element (element_t *root, char mask)
     case Exp:
     case Recall:
     case Not:
+    case Cond:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -625,6 +630,14 @@ double evaluate_element (element_t *root, char mask)
     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;