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: any generator whose scope encloses one contiguous body (see executeWithScopeId(Function, DebugInfo) when the body or the caller needs the scope id).

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
    • executeWithScopeId

      public ScopedInstructionExecutor.ScopedBlock executeWithScopeId(Function<String, List<IRInstr>> body, DebugInfo debugInfo)
      THE scoped body for a generator whose body needs, or whose caller needs, the scope id.

      Exactly the hand-rolled sequence: generateScopeId, enterScope, SCOPE_ENTER, body, SCOPE_EXIT, exitScope. The body is handed the scope id and runs between the enter and the exit, so anything it generates (scope ids, temporary names, labels) is numbered after the scope's own id, as it always was.

      Parameters:
      body - Function from the new scope's id to the instructions that run inside it
      debugInfo - Debug information for scope instructions
      Returns:
      The scope id and the complete instruction list, SCOPE_ENTER, body, SCOPE_EXIT