|
@@ -1,3 +1,5 @@
|
|
|
+const prompt = require("prompt-sync")({ sigint: true });
|
|
|
+
|
|
|
const Stack = require("./Stack");
|
|
|
const ComputerParameterMode = require("./ComputerParameterMode");
|
|
|
const { DeepClone } = require("./common");
|
|
@@ -14,6 +16,7 @@ module.exports = class Computer {
|
|
|
this.OPCODES = {
|
|
|
ADD: 1,
|
|
|
MULTIPLY: 2,
|
|
|
+ INPUT: 3,
|
|
|
OUTPUT: 4,
|
|
|
HALT: 99,
|
|
|
};
|
|
@@ -58,6 +61,10 @@ module.exports = class Computer {
|
|
|
this.Operation_Multiply(operandLeft, operandRight, position);
|
|
|
break;
|
|
|
}
|
|
|
+ case this.OPCODES.INPUT: {
|
|
|
+ this.Operation_Input();
|
|
|
+ break;
|
|
|
+ }
|
|
|
case this.OPCODES.OUTPUT: {
|
|
|
const outputPosition = this.stack.Next().Get();
|
|
|
|
|
@@ -132,6 +139,29 @@ module.exports = class Computer {
|
|
|
this.stack.Put(outputPosition, newValue);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Execute the Input opcode
|
|
|
+ *
|
|
|
+ * Prompts the user to input a value from the command line
|
|
|
+ *
|
|
|
+ * @returns {void}
|
|
|
+ */
|
|
|
+ Operation_Input() {
|
|
|
+ const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
|
|
|
+
|
|
|
+ console.log(`DEBUG: outputPosition: ${outputPosition}`);
|
|
|
+
|
|
|
+ let userInput;
|
|
|
+
|
|
|
+ do {
|
|
|
+ userInput = Number(prompt("Please enter a number: "));
|
|
|
+ } while (Number.isNaN(userInput));
|
|
|
+
|
|
|
+ console.log(`DEBUG: userInput: ${userInput}`);
|
|
|
+
|
|
|
+ this.stack.Put(outputPosition, userInput);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Execute the OUTPUT opcode
|
|
|
*
|