fix error managment
authorMazet Laurent <mazet@softndesign.org>
Sun, 22 Jan 2023 23:14:06 +0000 (00:14 +0100)
committerMazet Laurent <mazet@softndesign.org>
Sun, 22 Jan 2023 23:14:06 +0000 (00:14 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index a10f53ef6d9183cf17fc48932a3eac7f5148228c..82ec9712dde481364f8359303c27313cc2915509 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -231,7 +231,8 @@ int main (int argc, char *argv[])
 // 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 "2 + cos (pi +" | calc.exe | grep -q 'error'
+// test: echo "2 + cos (pi" | calc.exe | grep -vq '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'
@@ -242,7 +243,7 @@ int main (int argc, char *argv[])
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2
 // test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye
-// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 11 =
+// test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 12 =
 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
@@ -274,7 +275,7 @@ 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 -q bye
 // 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
index d6cb7728a720810699c1a42bada851f18d947903..a6992c83c10dbaebcb074d155f0e10eaead341e4 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -122,7 +122,7 @@ 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)) {
+    if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
         delelement (new->ops[1]);
         new->ops[1] = ERROR_OP;
     }
@@ -171,11 +171,11 @@ element_t *parser (char *str, char **next, int prio)
                 do {
                     found = 0;
                     new = parser (str + 1, &str, 0);
-                    if ((new != NULL) && (new != ERROR_OP) && (new->prio == 9)) {
+                    if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                         delelement (new);
                         new = ERROR_OP;
                     }
-                    if (new == ERROR_OP) {
+                    if ((new == NULL) || (new == ERROR_OP)) {
                         delelement (root);
                         return ERROR_OP;
                     }
@@ -198,11 +198,11 @@ element_t *parser (char *str, char **next, int prio)
                     return ERROR_OP;
                 }
                 new = parser (str + 1, &str, 0);
-                if ((new != NULL) && (new != ERROR_OP) && (new->prio == 9)) {
+                if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
                     delelement (new);
                     new = ERROR_OP;
                 }
-                if ((new == ERROR_OP) || (*str == ',')) {
+                if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
                     delelement (new);
                     delelement (root);
                     return ERROR_OP;