use tab_t for storage
authorLaurent Mazet <mazet@softndesign.org>
Sun, 12 Feb 2023 14:40:30 +0000 (15:40 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 12 Feb 2023 14:40:30 +0000 (15:40 +0100)
calc.c
program.c
program.h
storage.c
storage.h

diff --git a/calc.c b/calc.c
index 7cc1c3f5b3ccc08c79733a6156dea2af2d43c6ae..f589fe9a733d43febf3175f9be02f6b4061ad378 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -321,7 +321,7 @@ int main (int argc, char *argv[])
 // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
 // test: echo -e '\n\n\n' | calc.exe -n | grep -qv 'error'
 // test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9
-// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 =
+// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c error | xargs test 4 =
 // test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0'
 // test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1'
 // test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1'
@@ -360,8 +360,8 @@ int main (int argc, char *argv[])
 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe | grep -c error | xargs test 3 =
 // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (1)\ninc (1)\nrcl (1) == 6\nquit' | calc.exe -v 3 | grep -q '=> 1'
 // test: echo -e 'inc\ninc (\ndec\ndec (' | calc.exe | grep -c error | xargs test 4 =
-// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c invalid | xargs test 2 =
-// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 5050'
+// test: echo -e 'inc (11)\ndec (0)' | calc.exe | grep -c error | xargs test 2 =
+// test: echo -e 'while (inc (1) < 100, sto (2, rcl (1) + rcl (2)))' | calc.exe | grep -q '=> 4950'
 // test: echo -e 'while\nwhile (inc (1) < 3,\nwhile (inc (1) < 100, sto (2, rcl (1) + rcl (2))' | calc.exe | grep -c error | xargs test 3 =
 // test: echo -e 'while (0, 1)\nquit' | calc.exe -v 3 | grep -q While
 // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}\nquit' | calc.exe -v 3 | grep -q 'Code'
@@ -372,7 +372,7 @@ int main (int argc, char *argv[])
 // test: echo -e 'si\t\t (pi / 2)' | calc.exe | grep -q '=> 1'
 // test: echo -e '\t\t' | calc.exe | grep -q 'print'
 // test: echo -e '1 + 1;\nans + 1' | calc.exe | grep -qv 2
-// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q "invalid index"
+// test: echo -e 'mem (3)\nsto (4, pi)' | calc.exe | grep -q error
 // test: echo -e 'sto (2, 3)\nmem (2)\ndisp' | calc.exe | grep -q 'storage: 0 3$'
 // test: echo -e 'disp' | calc.exe | grep -q "storage: 0 0 0 0 0 0 0 0 0 0"
 // test: echo -e 'sto (3, 10)\ndisp' | calc.exe | grep -q "storage: 0 0 10 0 0 0 0 0 0 0"
@@ -402,10 +402,10 @@ int main (int argc, char *argv[])
 // test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord' | calc.exe -n | grep -c error | xargs test 8 =
 // test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 = 
 // Gauss sequence
-// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) < 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
+// test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) <= 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
 
 // Fibonacci sequence
-// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) < 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144'
+// test: echo -e '{sto (1, 1), sto (2, 1), sto (10, 1), while (inc (10) <= 12 - 1, {sto (3, rcl (1) + rcl (2)), sto (1, rcl (2)), print (sto (2, rcl (3)))})};' | calc.exe | grep -q '=> 144'
 
 // Gold number
 // 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'
index c966617c00562beef7d02e191d963ddcb9ff4347..9bbf392f39b967df066a7ed69307e3ad86dd8271 100644 (file)
--- a/program.c
+++ b/program.c
@@ -53,7 +53,7 @@ void prog (int id, element_t *root)
 
             /* clean old program */
             if ((programs + n)->storage) {
-                free ((programs + n)->storage);
+                free_tab ((programs + n)->storage);
             }
             if ((programs + n)->stack) {
                 free_tab ((programs + n)->stack);
@@ -72,7 +72,6 @@ void prog (int id, element_t *root)
     (programs + n)->id = id;
     (programs + n)->answer = 0;
     (programs + n)->storage = NULL;
-    (programs + n)->storage_size = 0;
     (programs + n)->stack = NULL;
     (programs + n)->root = dupelement (root);
 }
@@ -114,13 +113,11 @@ double call (int id, int nbargs, element_t **args)
     tmp.argument = argument;
     tmp.argument_size = argument_size;
     tmp.storage = storage;
-    tmp.storage_size = storage_size;
     tmp.stack = stack;
 
     /* change context */
     answer = 0;
     storage = (programs + n)->storage;
-    storage_size = (programs + n)->storage_size;
     argument = NULL;
     argument_size = 0;
     stack = (programs + n)->stack;
@@ -140,7 +137,6 @@ double call (int id, int nbargs, element_t **args)
     delelement (elements);
     (programs + n)->answer = answer;
     (programs + n)->storage = storage;
-    (programs + n)->storage_size = storage_size;
     if (argument) {
         free (argument);
     }
@@ -149,7 +145,6 @@ double call (int id, int nbargs, element_t **args)
     /* restore context */
     answer = tmp.answer;
     storage = tmp.storage;
-    storage_size = tmp.storage_size;
     argument = tmp.argument;
     argument_size = tmp.argument_size;
     stack = tmp.stack;
@@ -240,7 +235,7 @@ void del (int id)
 
     /* clean program */
     if ((programs + n)->storage) {
-        free ((programs + n)->storage);
+        free_tab ((programs + n)->storage);
     }
     if ((programs + n)->stack) {
         free_tab ((programs + n)->stack);
index 65382d9dca4aaff82f248b52876f31c2272a4aec..3d459d631c318aeac1ed05311aa5d5e2916f37a3 100644 (file)
--- a/program.h
+++ b/program.h
@@ -14,8 +14,7 @@ extern double *argument;
 typedef struct _workspace_t {
     int id;
     double answer;
-    double *storage;
-    int storage_size;
+    tab_t *storage;
     double *argument;
     int argument_size;
     element_t *root;
index 80ebf0af96dac923f4de3ddd4724c1e23a984661..dda1856155387d8f8d5b5afbd9697eabfaee2215 100644 (file)
--- a/storage.c
+++ b/storage.c
 #include <malloc.h>
+#include <math.h>
 #include <stdio.h>
 
 #include "alloc.h"
 #include "debug.h"
 #include "format.h"
+#include "tabular.h"
 
 #include "storage.h"
 
 /* global variables */
 
 #define DEFAULT_STORAGE_SIZE 10
-double *storage = NULL;
-
-int storage_size = -1;
+tab_t *storage = NULL;
 
 /* storage functions */
 
 void memory (int nb)
 {
-    int i, l;
-    double *tmp = NULL;
-    if (nb != storage_size) {
-        l = (nb < storage_size) ? nb : storage_size;
-        tmp = (double *) callocordie (nb, sizeof (double));
-        for (i = 0; i < l; i++) {
-            tmp[i] = storage[i];
-        }
-        if (storage != NULL) {
-            free (storage);
-        }
-        storage = tmp;
-        storage_size = nb;
+    if (nb != size_tab (storage)) {
+        storage = resize_tab (storage, nb);
     }
 }
 
-double store (int index, double value)
+double store (int id, double value)
 {
-    if (storage_size == -1) {
+    if (storage == NULL) {
         memory (DEFAULT_STORAGE_SIZE);
     }
-    if ((index > 0) && (index <= storage_size)) {
-        storage[index - 1] = value;
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
-    }
-    return value;
+    return set_tab (storage, id, value);
 }
 
-double recall (int index)
+double recall (int id)
 {
-    if (storage_size == -1) {
+    if (storage == NULL) {
         memory (DEFAULT_STORAGE_SIZE);
     }
-    if ((index > 0) && (index <= storage_size)) {
-        return storage[index - 1];
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
-    }
-    return 0;
+    return get_tab (storage, id);
 }
 
-double increase (int index)
+double increase (int id)
 {
-    if (storage_size == -1) {
+    if (storage == NULL) {
         memory (DEFAULT_STORAGE_SIZE);
     }
-    if ((index > 0) && (index <= storage_size)) {
-        return storage[index - 1]++;
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+    double val = get_tab (storage, id);
+    if (!isnan (val)) {
+        set_tab (storage, id, ++val);
     }
-    return 0;
+    return val;
 }
 
-double decrease (int index)
+double decrease (int id)
 {
-    if (storage_size == -1) {
+    if (storage == NULL) {
         memory (DEFAULT_STORAGE_SIZE);
     }
-    if ((index > 0) && (index <= storage_size)) {
-        return storage[index - 1]--;
-    } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+    double val = get_tab (storage, id);
+    if (!isnan (val)) {
+        set_tab (storage, id, --val);
     }
-    return 0;
+    return val;
 }
 
 void display (void)
 {
-    int i;
-    if (storage_size == -1) {
+    if (storage == NULL) {
         memory (DEFAULT_STORAGE_SIZE);
     }
+    int i, n = size_tab (storage);
     fprintf (stdout, "storage:");
-    for (i = 0; i < storage_size; i++) {
+    for (i = 0; i < n; i++) {
         fprintf (stdout, " ");
-        fprintf (stdout, minform, storage[i]);
+        fprintf (stdout, minform, get_tab (storage, i + 1));
     }
     fprintf (stdout, "\n");
 }
 
 void clear ()
 {
-    int i;
-    for (i = 0; i < storage_size; i++) {
-        storage[i] = 0;
+    if (storage == NULL) {
+        memory (DEFAULT_STORAGE_SIZE);
+    }
+    int i, n = size_tab (storage);
+    for (i = 0; i < n; i++) {
+        set_tab (storage, i, 0);
     }
 }
 
index 3a6ef86a5e4a4e36c3a3e7f59d28a663702dcfc7..d538e7b84deaf54d58be9104551696582d64b643 100644 (file)
--- a/storage.h
+++ b/storage.h
@@ -1,19 +1,19 @@
 #ifndef __STORAGE_H__
 #define __STORAGE_H__
 
-/* global variables */
+#include "tabular.h"
 
-extern double *storage;
+/* global variables */
 
-extern int storage_size;
+extern tab_t *storage;
 
 /* storage functions */
 
 void memory (int nb);
-double store (int index, double value);
-double recall (int index);
-double increase (int index);
-double decrease (int index);
+double store (int id, double value);
+double recall (int id);
+double increase (int id);
+double decrease (int id);
 void display (void);
 void clear ();