A custom function body could previously only be a single expression -- no way to iterate over a range's elements to build something like PRODUCT (multiply every cell together), since none of the built-ins (SUM/AVG/MIN/MAX/COUNT) do that and a body can only compose existing functions, not define a new reduction. This adds a second body kind, 'block': a sequence of `name = expression` assignments and `FOR var IN source DO name = expression` loops, ending in `RETURN expression`. Flat scope -- every introduced name stays visible for the rest of the body, including after its own loop, holding its last iteration's value. No runtime loop in the evaluator: a loop's source size is already known once the caller's real argument is substituted in, so userFunctions.ts's new compileBlock() unrolls every FOR loop into a single nested expression (a left fold) at the same parse-time inlining point Phase 1 already used -- the result is exactly the same shape of static AST as any other inlined function call, so evaluator.ts and graph.ts need no changes, dependency edges on every unrolled cell are picked up automatically, and a bounded MAX_UNROLL_STEPS (10,000) keeps it crash/hang-free. parser.ts's parseFunctionBlock() parses the new grammar (a purely additive EQUALS lexer token; FOR/IN/DO/RETURN are plain case-matched IDENTs, not new reserved tokens) into a BlockNode kept outside the existing Node union, so no existing switch needs a new case. Store: CustomFunction/FunctionDef gain `kind: 'expression' | 'block'` (defaulted for every function saved before this existed, including via JSON import). validateCustomFunction validates the new grammar's local/ loop-variable names the same way as parameters (not cell-shaped, no reserved words, no shadowing a parameter -- but reusing the same local across statements, the accumulator pattern, is correctly not a collision) and that every FOR source is structurally a parameter, cell, or range. UI: a "𝑓 Funktionen" form toggle between "Einfache Formel" and "Mit Schleife", with its own placeholder/hint (a PRODUCT example) and a small badge on block-kind rows in the list. 237 tests passing (34 new), vue-tsc/eslint/build clean. Verified live in the running app: defined PRODUCT via the loop UI, called =PRODUCT(Umsatz!B2:E6), confirmed the correct value and recalculation on a change to a cell inside the range, and an inline rejection for an invalid FOR source at save time. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
56 lines
1.1 KiB
TypeScript
56 lines
1.1 KiB
TypeScript
/**
|
|
* Public surface of the formula engine.
|
|
*
|
|
* Nothing in `./` imports Vue, the DOM or any spreadsheet library. See
|
|
* ./README.md for the grammar (EBNF), the evaluation model and the error model.
|
|
*/
|
|
|
|
export { tokenize, LexError, type Token, type TokenType } from './lexer';
|
|
export {
|
|
parseFormula,
|
|
parseFunctionBlock,
|
|
collectRefs,
|
|
ParseError,
|
|
type Node,
|
|
type CellNode,
|
|
type RangeNode,
|
|
type BlockNode,
|
|
} from './parser';
|
|
export { evaluate } from './evaluator';
|
|
export { DependencyGraph } from './graph';
|
|
export { FUNCTIONS, getFunction, hasFunction } from './functions';
|
|
export {
|
|
colToNumber,
|
|
numberToCol,
|
|
parseA1,
|
|
toA1,
|
|
looksLikeCell,
|
|
expandRange,
|
|
cycleReferenceLocks,
|
|
} from './refs';
|
|
export {
|
|
Workbook,
|
|
literalValue,
|
|
type TableModel,
|
|
} from './workbook';
|
|
export {
|
|
buildFunctionIndex,
|
|
expandUserFunctions,
|
|
collectCalledFunctionNames,
|
|
collectCalledFunctionNamesInBlock,
|
|
declaredBlockNames,
|
|
findInvalidForSource,
|
|
type FunctionDef,
|
|
} from './userFunctions';
|
|
export {
|
|
FormulaError,
|
|
isError,
|
|
keyOf,
|
|
parseKey,
|
|
ERROR_CODES,
|
|
type ErrorCode,
|
|
type CellValue,
|
|
type Scalar,
|
|
type EngineHost,
|
|
} from './types';
|