From: Laurent Mazet Date: Mon, 20 Feb 2023 15:01:09 +0000 (+0100) Subject: Merge branch 'refactoring' X-Git-Tag: v1.1~3 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=bdcb95da42ef20e0385e0c0404cb25844c3bb137;p=calc.git Merge branch 'refactoring' --- bdcb95da42ef20e0385e0c0404cb25844c3bb137 diff --cc calc.c index 0d5e8ea,7476252..08ff10c --- a/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 0000000,c36c559..15e6ecc mode 000000,100644..100644 --- a/element.h +++ b/element.h @@@ -1,0 -1,60 +1,60 @@@ + #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, Mem, Clear, ++ 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 storage.c index 0000000,dda1856..4a559f2 mode 000000,100644..100644 --- a/storage.c +++ b/storage.c @@@ -1,0 -1,91 +1,95 @@@ + #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 + tab_t *storage = NULL; + + /* storage functions */ + -void memory (int nb) ++int memory (int nb) + { - if (nb != size_tab (storage)) { ++ 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 0000000,d538e7b..896c264 mode 000000,100644..100644 --- a/storage.h +++ b/storage.h @@@ -1,0 -1,22 +1,22 @@@ + #ifndef __STORAGE_H__ + #define __STORAGE_H__ + + #include "tabular.h" + + /* global variables */ + + extern tab_t *storage; + + /* storage functions */ + -void memory (int nb); ++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: */