}
}
+/* 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
{ "|", 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},
{ "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},
{ "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
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);
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)
case Ans:
case Pi:
case E:
+ case While:
break;
}
} else {
return 0;
}
+ case While: return while_do (root->ops[0], root->ops[1]);
}
return 0;