123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- const ComputerParameterMode = require("./ComputerParameterMode");
- module.exports = class Stack {
- constructor(stack) {
- this.pointer = 0;
- this._stack = stack;
-
- this.relativeBaseOffset = 0;
- }
-
- Next() {
- this.pointer++;
- return this;
- }
-
- Prev() {
- this.pointer--;
- }
-
- Get(parameterMode = ComputerParameterMode.POSITION_MODE, isOutput = false) {
- let value = this._stack[this.pointer];
- if (!isOutput && parameterMode == ComputerParameterMode.POSITION_MODE) {
- value = this._stack[value];
- }
- else if (parameterMode == ComputerParameterMode.RELATIVE_MODE) {
- const newPointer = value + this.relativeBaseOffset;
- value = isOutput ? newPointer : this._stack[newPointer];
- }
- return value ?? 0;
- }
-
- GetAtIndex(index, parameterMode = ComputerParameterMode.POSITION_MODE) {
- if (index == null || Number.isNaN(index) || !Number.isInteger(index)) {
- throw new TypeError("index must be an integer");
- }
- let value = this._stack[index];
- if (parameterMode == ComputerParameterMode.POSITION_MODE) {
- value = this._stack[value];
- }
- else if (parameterMode == ComputerParameterMode.RELATIVE_MODE) {
- value = this._stack[value + this.relativeBaseOffset];
- }
- return value ?? 0;
- }
-
- Push(value) {
- this._stack[++this.pointer] = value;
- }
-
- Pop() {
- return this._stack.pop();
- }
-
- Put(index, value) {
- this._stack[index] = value;
- }
-
- SetPointerAddress(newAddress) {
- if (newAddress > this._stack.length) {
-
- const oldLength = this._stack.length;
- this._stack[newAddress] = 0;
- this._stack.fill(0, oldLength, newAddress);
- }
- this.pointer = newAddress;
- }
-
- AdjustRelativeBaseOffset(adjustment) {
- this.relativeBaseOffset += adjustment;
- }
-
- Dump() {
- return this._stack;
- }
- };
|