// 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"
--- /dev/null
- 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: */
--- /dev/null
-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: */
--- /dev/null
-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: */