Class ExpressionComplexityCounter

java.lang.Object
org.ek9lang.compiler.phase5.ExpressionComplexityCounter

class ExpressionComplexityCounter extends Object
Tracks complexity of coalescing expression trees with depth-based multiplier. Deeper nesting incurs exponentially higher complexity cost, encouraging developers to extract intermediate variables for readability.

Base costs are kept low (1-2) so flat usage doesn't inflate method complexity. The depth multiplier (3^(depth-1)) ensures nested expressions are penalized.

This counter is used during Phase 5 (PRE_IR_CHECKS) to track expression complexity as the AST is traversed. When a coalescing expression tree completes (depth returns to 0), the accumulated complexity is checked against the threshold.

  • Constructor Details

    • ExpressionComplexityCounter

      ExpressionComplexityCounter()
  • Method Details

    • enterCoalescing

      void enterCoalescing()
      Enter a coalescing expression - increment depth. Called when entering any coalescing operator in the expression tree.
    • exitCoalescing

      void exitCoalescing(EK9Parser.ExpressionContext ctx)
      Exit a coalescing expression - calculate weighted cost and add to total. The depth multiplier is 3^(depth-1), so deeper nesting costs exponentially more.
      Parameters:
      ctx - the expression context to determine operator type
    • getAccumulatedComplexity

      int getAccumulatedComplexity()
      Get the accumulated complexity for the current expression tree.
      Returns:
      total weighted complexity since last reset
    • reset

      void reset()
      Reset for next expression tree. Called after checking complexity at the root of an expression tree.
    • isAtRoot

      boolean isAtRoot()
      Check if we're at the root of an expression tree (depth returned to 0).
      Returns:
      true if no coalescing expressions are currently being processed