#include <malloc.h>
+#include <math.h>
#include <stdlib.h>
#include "debug.h"
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) {
}
new->func = function;
new->nbops = nbops;
+ new->prio = prio;
return new;
}
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+ int i;
+ if ((root != NULL) && (root != ERROR_OP)) {
+ for (i = 0; i < root->nbops; i++) {
+ if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+ delelement (root->ops[i]);
+ }
+ }
+ free (root);
+ }
+}
+
/* functions */
-#define NB_OPERATORS 7
+#define NB_OPERATORS 6
keyword_t operators[NB_OPERATORS] = {
- { "+ ", Add, 2, 1 },
- { "+\t", Add, 2, 1 },
- { "- ", Sub, 2, 1 },
- { "-\t", 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}
};
-#define NB_FUNCTIONS 7
+#define NB_FUNCTIONS 9
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", Atn, 1, 4, 5},
+ { "exp", Exp, 1, 3, 5},
+ { "log", Log, 1, 3, 5},
+ { "quit", Qui, 0, 4, 5},
+ { "help", Hel, 0, 4, 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) {
+ delelement (new);
+ *proot = NULL;
+ return ERROR_OP;
+ }
+ *proot = newelement (Val, 1, 5);
+ if (*proot == NULL) {
+ delelement (new);
+ 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, j;
+ int i;
VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
continue;
}
- /* skip commas */
-
- if (*str == ',') {
- if (root == NULL) {
- return ERROR_OP;
- } else if (root->func != Set) {
- new = newelement (Set, MAX_OPERANDS);
- new->ops[0] = root;
- root = new;
- }
- str++;
- continue;
- }
-
- /* check for parent */
+ /* check for open bracket */
if (*str == '(') {
VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
- new = parser (str + 1, &str);
- if (new == ERROR_OP) {
- return ERROR_OP;
- }
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 == 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);
+ if (root == NULL) {
+ return ERROR_OP;
}
- if (!found) {
+ new = parser (str + 1, &str, 0);
+ if ((new == ERROR_OP) || (*str == ',')) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
+ root->ops[0] = new;
}
+ str++;
VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
- if (next != NULL) {
- *next = str;
- }
- return root;
+ continue;
}
- if (*str == ')') {
+
+ /* check for closing bracket or koma */
+
+ if ((*str == ')') || (*str == ',')) {
if (next != NULL) {
- *next = str + 1;
+ *next = str;
}
return root;
}
keyword_t *operator = operators + i;
if (codecmp (operator->keyword, str) == 0) {
VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
- str += operator->offset;
- if ((root) && (root->func != Set)) {
+ if (root) {
+ if ((prio) && (prio > operator->prio)) {
+ VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ *next = str;
+ return root;
+ }
+ str += operator->offset;
VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
- new = newelement (operator->func, operator->nbops);
- if (new == NULL) {
+ if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
- new->ops[0] = root;
- root = new;
- new = parser (str, &str);
- if (new == ERROR_OP) {
+ } else if (*str == '-') {
+ new = newelement (Sig, 1, 9);
+ if (new == NULL) {
return ERROR_OP;
}
- root->ops[1] = new;
+ root = new;
} else {
return ERROR_OP;
}
VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
if (root == NULL) {
VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
- new = newelement (function->func, function->nbops);
+ new = newelement (function->func, function->nbops, function->prio);
if (new == NULL) {
return ERROR_OP;
}
root = new;
- } else {
+ } else {
+ delelement (root);
return ERROR_OP;
}
str += function->offset;
continue;
}
- /* last attend to detect addition and substraction */
-
- if (((*str == '-') || (*str == '+')) &&
- ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
- ((root) && (root->func == Val))) {
- VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
- new = newelement (Add, 2);
- if (new == NULL) {
- return ERROR_OP;
- }
- new->ops[0] = root;
- root = new;
- }
-
/* look for number */
- if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+ if (((*str >= '0') && (*str <= '9')) ||
+ (*str == '.') || (*str == '+') || (*str == '-')) {
VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
char *pt;
- float value = strtof (str, &pt);
+ double value = strtod (str, &pt);
VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
if (str != pt) {
- new = newelement (Val, 1);
- if (new == NULL) {
- return ERROR_OP;
- }
- new->value = value;
if (root == NULL) {
+ new = newelement (Val, 1, 5);
+ if (new == NULL) {
+ return ERROR_OP;
+ }
+ new->value = value;
root = new;
- } else {
- if (root->func == Val) {
- element_t *set = newelement (Set, MAX_OPERANDS);
- if (set == NULL) {
- return ERROR_OP;
+ str = pt;
+ } else if (root->func == Val) {
+ if ((*str == '+') || (*str == '-')) {
+ if ((prio) && (prio > 1)) {
+ VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
+ *next = str;
+ return root;
}
- 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;
+ if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+ delelement (root);
+ return ERROR_OP;
}
- }
- if (!found) {
+ } else {
+ delelement (root);
return ERROR_OP;
}
+ } else {
+ delelement (root);
+ return ERROR_OP;
}
- str = pt;
found = 1;
}
VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
/* error */
if (!found) {
+ delelement (root);
return ERROR_OP;
}
if (next != NULL) {
*next = str;
}
+
return root;
}
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 Atn: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
case Exp: func = "Exponantial"; break;
+ case Qui: func = "Quit"; break;
+ case Hel: func = "Help"; break;
}
PRINTOUT ("Function: %s\n", func);
- if (root->func == Val) {
+ if ((root->func == Val) && (root->ops[0] == NULL)) {
for (i = 0; i < level; i++) {
PRINTOUT (" ");
}
}
}
+/* quit function */
+
+void quit (void)
+{
+ PRINTOUT ("bye\n");
+ exit (0);
+}
+
+/* help message */
+
+void help (void)
+{
+ PRINTOUT ("calc is a simple calculator\n\n");
+ PRINTOUT ("supported operators:\n");
+ PRINTOUT (" + - * / %% ^\n\n");
+ PRINTOUT ("supported functions:\n");
+ PRINTOUT (" pow sqrt cos sin atan log exp\n\n");
+ PRINTOUT ("miscellaneous functions:\n");
+ PRINTOUT (" quit help\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, PRINTOUT ("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:
+ if (root->ops[1]) {
+ op1 = evaluate_element (root->ops[1], nextmask);
+ } 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], 0);
+ } else {
+ VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
+ return 0;
+ }
+ break;
+ case Qui:
+ case Hel:
+ 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 Atn: return atan (op0);
+ case Log: return log (op0);
+ case Exp: return exp (op0);
+ case Qui: quit (); break;
+ case Hel: help (); break;
+ }
+
+ return 0;
+}
+
/* vim: set ts=4 sw=4 et: */