Class ScopedInstructionExecutor

java.lang.Object
org.ek9lang.compiler.phase7.support.ScopedInstructionExecutor

public final class ScopedInstructionExecutor extends Object
CONCERN: Temporary scope lifecycle management. RESPONSIBILITY: Execute instructions within auto-cleanup scope. REUSABILITY: ALL generators (33+ usages across codebase).

Encapsulates the repetitive pattern: 1. Generate scope ID 2. Push scope onto stack (enterScope) 3. Add SCOPE_ENTER instruction 4. Execute instructions 5. Add SCOPE_EXIT instruction 6. Pop scope from stack (exitScope)

This ensures consistent scope lifecycle management across all generators, eliminating bugs from manual scope handling and reducing code duplication.

  • Constructor Details

    • ScopedInstructionExecutor

      public ScopedInstructionExecutor(IRGenerationContext stackContext)
  • Method Details

    • execute

      public List<IRInstr> execute(Supplier<List<IRInstr>> instructionGenerator, DebugInfo debugInfo)
      Execute instructions within temporary scope.

      The scope is automatically created, pushed onto the stack, and cleaned up after the instructions execute. This ensures proper memory management through scope-based reference counting.

      Parameters:
      instructionGenerator - Supplier that generates the instructions to execute
      debugInfo - Debug information for scope instructions
      Returns:
      Complete instruction list with SCOPE_ENTER, instructions, SCOPE_EXIT