From 62d56da6b0de8c4f27264bb1655f6940a364e87b Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 25 Jan 2023 18:17:31 +0100 Subject: [PATCH] full program feature --- calc.c | 3 +++ parser.c | 52 +++++++++++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/calc.c b/calc.c index 7013673..b964ad6 100644 --- a/calc.c +++ b/calc.c @@ -294,5 +294,8 @@ int main (int argc, char *argv[]) // 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 (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{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 5 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index 798abb8..ebb56a9 100644 --- a/parser.c +++ b/parser.c @@ -45,11 +45,13 @@ element_t *newelement (func_t function, int nbops, int prio) VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); return NULL; } - new->ops = (element_t **) calloc (1, sizeof (element_t *)); - if (new->ops == NULL) { - free (new); - VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); - return NULL; + if (nbops) { + new->ops = (element_t **) calloc (nbops, sizeof (element_t *)); + if (new->ops == NULL) { + free (new); + VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); + return NULL; + } } new->func = function; new->nbops = nbops; @@ -204,29 +206,20 @@ element_t *parser (char *str, char **next, int prio) if (*str == '{') { VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n")); - element_t *prog = newelement (Prog, 0, 5); - if (prog == NULL) { + if (root != NULL) { delelement (root); return ERROR_OP; } - if (root == NULL) { - root = prog; - } else { - for (i = 0; i < root->nbops; i++) { - if (root->ops[i] == NULL) { - root->ops[i] = prog; - found = 1; - } - } - if (!found) { - delelement (prog); - delelement (root); - return ERROR_OP; - } + element_t **prog = NULL; + new = newelement (Prog, 0, 5); + if (new == NULL) { + delelement (root); + return ERROR_OP; } + root = new; + prog = &root; do { - found = 0; new = parser (str + 1, &str, 0); if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) { delelement (new); @@ -234,15 +227,16 @@ element_t *parser (char *str, char **next, int prio) } if ((new == NULL) || (new == ERROR_OP)) { delelement (root); - return ERROR_OP; + return ERROR_OP; } - element_t *prognew = newelement (Prog, prog->nbops + 1, 5); - for (i = 0; i < prog->nbops; i++) { - prognew->ops[i] = prog->ops[i]; + element_t *newprog = newelement (Prog, (*prog)->nbops + 1, 5); + for (i = 0; i < (*prog)->nbops; i++) { + newprog->ops[i] = (*prog)->ops[i]; + (*prog)->ops[i] = NULL; } - prog->ops[prog->nbops] = new; - delelement (prog); - prog = prognew; + newprog->ops[(*prog)->nbops] = new; + delelement (*prog); + (*prog) = newprog; } while (*str == ','); if (*str != '}') { -- 2.30.2