aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
diff options
context:
space:
mode:
Diffstat (limited to 'chunk.d')
-rw-r--r--chunk.d20
1 files changed, 20 insertions, 0 deletions
diff --git a/chunk.d b/chunk.d
index 21ebb9d..fb1f97c 100644
--- a/chunk.d
+++ b/chunk.d
@@ -47,6 +47,7 @@ abstract class Seq {
abstract Seq rest();
abstract Seq most();
abstract Value last();
+ abstract Seq reverse();
abstract int length();
abstract bool isIn(Value val);
}
@@ -76,6 +77,15 @@ class String : Seq {
return new String(this.str[0..$ - 1]);
}
+ override Seq reverse() {
+ string s = "";
+ int end = to!int(this.str.length) - 1;
+ for (int i = end; i >= 0; i--) {
+ s ~= this.str[i];
+ }
+ return new String(s);
+ }
+
override Value last() {
return makeStringValue(to!string(this.str[$ - 1]));
}
@@ -155,6 +165,15 @@ class List : Seq {
return ret;
}
+ override Seq reverse() {
+ int length = to!int(this.inner.length);
+ List ret = new List(length);
+ for (int i = 0; i < length; i++) {
+ ret.addItemAtIndex(this.inner[i], length - i - 1);
+ }
+ return ret;
+ }
+
override Value last() {
return this.inner[$ - 1]; // this fails on NIL
}
@@ -225,6 +244,7 @@ enum OpCode {
OP_MEMBER,
OP_MOST,
OP_REST,
+ OP_REVERSE,
// DEFINITIONS/VARIABLES
OP_CONSTANT,