From 46a9b83a118207f414f2f7e4c0391785dc440fa4 Mon Sep 17 00:00:00 2001 From: mryouse Date: Fri, 26 May 2023 02:24:35 +0000 Subject: make 'concat' work for sequences --- chunk.d | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'chunk.d') diff --git a/chunk.d b/chunk.d index 08ba003..7a532f5 100644 --- a/chunk.d +++ b/chunk.d @@ -46,6 +46,7 @@ abstract class Seq { abstract Value first(); abstract Seq rest(); abstract int length(); + abstract Seq concat(Seq seq); } class String : Seq { @@ -53,10 +54,12 @@ class String : Seq { this(Value value) { this.str = value.as.str; + this.type = SeqType.STRING; } this(string str) { this.str = str; + this.type = SeqType.STRING; } override Value first() { @@ -71,6 +74,19 @@ class String : Seq { return to!int(str.length); } + override Seq concat(Seq seq) { + if (seq.type != SeqType.STRING) { + // how do i throw an error here? + writeln("must concat strings to strings!"); + return this; + } + String strSeq = cast(String)seq; + + writefln("in concat with '%s' and '%s'", this.str, strSeq.str); + + return new String(this.str ~ strSeq.str); + } + override string toString() { return format("\"%s\"", this.str); } @@ -104,6 +120,16 @@ class List : Seq { return to!int(this.inner.length); } + override Seq concat(Seq seq) { + int length = to!int(this.inner.length); + List ret = new List(length + 1); + for (int i = 0; i < length; i++) { + ret.addItemAtIndex(this.inner[i], i); + } + ret.addItemAtIndex(makeSeqValue(seq), length); + return ret; + } + override string toString() { return format("list ('%s' + %d)", printableValue(this.inner[0]), this.inner.length - 1); } -- cgit v1.2.3