Parcourir la source

Implement u and O

Added the ability to Dive and Rise. This makes it so that all
non-movement or mirror instructions are ignored.
ApisNecros il y a 1 an
Parent
commit
fb4199d952
2 fichiers modifiés avec 37 ajouts et 1 suppressions
  1. 4 1
      examples.txt
  2. 33 0
      starfish.js

+ 4 - 1
examples.txt

@@ -49,4 +49,7 @@ o;!?l<
 
 ;; Test Call and Ret instructions
 <;n&C10&a
-R
+R
+
+;; Test dive and rise instructions
+"Hello, world!"r>Ool?u!|;

+ 33 - 0
starfish.js

@@ -56,6 +56,21 @@ class CodeBox {
          * @type {boolean}
          */
         this.onTheHook = false;
+        /**
+         * Have we dived using `u`
+         *
+         * Until rising with `O`, all instructions except movement and mirror instructions are ignored.
+         * @type {boolean}
+         */
+        this.hasDove = false;
+        /**
+         * Constant of values to check when `this.hasDove == true`
+         *
+         * Includes `O` so that we can rise.
+         *
+         * @type {string[]}
+         */
+        this.MOVEMENT_AND_MIRRORS = [">", "<", "^", "v", "/", "\\", "|", "_", "#", "x", "`", "O"];
         /**
          * Are we currently processing code box instructions as a string
          *
@@ -262,6 +277,14 @@ class CodeBox {
 
     Execute(instruction) {
         let output = null;
+        
+        // Ignore non-movement and non-mirror instructions
+        if (this.hasDove) {
+            if (this.MOVEMENT_AND_MIRRORS.indexOf(instruction) < 0) {
+                return output;
+            }
+        }
+        
         try{
             switch(instruction) {
                 // NOP
@@ -340,6 +363,16 @@ class CodeBox {
                 case "'":
                     this.stringMode = !!this.stringMode ? 0 : dec(instruction);
                     break;
+                // Dive, Rise and Fisherman
+                case "u":
+                    this.hasDove = true;
+                    break;
+                case "O":
+                    this.hasDove = false;
+                    break;
+                case "`":
+                    // TODO
+                    break;
                 // Movement
                 case "^":
                     this.MoveUp();