#include <malloc.h>
+#include <math.h>
+#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
-#include "fdprintf.h"
#include "parser.h"
+/* global variables */
+
+double answer = 0;
+
+#define STORAGE_SIZE 10
+double storage[STORAGE_SIZE] = {0};
+
/* compare codes */
int codecmp (char *ref, char *str)
int sig;
while (*ref != '\0') {
- sig = *str++ - *ref++;
+ if (*ref == '\t') {
+ sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
+ } else {
+ sig = *str - *ref;
+ }
if (sig != 0) {
return (sig > 0) ? 1 : -1;
}
+ str++;
+ ref++;
}
return 0;
/* allocate new element */
-element_t *newelement (func_t function, int nbops)
+element_t *newelement (func_t function, int nbops, int prio)
{
element_t *new = (element_t *) calloc (1, sizeof (element_t));
if (new == NULL) {
- VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n"));
- return NULL;
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+ exit (1);
+ }
+ if (nbops) {
+ new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
+ if (new->ops == NULL) {
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+ exit (1);
+ }
}
new->func = function;
new->nbops = nbops;
+ new->prio = prio;
return new;
}
-/* functions */
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+ if ((root != NULL) && (root != ERROR_OP)) {
+ int i;
+ for (i = 0; i < root->nbops; i++) {
+ if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+ delelement (root->ops[i]);
+ }
+ }
+ if (root->nbops) {
+ free (root->ops);
+ }
+ free (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);
+ tmp->value = root->value;
+ for (i = 0; i < root->nbops; i++) {
+ tmp->ops[i] = dupelement (root->ops[i]);
+ }
+ return tmp;
+}
-#define NB_OPERATORS 7
+/* functions */
+#define NB_OPERATORS 14
keyword_t operators[NB_OPERATORS] = {
- { "+ ", Add, 2, 1 },
- { "+\t", Add, 2, 1 },
- { "- ", Sub, 2, 1 },
- { "- ", Sub, 2, 1 },
- { "*", Mul, 2, 1 },
- { "/", Div, 2, 1 },
- { "^", Pow, 2, 1 }
+ { "+\t", Add, 2, 1, 1},
+ { "-\t", Sub, 2, 1, 1},
+ { "*", Mul, 2, 1, 2},
+ { "/", Div, 2, 1, 2},
+ { "%", Mod, 2, 1, 3},
+ { "^", Pow, 2, 1, 4},
+ { "==", Equal, 2, 2, -1},
+ { "!=", Diff, 2, 2, -1},
+ { ">=", Ge, 2, 2, -1},
+ { "<=", Le, 2, 2, -1},
+ { ">", Gt, 2, 1, -1},
+ { "<", Lt, 2, 1, -1},
+ { "&", And, 2, 1, -2},
+ { "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 7
+#define NB_FUNCTIONS 17
keyword_t functions[NB_FUNCTIONS] = {
- { "sqrt", Sqr, 1, 4 },
- { "pow", Pow, 2, 3 },
- { "cos", Cos, 1, 3 },
- { "sin", Sin, 1, 3 },
- { "atan", Atn, 1, 4 },
- { "exp", Exp, 1, 3 },
- { "log", Log, 1, 3 }
+ { "sqrt", Sqr, 1, 4, 5},
+ { "pow", Pow, 2, 3, 5},
+ { "cos", Cos, 1, 3, 5},
+ { "sin", Sin, 1, 3, 5},
+ { "atan", Atan, 1, 4, 5},
+ { "exp", Exp, 1, 3, 5},
+ { "log", Log, 1, 3, 5},
+ { "sto", Store, 2, 3, 5},
+ { "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},
+ { "!", Not, 1, 1, 6},
+ { "cond", Cond, 3, 4, 5},
+ { "whl", While, 2, 3, 5}
};
+#define NB_CONSTANTS 3
+keyword_t constants[NB_CONSTANTS] = {
+ { "ans", Ans, 0, 3, 5},
+ { "e", E, 0, 1, 5},
+ { "pi", Pi, 0, 2, 5}
+};
+
+/* subparser function */
+
+element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
+{
+ element_t *new = newelement (func, nbops, prio);
+ new->ops[0] = *proot;
+ new->ops[1] = parser (*pstr, pstr, new->prio);
+ if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
+ delelement (new->ops[1]);
+ new->ops[1] = ERROR_OP;
+ }
+ if (new->ops[1] == ERROR_OP) {
+ delelement (new);
+ *proot = NULL;
+ return ERROR_OP;
+ }
+ *proot = newelement (Val, 1, 5);
+ (*proot)->ops[0] = new;
+
+ return *proot;
+}
+
/* parser function */
-element_t *parser (char *str, char **next)
+element_t *parser (char *str, char **next, int prio)
{
element_t *root = NULL;
- int i, j;
+ int i;
+
+ VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
/* main loop */
while (*str != '\0') {
int found = 0;
element_t *new = NULL;
- VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str));
+ VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
/* skip spaces and tabs */
continue;
}
- /* skip commas */
+ /* check for open brace */
- if (*str == ',') {
- if (root == NULL) {
+ if (*str == '{') {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
+ if (root != NULL) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ element_t **prog = NULL;
+ new = newelement (Prog, 0, 5);
+ root = new;
+ prog = &root;
+
+ do {
+ new = parser (str + 1, &str, 0);
+ if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+ delelement (new);
+ new = ERROR_OP;
+ }
+ if (new == ERROR_OP) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ element_t *newprog = newelement (Prog, (*prog)->nbops + 1, 5);
+ for (i = 0; i < (*prog)->nbops; i++) {
+ newprog->ops[i] = (*prog)->ops[i];
+ (*prog)->ops[i] = NULL;
+ }
+ newprog->ops[(*prog)->nbops] = new;
+ delelement (*prog);
+ (*prog) = newprog;
+ } while (*str == ',');
+
+ if (*str != '}') {
+ delelement (root);
return ERROR_OP;
- } else if (root->func != Set) {
- new = newelement (Set, MAX_OPERANDS);
- new->ops[0] = root;
- root = new;
}
str++;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
continue;
}
- /* check for parent */
+ /* check for open bracket */
- if (*str == ')') {
- if (next != NULL) {
- *next = str + 1;
- }
- return root;
- }
if (*str == '(') {
- new = parser (str + 1, &str);
- if (new == (void *)(-1)) {
- return new;
- }
+ VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
if (root) {
- for (i = 0, j = 0; i < root->nbops; i++) {
- if (root->ops[i] == NULL) {
- if (new->func == Set) {
- root->ops[i] = new->ops[j++];
- if (new->ops[j] == NULL) {
- found = 1;
- break;
- }
- } else {
+ do {
+ found = 0;
+ new = parser (str + 1, &str, 0);
+ if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+ delelement (new);
+ new = ERROR_OP;
+ }
+ if ((new == NULL) || (new == ERROR_OP)) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ for (i = 0; i < root->nbops; i++) {
+ if (root->ops[i] == NULL) {
root->ops[i] = new;
found = 1;
break;
}
}
+ if (!found) {
+ delelement (new);
+ delelement (root);
+ return ERROR_OP;
+ }
+ } while (*str == ',');
+ } else {
+ root = newelement (Val, 1, 5);
+ new = parser (str + 1, &str, 0);
+ if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+ delelement (new);
+ new = ERROR_OP;
}
- if (!found) {
+ if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
- } else {
- return root;
+ root->ops[0] = new;
}
+ if (*str != ')') {
+ delelement (root);
+ return ERROR_OP;
+ }
+ str++;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
+ continue;
+ }
+
+ /* check for closing bracket, closing brace or koma */
+
+ if ((*str == ')') || (*str == '}') || (*str == ',')) {
+ if (prio == -9) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ if (next != NULL) {
+ *next = str;
+ }
+ return root;
}
/* look for operators */
for (i = 0; i < NB_OPERATORS; i++) {
keyword_t *operator = operators + i;
if (codecmp (operator->keyword, str) == 0) {
- if ((root) && (root->func == Val)) {
- VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func));
- new = newelement (operator->func, operator->nbops);
- if (new == NULL) {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
+ if (root) {
+ if ((prio) && (prio > operator->prio)) {
+ VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
+ *next = str;
+ return root;
+ }
+ str += operator->offset;
+ VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
+ if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
- new->ops[0] = root;
- root = new;
- } else {
+ } else if (*str == '-') {
+ root = newelement (Sig, 1, 9);
+ } else {
return ERROR_OP;
}
- str += operator->offset;
found = 1;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
break;
}
}
if (found) {
- VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
continue;
}
-
+
/* look for functions */
for (i = 0; i < NB_FUNCTIONS; i++) {
keyword_t *function = functions + i;
if (codecmp (function->keyword, str) == 0) {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
if (root == NULL) {
- VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func));
- new = newelement (function->func, function->nbops);
- if (new == NULL) {
- return ERROR_OP;
- }
- root = new;
- } else {
+ VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
+ root = newelement (function->func, function->nbops, function->prio);
+ } else {
+ delelement (root);
return ERROR_OP;
}
str += function->offset;
found = 1;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
break;
}
}
if (found) {
- VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
continue;
}
- /* last attend to detect addition and substraction */
+ /* look for constant */
- if (((*str == '-') || (*str == '+')) &&
- ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
- ((root) && (root->func == Val))) {
- VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add));
- new = newelement (Add, 2);
- if (new == NULL) {
- return ERROR_OP;
+ for (i = 0; i < NB_CONSTANTS; i++) {
+ keyword_t *constant = constants + i;
+ if (codecmp (constant->keyword, str) == 0) {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
+ if (root == NULL) {
+ VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
+ root = newelement (constant->func, constant->nbops, constant->prio);
+ } else {
+ delelement (root);
+ return ERROR_OP;
+ }
+ str += constant->offset;
+ found = 1;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
+ break;
}
- new->ops[0] = root;
- root = new;
+ }
+ if (found) {
+ continue;
}
/* look for number */
if (((*str >= '0') && (*str <= '9')) ||
- (*str == '.') || (*str == '-') ||(*str == '+')) {
+ (*str == '.') || (*str == '+') || (*str == '-')) {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
char *pt;
- float value = strtof (str, &pt);
- VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value));
+ double value = strtod (str, &pt);
+ VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
if (str != pt) {
- new = newelement (Val, 1);
- new->value = value;
- if (new == NULL) {
- return ERROR_OP;
- }
- if (root == NULL) {
- root = new;
- } else {
- if (root->func == Val) {
- element_t *set = newelement (Set, MAX_OPERANDS);
- if (set == NULL) {
+ if ((root == NULL) || (root->prio == 6)) {
+ new = newelement (Val, 1, 5);
+ new->value = value;
+ if (root == NULL) {
+ root = new;
+ } else {
+ for (i = 0; i < root->nbops; i++) {
+ if (root->ops[i] == NULL) {
+ root->ops[i] = new;
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
- set->ops[0] = root;
- root = set;
}
- for (i = 0; i < root->nbops; i++) {
- if (root->ops[i] == NULL) {
- root->ops[i] = new;
- found = 1;
- break;
- }
+ str = pt;
+ } else if ((*str == '+') || (*str == '-')) {
+ if ((prio) && (prio > 1)) {
+ VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
+ *next = str;
+ return root;
}
- if (!found) {
+ if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
+ } else {
+ delelement (root);
+ return ERROR_OP;
}
- str = pt;
found = 1;
}
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
}
/* error */
if (!found) {
+ delelement (root);
return ERROR_OP;
}
}
+ if (next != NULL) {
+ *next = str;
+ }
+
return root;
}
char *func = NULL;
int i;
- if (root == NULL)
+ if ((root == NULL) || (root == ERROR_OP)) {
return;
-
+ }
+
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
switch (root->func) {
case Val: func = "Value"; break;
- case Set: func = "Set"; break;
+ case Sig: func = "Sign"; break;
case Add: func = "Addition"; break;
case Sub: func = "Subtraction"; break;
case Mul: func = "Multiplication"; break;
case Div: func = "Division"; break;
+ case Mod: func = "Modulo"; break;
case Pow: func = "Power"; break;
case Sqr: func = "Square Root"; break;
case Cos: func = "Cosine"; break;
case Sin: func = "Sine"; break;
- case Atn: func = "Arc Tangent"; break;
+ case Atan: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
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;
+ case Ans: func = "Ans"; break;
+ case Pi: func = "Pi"; break;
+ case E: func = "E"; break;
+ case Equal: func = "Equal"; break;
+ case Diff: func = "Different"; break;
+ case Ge: func = "Greater or equal"; break;
+ case Le: func = "Lesser or equal"; break;
+ case Gt: func = "Greater"; break;
+ case Lt: func = "Lesser"; break;
+ case And: func = "And"; break;
+ case Or: func = "Or"; break;
+ case Not: func = "Not"; break;
+ case Cond: func = "Condition"; break;
+ case While: func = "While"; break;
+ case Prog: func = "Program"; break;
}
- PRINTOUT ("Function: %s\n", func);
+ fprintf (stdout, "Function: %s\n", func);
- if (root->func == Val) {
+ if ((root->func == Val) && (root->ops[0] == NULL)) {
for (i = 0; i < level; i++) {
- PRINTOUT (" ");
+ fprintf (stdout, " ");
}
- PRINTOUT ("value: %f\n", root->value);
+ fprintf (stdout, "value: %f\n", root->value);
} else {
for (i = 0; i < root->nbops; i++) {
print_element (root->ops[i], level + 1);
}
}
+/* storage functions */
+
+double store (int index, double value)
+{
+ if ((index > 0) && (index <= STORAGE_SIZE)) {
+ storage[index - 1] = value;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+ }
+ return value;
+}
+
+double recall (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 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;
+ fprintf (stdout, "storage:");
+ for (i = 0; i < STORAGE_SIZE; i++) {
+ fprintf (stdout, " %g", storage[i]);
+ }
+ 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"));
+ 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;
+}
+
+/* program function */
+
+double program_do (element_t **prog, int nbcalls)
+{
+ double ret = 0;
+ int i;
+ for (i = 0; i < nbcalls; i++) {
+ ret = evaluate_element (prog[i], 0);
+ prog[i] = NULL;
+ }
+ return ret;
+}
+
+/* quit function */
+
+void quit (void)
+{
+ fprintf (stdout, "bye\n");
+ exit (0);
+}
+
+/* help message */
+
+void help (void)
+{
+ fprintf (stdout, "calc is a simple calculator\n\n");
+ fprintf (stdout, "supported operators:");
+ fprintf (stdout, " + - * / %% ^\n");
+ fprintf (stdout, "camparison operators:");
+ fprintf (stdout, " == != >= <= > <\n");
+ fprintf (stdout, "logical operators:");
+ fprintf (stdout, " & | !\n");
+ fprintf (stdout, "supported functions:");
+ fprintf (stdout, " pow sqrt cos sin atan log exp\n");
+ fprintf (stdout, "storage functions:");
+ fprintf (stdout, " sto rcl inc dec\n");
+ fprintf (stdout, "conditional functions:");
+ fprintf (stdout, " cond\n");
+ fprintf (stdout, "miscellaneous functions:");
+ fprintf (stdout, " quit help\n");
+ fprintf (stdout, "supported constants:");
+ fprintf (stdout, " e pi\n");
+}
+
+/* evaluate element tree */
+
+#define MASK_SUB 0x1
+#define MASK_DIV 0x2
+
+double evaluate_element (element_t *root, char mask)
+{
+ double op0 = 0, op1 = 0;
+ char nextmask = mask;
+
+ if ((root == NULL) || (root == ERROR_OP)) {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
+ return 0;
+ }
+
+ /* mask to manage sub operator sub and div */
+ switch (root->func) {
+ case Add:
+ nextmask &= ~MASK_SUB;
+ nextmask &= ~MASK_DIV;
+ break;
+ case Sub:
+ nextmask |= MASK_SUB;
+ nextmask &= ~MASK_DIV;
+ break;
+ case Mul:
+ nextmask &= ~MASK_DIV;
+ break;
+ case Div:
+ nextmask |= MASK_DIV;
+ break;
+ default:
+ nextmask = mask;
+ }
+
+ switch (root->func) {
+ case Val:
+ case Sig:
+ op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
+ break;
+ case Add:
+ case Sub:
+ case Mul:
+ case Div:
+ case Mod:
+ case Pow:
+ case Store:
+ case Equal:
+ case Diff:
+ case Ge:
+ case Le:
+ case Gt:
+ case Lt:
+ case And:
+ case Or:
+ if (root->ops[1]) {
+ op1 = evaluate_element (root->ops[1], nextmask);
+ } else if (root->func != Store) {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
+ return 0;
+ }
+ /* fallthrough */
+ case Sqr:
+ case Cos:
+ case Sin:
+ case Atan:
+ case Log:
+ case Exp:
+ case Recall:
+ case Inc:
+ case Dec:
+ case Not:
+ case Cond:
+ if (root->ops[0]) {
+ op0 = evaluate_element (root->ops[0], 0);
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+ return 0;
+ }
+ break;
+ case Disp:
+ case Quit:
+ case Help:
+ case Ans:
+ case Pi:
+ case E:
+ case Prog:
+ break;
+ case While:
+ if (root->ops[0] == NULL) {
+ VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
+ return 0;
+ }
+ break;
+ }
+
+ switch (root->func) {
+ case Val: return op0;
+ case Sig: return -op0;
+ case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
+ case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
+ case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
+ case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
+ case Mod: return fmod (op0, op1);
+ case Pow: return pow (op0, op1);
+ case Sqr: return sqrt (op0);
+ case Cos: return cos (op0);
+ case Sin: return sin (op0);
+ case Atan: return atan (op0);
+ case Log: return log (op0);
+ 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;
+ case Ans: return answer;
+ case Pi: return M_PI;
+ case E: return M_E;
+ case Equal: return op0 == op1;
+ case Diff: return op0 != op1;
+ case Ge: return op0 >= op1;
+ case Le: return op0 <= op1;
+ case Gt: return op0 > op1;
+ case Lt: return op0 < op1;
+ case And: return (op0 != 0) && (op1 != 0);
+ case Or: return (op0 != 0) || (op1 != 0);
+ case Not: return (op0 == 0);
+ case Cond:
+ if ((op0) && (root->ops[1])) {
+ return evaluate_element (root->ops[1], 0);
+ } else if ((!op0) && (root->ops[2])) {
+ return evaluate_element (root->ops[2], 0);
+ } else {
+ return 0;
+ }
+ case While: return while_do (root->ops[0], root->ops[1]);
+ case Prog: return program_do (root->ops, root->nbops);
+ }
+
+ return 0;
+}
+
/* vim: set ts=4 sw=4 et: */