From 45a631a8d16bd8d7b3a12e5a20d1b494ee19d66d Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 26 Jan 2023 10:51:49 +0100 Subject: [PATCH] fix some error cases --- calc.c | 21 +++++++++++---------- parser.c | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/calc.c b/calc.c index 60cdad3..5e6a78f 100644 --- a/calc.c +++ b/calc.c @@ -157,7 +157,7 @@ int main (int argc, char *argv[]) if (*line[i] == '\0') { continue; } - element_t *element = parser (line[i], NULL, 0); + element_t *element = parser (line[i], NULL, -9); if (element == ERROR_OP) { VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout)); ret = 1; @@ -279,23 +279,24 @@ int main (int argc, char *argv[]) // 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 = +// test: echo -e '1 + quit' | calc.exe | grep -q error +// test: echo -e 'cos (quit)' | calc.exe | grep -q error +// test: echo -e '(quit)' | calc.exe | grep -q error +// test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe | grep -c error | xargs test 3 = // test: echo -e 'sto (2, 3)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | 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 'sto (2, 1)\ncond (rcl (2) > 2, log (64))' | calc.exe | grep -q '=> 0' // 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 = +// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 = // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1' -// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe 2>&1 | grep -c error | xargs test 4 = -// test: echo -e 'inc (11)\ndec (0)' | calc.exe 2>&1 | grep -c invalid | xargs test 2 = +// test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 = +// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 = // test: echo -e 'whl (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050' -// test: echo -e 'whl\nwhl (inc (1) < 3,\nwhl (inc (1) < 100, sto (2, rcl (1) + rcl (2))' 2>&1 | calc.exe | grep -c error | xargs test 3 = +// test: echo -e 'whl\nwhl (inc (1) < 3,\nwhl (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 = // test: echo -e 'whl (0, 1)' | calc.exe -v 3 | grep -q While // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program' // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6' -// test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' 2>&1 | calc.exe | grep -c error | xargs test 6 = +// test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 = +// test: echo -e '1 }\n1 )\n1 , 2' | calc.exe | grep -c error | xargs test 3 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index f8a7367..e6fc31e 100644 --- a/parser.c +++ b/parser.c @@ -306,6 +306,10 @@ element_t *parser (char *str, char **next, int prio) /* check for closing bracket, closing brace or koma */ if ((*str == ')') || (*str == '}') || (*str == ',')) { + if (prio == -9) { + delelement (root); + return ERROR_OP; + } if (next != NULL) { *next = str; } -- 2.30.2