enhance store feature
[calc.git] / parser.c
index f3836c0e83550feeb75b1e5672054e3c46bf5546..43595e53845ade3e2476755fb8d791a620416ecf 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -7,6 +7,13 @@
 
 #include "parser.h"
 
+/* global variables */
+
+double answer = 0;
+
+#define STORAGE_SIZE 10
+double storage[STORAGE_SIZE] = {0};
+
 /* compare codes */
 
 int codecmp (char *ref, char *str)
@@ -73,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},
@@ -82,14 +89,18 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "atan", Atan, 1, 4, 5},
     { "exp",  Exp, 1, 3, 5},
     { "log",  Log, 1, 3, 5},
+    { "sto",  Store, 2, 3, 5},
+    { "rcl",  Recall, 1, 3, 5},
+    { "disp",  Disp, 0, 4, 5},
     { "quit", Quit, 0, 4, 5},
     { "help", Help, 0, 4, 5}
 };
 
-#define NB_CONSTANTS 2
+#define NB_CONSTANTS 3
 keyword_t constants[NB_CONSTANTS] = {
-    { "e",  E,  0, 1, 5},
-    { "pi", Pi, 0, 2, 5}
+    { "ans", Ans, 0, 3, 5},
+    { "e",   E, 0, 1, 5},
+    { "pi",  Pi, 0, 2, 5}
 };
 
 /* subparser function */
@@ -261,7 +272,7 @@ element_t *parser (char *str, char **next, int prio)
             if (codecmp (constant->keyword, str) == 0) {
                 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
                 if (root == NULL) {
-                    VERBOSE (INFO, fprintf (stdout, "Func: %d\n", constant->func));
+                    VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
                     new = newelement (constant->func, constant->nbops, constant->prio);
                     if (new == NULL) {
                         return ERROR_OP;
@@ -298,18 +309,13 @@ element_t *parser (char *str, char **next, int prio)
                     new->value = value;
                     root = new;
                     str = pt;
-                } else if (root->func == Val) {
-                    if ((*str == '+') || (*str == '-')) {
-                        if ((prio) && (prio > 1)) {
-                            VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
-                            *next = str;
-                            return root;
-                        }
-                        if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
-                            delelement (root);
-                            return ERROR_OP;
-                        }
-                    } else {
+                } else if ((*str == '+') || (*str == '-')) {
+                    if ((prio) && (prio > 1)) {
+                        VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
+                        *next = str;
+                        return root;
+                    }
+                    if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
                         delelement (root);
                         return ERROR_OP;
                     }
@@ -368,8 +374,12 @@ 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;
     case Pi: func = "Pi"; break;
     case E: func = "E"; break;
     }
@@ -388,6 +398,38 @@ void print_element (element_t *root, int level)
     }
 }
 
+/* storage functions */
+
+double store (int index, double value)
+{
+    if ((index > 0) && (index <= STORAGE_SIZE)) {
+        storage[index - 1] = value;
+    } else {
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+    }
+    return value;
+}
+
+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)
@@ -405,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");
@@ -457,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;
         }
@@ -470,6 +515,7 @@ double evaluate_element (element_t *root, char mask)
     case Atan:
     case Log:
     case Exp:
+    case Recall:
         if (root->ops[0]) {
             op0 = evaluate_element (root->ops[0], 0);
         } else {
@@ -477,8 +523,10 @@ double evaluate_element (element_t *root, char mask)
             return 0;
         }
         break;
+    case Disp:
     case Quit:
     case Help:
+    case Ans:
     case Pi:
     case E:
         break;
@@ -499,8 +547,12 @@ 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, (op1) ? op1 : answer);
+    case Recall: return recall ((int)op0);
+    case Disp: display (); break;
     case Quit: quit (); break;
     case Help: help (); break;
+    case Ans: return answer;
     case Pi: return M_PI;
     case E: return M_E;
     }