From 31458dd042da2a3ce732546cd94318457b4f5bcf Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <clyne@bitgloo.com>
Date: Mon, 2 Apr 2018 12:29:05 -0400
Subject: change load/run style, hunt for memory leaks

---
 calc        | 12 ++++++++++++
 ops.c       |  3 ++-
 parser.c    | 16 +++++++++++++---
 parser.h    | 18 +++++++++++++-----
 shell.c.bak | 22 ++++++++++++++++++----
 test3       |  4 ++--
 6 files changed, 60 insertions(+), 15 deletions(-)
 create mode 100644 calc

diff --git a/calc b/calc
new file mode 100644
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 e4874e8..74ec73b 100644
--- 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;
 }
diff --git a/parser.c b/parser.c
index 103b80d..cc09d99 100644
--- 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;
 	}
 
diff --git a/parser.h b/parser.h
index b032bb4..f3c65d0 100644
--- 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.
diff --git a/shell.c.bak b/shell.c.bak
index a2a2100..8658197 100644
--- a/shell.c.bak
+++ b/shell.c.bak
@@ -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 3c727fa..6bf6064 100644
--- 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) {
-- 
cgit v1.2.3