aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 00:45:32 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 00:48:18 +0100
commitd0ee1f3b1fb60a6116a15c7c204345601adc2410 (patch)
treec5bbd0f6bbb935b1539d0437aed22f5515c52b7c /src
parent919fae2df8cbe1793167d0525e7552937e504a88 (diff)
downloadovm-d0ee1f3b1fb60a6116a15c7c204345601adc2410.tar.gz
ovm-d0ee1f3b1fb60a6116a15c7c204345601adc2410.tar.bz2
ovm-d0ee1f3b1fb60a6116a15c7c204345601adc2410.zip
Check for and handle errors when interpreting bytecode
Prints an error message, returns a non zero code based on error code (255 - err). In main, I propagate this exit code.
Diffstat (limited to 'src')
-rw-r--r--src/main.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index f66848c..2ebb002 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,9 +28,19 @@ int interpret_bytecode(const char *filepath)
vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number);
- vm_execute_all(&vm);
+ 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);
+ fprintf(stderr, "\t VM State:\n");
+ vm_print_all(&vm, stderr);
+ ret = 255 - err;
+ }
free(instructions);
- return 0;
+ return ret;
}
int assemble_instructions(inst_t *instructions, size_t number,
@@ -49,6 +59,5 @@ int main(int argc, char *argv[])
filename = argv[1];
inst_t instructions[] = {INST_HALT};
assemble_instructions(instructions, ARR_SIZE(instructions), filename);
- interpret_bytecode(filename);
- return 0;
+ return interpret_bytecode(filename);
}