add increase and decrease features
authorLaurent Mazet <mazet@softndesign.org>
Mon, 23 Jan 2023 22:17:23 +0000 (23:17 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 23 Jan 2023 22:17:23 +0000 (23:17 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 4ced168785a883f40544b16ddb8876083f8cc625..02d0d9803ad91e1df4474be7ca5951faf47d097a 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -287,5 +287,7 @@ int main (int argc, char *argv[])
 // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe  | grep -q '=> 1808\.04'
 // test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
 // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 =
+// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1'
+// test: echo -e 'inc\ninc (\ndec\ndec ('| calc.exe 2>&1 | grep -c error | xargs test 4 =
 
 /* vim: set ts=4 sw=4 et: */
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;
index 409b4d43c16d4860c549d3f2af0cf63b2b5560e1..540f1c81ce46cc61965a58f1348d41550deba89f 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -14,7 +14,7 @@ typedef enum {
     Pow, Sqr,
     Cos, Sin, Atan,
     Log, Exp,
-    Store, Recall, Disp,
+    Store, Recall, Inc, Dec, Disp,
     Quit, Help,
     Ans, E, Pi,
     Equal, Diff, Ge, Le, Gt, Lt,