add increase and decrease features
[calc.git] / parser.c
index 8007f862c973707194388139c9092cd63ec0dd67..c3472876572965c8ee7ce7a92c50d9b635fc6186 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -87,7 +87,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 14
+#define NB_FUNCTIONS 16
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -98,6 +98,8 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "log",  Log, 1, 3, 5},
     { "sto",  Store, 2, 3, 9},
     { "rcl",  Recall, 1, 3, 5},
+    { "inc",  Inc, 1, 3, 5},
+    { "dec",  Dec, 1, 3, 5},
     { "disp", Disp, 0, 4, 9},
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
@@ -416,6 +418,8 @@ void print_element (element_t *root, int level)
     case Exp: func = "Exponantial"; break;
     case Store: func = "Store"; break;
     case Recall: func = "Recall"; break;
+    case Inc: func = "Increase"; break;
+    case Dec: func = "Decrease"; break;
     case Disp: func = "Display"; break;
     case Quit: func = "Quit"; break;
     case Help: func = "Help"; break;
@@ -470,6 +474,26 @@ double recall (int index)
     return 0;
 }
 
+double increase (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;
+}
+
+double decrease (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;
@@ -502,7 +526,7 @@ void help (void)
     fprintf (stdout, "supported functions:");
     fprintf (stdout, " pow sqrt cos sin atan log exp\n");
     fprintf (stdout, "storage functions:");
-    fprintf (stdout, " sto rcl\n");
+    fprintf (stdout, " sto rcl inc dec\n");
     fprintf (stdout, "conditional functions:");
     fprintf (stdout, " cond\n");
     fprintf (stdout, "miscellaneous functions:");
@@ -580,6 +604,8 @@ double evaluate_element (element_t *root, char mask)
     case Log:
     case Exp:
     case Recall:
+    case Inc:
+    case Dec:
     case Not:
     case Cond:
         if (root->ops[0]) {
@@ -615,6 +641,8 @@ double evaluate_element (element_t *root, char mask)
     case Exp: return exp (op0);
     case Store: return store ((int)op0, (op1) ? op1 : answer);
     case Recall: return recall ((int)op0);
+    case Inc: return increase ((int)op0);
+    case Dec: return decrease ((int)op0);
     case Disp: display (); break;
     case Quit: quit (); break;
     case Help: help (); break;