From ab7d7c155fb1bec5eed8f97462fbb656ea27dbb5 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 11:24:09 +0200 Subject: Add VM structure and fix AdressSanitizer bugs --- src/bytecode/main.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'src/bytecode/main.c') diff --git a/src/bytecode/main.c b/src/bytecode/main.c index c994e08..ca1f441 100644 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c @@ -4,9 +4,8 @@ #include #include -#include "types.h" -#include "chunk.h" -#include "darray.h" +#include "vm.h" + #include "ops.h" #include "debug.h" #include "errors.c" @@ -14,9 +13,16 @@ #include "read_line.c" #include "string_view.c" +static VM vm; + void init(void) { - // STUB + vm = vm_init(); +} + +void +halt(void) { + vm_free(vm); } void @@ -27,26 +33,14 @@ process_source(const StringView *source) { return; } - // Test chunks and debugging utilities. - // Initialize chunk. - Chunk chunk = {0}; - array_init(chunk.code, 0); - array_init(chunk.constants, 0); - array_init(chunk.lines, 0); - // Push some test instructions. - size_t const_idx = add_constant(chunk, 7); - add_code(chunk, OP_CONSTANT, 1, 1); - add_code(chunk, const_idx, 1, 1); - add_code(chunk, OP_RETURN, 1, 1); + size_t const_idx = add_constant(vm.chunk, 7); + add_code(vm.chunk, OP_CONSTANT, 1, 1); + add_code(vm.chunk, const_idx, 1, 1); + add_code(vm.chunk, OP_RETURN, 1, 1); // Disassemble the chunk. - disassemble_chunk(chunk, "test chunk"); - - // Free chunk. - array_free(chunk.code); - array_free(chunk.constants); - array_free(chunk.lines); + disassemble_chunk(vm.chunk, "test chunk"); array_free(tokens); } @@ -183,6 +177,7 @@ main(int argc, char *argv[]) { case 'i': { // Interactive mode. run_repl(); + halt(); return EXIT_SUCCESS; } break; default: { @@ -195,6 +190,7 @@ main(int argc, char *argv[]) { // Run from stdin. if (optind == argc) { run_stdin(); + halt(); return EXIT_SUCCESS; } @@ -205,5 +201,6 @@ main(int argc, char *argv[]) { optind++; } + halt(); return EXIT_SUCCESS; } -- cgit v1.2.1