fix not operator
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 23 Jan 2023 13:21:45 +0000 (14:21 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 23 Jan 2023 13:21:45 +0000 (14:21 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index 513c7812439b9c1c645d4e0c75cce0d2dc644ff6..deb2f8026becce8ef1f14fd2c76157d6817c99a2 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -276,11 +276,12 @@ int main (int argc, char *argv[])
 // test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0'
 // test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1'
 // test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0'
-// test: echo -e '1 & 1\n1 | 1\n!(1)\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '1 & 1\n1 | 1\n!1\nquit' | calc.exe -v 3 | grep -qv error
 // test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1'
 // test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1'
 // test: echo -e '1 + quit' | calc.exe 2>&1 | grep -q error
 // 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 =
 
 /* vim: set ts=4 sw=4 et: */
index 05b89c936b8a6d96b868f440aed06adc5323a4fa..85a74724602c0830f3d60cdf34193687b76db570 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -70,7 +70,6 @@ void delelement (element_t *root)
 /* functions */
 
 #define NB_OPERATORS 14
-
 keyword_t operators[NB_OPERATORS] = {
     { "+\t", Add, 2, 1, 1},
     { "-\t", Sub, 2, 1, 1},
@@ -102,7 +101,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "disp", Disp, 0, 4, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
-    { "!",    Not, 1, 1, 5}
+    { "!",    Not, 1, 1, 6}
 };
 
 #define NB_CONSTANTS 3
@@ -326,13 +325,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)) {
@@ -345,6 +359,7 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                 } else {
+printf("coucou\n"); fflush (stdout);
                     delelement (root);
                     return ERROR_OP;
                 }