aboutsummaryrefslogtreecommitdiff
path: root/vm/main.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 15:22:47 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-01 15:22:47 +0000
commit6d35283ef01114475b6069f1a3c24e3b537bd510 (patch)
tree25507bc7a0bb8405915e7f2a67dbb4048572a3eb /vm/main.c
parent6a270eda1e444a68d5db9102b11af9153fa58a1c (diff)
downloadovm-6d35283ef01114475b6069f1a3c24e3b537bd510.tar.gz
ovm-6d35283ef01114475b6069f1a3c24e3b537bd510.tar.bz2
ovm-6d35283ef01114475b6069f1a3c24e3b537bd510.zip
Clearer VERBOSE messages
Diffstat (limited to 'vm/main.c')
-rw-r--r--vm/main.c62
1 files changed, 38 insertions, 24 deletions
diff --git a/vm/main.c b/vm/main.c
index c0b576a..faf4299 100644
--- a/vm/main.c
+++ b/vm/main.c
@@ -17,17 +17,49 @@
#include "./runtime.h"
#include <lib/inst.h>
-int interpret_bytecode(const char *filepath)
+void usage(const char *program_name, FILE *out)
+{
+ fprintf(out,
+ "Usage: %s [OPTIONS] FILE\n"
+ "\t FILE: Bytecode file to execute\n"
+ "\tOptions:\n"
+ "\t\t To be developed...\n",
+ program_name);
+}
+
+int main(int argc, char *argv[])
{
- FILE *fp = fopen(filepath, "rb");
+ if (argc == 1)
+ {
+ usage(argv[0], stderr);
+ return 1;
+ }
+ const char *filename = argv[1];
+
+#if VERBOSE >= 1
+ printf("[%sINTERPRETER%s]: Interpreting `%s`\n", TERM_YELLOW, TERM_RESET,
+ filename);
+#endif
+
+ FILE *fp = fopen(filename, "rb");
size_t number = 0;
inst_t *instructions = insts_read_bytecode_file(fp, &number);
fclose(fp);
+#if VERBOSE >= 1
+ printf("\t[%sBYTECODE-READER%s]: Read %lu instructions\n", TERM_GREEN,
+ TERM_RESET, number);
+#endif
+
byte stack[256];
vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number);
+
+#if VERBOSE >= 1
+ printf("\t[%sVM-SETUP%s]: Loaded stack and program into VM\n", TERM_GREEN,
+ TERM_RESET);
+#endif
err_t err = vm_execute_all(&vm);
int ret = 0;
@@ -39,27 +71,9 @@ int interpret_bytecode(const char *filepath)
ret = 255 - err;
}
free(instructions);
- return ret;
-}
-
-void usage(const char *program_name, FILE *out)
-{
- fprintf(out,
- "Usage: %s [OPTIONS] FILE\n"
- "\t FILE: Bytecode file to execute\n"
- "\tOptions:\n"
- "\t\t To be developed...\n",
- program_name);
-}
-int main(int argc, char *argv[])
-{
- if (argc == 1)
- {
- usage(argv[0], stderr);
- return 1;
- }
- const char *filename = argv[1];
-
- return interpret_bytecode(filename);
+#if VERBOSE >= 1
+ printf("[%sINTERPRETER%s]: Finished execution\n", TERM_GREEN, TERM_RESET);
+#endif
+ return ret;
}