condition feature
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 23 Jan 2023 14:38:23 +0000 (15:38 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 23 Jan 2023 14:38:23 +0000 (15:38 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 67c4b2e11eabbf3cbd9c571859bcdae2d278db98..4ced168785a883f40544b16ddb8876083f8cc625 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -283,5 +283,9 @@ int main (int argc, char *argv[])
 // test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error
 // test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error
 // test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe 2>&1 | grep -c error | xargs test 3 =
+// test: echo -e 'sto (2, pi)\ncond (rcl (2) > 2, log (64), exp (75))' | calc.exe  | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
+// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
+// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 =
 
 /* vim: set ts=4 sw=4 et: */
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;
index 80fbd984ca68fe213910df0496f3a7d112908f74..409b4d43c16d4860c549d3f2af0cf63b2b5560e1 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -18,7 +18,8 @@ typedef enum {
     Quit, Help,
     Ans, E, Pi,
     Equal, Diff, Ge, Le, Gt, Lt,
-    And, Or, Not
+    And, Or, Not,
+    Cond
 } func_t;
 
 /* keyword type */