From d0ee1f3b1fb60a6116a15c7c204345601adc2410 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 23 Oct 2023 00:45:32 +0100 Subject: 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. --- src/main.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src') 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); } -- cgit v1.2.3-13-gbd6f