partial while feature
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 24 Jan 2023 18:25:13 +0000 (19:25 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 24 Jan 2023 18:25:13 +0000 (19:25 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 611e1550be12d901a5d7d0b88acac1eedae8dbab..41429cb93771d0871255224aaae06095746a9125 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -291,5 +291,6 @@ int main (int argc, char *argv[])
 // test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\ndec (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 =
 // test: echo -e 'inc (11)\ndec (0)' | calc.exe 2>&1 | grep -c invalid | xargs test 2 =
+// test: echo -e 'whl (inc (1) < 3, sto (rcl (2) + 3))' | calc.exe | xargs test 16 =
 
 /* vim: set ts=4 sw=4 et: */
index c3472876572965c8ee7ce7a92c50d9b635fc6186..8654fa8a78a61a3c6e90bc0c11de5f48566c8120 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -67,6 +67,31 @@ void delelement (element_t *root)
     }
 }
 
+/* duplicate element */
+
+element_t *dupelement (element_t *root)
+{
+    element_t *tmp = NULL;
+    int i;
+
+    if ((root == NULL) || (root == ERROR_OP)) {
+        return root;
+    }
+    tmp = newelement (root->func, root->nbops, root->prio);
+    if (tmp == NULL) {
+        return ERROR_OP;
+    }
+    tmp->value = root->value;
+    for (i = 0; i < root->nbops; i++) {
+        tmp->ops[i] = dupelement (root->ops[i]);
+        if (tmp->ops[i] == ERROR_OP) {
+            delelement (tmp);
+            return ERROR_OP;
+        }
+    }
+    return tmp;
+}
+
 /* functions */
 
 #define NB_OPERATORS 14
@@ -87,7 +112,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 16
+#define NB_FUNCTIONS 17
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -96,7 +121,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "atan", Atan, 1, 4, 5},
     { "exp",  Exp, 1, 3, 5},
     { "log",  Log, 1, 3, 5},
-    { "sto",  Store, 2, 3, 9},
+    { "sto",  Store, 2, 3, 5},
     { "rcl",  Recall, 1, 3, 5},
     { "inc",  Inc, 1, 3, 5},
     { "dec",  Dec, 1, 3, 5},
@@ -104,7 +129,8 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "quit", Quit, 0, 4, 9},
     { "help", Help, 0, 4, 9},
     { "!",    Not, 1, 1, 6},
-    { "cond", Cond, 3, 4, 5}
+    { "cond", Cond, 3, 4, 5},
+    { "whl",  While, 2, 3, 5}
 };
 
 #define NB_CONSTANTS 3
@@ -436,6 +462,7 @@ void print_element (element_t *root, int level)
     case Or: func = "Or"; break;
     case Not: func = "Not"; break;
     case Cond: func = "Condition"; break;
+    case While: func = "While"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -504,6 +531,35 @@ void display (void)
     fprintf (stdout, "\n");
 }
 
+/* While do function */
+
+double while_do (element_t *cond, element_t *action)
+{
+    double ret = 0;
+    element_t *temp = NULL;
+
+    VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
+    if (cond == NULL) {
+        return ret;
+    }
+    while (1) {
+        VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
+
+        temp = dupelement (cond);
+        if (!evaluate_element (temp, 0)) {
+            break;
+        }
+        if (action) {
+            temp = dupelement (action);
+            ret = evaluate_element (temp, 0);
+        }
+    }
+
+    VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
+
+    return ret;
+}
+
 /* quit function */
 
 void quit (void)
@@ -621,6 +677,7 @@ double evaluate_element (element_t *root, char mask)
     case Ans:
     case Pi:
     case E:
+    case While:
         break;
     }
 
@@ -666,6 +723,7 @@ double evaluate_element (element_t *root, char mask)
         } else {
             return 0;
         }
+    case While: return while_do (root->ops[0], root->ops[1]);
     }
 
     return 0;
index 540f1c81ce46cc61965a58f1348d41550deba89f..49acc2080005a8c4cef2f635c71010889f71d09a 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -19,7 +19,7 @@ typedef enum {
     Ans, E, Pi,
     Equal, Diff, Ge, Le, Gt, Lt,
     And, Or, Not,
-    Cond
+    Cond, While
 } func_t;
 
 /* keyword type */