use tab_t for argument
authorLaurent Mazet <mazet@softndesign.org>
Sun, 12 Feb 2023 21:33:08 +0000 (22:33 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 12 Feb 2023 21:33:08 +0000 (22:33 +0100)
argument.c [new file with mode: 0644]
argument.h [new file with mode: 0644]
calc.c
parser.c
program.c
program.h

diff --git a/argument.c b/argument.c
new file mode 100644 (file)
index 0000000..bda7684
--- /dev/null
@@ -0,0 +1,31 @@
+#include "parser.h"
+#include "tabular.h"
+
+#include "argument.h"
+
+/* global variables */
+
+tab_t *argument = NULL;
+
+/* argument management */
+
+/* get argument */
+
+double arg (int n)
+{
+    return get_tab (argument, n);
+}
+
+/* set arguments */
+
+double def (int nbops, element_t **ops)
+{
+    int i;
+    argument = resize_tab (argument, nbops);
+    for (i = 0; i < nbops; i++) {
+        set_tab (argument, i + 1, evaluate_element (ops[i], 0));
+    }
+    return size_tab (argument);
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/argument.h b/argument.h
new file mode 100644 (file)
index 0000000..01ae454
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __ARGUMENT_H__
+#define __ARGUMENT_H__
+
+#include "parser.h"
+#include "tabular.h"
+
+/* global variables */
+
+extern tab_t *argument;
+
+/* argument management */
+
+double arg (int n);
+double def (int nbops, element_t **ops);
+
+#endif /* __ARGUMENT_H__ */
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/calc.c b/calc.c
index f589fe9a733d43febf3175f9be02f6b4061ad378..920099b46a504c1fc88cd6d3ce733705ebd67584 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: alloc.o debug.o element.o format.o parser.o program.o stack.o storage.o tabular.o -lm -lreadline */
+/* linker: alloc.o argument.o debug.o element.o format.o parser.o program.o stack.o storage.o tabular.o -lm -lreadline */
 
 #include <malloc.h>
 #include <stddef.h>
index eba4fb7cf62e635927735e9912ad66bb9e734d12..1e9b9861731ac2ccbc81f844afbd9c80f91859d6 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4,6 +4,7 @@
 #include <string.h>
 
 #include "alloc.h"
+#include "argument.h"
 #include "debug.h"
 #include "element.h"
 #include "format.h"
index 9bbf392f39b967df066a7ed69307e3ad86dd8271..18a9719d3a318be6819dd5483f5f4a5a92e4cd30 100644 (file)
--- a/program.c
+++ b/program.c
@@ -3,6 +3,7 @@
 #include <string.h>
 
 #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;
index 3d459d631c318aeac1ed05311aa5d5e2916f37a3..d830b7f1f57ce46d0b8e8e18d4ec3d74f5cbe13f 100644 (file)
--- a/program.h
+++ b/program.h
@@ -6,19 +6,15 @@
 
 /* global variables */
 
-extern int argument_size;
-extern double *argument;
-
 /* workspace type */
 
 typedef struct _workspace_t {
-    int id;
     double answer;
-    tab_t *storage;
-    double *argument;
-    int argument_size;
+    tab_t *argument;
+    int id;
     element_t *root;
     tab_t *stack;
+    tab_t *storage;
     char *string;
 } workspace_t;
 
@@ -28,7 +24,6 @@ extern int nb_programs;
 /* program function */
 
 void prog (int id, element_t *root);
-double arg (int id);
 double call (int id, int nbargs, element_t **args);
 void list ();
 void edit (int id);