From ce6627f22a4f12cd2b9b89700f46169b0e7382b6 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 23 Jan 2023 14:21:45 +0100 Subject: [PATCH] fix not operator --- calc.c | 3 ++- parser.c | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/calc.c b/calc.c index 513c781..deb2f80 100644 --- 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: */ diff --git a/parser.c b/parser.c index 05b89c9..85a7472 100644 --- 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; } -- 2.30.2