diff options
| -rw-r--r-- | chunk.d | 18 | ||||
| -rw-r--r-- | compiler.d | 20 | ||||
| -rw-r--r-- | vm.d | 13 |
3 files changed, 25 insertions, 26 deletions
@@ -3,6 +3,7 @@ import std.string; import std.conv; import parser; +import dbg; enum ObjType { FUNCTION, @@ -37,22 +38,19 @@ class Function : Obj { } class List : Obj { - int[] addresses; + Value[] inner; - this() { + this(int length) { + this.inner = new Value[length]; this.type = ObjType.LIST; } - void addItem(int addr) { - addresses ~= addr; - } - - int first() { - return addresses[0]; // TODO this fails on empty lists + void addItemAtIndex(Value item, int idx) { + this.inner[idx] = item; } override string toString() { - return format("list (%d)", addresses.length); + return format("list ('%s' + %d)", printableValue(this.inner[0]), this.inner.length - 1); } } @@ -82,6 +80,8 @@ enum OpCode { OP_CALL, + OP_LIST, + OP_CONCAT, // No? OP_FIRST, // No? @@ -510,24 +510,12 @@ class Compiler { } void compileList(Form[] args) { - List lst = new List(); - ValueType vt; - int addr; + int length = to!int(args.length); foreach (Form arg ; args) { - vt = this.resolve(arg, ValueType.ANY); - // how do we get at the address? - addr = to!int(this.func.chunk.constants.length) - 1; // this is probably often wrong - lst.addItem(addr); - this.func.chunk.writeOp(OpCode.OP_POP, arg.line); // shouldn't use the stack for this (but how?) + resolve(arg, ValueType.ANY); // resolve everything onto the stack } - - this.func.chunk.writeOp(OpCode.OP_CONSTANT, args[0].line); - int idx = this.func.chunk.addConstant(makeObjValue(lst)); - this.func.chunk.writeOp(to!ubyte(idx), args[0].line); - //return ValueType.OBJ; - - - //advance(); // ?? + this.func.chunk.writeOp(OpCode.OP_LIST, args[0].line); + this.func.chunk.writeOp(to!ubyte(length), args[0].line); } void compileFirst(Form[] args) { @@ -322,7 +322,16 @@ class VM { //bStack[bTop + slot - 1] = peekA(0); this.popA(); break; - case OpCode.OP_CONSTANT: + case OpCode.OP_LIST: + int length = to!int(this.readByte()); + List lst = new List(length); + for (int i = length - 1; i >= 0; i--) { + //lst.appendItem(this.popA()); + lst.addItemAtIndex(this.popA(), i); + } + this.pushA(makeObjValue(lst)); + break; + case OpCode.OP_CONSTANT: Value constant = this.current.func.chunk.constants[this.readByte()]; //Value constant = current.func.chunk.constants[b]; this.pushA(constant); @@ -359,6 +368,7 @@ class VM { return InterpretResult.RUNTIME_ERROR; // TODO error } break; + /* case OpCode.OP_FIRST: Value val = this.popA(); List lst = cast(List)val.as.obj; // TODO this needs better checking @@ -367,6 +377,7 @@ class VM { Value first = this.current.func.chunk.constants[to!ubyte(addr)]; this.pushA(first); break; + */ case OpCode.OP_ADD: Value b = this.popA(); /* |
