diff options
Diffstat (limited to 'src/bytecode/vm.h')
-rw-r--r-- | src/bytecode/vm.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h index 36547cb..d53fcf9 100644 --- a/src/bytecode/vm.h +++ b/src/bytecode/vm.h | |||
@@ -2,14 +2,19 @@ | |||
2 | #define BDL_VM_H | 2 | #define BDL_VM_H |
3 | 3 | ||
4 | #include "types.h" | 4 | #include "types.h" |
5 | #include "errors.h" | ||
5 | #include "chunk.h" | 6 | #include "chunk.h" |
7 | #include "ops.h" | ||
8 | #include "debug.h" | ||
6 | 9 | ||
7 | typedef struct VM { | 10 | typedef struct VM { |
8 | Chunk *chunk; | 11 | Chunk *chunk; |
12 | u8 *pc; | ||
9 | } VM; | 13 | } VM; |
10 | 14 | ||
11 | VM vm_init(void); | 15 | VM vm_init(void); |
12 | void vm_free(VM vm); | 16 | void vm_free(VM vm); |
17 | void vm_interpret(VM vm); | ||
13 | 18 | ||
14 | VM | 19 | VM |
15 | vm_init(void) { | 20 | vm_init(void) { |
@@ -24,4 +29,51 @@ vm_free(VM vm) { | |||
24 | chunk_free(vm.chunk); | 29 | chunk_free(vm.chunk); |
25 | } | 30 | } |
26 | 31 | ||
32 | void | ||
33 | vm_interpret(VM vm) { | ||
34 | if (vm.chunk->code == NULL || array_size(vm.chunk->code) == 0) { | ||
35 | error_push((Error){ | ||
36 | .type = ERR_TYPE_RUNTIME, | ||
37 | .value = ERR_EMPTY_CHUNK, | ||
38 | }); | ||
39 | return; | ||
40 | } | ||
41 | |||
42 | vm.pc = vm.chunk->code; | ||
43 | u8 *last = vm.chunk->code + array_size(vm.chunk->code); | ||
44 | while (vm.pc < last) { | ||
45 | #ifdef DEBUG_TRACE_EXECUTION | ||
46 | disassemble_instruction(vm.chunk, (vm.pc - vm.chunk->code)); | ||
47 | #endif | ||
48 | u8 instruction = *vm.pc++; | ||
49 | switch (instruction) { | ||
50 | case OP_CONSTANT: { | ||
51 | u8 constant = *vm.pc++; | ||
52 | Object obj = vm.chunk->constants[constant]; | ||
53 | display(obj); | ||
54 | printf("\n"); | ||
55 | } break; | ||
56 | case OP_RETURN: { | ||
57 | return; | ||
58 | } break; | ||
59 | default: { | ||
60 | error_push((Error){ | ||
61 | .type = ERR_TYPE_RUNTIME, | ||
62 | .value = ERR_NOT_IMPLEMENTED, | ||
63 | .line = vm.chunk->lines[0].line, | ||
64 | .col = vm.chunk->lines[0].col, | ||
65 | }); | ||
66 | return; | ||
67 | } break; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | error_push((Error){ | ||
72 | .type = ERR_TYPE_RUNTIME, | ||
73 | .value = ERR_PC_OOB, | ||
74 | .line = vm.chunk->lines[0].line, | ||
75 | .col = vm.chunk->lines[0].col, | ||
76 | }); | ||
77 | } | ||
78 | |||
27 | #endif // BDL_VM_H | 79 | #endif // BDL_VM_H |