if (buffer[i] == '\n') {
buffer[i] = 0;
VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j));
- element_t *element = parser (buffer, NULL, 0);
- if (element == (void *)(-1)) {
+ element_t *element = parser (buffer + j, NULL, 0);
+ if (element == ERROR_OP) {
VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer));
ret = 1;
- } else {
+ } else {
VERBOSE (INFO, print_element (element, 0));
PRINTOUT ("=> %f\n", evaluate_element (element, 0));
+ delelement (element);
ret = 0;
}
//fsync (stdfdout);
// test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0'
// test: echo "quit" | calc.exe | grep -q 'bye'
// test: echo "help" | calc.exe | grep -q 'miscellaneous'
+// test: echo "1 + 2 *" | calc.exe | grep -q 'error'
+// test: echo "* 1 - 2" | calc.exe | grep -q 'error'
+// test: echo "2 + * 3" | calc.exe | grep -q 'error'
+// test: echo "sqrt 2" | calc.exe | grep -q 'error'
+// test: echo "pow (2)" | calc.exe | grep -q 'error'
/* vim: set ts=4 sw=4 et: */
rm -f purge $(ALLEXE:%=%.exe)
$(call PASS, SUCCESS)
+valgrinds:
+ $(MAKE) $(addprefix valgrind_,$(ALLEXE))
+
wipe: purge
$(call TITLE, "wiping")
touch wipe
done; \
test "$$RC" -ne 1
-valgrind_%: %
+valgrind_%: %.exe
VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \
export VALGRIND; \
- $(MAKE) test_$<
+ $(MAKE) $(@:valgrind_%=test_%)
%.d: %.c
$(call TITLE, "Building $@")
return new;
}
+/* desallocate element */
+
+void delelement (element_t *root)
+{
+ int i;
+ if ((root != NULL) && (root != ERROR_OP)) {
+ for (i = 0; i < root->nbops; i++) {
+ if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
+ delelement (root->ops[i]);
+ }
+ }
+ free (root);
+ }
+}
+
/* functions */
#define NB_OPERATORS 6
new->ops[0] = *proot;
new->ops[1] = parser (*pstr, pstr, new->prio);
if (new->ops[1] == ERROR_OP) {
+ delelement (new);
+ *proot = NULL;
return ERROR_OP;
}
*proot = newelement (Val, 1, 5);
- if (*proot == ERROR_OP) {
+ if (*proot == NULL) {
+ delelement (new);
return ERROR_OP;
}
(*proot)->ops[0] = new;
found = 0;
new = parser (str + 1, &str, 0);
if (new == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
for (i = 0; i < root->nbops; i++) {
}
}
if (!found) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
} while (*str == ',');
}
new = parser (str + 1, &str, 0);
if ((new == ERROR_OP) || (*str == ',')) {
+ delelement (new);
+ delelement (root);
return ERROR_OP;
}
root->ops[0] = new;
str += operator->offset;
VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
} else if (*str == '-') {
return ERROR_OP;
}
root = new;
- } else {
+ } else {
+ delelement (root);
return ERROR_OP;
}
str += function->offset;
return root;
}
if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
+ delelement (root);
return ERROR_OP;
}
} else {
+ delelement (root);
return ERROR_OP;
}
} else {
+ delelement (root);
return ERROR_OP;
}
found = 1;
/* error */
if (!found) {
+ delelement (root);
return ERROR_OP;
}
if (next != NULL) {
*next = str;
}
+
return root;
}
/* parser function */
+void delelement (element_t *root);
+
element_t *parser (char *str, char **next, int prio);
void print_element (element_t *root, int level);