Jelajahi Sumber

Implement beginning swim loop

Laid the foundation for the swim loop. Added enough instrutions to the
execution plan to do simple tests.
ApisNecros 2 tahun lalu
induk
melakukan
8ac6e7628d
1 mengubah file dengan 95 tambahan dan 0 penghapusan
  1. 95 0
      starfish.js

+ 95 - 0
starfish.js

@@ -42,6 +42,101 @@ class CodeBox {
          * @type {boolean}
          */
         this.onTheHook = false;
+        /**
+         * Are we currently processing code box instructions as a string
+         *
+         * 0 when false, otherwise it holds the char code for string delimiter, either
+         * 34 or 39
+         *
+         * @type {int}
+         */
+        this.stringMode = 0;
+        /**
+         * The stack for the code box to work with
+         *
+         * @TODO Implement multiple stacks
+         *
+         * @type {Stack}
+         */
+        this.stack = new Stack();
+    }
+
+    /**
+     * Print the value to the display 
+     *
+     * @TODO Set up an actual display
+     * @param {*} value 
+     */
+    Output(value) {
+        console.log(value);
+    }
+
+    Execute(instruction) {
+        let output = null;
+        switch(instruction) {
+            case 1:
+            case 2:
+            case 3:
+            case 4:
+            case 5:
+            case 6:
+            case 7:
+            case 8:
+            case 9:
+            case 0:
+            case "a":
+            case "b":
+            case "c":
+            case "d":
+            case "e":
+            case "f":
+                this.stack.Push(parseInt(instruction, 16));
+                break;
+            case "+": {
+                const x = this.stack.Pop();
+                const y = this.stack.Pop();
+                this.stack.Push(x + y);
+                break;
+            }
+            case "-": {
+                const x = this.stack.Pop();
+                const y = this.stack.Pop();
+                this.stack.Push(x - y);
+                break;
+            }
+            case "n": 
+                output = this.stack.Pop();
+                break;
+            case "o":
+                output = String.fromCharCode(this.stack.Pop());
+                break;
+            case ";":
+                output = true;
+                break;
+            default:
+                throw new Error("Something's fishy!");
+        }
+
+        return output;
+    }
+
+    Swim() {
+        const instruction = this.box[this.pointer[0], this.pointer[1]];
+
+        if(this.stringMode != 0 && instruction != this.stringMode) {
+            this.stack.Push(dec(instruction));
+        }
+        else {
+            const exeResult = this.Execute(instruction);
+            if(exeResult === true) {
+                return true;
+            }
+            else if(exeResult != null) {
+                this.Output(exeResult);
+            }
+        }
+
+        this.SetPointer(this.curr_direction[0], this.curr_direction[1]);
     }
 
     /**