diff options
Diffstat (limited to 'src/bytecode/vm.h')
-rw-r--r-- | src/bytecode/vm.h | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index 6c5dfa7..f9e64d1 100644 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h | |||
@@ -18,27 +18,33 @@ typedef struct VM { | |||
18 | Object *stack; | 18 | Object *stack; |
19 | } VM; | 19 | } VM; |
20 | 20 | ||
21 | VM vm_init(void); | 21 | void vm_init(VM *vm); |
22 | void vm_free(VM vm); | 22 | void vm_free(VM *vm); |
23 | void vm_interpret(VM vm); | 23 | void vm_reset(VM *vm); |
24 | void vm_interpret(VM vm, Chunk *chunk); | ||
24 | 25 | ||
25 | VM | 26 | void |
26 | vm_init(void) { | 27 | vm_init(VM *vm) { |
27 | VM vm = { | 28 | *vm = (VM){0}; |
28 | .chunk = chunk_init(), | 29 | array_init(vm->stack, VM_STACK_CAP); |
29 | }; | 30 | } |
30 | array_init(vm.stack, VM_STACK_CAP); | 31 | |
31 | return vm; | 32 | void |
33 | vm_free(VM *vm) { | ||
34 | array_free(vm->stack); | ||
32 | } | 35 | } |
33 | 36 | ||
34 | void | 37 | void |
35 | vm_free(VM vm) { | 38 | vm_reset(VM *vm) { |
36 | chunk_free(vm.chunk); | 39 | vm_free(vm); |
37 | array_free(vm.stack); | 40 | vm_init(vm); |
38 | } | 41 | } |
39 | 42 | ||
40 | void | 43 | void |
41 | vm_interpret(VM vm) { | 44 | vm_interpret(VM vm, Chunk *chunk) { |
45 | vm.chunk = chunk; | ||
46 | vm.pc = vm.chunk->code; | ||
47 | |||
42 | if (vm.chunk->code == NULL || array_size(vm.chunk->code) == 0) { | 48 | if (vm.chunk->code == NULL || array_size(vm.chunk->code) == 0) { |
43 | error_push((Error){ | 49 | error_push((Error){ |
44 | .type = ERR_TYPE_RUNTIME, | 50 | .type = ERR_TYPE_RUNTIME, |
@@ -47,7 +53,6 @@ vm_interpret(VM vm) { | |||
47 | return; | 53 | return; |
48 | } | 54 | } |
49 | 55 | ||
50 | vm.pc = vm.chunk->code; | ||
51 | u8 *last = vm.chunk->code + array_size(vm.chunk->code); | 56 | u8 *last = vm.chunk->code + array_size(vm.chunk->code); |
52 | while (vm.pc < last) { | 57 | while (vm.pc < last) { |
53 | #ifdef DEBUG_TRACE_EXECUTION | 58 | #ifdef DEBUG_TRACE_EXECUTION |