seems ok
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 17 Feb 2023 10:02:17 +0000 (11:02 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 17 Feb 2023 10:02:17 +0000 (11:02 +0100)
argument.c
calc.c
getcomments.pl [changed mode: 0755->0644]
parser.c
program.c
workspace.c
workspace.h

index 2c6a93d6aff50d3eea10e2abefbc1946f66b5dd0..c5b1504b25409f9795459eb3885e1813d12ea6c4 100644 (file)
@@ -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 cad608fe528f52bfcded3425a3c4bc5f5a595da3..7476252d1c42312ffd17042e69c0cfb3bcf0ffbc 100644 (file)
--- 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: */
old mode 100755 (executable)
new mode 100644 (file)
index 1e9b9861731ac2ccbc81f844afbd9c80f91859d6..7bdcda5a1ec7c06b06c4400dcfd6dd39ef631ab9 100644 (file)
--- 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++;
             }
index 1e960f728641a02a44c5f660600031403ae28f3d..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,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;
 }
index 9fc4d83d522795b0bfab4c1e4043527694dd1e5c..eca1bf2bb182178bba5cd3372a3e0dbc9e3f45cc 100644 (file)
@@ -1,7 +1,6 @@
 #include <malloc.h>
 #include <stdio.h>
 
-#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);
     }
index 7bca4e08345366a8eeff87cda916d61ecf248cf6..984fe5184784baced0822fe9c708cfb861b6216a 100644 (file)
@@ -10,7 +10,6 @@
 
 typedef struct _workspace_t {
     double answer;
-    tab_t *argument;
     int id;
     element_t *root;
     tab_t *stack;