Bläddra i källkod

Implement g

Added the ability to take values from the code box, and push them onto
the stack. Used the original ><> implementation to do this.
ApisNecros 1 år sedan
förälder
incheckning
21298b5088
1 ändrade filer med 35 tillägg och 1 borttagningar
  1. 35 1
      starfish.js

+ 35 - 1
starfish.js

@@ -342,6 +342,10 @@ class CodeBox {
                 case "o":
                     output = String.fromCharCode(this.stacks[this.curr_stack].Pop());
                     break;
+                // Misc
+                case "g":
+                    this.PushFromCodeBox();
+                    break;
                 // End execution
                 case ";":
                     output = true;
@@ -530,6 +534,8 @@ class CodeBox {
     }
 
     /**
+     * Implement [
+     *
      * 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.
@@ -560,8 +566,9 @@ class CodeBox {
         this.curr_stack++;
     }
     /**
+     * Implement ]
+     * 
      * Collapses the current stack onto the one below it
-     *
      * If the current stack is the only one, it is replaced with a blank stack
      */
     CollapseStack() {
@@ -582,6 +589,33 @@ class CodeBox {
         }
     }
 
+    /**
+     * Implement g
+     * 
+     * Pops `y` and `x` from the stack, and then pushes the value of the character
+     * at `[x, y]` in the code box.
+     * 
+     * NOP's and coords that are out of bounds are converted to 0.
+     * 
+     * Implements the behavior as defined by the original {@link https://gist.github.com/anonymous/6392418#file-fish-py-L306 ><>}, and not {@link https://github.com/redstarcoder/go-starfish/blob/master/starfish/starfish.go#L378 go-starfish}
+     */
+    PushFromCodeBox() {
+        const y = this.stacks[this.curr_stack].Pop();
+        const x = this.stacks[this.curr_stack].Pop();
+
+        let val = undefined;
+        try {
+            val = this.box[y][x] || " ";
+        }
+        catch (e) {
+            val = " ";
+        }
+
+        const valParsed = val == " " ? 0 : dec(val);
+
+        this.stacks[this.curr_stack].Push(valParsed);
+    }
+
     /**
      * Implement `
      *