X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=program.c;h=70ce6c12f943cc5519ba1b2e74d73843d264c5f7;hb=7d32325ab262aaf9cb62151de5a9c889cb5ebfab;hp=ef6d833be6c1ca651008d69a183cab3ee294747f;hpb=5d50462b1e3cc5687f31fad0d295b31fb9934c15;p=calc.git diff --git a/program.c b/program.c index ef6d833..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,35 +85,46 @@ double call (int id, int nbargs, element_t **args) return 0; } + /* 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]); - + /* set arguments */ - free_tab (argument); - argument = NULL; - if (nbargs > 0) { - def (nbargs, args); + 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); ret = evaluate_element (elements, 0); + VERBOSE (DEBUG, fprintf (stdout, "ret; %g\n", ret)); delelement (elements); - /* clean arguments */ - if (argument) { - free_tab (argument); - } - argument = NULL; - if (nbargs > 0) { - def (nbargs, args); - } - /* restore context */ backup_ws (programs[n]); restore_ws (tmp); + free_ws (tmp); + + /* clean arguments */ + free_tab (argument); + argument = old_argument; return ret; }