fix some error cases
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 26 Jan 2023 09:51:49 +0000 (10:51 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 26 Jan 2023 09:54:53 +0000 (10:54 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index 60cdad3596dc84c259b0ae12aab3b85d57d96838..5e6a78fc3301f401df156e12c9a665df0f5ec75c 100644 (file)
--- 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: */
index f8a7367a8f5a1812a51257b3551c1e2c7624c4f9..e6fc31eb4cc209c06286245091578033395401d0 100644 (file)
--- 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;
             }