|
@@ -314,16 +314,26 @@ class CodeBox {
|
|
|
case "l":
|
|
|
this.stacks[this.curr_stack].PushLength();
|
|
|
break;
|
|
|
- // case "[":
|
|
|
- // break;
|
|
|
+ case "[": {
|
|
|
+ this.SpliceStack(this.stacks[this.curr_stack].Pop());
|
|
|
+ break;
|
|
|
+ }
|
|
|
// case "]":
|
|
|
// break;
|
|
|
- // case "I":
|
|
|
- // this.curr_stack++;
|
|
|
- // break;
|
|
|
- // case "D":
|
|
|
- // this.curr_stack--;
|
|
|
- // break;
|
|
|
+ case "I": {
|
|
|
+ this.curr_stack++;
|
|
|
+ if (this.curr_stack >= this.stacks.length) {
|
|
|
+ throw new RangeError("curr_stack value out of bounds");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "D": {
|
|
|
+ this.curr_stack--;
|
|
|
+ if (this.curr_stack < 0) {
|
|
|
+ throw new RangeError("curr_stack value out of bounds");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
// Output
|
|
|
case "n":
|
|
|
output = this.stacks[this.curr_stack].Pop();
|
|
@@ -518,6 +528,37 @@ class CodeBox {
|
|
|
this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Takes X number of elements out of a stack and into a new stack
|
|
|
+ *
|
|
|
+ * This action creates a new stack, and places it on top of the one it was created from.
|
|
|
+ * So, if you have three stacks, A, B, and C, and you splice a stack off of stack B,
|
|
|
+ * the new order will be: A, B, D, and C.
|
|
|
+ *
|
|
|
+ * @see {@link https://esolangs.org/wiki/Fish#Stacks ><> Documentation}
|
|
|
+ *
|
|
|
+ * @param {int} spliceCount The number of elements to pop into a new stack
|
|
|
+ */
|
|
|
+ SpliceStack(spliceCount) {
|
|
|
+ const stackCount = this.stacks[this.curr_stack].stack.length;
|
|
|
+
|
|
|
+ if (spliceCount > stackCount) {
|
|
|
+ throw new RangeError(`Cannot remove ${spliceCount} elements from a stack of only ${stackCount} elements`);
|
|
|
+ }
|
|
|
+
|
|
|
+ const newStack = new Stack(this.stacks[this.curr_stack].stack.splice(stackCount - spliceCount, spliceCount));
|
|
|
+
|
|
|
+ // We're at the top of the stacks stack, so we can use .push
|
|
|
+ if (this.curr_stack == this.stacks.length - 1) {
|
|
|
+ this.stacks.push(newStack);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ this.stacks.splice(this.curr_stack + 1, 0, newStack);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.curr_stack++;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Implement `
|
|
|
*
|