aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/vm.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-07-22 01:09:11 -0700
committerGravatar Tom Willemse2021-07-22 01:09:11 -0700
commit50473e5ab573d85b1a555874332f6aecdd36f659 (patch)
tree4371889b4ee894a8cb9413def9726896f53e45e2 /clox/src/vm.c
parent7fe0c7f639ca15312f2ed5a77beddfdb0ada1ae6 (diff)
downloadcrafting-interpreters-50473e5ab573d85b1a555874332f6aecdd36f659.tar.gz
crafting-interpreters-50473e5ab573d85b1a555874332f6aecdd36f659.zip
Chapter 15.1
Diffstat (limited to 'clox/src/vm.c')
-rw-r--r--clox/src/vm.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/clox/src/vm.c b/clox/src/vm.c
new file mode 100644
index 0000000..14a206e
--- /dev/null
+++ b/clox/src/vm.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+
+#include "common.h"
+#include "debug.h"
+#include "vm.h"
+
+VM vm;
+
+void initVM() {}
+
+void freeVM() {}
+
+static InterpretResult run() {
+#define READ_BYTE() (*vm.ip++)
+#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
+
+ for (;;) {
+#ifdef DEBUG_TRACE_EXECUTION
+ disassembleInstruction(vm.chunk, (int)(vm.ip - vm.chunk->code));
+#endif
+
+ uint8_t instruction;
+ switch (instruction = READ_BYTE()) {
+ case OP_CONSTANT: {
+ Value constant = READ_CONSTANT();
+ printValue(constant);
+ printf("\n");
+ break;
+ }
+ case OP_RETURN: {
+ return INTERPRET_OK;
+ }
+ }
+ }
+
+#undef READ_BYTE
+#undef READ_CONSTANT
+}
+
+InterpretResult interpret(Chunk *chunk) {
+ vm.chunk = chunk;
+ vm.ip = vm.chunk->code;
+ return run();
+}