if (buffer[i] == '\n') {
buffer[i] = 0;
VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
- element_t *element = parser (buffer, NULL);
+ element_t *element = parser (buffer, NULL, 0);
if (element == (void *)(-1)) {
VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
ret = 1;
// test: echo "log (2)" | calc.exe
// test: echo "1 + 2 - 3" | calc.exe
// test: echo "1 + cos (2 - 3)" | calc.exe
+// test: echo "cos (1 / 2) * 3" | calc.exe
// test: echo "1 + 4 * (2 - 3)" | calc.exe
// test: echo "(2 - 3) / 4" | calc.exe
// test: echo "pow (2 - 3, 8 / 3)" | calc.exe
// test: echo "-1+2" | calc.exe
// test: echo "1-2" | calc.exe
// test: echo "1 * 2 / 3 + 4" | calc.exe
-// test: echo "2 ^ 3 * 4 + cos (5)" | calc.exe
+// test: echo "2 ^ 3 * 4 + 5" | calc.exe
+// test: echo "2 + 3 * 4 ^ 5" | calc.exe
+// test: echo "2 ^ 3 * 4 + cos(5/6)" | calc.exe
/* vim: set ts=4 sw=4 et: */
/* 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) {
+ if ((prio) && (prio > operator->prio)) {
+ VERBOSE (DEBUG, PRINTOUT ("stop processing operator 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, operator->prio);
if (new == NULL) {
return ERROR_OP;
}
- element_t *next = parser (str, &str);
- if (next == ERROR_OP) {
+ new->ops[0] = root;
+ new->ops[1] = parser (str, &str, new->prio);
+ if (new->ops[1] == 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;
+ root = newelement (Val, 1, 4);
+ if (root == ERROR_OP) {
+ return ERROR_OP;
}
+ root->ops[0] = new;
} else {
return ERROR_OP;
}