From 686c4bc2a512a57b6c672792b6b44e2d61458dfb Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Mon, 23 Jan 2023 00:14:06 +0100 Subject: [PATCH] fix error managment --- calc.c | 7 ++++--- parser.c | 10 +++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/calc.c b/calc.c index a10f53e..82ec971 100644 --- 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 diff --git a/parser.c b/parser.c index d6cb772..a6992c8 100644 --- 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; -- 2.30.2