partial while feature
[calc.git] / parser.c
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;