]> code.bitgloo.com Git - clyne/interpreter.git/commitdiff
change load/run style, hunt for memory leaks
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 2 Apr 2018 16:29:05 +0000 (12:29 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 2 Apr 2018 16:29:05 +0000 (12:29 -0400)
calc [new file with mode: 0644]
ops.c
parser.c
parser.h
shell.c.bak
test3

diff --git a/calc b/calc
new file mode 100644 (file)
index 0000000..fd67c28
--- /dev/null
+++ b/calc
@@ -0,0 +1,12 @@
+go = 1
+while (go == 1) {
+       print("> ")
+       input = gets()
+       answer = solve(input)
+       if (answer == 42) {
+               go = 0
+       }
+       print("\n")
+       print(answer)
+       print("\n")
+}
diff --git a/ops.c b/ops.c
index e4874e81a31410ef694624e2fb7ef20512b5cdcf..74ec73b02d74e9b6313b5436827831370c760b17 100644 (file)
--- a/ops.c
+++ b/ops.c
@@ -193,7 +193,8 @@ OP_DEF(eq)
        else if (a->type == STRING && b->type == STRING)
                r->value.f = !strcmp((const char *)a->value.p, (const char *)b->value.p);
        else
-               return seterror(EBADPARAM);
+               r->value.f = 0.0f; // *sshhh*
+               //return seterror(EBADPARAM);
 
        return 0;
 }
index 103b80d2a1e9ca55ba8d268269aa8959eed16d83..cc09d992cbe0d39b10f9abaef4642968b43f0db4 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -84,9 +84,12 @@ void idelinstance(instance *it)
        }
        free(it->lines);
 
-       free(it->vars);
-       for (uint32_t i = 0; i < MAX_VARS; i++)
+       for (uint32_t i = 0; i < MAX_VARS; i++) {
+               if (it->vars[i].type == STRING)
+                       free((void *)it->vars[i].value.p);
                free(it->names[i]);
+       }
+       free(it->vars);
        free(it->names);
 
        free(it->stack);
@@ -177,14 +180,20 @@ void inew_string(instance *it, const char *name, const char *s)
        v->value.p = (uint32_t)strclone(s);
 }
 
-int idoline(instance *it, const char *s)
+int iaddline(instance *it, const char *s)
 {
        variable **ops = iparse(it, s);
        if (ops == 0)
                return 0;
        it->lines[it->lnidx] = ops;
+       it->lnidx++;
+       return 0;
+}
 
+int irun(instance *it)
+{
        variable **copy;
+       it->lnidx = 0;
 loop:
        //if (it->ret != 0)
        //      itryfree(it->ret);
@@ -222,6 +231,7 @@ loop:
                variable *ret = igetvar(it, "ANS");
                ret->type = it->ret->type;
                ret->value.p = it->ret->value.p;
+               itryfree(it->ret);
                it->ret = ret;
        }
 
index b032bb4f0d08e213bb17d02fa29fe86e01d4330c..f3c65d0b74fee6afb73d21d154f2ed26dd50f25e 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -63,12 +63,20 @@ instance *inewinstance(void);
 void idelinstance(instance *it);
 
 /**
- * Adds and runs the line through the parser instance.
- * This parses and runs the line, while storing it at the current line index.
- * @param it the current instance
- * @param s the line to parse/run
+ * Parses the given line of script, and adds it to the end of the instance's
+ * stored script.
+ * @param it the instance to use
+ * @param s the line to load
+ * @return zero for success, otherwise error
+ */
+int iaddline(instance *it, const char *s);
+
+/**
+ * Runs the instance with its loaded script until there's nothing more to run.
+ * @param the instance to use
+ * @return an error code, or zero if success
  */
-int idoline(instance *it, const char *s);
+int irun(instance *it);
 
 /**
  * Makes a C function visible to the script.
index a2a2100e0ba124afde7325a37be2cec15d8dd238..8658197b9f71250b96c518d439ed04c0dd42750e 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "parser.h"
@@ -37,6 +38,18 @@ int print(instance *it)
        return 0;
 }
 
+int gets(instance *it)
+{
+       char *line = 0;
+       size_t size;
+       getline(&line, &size, stdin);
+       *strchr(line, '\n') = '\0';
+       variable *v = make_vars(0, line);
+       free(line);
+       ipush(it, (uint32_t)v);
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        if (argc != 2) {
@@ -52,20 +65,21 @@ int main(int argc, char **argv)
 
        instance *it = inewinstance();
        inew_cfunc(it, "print", print);
+       inew_cfunc(it, "gets", gets);
 
        char *line = 0;
        size_t size;
        int result;
        while (getline(&line, &size, fp) != -1) {
                *strchr(line, '\n') = '\0';
-               result = idoline(it, line);
+               result = iaddline(it, line);
                if (result != 0)
                        printf("Error: %d\n", result);
-               //if (it->ret != 0)
-               //      printf("%s = %f\n", line, it->ret->value.f);
        }
-
+       free(line);
        fclose(fp);
+
+       irun(it);
        idelinstance(it);
        return 0;
 }
diff --git a/test3 b/test3
index 3c727fa92559c499663000cbec1e78fc7c6fb1bf..6bf60645c9a041469c3ce2d3c4248801428df79c 100644 (file)
--- a/test3
+++ b/test3
@@ -11,11 +11,11 @@ func(checka) {
        }
 }
 
-checka
+checka()
 
 print("Increment a...")
 a = a + 1
-checka
+checka()
 
 d = 0
 while (d < 20) {