|
@@ -66,13 +66,17 @@ class CodeBox {
|
|
*/
|
|
*/
|
|
this.stringMode = 0;
|
|
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);
|
|
this.codeBoxDOM = document.getElementById(codeBoxID);
|
|
if(!this.codeBoxDOM) {
|
|
if(!this.codeBoxDOM) {
|
|
@@ -96,8 +100,10 @@ class CodeBox {
|
|
* Transforms the textual code box into usable matrix
|
|
* Transforms the textual code box into usable matrix
|
|
*/
|
|
*/
|
|
ParseCodeBox() {
|
|
ParseCodeBox() {
|
|
- // Reset some field for a clean run
|
|
|
|
|
|
+ // Reset some fields for a clean run
|
|
this.box = [];
|
|
this.box = [];
|
|
|
|
+ this.stacks = [new Stack()];
|
|
|
|
+ this.curr_stack = 0;
|
|
this.pointer = {X: 0, Y: 0};
|
|
this.pointer = {X: 0, Y: 0};
|
|
this.curr_direction = this.directions.EAST;
|
|
this.curr_direction = this.directions.EAST;
|
|
this.outputDOM.value = "";
|
|
this.outputDOM.value = "";
|
|
@@ -191,55 +197,55 @@ class CodeBox {
|
|
case "d":
|
|
case "d":
|
|
case "e":
|
|
case "e":
|
|
case "f":
|
|
case "f":
|
|
- this.stack.Push(parseInt(instruction, 16));
|
|
|
|
|
|
+ this.stacks[this.curr_stack].Push(parseInt(instruction, 16));
|
|
break;
|
|
break;
|
|
// Operators
|
|
// Operators
|
|
case "+": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case "-": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case "*": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case ",": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case "%": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case "(": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case ")": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
case "=": {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
//String mode
|
|
//String mode
|
|
@@ -281,32 +287,32 @@ class CodeBox {
|
|
this.Move();
|
|
this.Move();
|
|
break;
|
|
break;
|
|
case "?":
|
|
case "?":
|
|
- if(this.stack.Pop() === 0){ this.Move(); }
|
|
|
|
|
|
+ if(this.stacks[this.curr_stack].Pop() === 0){ this.Move(); }
|
|
break;
|
|
break;
|
|
// Stack manipulation
|
|
// Stack manipulation
|
|
case ":":
|
|
case ":":
|
|
- this.stack.Duplicate();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].Duplicate();
|
|
break;
|
|
break;
|
|
case "~":
|
|
case "~":
|
|
- this.stack.Remove();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].Remove();
|
|
break;
|
|
break;
|
|
case "$":
|
|
case "$":
|
|
- this.stack.SwapTwo();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].SwapTwo();
|
|
break;
|
|
break;
|
|
case "@":
|
|
case "@":
|
|
- this.stack.SwapThree();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].SwapThree();
|
|
break;
|
|
break;
|
|
case "{":
|
|
case "{":
|
|
- this.stack.ShiftLeft();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].ShiftLeft();
|
|
break;
|
|
break;
|
|
case "}":
|
|
case "}":
|
|
- this.stack.ShiftRight();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].ShiftRight();
|
|
break;
|
|
break;
|
|
case "r":
|
|
case "r":
|
|
- this.stack.Reverse();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].Reverse();
|
|
break;
|
|
break;
|
|
case "l":
|
|
case "l":
|
|
- this.stack.PushLength();
|
|
|
|
|
|
+ this.stacks[this.curr_stack].PushLength();
|
|
break;
|
|
break;
|
|
// case "[":
|
|
// case "[":
|
|
// break;
|
|
// break;
|
|
@@ -320,10 +326,10 @@ class CodeBox {
|
|
// break;
|
|
// break;
|
|
// Output
|
|
// Output
|
|
case "n":
|
|
case "n":
|
|
- output = this.stack.Pop();
|
|
|
|
|
|
+ output = this.stacks[this.curr_stack].Pop();
|
|
break;
|
|
break;
|
|
case "o":
|
|
case "o":
|
|
- output = String.fromCharCode(this.stack.Pop());
|
|
|
|
|
|
+ output = String.fromCharCode(this.stacks[this.curr_stack].Pop());
|
|
break;
|
|
break;
|
|
// End execution
|
|
// End execution
|
|
case ";":
|
|
case ";":
|
|
@@ -334,7 +340,7 @@ class CodeBox {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch(e) {
|
|
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;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -345,7 +351,7 @@ class CodeBox {
|
|
const instruction = this.box[this.pointer.Y][this.pointer.X];
|
|
const instruction = this.box[this.pointer.Y][this.pointer.X];
|
|
|
|
|
|
if(this.stringMode != 0 && dec(instruction) != this.stringMode) {
|
|
if(this.stringMode != 0 && dec(instruction) != this.stringMode) {
|
|
- this.stack.Push(dec(instruction));
|
|
|
|
|
|
+ this.stacks[this.curr_stack].Push(dec(instruction));
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
const exeResult = this.Execute(instruction);
|
|
const exeResult = this.Execute(instruction);
|