ComputerParameterMode.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const { DecimalPlaceIsNonZero } = require("./common");
  2. module.exports = {
  3. /**
  4. * Position Mode
  5. *
  6. * When the int computer is in Position mode, the value at the pointer
  7. * is interpreted as another address in memory.
  8. */
  9. POSITION_MODE: 0,
  10. /**
  11. * Immediate Mode
  12. *
  13. * When the int computer is in Immediate Mode, the value at the pointer
  14. * is interpreted as its literal value.
  15. */
  16. IMMEDIATE_MODE: 1,
  17. /**
  18. * Parse an opcode to get the Parameter Mode for an operation's parameter
  19. *
  20. * Note: `parameterIndex` is a 1-based index, not 0-based.
  21. *
  22. * @example
  23. * // POSITION_MODE
  24. * const firstParamMode = ComputerParameterMode.ParseParameterMode(1002, 1);
  25. * // IMMEDIATE_MODE
  26. * const secondParamMode = ComputerParameterMode.ParseParameterMode(1002, 2);
  27. *
  28. * @param {number} rawOpcode The opcode in memory
  29. * @param {number} parameterIndex The index of the parameter in the function arguments
  30. * @returns {number} Either POSITION_MODE or IMMEDIATE_MODE
  31. */
  32. ParseParameterMode: function (rawOpcode, parameterIndex) {
  33. if (DecimalPlaceIsNonZero(rawOpcode, parameterIndex + 1)) { return this.IMMEDIATE_MODE; }
  34. return this.POSITION_MODE;
  35. },
  36. };