/* parser function */
-element_t *parser (char *str) {
+element_t *parser (char *str, char **next)
+{
element_t *root = NULL;
- int i;
+ int i, j;
/* main loop */
while (*str != '\0') {
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 */
+
+ if (*str == ')') {
+ if (next != NULL) {
+ *next = str + 1;
+ }
+ return root;
+ }
+ if (*str == '(') {
+ new = parser (str + 1, &str);
+ if (new == (void *)(-1)) {
+ return new;
+ }
+ 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 {
+ root->ops[i] = new;
+ found = 1;
+ break;
+ }
+ }
+ }
+ if (!found) {
+ return ERROR_OP;
+ }
+ } else {
+ return root;
+ }
+ }
+
/* look for operators */
for (i = 0; i < NB_OPERATORS; i++) {
VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func));
new = newelement (operator->func, operator->nbops);
if (new == NULL) {
- return (element_t *)-1;
+ return ERROR_OP;
}
new->ops[0] = root;
root = new;
} else {
- return (element_t *)(-1);
+ return ERROR_OP;
}
str += operator->offset;
found = 1;
VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func));
new = newelement (function->func, function->nbops);
if (new == NULL) {
- return (element_t *)-1;
+ return ERROR_OP;
}
root = new;
} else {
- return (element_t *)(-1);
+ return ERROR_OP;
}
str += function->offset;
found = 1;
continue;
}
- /* last attend to detect addition and substraction */
+ /* last attend to detect addition and substraction */
- if (((*str == '-') || (*str == '+')) &&
- ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
- ((root) && (root->func == Val))) {
+ 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 (element_t *)-1;
+ return ERROR_OP;
}
new->ops[0] = root;
root = new;
- }
+ }
/* look for number */
new = newelement (Val, 1);
new->value = value;
if (new == NULL) {
- return (element_t *)-1;
+ return ERROR_OP;
}
if (root == NULL) {
root = new;
} else {
+ if (root->func == Val) {
+ element_t *set = newelement (Set, MAX_OPERANDS);
+ if (set == NULL) {
+ 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;
}
}
if (!found) {
- return (element_t *)-1;
+ return ERROR_OP;
}
}
str = pt;
/* error */
if (!found) {
- return (element_t *)-1;
+ return ERROR_OP;
}
}
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;