Class ExpressionComplexityCounter
java.lang.Object
org.ek9lang.compiler.phase5.ExpressionComplexityCounter
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidEnter a coalescing expression - increment depth.(package private) voidExit a coalescing expression - calculate weighted cost and add to total.(package private) intGet the accumulated complexity for the current expression tree.(package private) booleanisAtRoot()Check if we're at the root of an expression tree (depth returned to 0).(package private) voidreset()Reset for next expression tree.
-
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
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
-