|
@@ -1,4 +1,5 @@
|
|
|
const prompt = require("prompt-sync")({ sigint: true });
|
|
|
+const util = require("util");
|
|
|
|
|
|
const Stack = require("./Stack");
|
|
|
const ComputerParameterMode = require("./ComputerParameterMode");
|
|
@@ -10,7 +11,7 @@ const { DeepClone } = require("./common");
|
|
|
* @author Apis Necros
|
|
|
*/
|
|
|
module.exports = class Computer {
|
|
|
- constructor(stack) {
|
|
|
+ constructor(stack, options = {}) {
|
|
|
this._initialMemory = DeepClone(stack);
|
|
|
this.stack = new Stack(stack);
|
|
|
this.OPCODES = {
|
|
@@ -34,6 +35,10 @@ module.exports = class Computer {
|
|
|
* the Execute function move it after the opcode finishes executing.
|
|
|
*/
|
|
|
this.skipNext = false;
|
|
|
+
|
|
|
+ this.options = {
|
|
|
+ followPointer: options.followPointer ?? false,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -58,6 +63,10 @@ module.exports = class Computer {
|
|
|
let status = true;
|
|
|
this.skipNext = false;
|
|
|
|
|
|
+ if (this.options.followPointer) {
|
|
|
+ this.DumpMemory(true);
|
|
|
+ }
|
|
|
+
|
|
|
const opcode = rawOpcode % 100;
|
|
|
|
|
|
// console.log(`DEBUG: opcode: ${opcode}`);
|
|
@@ -217,10 +226,18 @@ module.exports = class Computer {
|
|
|
|
|
|
/**
|
|
|
* Outputs the computer's stack to the console
|
|
|
+ *
|
|
|
+ * @param {boolean} [highlightPointer=false] Should the memory address of the current pointer be highlighted
|
|
|
* @returns {void}
|
|
|
*/
|
|
|
- DumpMemory() {
|
|
|
- console.log(this.stack.Dump());
|
|
|
+ DumpMemory(highlightPointer = false) {
|
|
|
+ let memory = this.stack.Dump();
|
|
|
+
|
|
|
+ if (highlightPointer) {
|
|
|
+ memory = memory.map((instruction, idx) => (idx == this.stack.pointer ? `{${instruction}}` : instruction));
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(util.inspect(memory, { breakLength: Infinity, colors: true, compact: true }));
|
|
|
}
|
|
|
|
|
|
/**
|