Pārlūkot izejas kodu

Add try/catch around intruction execution

Doing this allows all errors from any functions in the call stack to get
caught in one place.
ApisNecros 1 gadu atpakaļ
vecāks
revīzija
b1b1fbfeec
1 mainītis faili ar 153 papildinājumiem un 148 dzēšanām
  1. 153 148
      starfish.js

+ 153 - 148
starfish.js

@@ -169,155 +169,160 @@ class CodeBox {
 
     Execute(instruction) {
         let output = null;
-        switch(instruction) {
-            // NOP
-            case " ":
-                break;
-            // Numbers
-            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;
-            // Operators
-            case "+": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.Push(y + x);
-                break;
-            }
-            case "-": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.Push(y - x);
-                break;
-            }
-            case "*": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.Push(y * x);
-                break;
-            }
-            case ",": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.Push(y / x);
-                break;
-            }
-            case "%": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.Push(y % x);
-                break;
-            }
-            case "(": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.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);
-                break;
-            }
-            case "=": {
-                const x = this.stack.Pop();
-                const y = this.stack.Pop();
-                this.stack.push(y == x ? 1 : 0);
-                break;
+        try{
+            switch(instruction) {
+                // NOP
+                case " ":
+                    break;
+                // Numbers
+                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;
+                // Operators
+                case "+": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.Push(y + x);
+                    break;
+                }
+                case "-": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.Push(y - x);
+                    break;
+                }
+                case "*": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.Push(y * x);
+                    break;
+                }
+                case ",": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.Push(y / x);
+                    break;
+                }
+                case "%": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.Push(y % x);
+                    break;
+                }
+                case "(": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.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);
+                    break;
+                }
+                case "=": {
+                    const x = this.stack.Pop();
+                    const y = this.stack.Pop();
+                    this.stack.push(y == x ? 1 : 0);
+                    break;
+                }
+                // Movement
+                case "^":
+                    this.MoveUp();
+                    break;
+                case ">":
+                    this.MoveRight();
+                    break;
+                case "v":
+                    this.MoveDown();
+                    break;
+                case "<":
+                    this.MoveLeft();
+                    break;
+                // Mirrors
+                case "/":
+                    this.ReflectForward();
+                    break;
+                case "\\":
+                    this.ReflectBack();
+                    break;
+                case "_":
+                    this.VerticalMirror();
+                    break;
+                case "|":
+                    this.HorizontalMirror();
+                    break;
+                case "#":
+                    this.OmniMirror();
+                    break;
+                // Stack manipulation
+                case ":":
+                    this.stack.Duplicate();
+                    break;
+                case "~":
+                    this.stack.Remove();
+                    break;
+                case "$":
+                    this.stack.SwapTwo();
+                    break;
+                case "@":
+                    this.stack.SwapThree();
+                    break;
+                case "{":
+                    this.stack.ShiftLeft();
+                    break;
+                case "}":
+                    this.stack.ShiftRight();
+                    break;
+                case "r":
+                    this.stack.Reverse();
+                    break;
+                case "l":
+                    this.stack.PushLength();
+                    break;
+                // case "[":
+                //     break;
+                // case "]":
+                //     break;
+                // case "I":
+                //     this.curr_stack++;
+                //     break;
+                // case "D":
+                //     this.curr_stack--;
+                //     break;
+                // Output
+                case "n": 
+                    output = this.stack.Pop();
+                    break;
+                case "o":
+                    output = String.fromCharCode(this.stack.Pop());
+                    break;
+                // End execution
+                case ";":
+                    output = true;
+                    break;
+                default:
+                    throw new Error();
             }
-            // Movement
-            case "^":
-                this.MoveUp();
-                break;
-            case ">":
-                this.MoveRight();
-                break;
-            case "v":
-                this.MoveDown();
-                break;
-            case "<":
-                this.MoveLeft();
-                break;
-            // Mirrors
-            case "/":
-                this.ReflectForward();
-                break;
-            case "\\":
-                this.ReflectBack();
-                break;
-            case "_":
-                this.VerticalMirror();
-                break;
-            case "|":
-                this.HorizontalMirror();
-                break;
-            case "#":
-                this.OmniMirror();
-                break;
-            // Stack manipulation
-            case ":":
-                this.stack.Duplicate();
-                break;
-            case "~":
-                this.stack.Remove();
-                break;
-            case "$":
-                this.stack.SwapTwo();
-                break;
-            case "@":
-                this.stack.SwapThree();
-                break;
-            case "{":
-                this.stack.ShiftLeft();
-                break;
-            case "}":
-                this.stack.ShiftRight();
-                break;
-            case "r":
-                this.stack.Reverse();
-                break;
-            case "l":
-                this.stack.PushLength();
-                break;
-            // case "[":
-            //     break;
-            // case "]":
-            //     break;
-            // case "I":
-            //     this.curr_stack++;
-            //     break;
-            // case "D":
-            //     this.curr_stack--;
-            //     break;
-            // Output
-            case "n": 
-                output = this.stack.Pop();
-                break;
-            case "o":
-                output = String.fromCharCode(this.stack.Pop());
-                break;
-            // End execution
-            case ";":
-                output = true;
-                break;
-            default:
-                throw new Error("Something's fishy!");
+        }
+        catch(e) {
+            console.error(`Something smells fishy!\nInstruction: ${instruction}\nStack: ${JSON.stringify(this.stack.stack)}`);
         }
 
         return output;