# Evaluator Class
Responsible for computing cell values based on their formulas and dependencies.
Uses topological sorting for acyclic portions and iterative calculation for cycles. Maintains integration with column search index for VLOOKUP/MATCH optimization.
# Evaluation Workflow
Topological Sort with SCC Detection
- Sorts vertices (cells/ranges) by dependency order
- Identifies Strongly Connected Components (SCCs) as cycles
Pre-cycle Evaluation
- Computes all vertices that don't depend on cycles
- These provide stable inputs for cycle resolution
Cycle Resolution (if cycles exist)
- If iterative calculation disabled: sets #CYCLE! error on all cycle members
- If enabled: iterates the cycle calculation until convergence or max iterations
Post-cycle Evaluation
- Recomputes vertices that depend on cycle results
- Uses subgraph traversal for efficiency
# Iterative Calculation Algorithm
When circular references exist and enableIterativeCalculation is true:
- Initialize all cycle cells to
iterativeCalculationInitialValue - Repeat up to
iterativeCalculationLimittimes: a. Clear caches for ranges containing cycle cells b. Store current values c. Recompute all cycle cells in address order (Gauss-Seidel style) d. Check convergence: |new - old| <iterativeCalculationThreshold - If all cells converge, stop early; otherwise continue to max iterations
# Entry Points
recomputeWholeGraph(): Full evaluation from scratch (initial load or major changes)recomputeSubgraph(): Incremental evaluation starting from changed verticesevaluateSingleFormula(): One-off formula evaluation without side effects
# Constructors
# constructor
+ new Evaluator(config: Config, stats: Statistics, interpreter: Interpreter, lazilyTransformingAstService: LazilyTransformingAstService, dependencyGraph: DependencyGraph, columnSearch: ColumnSearchStrategy): Evaluator
Defined in src/Evaluator.ts:64 (opens new window)
Parameters:
| Name | Type | Description |
|---|---|---|
config | Config | Configuration including iterative calculation settings |
stats | Statistics | Statistics collector for performance measurement |
interpreter | Interpreter | AST interpreter for formula evaluation |
lazilyTransformingAstService | LazilyTransformingAstService | Service for lazy AST transformations (address updates) |
dependencyGraph | DependencyGraph | Graph of cell/range dependencies |
columnSearch | ColumnSearchStrategy | Search index for efficient column lookups |
Returns: Evaluator
# Properties
# interpreter Readonly
• interpreter: Interpreter
Defined in src/Evaluator.ts:76 (opens new window)
AST interpreter for formula evaluation
# Methods
# evaluateSingleFormula
▸ evaluateSingleFormula(ast: Ast, address: SimpleCellAddress, dependencies: RelativeDependency[]): InterpreterValue
Defined in src/Evaluator.ts:172 (opens new window)
Evaluates a formula without persisting the result or modifying the graph.
Used for one-off calculations like conditional formatting or data validation. Temporarily creates range vertices if needed, then cleans them up.
Parameters:
| Name | Type | Description |
|---|---|---|
ast | Ast | Parsed formula AST to evaluate |
address | SimpleCellAddress | Cell address context for relative references |
dependencies | RelativeDependency[] | Relative dependencies extracted from the formula |
Returns: InterpreterValue
Computed value (number, string, boolean, error, or array)
# recomputeSubgraph
▸ recomputeSubgraph(changedVertices: Vertex[]): ContentChanges
Defined in src/Evaluator.ts:118 (opens new window)
Performs incremental evaluation starting from a set of changed vertices.
More efficient than recomputeWholeGraph() when only a subset of cells have changed.
Traverses only the subgraph reachable from the changed vertices.
Algorithm:
- Traverse subgraph from starting vertices in topological order
- Collect any cycles encountered during traversal
- Mark vertices that depend on cycles for deferred processing
- Process cycles via iterative calculation
- Cycle dependents are handled inside iterateCircularDependencies
Parameters:
| Name | Type | Description |
|---|---|---|
changedVertices | Vertex[] | Starting vertices (typically cells that were directly modified) |
Returns: ContentChanges
Content changes describing all value updates
# recomputeWholeGraph
▸ recomputeWholeGraph(): void
Defined in src/Evaluator.ts:92 (opens new window)
Performs full evaluation of all formulas in the dependency graph.
Used for initial spreadsheet load or when dependencies have changed significantly. Performs topological sort to determine evaluation order and handles any cycles.
Complexity: O(V + E) for topological sort + O(I × C) for cycles where V=vertices, E=edges, I=iterations, C=cycle size
Returns: void