|
@@ -75,6 +75,20 @@ module.exports = class Computer {
|
|
return status;
|
|
return status;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Parse operands based on the current parameter mode
|
|
|
|
+ *
|
|
|
|
+ * When the int computer is in Immediate Mode, the values are returned
|
|
|
|
+ * as-is. When in Position Mode, the operands are used as memory
|
|
|
|
+ * addresses, and the values at those addresses are returned instead.
|
|
|
|
+ *
|
|
|
|
+ * @returns {number[]} The parsed list of operands
|
|
|
|
+ */
|
|
|
|
+ _ParseOperands(...operands) {
|
|
|
|
+ if (this.parameterMode == ComputerParameterMode.IMMEDIATE_MODE) { return operands; }
|
|
|
|
+ return operands.map((operand) => this.stack.Get(operand));
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Execute the Add opcode
|
|
* Execute the Add opcode
|
|
*
|
|
*
|
|
@@ -87,6 +101,7 @@ module.exports = class Computer {
|
|
* @returns {void}
|
|
* @returns {void}
|
|
*/
|
|
*/
|
|
Operation_Add(operandLeft, operandRight, outputPosition) {
|
|
Operation_Add(operandLeft, operandRight, outputPosition) {
|
|
|
|
+ [operandLeft, operandRight] = this._ParseOperands(operandLeft, operandRight);
|
|
const newValue = operandLeft + operandRight;
|
|
const newValue = operandLeft + operandRight;
|
|
this.stack.Put(outputPosition, newValue);
|
|
this.stack.Put(outputPosition, newValue);
|
|
}
|
|
}
|
|
@@ -103,6 +118,8 @@ module.exports = class Computer {
|
|
* @returns {void}
|
|
* @returns {void}
|
|
*/
|
|
*/
|
|
Operation_Multiply(operandLeft, operandRight, outputPosition) {
|
|
Operation_Multiply(operandLeft, operandRight, outputPosition) {
|
|
|
|
+ [operandLeft, operandRight] = this._ParseOperands(operandLeft, operandRight);
|
|
|
|
+
|
|
const newValue = operandLeft * operandRight;
|
|
const newValue = operandLeft * operandRight;
|
|
this.stack.Put(outputPosition, newValue);
|
|
this.stack.Put(outputPosition, newValue);
|
|
}
|
|
}
|