Class InterpolatedStringGenerator

java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.InterpolatedStringGenerator
All Implemented Interfaces:
Function<ExprProcessingDetails, List<IRInstr>>

public final class InterpolatedStringGenerator extends AbstractGenerator implements Function<ExprProcessingDetails, List<IRInstr>>
Generates IR instructions for interpolated string expressions.

Handles the syntax: "Hello ${name}, you are ${age} years old"

IR Generation Strategy:
String interpolation generates a high-level STRING_INTERPOLATION instruction that provides structured data to backends for optimal concatenation:

  1. Evaluate each expression part and convert to String (if not already)
  2. Collect all parts (literals and converted variables)
  3. Create STRING_INTERPOLATION instruction with parts list

Backend Optimization:
The bespoke instruction allows backends to choose optimal strategies:

  • JVM: invokedynamic StringConcatFactory.makeConcatWithConstants()
  • LLVM: Single allocation with pre-calculated length and memcpy

Pre-conversion:
All expressions are converted to String BEFORE the interpolation instruction, keeping backends purely mechanical and enabling Phase 12 optimization (reusing previously converted Strings if source hasn't been mutated).

  • Constructor Details

    • InterpolatedStringGenerator

      InterpolatedStringGenerator(IRGenerationContext stackContext, GeneratorSet generators)
      Constructor for interpolated string IR generator.
      Parameters:
      stackContext - IRGenerationContext for scope and symbol access
      generators - GeneratorSet for accessing other generators (expression processor, etc.)
  • Method Details

    • apply

      public List<IRInstr> apply(ExprProcessingDetails details)
      Specified by:
      apply in interface Function<ExprProcessingDetails, List<IRInstr>>
    • interpolate

      public List<IRInstr> interpolate(EK9Parser.StringLitContext stringLitCtx, VariableDetails resultDetails)
      THE interpolation entry point for callers that already hold a stringLit - use this, do not re-derive it.

      A text body's string is a stringLit directly rather than one wrapped in an expression, so it cannot use apply(ExprProcessingDetails). That is the only difference, and it is not a reason to hand-roll the lowering: TextBodyMethodGenerator previously copied this class's part processing and drifted from it three ways - trait dispatch emitted as a non-trait call, #^ promotion omitted entirely, and escapes read raw from the token instead of from the phase-1 ConstantSymbol.

      Parameters:
      stringLitCtx - the stringLit containing the interpolation parts
      resultDetails - where the resulting String goes, plus debug info
      Returns:
      the single STRING_INTERPOLATION instruction (its setup is carried inside it)