aboutsummaryrefslogtreecommitdiff
path: root/vm.d
diff options
context:
space:
mode:
Diffstat (limited to 'vm.d')
-rw-r--r--vm.d46
1 files changed, 35 insertions, 11 deletions
diff --git a/vm.d b/vm.d
index ff2eb1d..8ac1934 100644
--- a/vm.d
+++ b/vm.d
@@ -18,7 +18,8 @@ enum InterpretResult {
struct CallFrame {
Function func;
ubyte ip;
- Value[] slots;
+ //Value[] slots;
+ int slotStart;
int frameStart;
}
@@ -88,6 +89,14 @@ class VM {
this.aTop++;
}
+ Value peekB(int offset) {
+ if (offset >= this.bTop) {
+ writefln("offset of %d greater than stack size %d", offset, this.bTop);
+ }
+ return this.bStack[this.bTop - offset];
+ //return this.bStack[this.bTop - offset - 1];
+ }
+
Value popB() {
if (this.bTop > 0) {
this.bTop--;
@@ -95,8 +104,12 @@ class VM {
} else {
writeln("popB() on an empty stack!!");
}
+
+
//return bStack[bTop];
- return this.current.slots[this.bTop - this.current.frameStart - 1];
+ //return this.current.slots[this.bTop - this.current.frameStart - 1];
+ //return this.bStack[this.bTop - this.current.frameStart - 1];
+ return this.bStack[this.bTop];
}
@@ -214,7 +227,8 @@ class VM {
}
bool call(Function func, int argCount) {
- CallFrame frame = { func, 0, this.bStack, this.bTop - argCount - 1 }; // i need to do sthg with argCount
+ //CallFrame frame = { func, 0, this.bStack, this.bTop - argCount - 1 }; // i need to do sthg with argCount
+ CallFrame frame = { func, 0, 0, this.bTop - argCount - 1 }; // i need to do sthg with argCount
//frames ~= frame;
//frameCount++;
this.pushFrame(frame);
@@ -227,13 +241,13 @@ class VM {
if (DEBUG) {
writeln(" Stacks:");
write(" A> ");
- for (int i = 0; i < aTop; i++) {
+ for (int i = 0; i < this.aTop; i++) {
writef("[ %s ]", printableValue(this.aStack[i]));
}
write("\n B> ");
- for (int i = 0; i < bTop; i++) {
- //writef("[ %s ]", printableValue(bStack[i]));
- writef("[ %s ]", printableValue(this.current.slots[i]));
+ for (int i = 0; i < this.bTop; i++) {
+ writef("[ %s ]", printableValue(bStack[i]));
+ //writef("[ %s ]", printableValue(this.current.slots[i]));
}
write("\n constants> ");
@@ -263,8 +277,10 @@ class VM {
}
Value val = this.popA();
this.globals[asString(name)] = val;
+ /*
Value nil = { ValueType.NIL };
this.pushA(nil);
+ */
break;
case OpCode.OP_GET_GLOBAL:
Value name = this.current.func.chunk.constants[this.readByte()];
@@ -284,18 +300,25 @@ class VM {
Value val = this.popA();
this.pushB(val);
//current.slots ~= val;
+
+ /*
+ Value nil = { ValueType.NIL };
+ this.pushA(nil);
+ */
break;
case OpCode.OP_GET_LOCAL:
ubyte slot = this.readByte();
//pushA(bStack[bTop - slot - 1]);
//pushA(current.slots[slot - 1]);
//pushA(current.slots[current.frameStart + slot + 1]);
- pushA(this.current.slots[this.current.frameStart + slot]);
+
+ //pushA(this.current.slots[this.current.frameStart + slot]);
+ pushA(this.peekB(this.bTop - (this.current.frameStart + slot)));
break;
case OpCode.OP_SET_LOCAL:
ubyte slot = this.readByte();
- //bStack[bTop + slot - 1] = peekA(0);
- this.current.slots[slot] = this.peekA(0);
+ //this.current.slots[slot] = this.peekA(0);
+ this.pushB(this.peekA(0));
//bStack[bTop + slot - 1] = peekA(0);
this.popA();
break;
@@ -518,7 +541,8 @@ InterpretResult interpret(string source, VM vm) {
vm.frameCount = 0;
vm.pushA(makeObjValue(func));
- CallFrame frame = { func, 0, vm.bStack, 0 };
+ //CallFrame frame = { func, 0, vm.bStack, 0 };
+ CallFrame frame = { func, 0, 0, 0 };
vm.pushFrame(frame);
vm.call(func, 0);