storage feature
authorLaurent Mazet <mazet@softndesign.org>
Fri, 20 Jan 2023 23:55:29 +0000 (00:55 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 20 Jan 2023 23:55:29 +0000 (00:55 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 6ecd2f97d6906270ccd57861c91a7a93496ad2b4..9e2134426de8b15aeefe5a91fd7ab12af28194df 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -237,12 +237,15 @@ int main (int argc, char *argv[])
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 2
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64
 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2
-// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nhelp\nquit' | calc.exe -v 3 | grep -q bye
+// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye
 // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 11 =
 // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 =
 // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1
 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906'
 // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error'
 // test: echo -e '\n\n\n' | calc.exe -n
+// 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 (2)\n3\nsto (5)\ndisp' | calc.exe | grep -q '0 1 0 0 3 0 0 0 0 0'
 
 /* vim: set ts=4 sw=4 et: */
index a1c6666a9f25f496e5eee9470957fd1c94f3050d..e2c4673f4106d4c80074cc30b22e5f471cec3f45 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -11,6 +11,9 @@
 
 double answer = 0;
 
+#define STORAGE_SIZE 10
+double storage[STORAGE_SIZE] = {0};
+
 /* compare codes */
 
 int codecmp (char *ref, char *str)
@@ -77,7 +80,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "^",   Pow, 2, 1, 4}
 };
 
-#define NB_FUNCTIONS 9
+#define NB_FUNCTIONS 12
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -86,6 +89,9 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "atan", Atan, 1, 4, 5},
     { "exp",  Exp, 1, 3, 5},
     { "log",  Log, 1, 3, 5},
+    { "sto",  Store, 1, 3, 5},
+    { "rcl",  Recall, 1, 3, 5},
+    { "disp",  Disp, 0, 4, 5},
     { "quit", Quit, 0, 4, 5},
     { "help", Help, 0, 4, 5}
 };
@@ -368,6 +374,9 @@ void print_element (element_t *root, int level)
     case Atan: func = "Arc Tangent"; break;
     case Log: func = "Logarithm"; break;
     case Exp: func = "Exponantial"; break;
+    case Store: func = "Store"; break;
+    case Recall: func = "Recall"; break;
+    case Disp: func = "Display"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
     case Ans: func = "Ans"; break;
@@ -389,6 +398,38 @@ void print_element (element_t *root, int level)
     }
 }
 
+/* storage functions */
+
+double store (int index)
+{
+    if ((index > 0) && (index <= STORAGE_SIZE)) {
+        storage[index - 1] = answer;
+    } else {
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+    }
+    return answer;
+}
+
+double recall (int index)
+{
+    if ((index > 0) && (index <= STORAGE_SIZE)) {
+        return storage[index - 1];
+    } else {
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+    }
+    return 0;
+}
+
+void display (void)
+{
+    int i;
+    fprintf (stdout, "storage:");
+    for (i = 0; i < STORAGE_SIZE; i++) {
+        fprintf (stdout, " %g", storage[i]);
+    }
+    fprintf (stdout, "\n");
+}
+
 /* quit function */
 
 void quit (void)
@@ -406,6 +447,8 @@ void help (void)
     fprintf (stdout, " + - * / %% ^\n\n");
     fprintf (stdout, "supported functions:\n");
     fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
+    fprintf (stdout, "storage functions:\n");
+    fprintf (stdout, " sto rcl\n\n");
     fprintf (stdout, "miscellaneous functions:\n");
     fprintf (stdout, " quit help\n\n");
     fprintf (stdout, "supported constants:\n");
@@ -471,6 +514,8 @@ double evaluate_element (element_t *root, char mask)
     case Atan:
     case Log:
     case Exp:
+    case Store:
+    case Recall:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -478,6 +523,7 @@ double evaluate_element (element_t *root, char mask)
             return 0;
         }
         break;
+    case Disp:
     case Quit:
     case Help:
     case Ans:
@@ -501,6 +547,9 @@ double evaluate_element (element_t *root, char mask)
     case Atan: return atan (op0);
     case Log: return log (op0);
     case Exp: return exp (op0);
+    case Store: return store ((int)op0);
+    case Recall: return recall ((int)op0);
+    case Disp: display (); break;
     case Quit: quit (); break;
     case Help: help (); break;
     case Ans: return answer;
index 10fec970e2bb0c055151b4b01bac1f20d6f485ff..95bfa2fa4715510b3e2789a14f22d0098ae12994 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -14,6 +14,7 @@ typedef enum {
     Pow, Sqr,
     Cos, Sin, Atan,
     Log, Exp,
+    Store, Recall, Disp,
     Quit, Help,
     Ans, E, Pi
 } func_t;