#include <malloc.h>
+#include <math.h>
#include <stdlib.h>
#include "debug.h"
/* functions */
-#define NB_OPERATORS 5
+#define NB_OPERATORS 6
keyword_t operators[NB_OPERATORS] = {
{ "+\t", Add, 2, 1, 1},
{ "-\t", Sub, 2, 1, 1},
{ "*", Mul, 2, 1, 2},
{ "/", Div, 2, 1, 2},
- { "^", Pow, 2, 1, 3}
+ { "%", Mod, 2, 1, 3},
+ { "^", Pow, 2, 1, 4}
};
#define NB_FUNCTIONS 7
keyword_t functions[NB_FUNCTIONS] = {
- { "sqrt", Sqr, 1, 4, 4},
- { "pow", Pow, 2, 3, 4},
- { "cos", Cos, 1, 3, 4},
- { "sin", Sin, 1, 3, 4},
- { "atan", Atn, 1, 4, 4},
- { "exp", Exp, 1, 3, 4},
- { "log", Log, 1, 3, 4}
+ { "sqrt", Sqr, 1, 4, 5},
+ { "pow", Pow, 2, 3, 5},
+ { "cos", Cos, 1, 3, 5},
+ { "sin", Sin, 1, 3, 5},
+ { "atan", Atn, 1, 4, 5},
+ { "exp", Exp, 1, 3, 5},
+ { "log", Log, 1, 3, 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);
+ if (new == NULL) {
+ return ERROR_OP;
+ }
+ new->ops[0] = *proot;
+ new->ops[1] = parser (*pstr, pstr, new->prio);
+ if (new->ops[1] == ERROR_OP) {
+ return ERROR_OP;
+ }
+ *proot = newelement (Val, 1, 4);
+ if (*proot == ERROR_OP) {
+ return ERROR_OP;
+ }
+ (*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;
if (root) {
do {
found = 0;
- new = parser (str + 1, &str);
+ new = parser (str + 1, &str, 0);
if (new == ERROR_OP) {
return ERROR_OP;
}
if (root == NULL) {
return ERROR_OP;
}
- new = parser (str + 1, &str);
+ new = parser (str + 1, &str, 0);
if ((new == ERROR_OP) || (*str == ',')) {
return ERROR_OP;
}
keyword_t *operator = operators + i;
if (codecmp (operator->keyword, str) == 0) {
VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
- str += operator->offset;
if (root) {
- VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
- new = newelement (operator->func, operator->nbops, operator->prio);
- if (new == NULL) {
- return ERROR_OP;
+ if ((prio) && (prio > operator->prio)) {
+ VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ *next = str;
+ return root;
}
- element_t *next = parser (str, &str);
- if (next == ERROR_OP) {
+ str += operator->offset;
+ VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
+ if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
return ERROR_OP;
}
- VERBOSE (DEBUG, PRINTOUT ("Priorities: %d > %d -> %s\n", new->prio, next->prio, (new->prio > next->prio) ? "True" : "False"));
- if (new->prio > next->prio) {
- VERBOSE (DEBUG, PRINTOUT ("Take account of priority\n"));
- new->ops[0] = root;
- new->ops[1] = next->ops[0];
- next->ops[0] = new;
- root = next;
- } else {
- new->ops[0] = root;
- new->ops[1] = next;
- root = new;
- }
} else {
return ERROR_OP;
}
float value = strtof (str, &pt);
VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
if (str != pt) {
- new = newelement (Val, 1, 4);
- if (new == NULL) {
- return ERROR_OP;
- }
- new->value = value;
if (root == NULL) {
+ new = newelement (Val, 1, 4);
+ if (new == NULL) {
+ return ERROR_OP;
+ }
+ new->value = value;
root = new;
+ str = pt;
} else if (root->func == Val) {
if ((*str == '+') || (*str == '-')) {
- element_t *add = newelement (Add, 2, 1);
- if (add == NULL) {
+ if ((prio) && (prio > 1)) {
+ VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ *next = str;
+ return root;
+ }
+ if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
return ERROR_OP;
}
- add->ops[0] = root;
- add->ops[1] = new;
- root = add;
} else {
return ERROR_OP;
}
} else {
return ERROR_OP;
}
- str = pt;
found = 1;
}
VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
switch (root->func) {
case Val: func = "Value"; break;
- case Set: func = "Set"; 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;
}
}
+/* evaluate element tree */
+
+double evaluate_element (element_t *root)
+{
+ double op0 = 0, op1 = 0;
+
+ if ((root == NULL) || (root == ERROR_OP)) {
+ VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
+ return 0;
+ }
+
+ switch (root->func) {
+ case Val:
+ if (root->ops[0]) {
+ return evaluate_element (root->ops[0]);
+ } else {
+ return root->value;
+ }
+ break;
+ case Add:
+ case Sub:
+ case Mul:
+ case Div:
+ case Mod:
+ case Pow:
+ if (root->ops[1]) {
+ op1 = evaluate_element (root->ops[1]);
+ } else {
+ VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
+ return 0;
+ }
+ /* fallthrough */
+ case Sqr:
+ case Cos:
+ case Sin:
+ case Atn:
+ case Log:
+ case Exp:
+ if (root->ops[0]) {
+ op0 = evaluate_element (root->ops[0]);
+ } else {
+ VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+ return 0;
+ }
+ }
+
+ switch (root->func) {
+ case Add: return op0 + op1;
+ case Sub: return op0 - op1;
+ case Mul: return op0 * op1;
+ case Div: return 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 Atn: return atan (op0);
+ case Log: return log (op0);
+ case Exp: return exp (op0);
+ default: break;
+ }
+
+ return 0;
+}
+
/* vim: set ts=4 sw=4 et: */