aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/vm.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 12:05:41 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 12:05:41 +0200
commitd9474f1d7c0c6674179fd0f27cd1c084c8227ed5 (patch)
tree9ccad3b53388795e7cc03353f1860a0eddec593e /src/bytecode/vm.h
parentab7d7c155fb1bec5eed8f97462fbb656ea27dbb5 (diff)
downloadbdl-d9474f1d7c0c6674179fd0f27cd1c084c8227ed5.tar.gz
bdl-d9474f1d7c0c6674179fd0f27cd1c084c8227ed5.zip
Add interpretation function for VM
Diffstat (limited to 'src/bytecode/vm.h')
-rw-r--r--src/bytecode/vm.h52
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
7typedef struct VM { 10typedef struct VM {
8 Chunk *chunk; 11 Chunk *chunk;
12 u8 *pc;
9} VM; 13} VM;
10 14
11VM vm_init(void); 15VM vm_init(void);
12void vm_free(VM vm); 16void vm_free(VM vm);
17void vm_interpret(VM vm);
13 18
14VM 19VM
15vm_init(void) { 20vm_init(void) {
@@ -24,4 +29,51 @@ vm_free(VM vm) {
24 chunk_free(vm.chunk); 29 chunk_free(vm.chunk);
25} 30}
26 31
32void
33vm_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