Merge branch 'refactoring'
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 20 Feb 2023 15:01:09 +0000 (16:01 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Mon, 20 Feb 2023 15:01:09 +0000 (16:01 +0100)
1  2 
calc.c
element.h
parser.c
storage.c
storage.h

diff --cc calc.c
index 0d5e8eafa92c2d8737db154356457c43a62f166b,7476252d1c42312ffd17042e69c0cfb3bcf0ffbc..08ff10c14308c34769a49fb69af5111c367033a9
--- 1/calc.c
--- 2/calc.c
+++ b/calc.c
@@@ -370,7 -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\nmem (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 'mem\nmem (3)\nsto (4, pi)' | calc.exe | grep -q "error out of bound"
  // 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"
diff --cc element.h
index 0000000000000000000000000000000000000000,c36c5596e5b3ed04f4e8a96ebffe5ba885608e81..15e6ecc2eb991302afb9a4ec412c839b3944c918
mode 000000,100644..100644
--- /dev/null
+++ b/element.h
@@@ -1,0 -1,60 +1,60 @@@
 -    Store, Recall, Inc, Dec, Disp, Mem, Clear,
+ #ifndef __ELEMENT_H__
+ #define __ELEMENT_H__
+ /* global variables */
+ /* function type */
+ typedef enum {
+     Val = 0, Sig,
+     Add, Sub,
+     Mul, Div, Mod,
+     Pow, Sqr,
+     Cos, Sin, Tan, Acos, Asin, Atan,
+     Ln, Log, Exp,
+     Erfc, Erf,
+     Abs, Ceil, Floor,
++    Store, Recall, Inc, Dec, Disp, Memory, Clear,
+     Quit, Help,
+     Ans, E, Pi,
+     Equal, Diff, Ge, Le, Gt, Lt,
+     And, Or, Not,
+     Cond, While, Code, Print,
+     Prog, Arg, Call, List, Edit, Del,
+     Get, Length, Pop, Push, Put, Set, Show,
+     Max, Mean, Median, Min, Order, Prod, Sum, Variance
+ } func_t;
+ /* keyword type */
+ typedef struct _keyword_t {
+     char *keyword;
+     func_t func;
+     int nbops;
+     int offset;
+     float prio;
+ } keyword_t;
+ /* calculus element type */
+ typedef struct _element_t {
+     func_t func;
+     int nbops;
+     struct _element_t **ops;
+     double value;
+     int prio;
+     int hidden;
+     char *string;
+ } element_t;
+ #define ERROR_OP ((element_t *)(-1))
+ /* functions */
+ element_t *newelement (func_t function, int nbops, int prio);
+ void delelement (element_t *root);
+ element_t *dupelement (element_t *root);
+ #endif /* __ELEMENT_H__ */
+ /* vim: set ts=4 sw=4 et: */
diff --cc parser.c
Simple merge
diff --cc storage.c
index 0000000000000000000000000000000000000000,dda1856155387d8f8d5b5afbd9697eabfaee2215..4a559f27433086ceda169ce7f734afffd8450157
mode 000000,100644..100644
--- /dev/null
+++ b/storage.c
@@@ -1,0 -1,91 +1,95 @@@
 -void memory (int nb)
+ #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
+ tab_t *storage = NULL;
+ /* storage functions */
 -    if (nb != size_tab (storage)) {
++int memory (int nb)
+ {
++    if ((nb != -1) && (nb != size_tab (storage))) {
+         storage = resize_tab (storage, nb);
+     }
++    if (size_tab (storage) == -1) {
++        memory (DEFAULT_STORAGE_SIZE);
++    }
++    return size_tab (storage);
+ }
+ double store (int id, double value)
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     return set_tab (storage, id, value);
+ }
+ double recall (int id)
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     return get_tab (storage, id);
+ }
+ double increase (int id)
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     double val = get_tab (storage, id);
+     if (!isnan (val)) {
+         set_tab (storage, id, ++val);
+     }
+     return val;
+ }
+ double decrease (int id)
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     double val = get_tab (storage, id);
+     if (!isnan (val)) {
+         set_tab (storage, id, --val);
+     }
+     return val;
+ }
+ void display (void)
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     int i, n = size_tab (storage);
+     fprintf (stdout, "storage:");
+     for (i = 0; i < n; i++) {
+         fprintf (stdout, " ");
+         fprintf (stdout, minform, get_tab (storage, i + 1));
+     }
+     fprintf (stdout, "\n");
+ }
+ void clear ()
+ {
+     if (storage == NULL) {
+         memory (DEFAULT_STORAGE_SIZE);
+     }
+     int i, n = size_tab (storage);
+     for (i = 0; i < n; i++) {
+         set_tab (storage, i, 0);
+     }
+ }
+ /* vim: set ts=4 sw=4 et: */
diff --cc storage.h
index 0000000000000000000000000000000000000000,d538e7b84deaf54d58be9104551696582d64b643..896c264459fe78efc4dae5f4a45040615f6a7368
mode 000000,100644..100644
--- /dev/null
+++ b/storage.h
@@@ -1,0 -1,22 +1,22 @@@
 -void memory (int nb);
+ #ifndef __STORAGE_H__
+ #define __STORAGE_H__
+ #include "tabular.h"
+ /* global variables */
+ extern tab_t *storage;
+ /* storage functions */
++int memory (int nb);
+ double store (int id, double value);
+ double recall (int id);
+ double increase (int id);
+ double decrease (int id);
+ void display (void);
+ void clear ();
+ #endif /* __STORAGE_H__ */
+ /* vim: set ts=4 sw=4 et: */