|
@@ -169,155 +169,160 @@ class CodeBox {
|
|
|
|
|
|
Execute(instruction) {
|
|
Execute(instruction) {
|
|
let output = null;
|
|
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;
|
|
return output;
|