From 2ddb38f03fffaba277fdd99ad5ae2b85e2f01dec Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 17 Feb 2023 11:02:17 +0100 Subject: [PATCH] seems ok --- argument.c | 2 +- calc.c | 2 +- getcomments.pl | 0 parser.c | 2 +- program.c | 31 +++++++++++++++++++++---------- workspace.c | 15 --------------- workspace.h | 1 - 7 files changed, 24 insertions(+), 29 deletions(-) mode change 100755 => 100644 getcomments.pl diff --git a/argument.c b/argument.c index 2c6a93d..c5b1504 100644 --- a/argument.c +++ b/argument.c @@ -26,7 +26,7 @@ tab_t *def (int nbops, element_t **ops) tab_t *new = alloc_tab (nbops); for (i = 0; i < nbops; i++) { double val = evaluate_element (ops[i], 0); - VERBOSE (DEBUG, fprintf (stdout, "arg[%d] = %g\n", i + i, val)); + VERBOSE (DEBUG, fprintf (stdout, "value %d/%d = %g\n", i + 1, nbops, val)); set_tab (new, i + 1, val); } return new; diff --git a/calc.c b/calc.c index cad608f..7476252 100644 --- a/calc.c +++ b/calc.c @@ -413,6 +413,6 @@ int main (int argc, char *argv[]) // test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 15 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)) / rcl (1))})};' | calc.exe | grep -q '=> 1.61803' // Factorial sequence -// test: echo -e 'prog (1, cond (arg (1) > 1, arg (1) * call (1, arg (1) - 1), 1))\ncall (1, 10)' | ./calc.exe | grep -q '=> 3628800' +// test: echo -e 'prog (1, cond (arg (1) > 1, arg (1) * call (1, arg (1) - 1), 1))\ncall (1, 10)' | calc.exe | grep -q '=> 3.6288e+06' /* vim: set ts=4 sw=4 et: */ diff --git a/getcomments.pl b/getcomments.pl old mode 100755 new mode 100644 diff --git a/parser.c b/parser.c index 1e9b986..7bdcda5 100644 --- a/parser.c +++ b/parser.c @@ -828,7 +828,7 @@ double evaluate_element (element_t *root, char mask) break; case Arg: return arg ((int)op0); case Call: - for (i = 1, nb =0; i < root->nbops; i++) { + for (i = 1, nb = 0; i < root->nbops; i++) { if (root->ops[i]) { nb++; } diff --git a/program.c b/program.c index 1e960f7..70ce6c1 100644 --- a/program.c +++ b/program.c @@ -73,8 +73,11 @@ void prog (int id, element_t *root) double call (int id, int nbargs, element_t **args) { + int i; double ret = 0; + VERBOSE (DEBUG, fprintf (stdout, "new call (%d)\n", id)); + /* look for program */ int n = lookfor_program (id); if (n == -1) { @@ -82,38 +85,46 @@ double call (int id, int nbargs, element_t **args) return 0; } - /* set arguments */ - VERBOSE (DEBUG, fprintf (stdout, "1 arg[0]: %g\n", get_tab (argument, 1))); - tab_t *old_argument = copy_tab (argument); + /* display debug information */ + VERBOSE (DEBUG, fprintf (stdout, "nbargs: %d\n", nbargs)); + for (i = 0; i < nbargs; i++) { + VERBOSE (DEBUG, fprintf (stdout, "argument %d\n", i + 1); print_element (args[i], 0)); + } + VERBOSE (DEBUG, fprintf (stdout, "program\n"); print_element (programs[n]->root, 0)); /* backup context */ workspace_t *tmp = backup_ws (alloc_ws ()); restore_ws (programs[n]); - VERBOSE (DEBUG, fprintf (stdout, "2 arg[0]: %g\n", get_tab (argument, 1))); /* set arguments */ - argument = def (nbargs, args); - VERBOSE (DEBUG, fprintf (stdout, "3 arg[0]: %g\n", get_tab (argument, 1))); + VERBOSE (DEBUG, fprintf (stdout, "argument before evaluation (%d)\n", size_tab (argument))); + for (i = 0; i < size_tab (argument); i++) { + VERBOSE (DEBUG, fprintf (stdout, "arg %d value: %g\n", i + 1, get_tab (argument, i + 1))); + } + VERBOSE (DEBUG, fprintf (stdout, "evaluate %d args\n", nbargs)); + tab_t *new_argument = def (nbargs, args); + tab_t *old_argument = argument; + argument = new_argument; + VERBOSE (DEBUG, fprintf (stdout, "argument after evaluation (%d)\n", size_tab (argument))); + for (i = 0; i < size_tab (argument); i++) { + VERBOSE (DEBUG, fprintf (stdout, "arg %d value: %g\n", i + 1, get_tab (argument, i + 1))); + } /* evaluate program */ answer = 0; element_t *elements = dupelement (programs[n]->root); - VERBOSE (DEBUG, fprintf (stdout, "4 arg[0]: %g\n", get_tab (argument, 1))); ret = evaluate_element (elements, 0); VERBOSE (DEBUG, fprintf (stdout, "ret; %g\n", ret)); - VERBOSE (DEBUG, fprintf (stdout, "5 arg[0]: %g\n", get_tab (argument, 1))); delelement (elements); /* restore context */ backup_ws (programs[n]); restore_ws (tmp); free_ws (tmp); - VERBOSE (DEBUG, fprintf (stdout, "6 arg[0]: %g\n", get_tab (argument, 1))); /* clean arguments */ free_tab (argument); argument = old_argument; - VERBOSE (DEBUG, fprintf (stdout, "7 arg[0]: %g\n", get_tab (argument, 1))); return ret; } diff --git a/workspace.c b/workspace.c index 9fc4d83..eca1bf2 100644 --- a/workspace.c +++ b/workspace.c @@ -1,7 +1,6 @@ #include #include -#include "argument.h" #include "debug.h" #include "element.h" #include "parser.h" @@ -23,10 +22,6 @@ workspace_t *alloc_ws () workspace_t *backup_ws (workspace_t *ws) { ws->answer = answer; - if (ws->argument) { - free_tab (ws->argument); - } - ws->argument = copy_tab (argument); if (ws->stack) { free_tab (ws->stack); } @@ -44,11 +39,6 @@ workspace_t *clean_ws (workspace_t *ws) { ws->answer = 0; - if (ws->argument) { - free_tab (ws->argument); - ws->argument = NULL; - } - ws->id = 0; if (ws->root) { @@ -90,11 +80,6 @@ void restore_ws (workspace_t *ws) { answer = ws->answer; - if (argument) { - free_tab (argument); - } - argument = copy_tab (ws->argument); - if (stack) { free_tab (stack); } diff --git a/workspace.h b/workspace.h index 7bca4e0..984fe51 100644 --- a/workspace.h +++ b/workspace.h @@ -10,7 +10,6 @@ typedef struct _workspace_t { double answer; - tab_t *argument; int id; element_t *root; tab_t *stack; -- 2.30.2