if (element == ERROR_OP) {
VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout));
ret = 1;
- } else {
+ } else if (element != NULL) {
VERBOSE (INFO, print_element (element, 0));
answer = evaluate_element (element, 0);
fprintf (stdout, format, answer);
// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program'
// test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6'
// test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 =
-// test: echo -e '1 }\n1 )\n1 , 2' | calc.exe | grep -c error | xargs test 3 =
+// test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 =
/* vim: set ts=4 sw=4 et: */
element_t *new = (element_t *) calloc (1, sizeof (element_t));
if (new == NULL) {
VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
- return NULL;
+ exit (1);
}
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;
+ exit (1);
}
}
new->func = function;
return root;
}
tmp = newelement (root->func, root->nbops, root->prio);
- if (tmp == NULL) {
- return ERROR_OP;
- }
tmp->value = root->value;
for (i = 0; i < root->nbops; i++) {
tmp->ops[i] = dupelement (root->ops[i]);
- if (tmp->ops[i] == ERROR_OP) {
- delelement (tmp);
- return ERROR_OP;
- }
}
return tmp;
}
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] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
return ERROR_OP;
}
*proot = newelement (Val, 1, 5);
- if (*proot == NULL) {
- delelement (new);
- return ERROR_OP;
- }
(*proot)->ops[0] = new;
return *proot;
}
element_t **prog = NULL;
new = newelement (Prog, 0, 5);
- if (new == NULL) {
- delelement (root);
- return ERROR_OP;
- }
root = new;
prog = &root;
} while (*str == ',');
} else {
root = newelement (Val, 1, 5);
- if (root == NULL) {
- return ERROR_OP;
- }
new = parser (str + 1, &str, 0);
if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
delelement (new);
return ERROR_OP;
}
} else if (*str == '-') {
- new = newelement (Sig, 1, 9);
- if (new == NULL) {
- return ERROR_OP;
- }
- root = new;
+ root = newelement (Sig, 1, 9);
} else {
return ERROR_OP;
}
VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
if (root == NULL) {
VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
- new = newelement (function->func, function->nbops, function->prio);
- if (new == NULL) {
- return ERROR_OP;
- }
- root = new;
+ root = newelement (function->func, function->nbops, function->prio);
} else {
delelement (root);
return ERROR_OP;
VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
if (root == NULL) {
VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
- new = newelement (constant->func, constant->nbops, constant->prio);
- if (new == NULL) {
- return ERROR_OP;
- }
- root = new;
+ root = newelement (constant->func, constant->nbops, constant->prio);
} else {
delelement (root);
return ERROR_OP;
if (str != pt) {
if ((root == NULL) || (root->prio == 6)) {
new = newelement (Val, 1, 5);
- if (new == NULL) {
- return ERROR_OP;
- }
new->value = value;
if (root == NULL) {
root = new;
element_t *temp = NULL;
VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
- if (cond == NULL) {
- return ret;
- }
while (1) {
VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));