From d13ae11c4f7cccd8f5e3ad072597c55c9471a638 Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 25 May 2023 02:00:37 +0000 Subject: fix def bug (maybe?) and block bug (maybe?) --- compiler.d | 7 ------- dbg.d | 3 ++- parser.d | 3 ++- vm.d | 46 +++++++++++++++++++++++++++++++++++----------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/compiler.d b/compiler.d index 665fe61..1c9e507 100644 --- a/compiler.d +++ b/compiler.d @@ -356,11 +356,6 @@ class Compiler { } void compileSymbol(Form form, ValueType expecting = ValueType.ANY) { - if (form.type != FormType.SYMBOL) { - writeln("NOT A SYMBOL!"); - } else { - writeln("it's a symbol"); - } Symbol sym = cast(Symbol)form; int arg = this.resolveLocal(sym); @@ -371,8 +366,6 @@ class Compiler { this.func.chunk.writeOp(OpCode.OP_GET_GLOBAL, sym.line); } - writefln("this is the addr: %d", arg); - // get the variable this.func.chunk.writeOp(to!ubyte(arg), sym.line); diff --git a/dbg.d b/dbg.d index e6ec197..65c83af 100644 --- a/dbg.d +++ b/dbg.d @@ -8,7 +8,8 @@ import parser; //import dbg; //import value; -bool DEBUG = true; +//bool DEBUG = true; +bool DEBUG = false; void printForm(Form f, string prefix = "") { diff --git a/parser.d b/parser.d index 11691f9..97557ca 100644 --- a/parser.d +++ b/parser.d @@ -435,7 +435,8 @@ class Parser { Def def = new Def(cast(Symbol)sym, val, line); - advance(); // closing paren + char ch = advance(); // closing paren + writefln("got this char: '%c'", ch); return def; } 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); -- cgit v1.2.3