aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/vm.c
diff options
context:
space:
mode:
Diffstat (limited to 'clox/src/vm.c')
-rw-r--r--clox/src/vm.c30
1 files changed, 27 insertions, 3 deletions
diff --git a/clox/src/vm.c b/clox/src/vm.c
index 14a206e..ffd207d 100644
--- a/clox/src/vm.c
+++ b/clox/src/vm.c
@@ -6,16 +6,39 @@
VM vm;
-void initVM() {}
+static void resetStack() {
+ vm.stackTop = vm.stack;
+}
+
+void initVM() {
+ resetStack();
+}
void freeVM() {}
+void push(Value value) {
+ *vm.stackTop = value;
+ vm.stackTop++;
+}
+
+Value pop() {
+ vm.stackTop--;
+ return *vm.stackTop;
+}
+
static InterpretResult run() {
#define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
for (;;) {
#ifdef DEBUG_TRACE_EXECUTION
+ printf(" ");
+ for (Value *slot = vm.stack; slot < vm.stackTop; slot++) {
+ printf("[ ");
+ printValue(*slot);
+ printf(" ]");
+ }
+ printf("\n");
disassembleInstruction(vm.chunk, (int)(vm.ip - vm.chunk->code));
#endif
@@ -23,11 +46,12 @@ static InterpretResult run() {
switch (instruction = READ_BYTE()) {
case OP_CONSTANT: {
Value constant = READ_CONSTANT();
- printValue(constant);
- printf("\n");
+ push(constant);
break;
}
case OP_RETURN: {
+ printValue(pop());
+ printf("\n");
return INTERPRET_OK;
}
}