소스 검색

Add StackOutOfBoundsError class

Added a new error for when the CodeBox attempts to increment or
decrement outside of the range of stacks available.
ApisNecros 1 년 전
부모
커밋
5c74ac6b3a
2개의 변경된 파일9개의 추가작업 그리고 3개의 파일을 삭제
  1. 3 3
      src/codebox.ts
  2. 6 0
      src/errors.ts

+ 3 - 3
src/codebox.ts

@@ -1,6 +1,6 @@
 import { Stack } from "./stack.js";
 import { InstructionPointer } from "./pointer.js";
-import { EmptyCodeBoxError, FailedToParseCodeBoxError, WrongStackSizeError } from "./errors.js";
+import { EmptyCodeBoxError, FailedToParseCodeBoxError, StackOutOfBoundsError, WrongStackSizeError } from "./errors.js";
 import { CodeBoxOptions, OrdinalDirection } from "./types.js";
 import { charToDecimal, deepClone } from "./common.js";
 import { Node } from "typescript";
@@ -377,14 +377,14 @@ export class CodeBox {
             case InstructionDictionary.STACK_INDEX_INCREMENT: {
                 this.currentStackIndex++;
                 if (this.currentStackIndex > this.stacks.length) {
-                    throw new RangeError("stack index out of bounds");
+                    throw new StackOutOfBoundsError(this.currentStackIndex);
                 }
                 break;
             }
             case InstructionDictionary.STACK_INDEX_DECREMENT: {
                 this.currentStackIndex--;
                 if (this.currentStackIndex < 0) {
-                    throw new RangeError("stack index out of bounds");
+                    throw new StackOutOfBoundsError(this.currentStackIndex);
                 }
                 break;
             }

+ 6 - 0
src/errors.ts

@@ -34,4 +34,10 @@ export class WrongStackSizeError extends SunfishError {
     constructor(elementsWanted: number, stackSize: number) {
         super(`Failed to perform an operation on ${elementsWanted} on a stack with only ${stackSize} elements`);
     }
+}
+
+export class StackOutOfBoundsError extends SunfishError {
+    constructor(badIndex: number) {
+        super(`The stack pointer has gone out of bounds at index ${badIndex}`);
+    }
 }