partial recursivity
[calc.git] / program.c
index a6801f8a48574ed34a003ed5451e1ef8e6a35ea7..7db9d40291b3433a503b32f07c0b605f2ff7497e 100644 (file)
--- a/program.c
+++ b/program.c
@@ -81,20 +81,17 @@ double call (int id, int nbargs, element_t **args)
         VERBOSE (WARNING, fprintf (stdout, "error unknown program (%d)\n", id));
         return 0;
     }
-    VERBOSE (DEBUG, fprintf (stdout, "id -> n; %d -> %d\n", id, n));
 
     /* set arguments */
-    free_tab (argument);
-    argument = NULL;
-    VERBOSE (DEBUG, fprintf (stdout, "nbargs; %d\n", nbargs));
-    if (nbargs > 0) {
-        def (nbargs, args);
-    }
+    tab_t *old_argument = copy_tab (argument);
 
     /* backup context */
     workspace_t *tmp = backup_ws (alloc_ws ());
     restore_ws (programs[n]);
 
+    /* set arguments */
+    argument = def (nbargs, args);
+
     /* evaluate program */
     answer = 0;
     element_t *elements = dupelement (programs[n]->root);
@@ -102,15 +99,15 @@ double call (int id, int nbargs, element_t **args)
     VERBOSE (DEBUG, fprintf (stdout, "ret; %g\n", ret));
     delelement (elements);
 
-    /* clean arguments */
-    free_tab (argument);
-    argument = NULL;
-
     /* restore context */
     backup_ws (programs[n]);
     restore_ws (tmp);
     free_ws (tmp);
 
+    /* clean arguments */
+    free_tab (argument);
+    argument = old_argument;
+
     return ret;
 }