aboutsummaryrefslogtreecommitdiff
path: root/vm/main.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 03:58:34 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 03:58:34 +0100
commitb44a61be41e44b415b1293fcbc1f1e8c4ce3373d (patch)
tree218cdb4901d23089ab0b54b4ff3097202c442acd /vm/main.c
parent587f31a63b26fe3789acd0ba129474a2be200d37 (diff)
downloadovm-b44a61be41e44b415b1293fcbc1f1e8c4ce3373d.tar.gz
ovm-b44a61be41e44b415b1293fcbc1f1e8c4ce3373d.tar.bz2
ovm-b44a61be41e44b415b1293fcbc1f1e8c4ce3373d.zip
src->vm, Makefile is now a bit more abstracted and pretty colours
Changed folder names for sake of clarity (will be introducing a new build target soon), and Makefile can now easily support more targets.
Diffstat (limited to 'vm/main.c')
-rw-r--r--vm/main.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/vm/main.c b/vm/main.c
new file mode 100644
index 0000000..005b61e
--- /dev/null
+++ b/vm/main.c
@@ -0,0 +1,62 @@
+/* Copyright (C) 2023 Aryadev Chavali
+
+ * You may distribute and modify this code under the terms of the
+ * GPLv2 license. You should have received a copy of the GPLv2
+ * license with this file. If not, please write to:
+ * aryadev@aryadevchavali.com.
+
+ * Created: 2023-10-15
+ * Author: Aryadev Chavali
+ * Description: Entrypoint to program
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "./inst.h"
+#include "./runtime.h"
+
+int interpret_bytecode(const char *filepath)
+{
+ FILE *fp = fopen(filepath, "rb");
+ size_t number = 0;
+ inst_t *instructions = insts_read_bytecode_file(fp, &number);
+ fclose(fp);
+
+ byte stack[256];
+ vm_t vm = {0};
+ vm_load_stack(&vm, stack, ARR_SIZE(stack));
+ vm_load_program(&vm, instructions, number);
+ err_t err = vm_execute_all(&vm);
+
+ int ret = 0;
+ if (err)
+ {
+ const char *error_str = err_as_cstr(err);
+ fprintf(stderr, "[ERROR]: %s\n", error_str);
+ vm_print_all(&vm, stderr);
+ ret = 255 - err;
+ }
+ free(instructions);
+ return ret;
+}
+
+int assemble_instructions(inst_t *instructions, size_t number,
+ const char *filepath)
+{
+ FILE *fp = fopen(filepath, "wb");
+ insts_write_bytecode_file(instructions, number, fp);
+ fclose(fp);
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ const char *filename = "out.bin";
+ if (argc >= 2)
+ filename = argv[1];
+ inst_t instructions[] = {INST_HALT};
+ assemble_instructions(instructions, ARR_SIZE(instructions), filename);
+ return interpret_bytecode(filename);
+}