enhance store feature
authorLaurent Mazet <mazet@softndesign.org>
Sun, 22 Jan 2023 08:33:23 +0000 (09:33 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 22 Jan 2023 08:33:23 +0000 (09:33 +0100)
calc.c
parser.c

diff --git a/calc.c b/calc.c
index 9e2134426de8b15aeefe5a91fd7ab12af28194df..fe752407871ae429ab4a4a91aa8df1abdbb02059 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -246,6 +246,6 @@ int main (int argc, char *argv[])
 // 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'
+// 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'
 
 /* vim: set ts=4 sw=4 et: */
index e2c4673f4106d4c80074cc30b22e5f471cec3f45..43595e53845ade3e2476755fb8d791a620416ecf 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -89,7 +89,7 @@ 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},
+    { "sto",  Store, 2, 3, 5},
     { "rcl",  Recall, 1, 3, 5},
     { "disp",  Disp, 0, 4, 5},
     { "quit", Quit, 0, 4, 5},
@@ -400,14 +400,14 @@ void print_element (element_t *root, int level)
 
 /* storage functions */
 
-double store (int index)
+double store (int index, double value)
 {
     if ((index > 0) && (index <= STORAGE_SIZE)) {
-        storage[index - 1] = answer;
+        storage[index - 1] = value;
     } else {
         VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
     }
-    return answer;
+    return value;
 }
 
 double recall (int index)
@@ -501,9 +501,10 @@ double evaluate_element (element_t *root, char mask)
     case Div:
     case Mod:
     case Pow:
+    case Store:
         if (root->ops[1]) {
             op1 = evaluate_element (root->ops[1], nextmask);
-        } else {
+        } else if (root->func != Store) {
             VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
             return 0;
         }
@@ -514,7 +515,6 @@ 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);
@@ -547,7 +547,7 @@ 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 Store: return store ((int)op0, (op1) ? op1 : answer);
     case Recall: return recall ((int)op0);
     case Disp: display (); break;
     case Quit: quit (); break;