free memories
authorLaurent Mazet <mazet@softndesign.org>
Fri, 30 Dec 2022 23:49:28 +0000 (00:49 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 30 Dec 2022 23:49:28 +0000 (00:49 +0100)
calc.c
makefile
parser.c
parser.h

diff --git a/calc.c b/calc.c
index d24bda307c1bf7e8a177091c7e3e22193c837b17..41cc6e048a7e8129b7ea06c850403ba640f69ce2 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -98,13 +98,14 @@ int main (int argc, char *argv[])
             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);
@@ -168,5 +169,10 @@ int main (int argc, char *argv[])
 // 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: */
index 090167546568a9e47e12ff1688f0d3003d59d68c..b10fc9fdb46825141ef65786ae50c1eec847521e 100644 (file)
--- a/makefile
+++ b/makefile
@@ -63,6 +63,9 @@ purge: clean
        rm -f purge $(ALLEXE:%=%.exe)
        $(call PASS, SUCCESS)
 
+valgrinds:
+       $(MAKE) $(addprefix valgrind_,$(ALLEXE))
+
 wipe: purge
        $(call TITLE, "wiping")
        touch wipe
@@ -102,10 +105,10 @@ test_%: %.test %.exe
        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 $@")
index 1d35b4c44785dc85eee79b7600b2c62eebb40910..2660b43365569ceab2a284b811e3d59c99effeca 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -45,6 +45,21 @@ element_t *newelement (func_t function, int nbops, int prio)
     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
@@ -82,10 +97,13 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in
     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;
@@ -124,6 +142,7 @@ element_t *parser (char *str, char **next, int prio)
                     found = 0;
                     new = parser (str + 1, &str, 0);
                     if (new == ERROR_OP) {
+                        delelement (root);
                         return ERROR_OP;
                     }
                     for (i = 0; i < root->nbops; i++) {
@@ -134,6 +153,8 @@ element_t *parser (char *str, char **next, int prio)
                         }
                     }
                     if (!found) {
+                        delelement (new);
+                        delelement (root);
                         return ERROR_OP;
                     }
                 } while (*str == ',');
@@ -144,6 +165,8 @@ element_t *parser (char *str, char **next, int prio)
                 }
                 new = parser (str + 1, &str, 0);
                 if ((new == ERROR_OP) || (*str == ',')) {
+                    delelement (new);
+                    delelement (root);
                     return ERROR_OP;
                 }
                 root->ops[0] = new;
@@ -177,6 +200,7 @@ element_t *parser (char *str, char **next, int prio)
                     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 == '-') {
@@ -210,7 +234,8 @@ element_t *parser (char *str, char **next, int prio)
                         return ERROR_OP;
                     }
                     root = new;
-                 } else {
+                } else {
+                    delelement (root);
                     return ERROR_OP;
                 }
                 str += function->offset;
@@ -248,12 +273,15 @@ element_t *parser (char *str, char **next, int prio)
                             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;
@@ -264,6 +292,7 @@ element_t *parser (char *str, char **next, int prio)
         /* error */
 
         if (!found) {
+            delelement (root);
             return ERROR_OP;
         }
 
@@ -272,6 +301,7 @@ element_t *parser (char *str, char **next, int prio)
     if (next != NULL) {
         *next = str;
     }
+
     return root;
 }
 
index 26022e5e585a469c67b824484a86912be28bf5f4..90dba49323fe032e7cfd7ce184d2d174a964bbdc 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -38,6 +38,8 @@ typedef struct _element_t {
 
 /* parser function */
 
+void delelement (element_t *root);
+
 element_t *parser (char *str, char **next, int prio);
 
 void print_element (element_t *root, int level);