diff options
| author | mryouse | 2023-05-25 21:15:04 +0000 |
|---|---|---|
| committer | mryouse | 2023-05-25 21:15:04 +0000 |
| commit | 9fe6496202bd95d252ed2323a88fd24781780b64 (patch) | |
| tree | 907f85973ab3e5b22b076a27a3933b88855e07ba /vm.d | |
| parent | d95e4f6f988b16bbd95a9c78c0355d190390f003 (diff) | |
lists as sequences, first and rest
Diffstat (limited to 'vm.d')
| -rw-r--r-- | vm.d | 37 |
1 files changed, 27 insertions, 10 deletions
@@ -168,14 +168,26 @@ class VM { return value.type == ValueType.OBJ; } + bool isSeq(Value value) { + return value.type == ValueType.SEQ; + } + bool isList(Value value) { - return isObj(value) && objTypeOf(value) == ObjType.LIST; + return isSeq(value) && seqTypeOf(value) == SeqType.LIST; } ObjType objTypeOf(Value value) { return value.as.obj.type; } + SeqType seqTypeOf(Value value) { + return value.as.seq.type; + } + + Seq asSeq(Value value) { + return value.as.seq; + } + double asNumber(Value value) { return value.as.number; } @@ -228,6 +240,7 @@ 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, 0, 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++; @@ -329,7 +342,7 @@ class VM { //lst.appendItem(this.popA()); lst.addItemAtIndex(this.popA(), i); } - this.pushA(makeObjValue(lst)); + this.pushA(makeSeqValue(lst)); break; case OpCode.OP_CONSTANT: Value constant = this.current.func.chunk.constants[this.readByte()]; @@ -344,6 +357,12 @@ class VM { double val = asNumber(this.popA()); this.pushA(makeNumberValue(val * -1)); break; + case OpCode.OP_TYPE_CHECK_SEQ: + if (!isSeq(this.peekA(0))) { + writeln("VM type check: not a seq!"); + return InterpretResult.RUNTIME_ERROR; // TODO error + } + break; case OpCode.OP_TYPE_CHECK_LIST: if (!isList(this.peekA(0))) { writeln("VM type check: not a list!"); @@ -368,16 +387,14 @@ 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 - int addr = lst.first(); - writefln("got this address: %d", addr); - Value first = this.current.func.chunk.constants[to!ubyte(addr)]; - this.pushA(first); + Seq seq = asSeq(this.popA()); + this.pushA(seq.first()); + break; + case OpCode.OP_REST: + Seq seq = asSeq(this.popA()); + this.pushA(makeSeqValue(seq.rest())); break; - */ case OpCode.OP_ADD: Value b = this.popA(); /* |
