Explorar o código

Add ability for CodeBox to have multiple stacks

ApisNecros hai 1 ano
pai
achega
c68f4f6479
Modificáronse 1 ficheiros con 49 adicións e 43 borrados
  1. 49 43
      starfish.js

+ 49 - 43
starfish.js

@@ -66,13 +66,17 @@ class CodeBox {
          */
         this.stringMode = 0;
         /**
-         * The stack for the code box to work with
+         * A list of stacks for the script to work with
          *
-         * @TODO Implement multiple stacks
+         * @type {Stack[]}
+         */
+        this.stacks = [new Stack()];
+        /**
+         * The index of the currently used stack
          *
-         * @type {Stack}
+         * @type {int}
          */
-        this.stack = new Stack();
+        this.curr_stack = 0;
 
         this.codeBoxDOM = document.getElementById(codeBoxID);
         if(!this.codeBoxDOM) {
@@ -96,8 +100,10 @@ class CodeBox {
      * Transforms the textual code box into usable matrix
      */
     ParseCodeBox() {
-        // Reset some field for a clean run
+        // Reset some fields for a clean run
         this.box = [];
+        this.stacks = [new Stack()];
+        this.curr_stack = 0;
         this.pointer = {X: 0, Y: 0};
         this.curr_direction = this.directions.EAST;
         this.outputDOM.value = "";
@@ -191,55 +197,55 @@ class CodeBox {
                 case "d":
                 case "e":
                 case "f":
-                    this.stack.Push(parseInt(instruction, 16));
+                    this.stacks[this.curr_stack].Push(parseInt(instruction, 16));
                     break;
                 // Operators
                 case "+": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y + x);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y + x);
                     break;
                 }
                 case "-": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y - x);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y - x);
                     break;
                 }
                 case "*": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y * x);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y * x);
                     break;
                 }
                 case ",": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y / x);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y / x);
                     break;
                 }
                 case "%": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y % x);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y % x);
                     break;
                 }
                 case "(": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y < x ? 1 : 0);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y < x ? 1 : 0);
                     break;
                 }
                 case ")": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.Push(y > x ? 1 : 0);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].Push(y > x ? 1 : 0);
                     break;
                 }
                 case "=": {
-                    const x = this.stack.Pop();
-                    const y = this.stack.Pop();
-                    this.stack.push(y == x ? 1 : 0);
+                    const x = this.stacks[this.curr_stack].Pop();
+                    const y = this.stacks[this.curr_stack].Pop();
+                    this.stacks[this.curr_stack].push(y == x ? 1 : 0);
                     break;
                 }
                 //String mode
@@ -281,32 +287,32 @@ class CodeBox {
                     this.Move();
                     break;
                 case "?":
-                    if(this.stack.Pop() === 0){ this.Move(); }
+                    if(this.stacks[this.curr_stack].Pop() === 0){ this.Move(); }
                     break;
                 // Stack manipulation
                 case ":":
-                    this.stack.Duplicate();
+                    this.stacks[this.curr_stack].Duplicate();
                     break;
                 case "~":
-                    this.stack.Remove();
+                    this.stacks[this.curr_stack].Remove();
                     break;
                 case "$":
-                    this.stack.SwapTwo();
+                    this.stacks[this.curr_stack].SwapTwo();
                     break;
                 case "@":
-                    this.stack.SwapThree();
+                    this.stacks[this.curr_stack].SwapThree();
                     break;
                 case "{":
-                    this.stack.ShiftLeft();
+                    this.stacks[this.curr_stack].ShiftLeft();
                     break;
                 case "}":
-                    this.stack.ShiftRight();
+                    this.stacks[this.curr_stack].ShiftRight();
                     break;
                 case "r":
-                    this.stack.Reverse();
+                    this.stacks[this.curr_stack].Reverse();
                     break;
                 case "l":
-                    this.stack.PushLength();
+                    this.stacks[this.curr_stack].PushLength();
                     break;
                 // case "[":
                 //     break;
@@ -320,10 +326,10 @@ class CodeBox {
                 //     break;
                 // Output
                 case "n": 
-                    output = this.stack.Pop();
+                    output = this.stacks[this.curr_stack].Pop();
                     break;
                 case "o":
-                    output = String.fromCharCode(this.stack.Pop());
+                    output = String.fromCharCode(this.stacks[this.curr_stack].Pop());
                     break;
                 // End execution
                 case ";":
@@ -334,7 +340,7 @@ class CodeBox {
             }
         }
         catch(e) {
-            console.error(`Something smells fishy!\n${e != "" ? `${e}\n` : ""}Instruction: ${instruction}\nStack: ${JSON.stringify(this.stack.stack)}`);
+            console.error(`Something smells fishy!\n${e != "" ? `${e}\n` : ""}Instruction: ${instruction}\nStack: ${JSON.stringify(this.stacks[this.curr_stack].stack)}`);
             return true;
         }
 
@@ -345,7 +351,7 @@ class CodeBox {
         const instruction = this.box[this.pointer.Y][this.pointer.X];
 
         if(this.stringMode != 0 && dec(instruction) != this.stringMode) {
-            this.stack.Push(dec(instruction));
+            this.stacks[this.curr_stack].Push(dec(instruction));
         }
         else {
             const exeResult = this.Execute(instruction);