aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
diff options
context:
space:
mode:
authormryouse2023-05-25 21:15:04 +0000
committermryouse2023-05-25 21:15:04 +0000
commit9fe6496202bd95d252ed2323a88fd24781780b64 (patch)
tree907f85973ab3e5b22b076a27a3933b88855e07ba /chunk.d
parentd95e4f6f988b16bbd95a9c78c0355d190390f003 (diff)
lists as sequences, first and rest
Diffstat (limited to 'chunk.d')
-rw-r--r--chunk.d30
1 files changed, 27 insertions, 3 deletions
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 {