From 893638e2b94ab197b883c748a0f6044ca78b2072 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 12 Feb 2023 15:40:30 +0100 Subject: [PATCH] use tab_t for storage --- calc.c | 12 ++++---- program.c | 9 ++---- program.h | 3 +- storage.c | 86 +++++++++++++++++++++---------------------------------- storage.h | 14 ++++----- 5 files changed, 49 insertions(+), 75 deletions(-) diff --git a/calc.c b/calc.c index 7cc1c3f..f589fe9 100644 --- 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' diff --git a/program.c b/program.c index c966617..9bbf392 100644 --- 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); diff --git a/program.h b/program.h index 65382d9..3d459d6 100644 --- 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; diff --git a/storage.c b/storage.c index 80ebf0a..dda1856 100644 --- a/storage.c +++ b/storage.c @@ -1,110 +1,90 @@ #include +#include #include #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); } } diff --git a/storage.h b/storage.h index 3a6ef86..d538e7b 100644 --- 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 (); -- 2.30.2