diff options
Diffstat (limited to 'clox/src/vm.c')
-rw-r--r-- | clox/src/vm.c | 44 |
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(); +} |