check parenthesis
[calc.git] / program.c
index ef6d833be6c1ca651008d69a183cab3ee294747f..70ce6c12f943cc5519ba1b2e74d73843d264c5f7 100644 (file)
--- 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;
 }