diff options
Diffstat (limited to 'vm.d')
| -rw-r--r-- | vm.d | 40 |
1 files changed, 37 insertions, 3 deletions
@@ -155,6 +155,10 @@ class VM { return value.type == ValueType.OBJ; } + bool isList(Value value) { + return isObj(value) && objTypeOf(value) == ObjType.LIST; + } + ObjType objTypeOf(Value value) { return value.as.obj.type; } @@ -220,7 +224,6 @@ class VM { InterpretResult run() { writeln("== VM running =="); while (true) { - /* writeln(" Stacks:"); write(" A> "); for (int i = 0; i < aTop; i++) { @@ -231,8 +234,12 @@ class VM { //writef("[ %s ]", printableValue(bStack[i])); writef("[ %s ]", printableValue(current.slots[i])); } + + write("\n constants> "); + for (int i = 0; i < current.func.chunk.constants.length; i++) { + writef("[ %s ]", printableValue(current.func.chunk.constants[i])); + } writeln("\n--"); - */ /* write(" globals >"); @@ -243,7 +250,7 @@ class VM { writeln("\n--"); */ - //disassemble(current.func.chunk, current.ip); + disassemble(current.func.chunk, current.ip); ubyte inst; switch (inst = this.readByte()) { case OpCode.OP_DEF_GLOBAL: @@ -301,6 +308,18 @@ class VM { double val = asNumber(popA()); pushA(makeNumberValue(val * -1)); break; + case OpCode.OP_TYPE_CHECK_LIST: + if (!isList(peekA(0))) { + writeln("VM type check: not a list!"); + return InterpretResult.RUNTIME_ERROR; // TODO error + } + break; + case OpCode.OP_TYPE_CHECK_STRING: + if (!isString(peekA(0))) { + writeln("VM type check: not a string!"); + return InterpretResult.RUNTIME_ERROR; // TODO error + } + break; case OpCode.OP_TYPE_CHECK_NUMBER: if (!isNumber(peekA(0))) { writeln("VM type check: not a number!"); @@ -313,6 +332,14 @@ class VM { return InterpretResult.RUNTIME_ERROR; // TODO error } break; + case OpCode.OP_FIRST: + Value val = popA(); + List lst = cast(List)val.as.obj; // TODO this needs better checking + int addr = lst.first(); + writefln("got this address: %d", addr); + Value first = current.func.chunk.constants[to!ubyte(addr)]; + pushA(first); + break; case OpCode.OP_ADD: Value b = popA(); /* @@ -356,6 +383,13 @@ class VM { bool bval = asBoolean(val); pushA(makeBooleanValue(!bval)); break; + case OpCode.OP_CONCAT: + Value b = popA(); + Value a = popA(); + string bstr = asString(b); + string astr = asString(a); + pushA(makeStringValue(astr ~ bstr)); + break; case OpCode.OP_GREATER: Value b = popA(); Value a = popA(); |
