add comparison operators
authorLaurent Mazet <mazet@softndesign.org>
Sun, 22 Jan 2023 14:30:46 +0000 (15:30 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 22 Jan 2023 14:30:46 +0000 (15:30 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index fe752407871ae429ab4a4a91aa8df1abdbb02059..0134b74ae5d2ddac68961c31dc6bec1584f3f5d6 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -229,6 +229,10 @@ int main (int argc, char *argv[])
 // test: echo "1 + 2 *" | calc.exe | grep -q 'error'
 // test: echo "* 1 - 2" | calc.exe | grep -q 'error'
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
+// test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error'
+// test: echo "2 + (foo)" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi" | calc.exe | grep -q 'error'
+// test: echo "cos (1, 2)" | calc.exe | grep -q 'error'
 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
 // test: echo "pow (2)" | calc.exe | grep -q 'error'
 // test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
@@ -247,5 +251,16 @@ int main (int argc, char *argv[])
 // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
 // test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
 // test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0'
+// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '1 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '2 > 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 < 2' | calc.exe | grep -q '=> 1'
+// test: echo -e '2 < 2' | calc.exe | grep -q '=> 0'
+// test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye
 
 /* vim: set ts=4 sw=4 et: */
index 43595e53845ade3e2476755fb8d791a620416ecf..844e9417f18b19d2ec4b32780e514dda6338c224 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -69,7 +69,7 @@ void delelement (element_t *root)
 
 /* functions */
 
-#define NB_OPERATORS 6
+#define NB_OPERATORS 12
 
 keyword_t operators[NB_OPERATORS] = {
     { "+\t", Add, 2, 1, 1},
@@ -77,7 +77,13 @@ keyword_t operators[NB_OPERATORS] = {
     { "*",   Mul, 2, 1, 2},
     { "/",   Div, 2, 1, 2},
     { "%",   Mod, 2, 1, 3},
-    { "^",   Pow, 2, 1, 4}
+    { "^",   Pow, 2, 1, 4},
+    { "==",  Equal, 2, 2, 6},
+    { "!=",  Diff, 2, 2, 6},
+    { ">=",  Ge, 2, 2, 6},
+    { "<=",  Le, 2, 2, 6},
+    { ">",   Gt, 2, 1, 6},
+    { "<",   Lt, 2, 1, 6}
 };
 
 #define NB_FUNCTIONS 12
@@ -91,7 +97,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "log",  Log, 1, 3, 5},
     { "sto",  Store, 2, 3, 5},
     { "rcl",  Recall, 1, 3, 5},
-    { "disp",  Disp, 0, 4, 5},
+    { "disp", Disp, 0, 4, 5},
     { "quit", Quit, 0, 4, 5},
     { "help", Help, 0, 4, 5}
 };
@@ -382,6 +388,12 @@ 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;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -445,6 +457,8 @@ 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, "supported functions:\n");
     fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
     fprintf (stdout, "storage functions:\n");
@@ -502,6 +516,12 @@ 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:
         if (root->ops[1]) {
             op1 = evaluate_element (root->ops[1], nextmask);
         } else if (root->func != Store) {
@@ -555,6 +575,12 @@ 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;
     }
 
     return 0;
index 95bfa2fa4715510b3e2789a14f22d0098ae12994..fb09da26debb0caf909dfb211775af657ec765de 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -16,7 +16,8 @@ typedef enum {
     Log, Exp,
     Store, Recall, Disp,
     Quit, Help,
-    Ans, E, Pi
+    Ans, E, Pi,
+    Equal, Diff, Ge, Le, Gt, Lt
 } func_t;
 
 /* keyword type */