X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=program.c;h=18a9719d3a318be6819dd5483f5f4a5a92e4cd30;hb=c91672f948d371d3ba2b1a47db9a31fcabb763f5;hp=9bbf392f39b967df066a7ed69307e3ad86dd8271;hpb=893638e2b94ab197b883c748a0f6044ca78b2072;p=calc.git diff --git a/program.c b/program.c index 9bbf392..18a9719 100644 --- a/program.c +++ b/program.c @@ -3,6 +3,7 @@ #include #include "alloc.h" +#include "argument.h" #include "debug.h" #include "parser.h" #include "stack.h" @@ -13,9 +14,6 @@ /* global variables */ -int argument_size = 0; -double *argument = NULL; - workspace_t *programs = NULL; int nb_programs = 0; @@ -76,21 +74,10 @@ void prog (int id, element_t *root) (programs + n)->root = dupelement (root); } -double arg (int id) -{ - double ret = 0; - if ((id <= 0) || (id > argument_size)) { - VERBOSE (WARNING, fprintf (stdout, "error out of bound (%d/%d)\n", id, argument_size)); - } else { - ret = argument[id - 1]; - } - return ret; -} - double call (int id, int nbargs, element_t **args) { workspace_t tmp = {0}; - int i, l, n = -1; + int i, n = -1; double ret = 0; if (programs) { @@ -111,42 +98,35 @@ double call (int id, int nbargs, element_t **args) /* store context */ tmp.answer = answer; tmp.argument = argument; - tmp.argument_size = argument_size; - tmp.storage = storage; tmp.stack = stack; + tmp.storage = storage; /* change context */ answer = 0; - storage = (programs + n)->storage; argument = NULL; - argument_size = 0; - stack = (programs + n)->stack; if (nbargs > 0) { - argument = (double *) callocordie (nbargs, sizeof (double)); - for (i = 0, l = 0; i < nbargs; l++) { - if (args[l]) { - argument[i++] = evaluate_element (args[l], 0); - } - } - argument_size = nbargs; + def (nbargs, args); } + stack = (programs + n)->stack; + storage = (programs + n)->storage; /* evaluate program */ element_t *elements = dupelement ((programs + n)->root); ret = evaluate_element (elements, 0); delelement (elements); + + /* cleaning context */ (programs + n)->answer = answer; - (programs + n)->storage = storage; if (argument) { - free (argument); + free_tab (argument); } (programs + n)->stack = stack; + (programs + n)->storage = storage; /* restore context */ answer = tmp.answer; - storage = tmp.storage; argument = tmp.argument; - argument_size = tmp.argument_size; + storage = tmp.storage; stack = tmp.stack; return ret;