/* read from input stream */
while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
- VERBOSE (DEBUG, PRINTOUT ("read %d bytes\n", n));
+ VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n));
n += (pt - buffer);
if ((n == 2) && (buffer[0] == '.')) {
return 0;
for (i = 0, j = 0; i < n; i++) {
if (buffer[i] == '\n') {
buffer[i] = 0;
- VERBOSE (DEBUG, PRINTOUT ("line(%d): %s\n", j, buffer + j));
+ VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
element_t *element = parser (buffer, NULL);
if (element == (void *)(-1)) {
VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
{ "+ ", Add, 2, 1 },
{ "+\t", Add, 2, 1 },
{ "- ", Sub, 2, 1 },
- { "- ", Sub, 2, 1 },
+ { "-\t", Sub, 2, 1 },
{ "*", Mul, 2, 1 },
{ "/", Div, 2, 1 },
{ "^", Pow, 2, 1 }
element_t *root = NULL;
int i, j;
+ VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
+
/* main loop */
while (*str != '\0') {
int found = 0;
element_t *new = NULL;
- VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str));
+ VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
/* skip spaces and tabs */
/* check for parent */
- if (*str == ')') {
- if (next != NULL) {
- *next = str + 1;
- }
- return root;
- }
if (*str == '(') {
+ VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
new = parser (str + 1, &str);
- if (new == (void *)(-1)) {
- return new;
+ if (new == ERROR_OP) {
+ return ERROR_OP;
}
if (root) {
for (i = 0, j = 0; i < root->nbops; i++) {
if (!found) {
return ERROR_OP;
}
- } else {
- return root;
}
+ VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
+ if (next != NULL) {
+ *next = str;
+ }
+ return root;
+ }
+ if (*str == ')') {
+ if (next != NULL) {
+ *next = str + 1;
+ }
+ 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));
+ VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
+ str += operator->offset;
+ if ((root) && (root->func != Set)) {
+ VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
new = newelement (operator->func, operator->nbops);
if (new == NULL) {
return ERROR_OP;
}
new->ops[0] = root;
root = new;
- } else {
+ new = parser (str, &str);
+ if (new == ERROR_OP) {
+ return ERROR_OP;
+ }
+ root->ops[1] = new;
+ } else {
return ERROR_OP;
}
- str += operator->offset;
found = 1;
+ VERBOSE (DEBUG, PRINTOUT ("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, PRINTOUT ("start processing function\n"));
if (root == NULL) {
- VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func));
+ VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
new = newelement (function->func, function->nbops);
if (new == NULL) {
return ERROR_OP;
}
str += function->offset;
found = 1;
+ VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
break;
}
}
if (found) {
- VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
continue;
}
if (((*str == '-') || (*str == '+')) &&
((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
((root) && (root->func == Val))) {
- VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add));
+ VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
new = newelement (Add, 2);
if (new == NULL) {
return ERROR_OP;
/* look for number */
- if (((*str >= '0') && (*str <= '9')) ||
- (*str == '.') || (*str == '-') ||(*str == '+')) {
+ if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+ VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
char *pt;
float value = strtof (str, &pt);
- VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value));
+ VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
if (str != pt) {
new = newelement (Val, 1);
- new->value = value;
if (new == NULL) {
return ERROR_OP;
}
+ new->value = value;
if (root == NULL) {
root = new;
} else {
str = pt;
found = 1;
}
+ VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
}
/* error */
}
+ 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 (" ");
}