From 9ce9a7f510e6ba407c2e14d3eae4d603b38edde7 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 13:58:02 +0200 Subject: Prepare compilation pipeline --- src/bytecode/main.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'src/bytecode/main.c') diff --git a/src/bytecode/main.c b/src/bytecode/main.c index 5661747..d6cb5d3 100644 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c @@ -11,6 +11,7 @@ #define DEBUG_TRACE_EXECUTION #include "vm.h" +#include "compiler.h" #include "ops.h" #include "debug.h" #include "errors.c" @@ -22,43 +23,38 @@ static VM vm; void init(void) { - vm = vm_init(); + vm_init(&vm); } void halt(void) { - vm_free(vm); + vm_free(&vm); } void process_source(const StringView *source) { + // Read tokens. Token *tokens = tokenize(source); if (errors_n != 0) { array_free(tokens); return; } - for (size_t i = 0; i < array_size(tokens); i++) { - print_token(tokens[i]); + // Compile chunk. + Chunk *chunk = compile(tokens); + if (errors_n != 0) { + chunk_free(chunk); + array_free(tokens); + return; } - size_t const_a = add_constant(vm.chunk, 7); - add_code(vm.chunk, OP_CONSTANT, 1, 1); - add_code(vm.chunk, const_a, 1, 1); - size_t const_b = add_constant(vm.chunk, 2); - add_code(vm.chunk, OP_CONSTANT, 1, 2); - add_code(vm.chunk, const_b, 1, 2); - - // Arithmetic. - add_code(vm.chunk, OP_MOD, 1, 3); - - add_code(vm.chunk, OP_RETURN, 1, 1); - - vm_interpret(vm); + // Interpret chunk. + vm_interpret(vm, chunk); if (errors_n != 0) { disassemble_chunk(vm.chunk, "test chunk"); } + chunk_free(chunk); array_free(tokens); } -- cgit v1.2.1