VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
return NULL;
}
- new->ops = (element_t **) calloc (1, sizeof (element_t *));
- if (new->ops == NULL) {
- free (new);
- VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
- return NULL;
+ if (nbops) {
+ new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
+ if (new->ops == NULL) {
+ free (new);
+ VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+ return NULL;
+ }
}
new->func = function;
new->nbops = nbops;
continue;
}
+ /* check for open brace */
+
+ if (*str == '{') {
+ VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
+ if (root != NULL) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ element_t **prog = NULL;
+ new = newelement (Prog, 0, 5);
+ if (new == NULL) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ root = new;
+ prog = &root;
+
+ do {
+ new = parser (str + 1, &str, 0);
+ if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
+ delelement (new);
+ new = ERROR_OP;
+ }
+ if (new == ERROR_OP) {
+ delelement (root);
+ return ERROR_OP;
+ }
+ element_t *newprog = newelement (Prog, (*prog)->nbops + 1, 5);
+ for (i = 0; i < (*prog)->nbops; i++) {
+ newprog->ops[i] = (*prog)->ops[i];
+ (*prog)->ops[i] = NULL;
+ }
+ newprog->ops[(*prog)->nbops] = new;
+ delelement (*prog);
+ (*prog) = newprog;
+ } while (*str == ',');
+
+ if (*str != '}') {
+ delelement (root);
+ return ERROR_OP;
+ }
+ str++;
+ VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
+ continue;
+ }
+
/* check for open bracket */
if (*str == '(') {
continue;
}
- /* check for closing bracket or koma */
+ /* check for closing bracket, closing brace or koma */
- if ((*str == ')') || (*str == ',')) {
+ if ((*str == ')') || (*str == '}') || (*str == ',')) {
if (next != NULL) {
*next = str;
}
case Not: func = "Not"; break;
case Cond: func = "Condition"; break;
case While: func = "While"; break;
+ case Prog: func = "Program"; break;
}
fprintf (stdout, "Function: %s\n", func);
return ret;
}
+/* program function */
+
+double program_do (element_t **prog, int nbcalls)
+{
+ double ret = 0;
+ int i;
+ for (i = 0; i < nbcalls; i++) {
+ ret = evaluate_element (prog[i], 0);
+ prog[i] = NULL;
+ }
+ return ret;
+}
+
/* quit function */
void quit (void)
case Ans:
case Pi:
case E:
+ case Prog:
break;
case While:
if (root->ops[0] == NULL) {
return 0;
}
case While: return while_do (root->ops[0], root->ops[1]);
+ case Prog: return program_do (root->ops, root->nbops);
}
return 0;