aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode/main.c')
-rw-r--r--src/bytecode/main.c30
1 files changed, 13 insertions, 17 deletions
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 @@
11#define DEBUG_TRACE_EXECUTION 11#define DEBUG_TRACE_EXECUTION
12 12
13#include "vm.h" 13#include "vm.h"
14#include "compiler.h"
14#include "ops.h" 15#include "ops.h"
15#include "debug.h" 16#include "debug.h"
16#include "errors.c" 17#include "errors.c"
@@ -22,43 +23,38 @@ static VM vm;
22 23
23void 24void
24init(void) { 25init(void) {
25 vm = vm_init(); 26 vm_init(&vm);
26} 27}
27 28
28void 29void
29halt(void) { 30halt(void) {
30 vm_free(vm); 31 vm_free(&vm);
31} 32}
32 33
33void 34void
34process_source(const StringView *source) { 35process_source(const StringView *source) {
36 // Read tokens.
35 Token *tokens = tokenize(source); 37 Token *tokens = tokenize(source);
36 if (errors_n != 0) { 38 if (errors_n != 0) {
37 array_free(tokens); 39 array_free(tokens);
38 return; 40 return;
39 } 41 }
40 42
41 for (size_t i = 0; i < array_size(tokens); i++) { 43 // Compile chunk.
42 print_token(tokens[i]); 44 Chunk *chunk = compile(tokens);
45 if (errors_n != 0) {
46 chunk_free(chunk);
47 array_free(tokens);
48 return;
43 } 49 }
44 50
45 size_t const_a = add_constant(vm.chunk, 7); 51 // Interpret chunk.
46 add_code(vm.chunk, OP_CONSTANT, 1, 1); 52 vm_interpret(vm, chunk);
47 add_code(vm.chunk, const_a, 1, 1);
48 size_t const_b = add_constant(vm.chunk, 2);
49 add_code(vm.chunk, OP_CONSTANT, 1, 2);
50 add_code(vm.chunk, const_b, 1, 2);
51
52 // Arithmetic.
53 add_code(vm.chunk, OP_MOD, 1, 3);
54
55 add_code(vm.chunk, OP_RETURN, 1, 1);
56
57 vm_interpret(vm);
58 if (errors_n != 0) { 53 if (errors_n != 0) {
59 disassemble_chunk(vm.chunk, "test chunk"); 54 disassemble_chunk(vm.chunk, "test chunk");
60 } 55 }
61 56
57 chunk_free(chunk);
62 array_free(tokens); 58 array_free(tokens);
63} 59}
64 60