From 1cc7990ef7d5483d0434dda412f2d88e0b17df27 Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Sat, 20 Apr 2024 03:39:40 +0300
Subject: initial working bytecode

---
 src/core.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 10 deletions(-)

(limited to 'src/core.c')

diff --git a/src/core.c b/src/core.c
index 7eca8bd..c50860e 100644
--- a/src/core.c
+++ b/src/core.c
@@ -3,9 +3,14 @@
 #include <limits.h>
 #include <errno.h>
 
-#include <posthaste/debug.h>
+#include <posthaste/execute.h>
 #include <posthaste/parser.h>
+#include <posthaste/debug.h>
+#include <posthaste/scope.h>
+#include <posthaste/check.h>
+#include <posthaste/lower.h>
 #include <posthaste/core.h>
+#include <posthaste/ast.h>
 
 /**
  * Read whole file into a buffer and return pointer to buffer.
@@ -41,28 +46,63 @@ static char *read_file(const char *fname, FILE *f)
 
 int run(const char *fname)
 {
+	int ret = 0;
+	const char *buf = NULL;
+	struct scope *scope = NULL;
+	struct parser *p = NULL;
+
 	FILE *f = fopen(fname, "rb");
 	if (!f) {
 		error("failed opening %s: %s\n", fname, strerror(errno));
 		return -1;
 	}
 
-	const char *buf = read_file(fname, f);
+	buf = read_file(fname, f);
 	fclose(f);
 
-	if (!buf)
-		return -1;
+	if (!buf) {
+		ret = -1;
+		goto out;
+	}
 
-	struct parser *p = create_parser();
-	if (!p)
-		return -1;
+	p = create_parser();
+	if (!p) {
+		ret = -1;
+		goto out;
+	}
 
 	parse(p, fname, buf);
-	int ret = p->failed ? -1 : 0;
+	if (p->failed) {
+		ret = -1;
+		goto out;
+	}
 
-	/* eventually do other stuff as well */
-	free((void *)buf);
+	scope = create_scope();
+	if (!scope) {
+		ret = -1;
+		goto out;
+	}
+
+	scope_set_file(scope, fname, buf);
+
+	struct ast *ast = p->tree;
+	if (check(scope, ast)) {
+		ret = -1;
+		goto out;
+	}
 
+	if (lower_ast(ast)) {
+		ret = -1;
+		goto out;
+	}
+
+	execute();
+
+out:
+	free((void *)buf);
+	destroy_lowering();
+	destroy_scopes();
+	destroy_ast_nodes();
 	destroy_parser(p);
 	return ret;
 }
-- 
cgit v1.2.3