Class AssertThrowsInstr

java.lang.Object
org.ek9lang.compiler.ir.instructions.IRInstr
org.ek9lang.compiler.ir.instructions.AssertThrowsInstr
All Implemented Interfaces:
INode

public final class AssertThrowsInstr extends IRInstr
IR instruction for test-only assertion constructs that wrap expressions in try-catch.

Handles two opcodes:

  • ASSERT_THROWS - Expects a specific exception type; returns caught exception
  • ASSERT_DOES_NOT_THROW - Expects no exception; returns Void

These instructions are test-only constructs that throw non-catchable AssertionError when the assertion fails. The test runner detects program failure.

Memory management:

  • Expression body instructions are evaluated within a try block
  • For ASSERT_THROWS: caught exception is stored to result variable
  • Scope management is handled by the contained instructions
  • Method Details

    • assertThrows

      public static AssertThrowsInstr assertThrows(String result, String expectedType, List<IRInstr> bodyInstructions, String message, String scopeId, DebugInfo debugInfo)
      Create ASSERT_THROWS instruction.

      When the expression throws the expected exception type, the exception is caught and stored to the result variable. If no exception or wrong type, throws AssertionError.

      Parameters:
      result - Variable to store caught exception (can be null if not used)
      expectedType - Fully qualified name of expected exception type
      bodyInstructions - Instructions to evaluate within try block
      message - Error message for assertion failure
      scopeId - Scope ID for memory management
      debugInfo - Debug info with source location
    • assertDoesNotThrow

      public static AssertThrowsInstr assertDoesNotThrow(List<IRInstr> bodyInstructions, String message, String scopeId, DebugInfo debugInfo)
      Create ASSERT_DOES_NOT_THROW instruction.

      When the expression throws any exception, throws AssertionError with details. When no exception, the assertion passes (returns Void).

      Parameters:
      bodyInstructions - Instructions to evaluate within try block
      message - Error message prefix for assertion failure
      scopeId - Scope ID for memory management
      debugInfo - Debug info with source location
    • getExpectedExceptionType

      public String getExpectedExceptionType()
      Get the expected exception type (for ASSERT_THROWS).
      Returns:
      Fully qualified exception type name, or null for ASSERT_DOES_NOT_THROW
    • getExpressionBodyInstructions

      public List<IRInstr> getExpressionBodyInstructions()
      Get the expression body instructions to evaluate within the try block.
    • getMessage

      public String getMessage()
      Get the error message for assertion failure.
    • getScopeId

      public String getScopeId()
      Get the scope ID for memory management.
    • isAssertThrows

      public boolean isAssertThrows()
      Check if this is an ASSERT_THROWS instruction.
    • isAssertDoesNotThrow

      public boolean isAssertDoesNotThrow()
      Check if this is an ASSERT_DOES_NOT_THROW instruction.
    • toString

      public String toString()
      Overrides:
      toString in class IRInstr