From 9fe6496202bd95d252ed2323a88fd24781780b64 Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 25 May 2023 21:15:04 +0000 Subject: lists as sequences, first and rest --- chunk.d | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'chunk.d') diff --git a/chunk.d b/chunk.d index 0573687..70d7b1c 100644 --- a/chunk.d +++ b/chunk.d @@ -8,7 +8,6 @@ import dbg; enum ObjType { FUNCTION, SCRIPT, - LIST, } abstract class Obj { @@ -37,18 +36,41 @@ class Function : Obj { } } -class List : Obj { +enum SeqType { + LIST, + STRING, +} + +abstract class Seq { + SeqType type; + abstract Value first(); + abstract Seq rest(); +} + +class List : Seq { Value[] inner; this(int length) { this.inner = new Value[length]; - this.type = ObjType.LIST; + this.type = SeqType.LIST; } void addItemAtIndex(Value item, int idx) { this.inner[idx] = item; } + override Value first() { + return this.inner[0]; // this fails on NIL + } + + override Seq rest() { + List ret = new List(to!int(this.inner.length) - 1); + for (int i = 1; i < this.inner.length; i++) { + ret.addItemAtIndex(this.inner[i], i - 1); + } + return ret; + } + override string toString() { return format("list ('%s' + %d)", printableValue(this.inner[0]), this.inner.length - 1); } @@ -85,11 +107,13 @@ enum OpCode { OP_CONCAT, // No? OP_FIRST, // No? + OP_REST, OP_TYPE_CHECK_NUMBER, OP_TYPE_CHECK_BOOLEAN, OP_TYPE_CHECK_STRING, OP_TYPE_CHECK_LIST, + OP_TYPE_CHECK_SEQ, } class Chunk { -- cgit v1.2.3