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).