Browse Source

Add followPointer option

Added an option to the computer to dump the memory upon every Execute
call that has the current memory address highlighted.
ApisNecros 1 year ago
parent
commit
1af1ba9c1d
1 changed files with 20 additions and 3 deletions
  1. 20 3
      IntComp/Computer.js

+ 20 - 3
IntComp/Computer.js

@@ -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 }));
     }
 
     /**