diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-23 00:45:32 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-23 00:48:18 +0100 |
commit | d0ee1f3b1fb60a6116a15c7c204345601adc2410 (patch) | |
tree | c5bbd0f6bbb935b1539d0437aed22f5515c52b7c | |
parent | 919fae2df8cbe1793167d0525e7552937e504a88 (diff) | |
download | ovm-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.
-rw-r--r-- | src/main.c | 17 |
1 files changed, 13 insertions, 4 deletions
@@ -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); } |