T0008 — Compiler and evaluator core
Part of the roadmap. Phase 1 · wave 3 · size L.
- Depends on: T0002, T0003, T0004, T0005, T0006
- Parallel with: T0007, T0009, T0023
- Background: ADR-0003, ADR-0004
Goal
Compile an AST into closures and evaluate them over plain JSON (compiling to closures, ADR-0003, ADR-0004).
Scope
src/compiler/:compileNode()for every AST kind- literal construction through the T0004/T0005/T0006 value modules. Invalid literal values (out-of-range dates, a timezone on a Time literal, a Decimal literal beyond the Decimal size bound, an Integer literal outside the 32-bit range) compile to a closure that raises
eval/invalid-literalwhen evaluated, not a compile-time error (invalid literal values; teststestLiteralTimeUTCandtestLiteralTimeTimezoneOffset) - constant folding of
-<number>and-<quantity>, so-2147483648is a valid Integer; folding gives the same result as evaluation, so further polarity operators overflow to empty as at runtime - lambda compilation for higher-order parameters
- a depth limit on the expression tree (
nesting.ts), checked without recursion before compiling, so a tree built by hand cannot overflow the stack of the recursive compiler and its closures. The parser keeps the trees it builds, chains included, within the same limit (#107). Too deep raisessyntax/nesting-limit.
src/runtime/:EvalContextandFrame($this,$index,$total)MaybePromisehelpers inmaybe-promise.ts:andThen,mapSequential, and the sync guard (adoptResult,requireSync), built on a structuralisThenable()(a callablethenproperty, neverinstanceof Promise; T0021: sync and async). They live inruntime/, notcompiler/, because library functions use them andfunctionsimportsruntimeandvaluesbut nevercompiler(module rules). The helper that chains is not calledthen: a module that exportsthenis a thenable itself, andimport()of it calls that function instead of returning the module. Arguments and lambda items are evaluated one after another (mapSequential), never overlapping, so no promise is left unobserved when one fails; concurrent hook calls are T0021.- model-less JSON navigation: array flattening, own-property checks,
ModelNodecreation for every JSON value, objects and primitives alike, so later operations keep its provenance - the root-identifier rule against
resourceType - variables:
%context,%ucum, user variables, and theeval/undefined-variableerror - type targets (
type-target.ts): compile-time resolution, the model-less resource-name matcher, andis/ason collections, for T0012 to reuse inofType() - the shared node conversion
toSystemValue()(node-value.ts), merged ahead of this task in #98 because T0009 needs it too
- In the compiler, the operators that do not need equality: the indexer
[], unary+/-,and/or/xor/implies(three-valued logic, both operands evaluated), andis/ason System types. The other operators compile to a closure that raises the temporaryeval/not-implementedwhen evaluated, until T0010 implements them; their operands are still compiled, so unknown functions and wrong arities in them fail at compile time. src/functions/registry.tsanddefine.ts:FunctionDefinition,ParamSpec,createRegistry()- compile-time lookup with
semantic/unknown-functionandsemantic/wrong-arity; a type argument that is not a type name issemantic/invalid-argument - helpers for value, lambda and host functions
- test-only functions to exercise the machinery (
packages/core/test/support/test-functions.ts) - the function forms
is(type)andas(type)(types.ts), andnot()(logic.ts) from N1 §6.5: it belongs with the Boolean operators, and no other task owns it
src/api/: internalcompile(),evaluate()andevaluateAsync(), plus basicplain/typedoutput adapters (finished in T0025).- The conformance runner's
EvaluatorAdapteris wired to this internal API, replacing the runner's stub intools/conformance/src/engine-adapter.ts(T0022: adapter interface, runner contract). It reports spec functions, FHIR variables and operators that later tasks add as "not implemented", so a test that expects an error never passes because a feature is missing (spec notes). T0025 later switches that entry point to the public API (#48).
Out of scope
Equality-based operators (T0010), model-aware navigation (T0018), real library functions (T0011–T0017).
Design notes
Compiling to closures
Decision (ADR-0003): compile() turns the AST into a tree of JavaScript closures once. Evaluating means calling the root closure.
type Collection = readonly Item[];type Evaluator = (ctx: EvalContext, focus: Collection, frame: Frame) => MaybePromise<Collection>;function compileNode(node: ExpressionNode, cc: CompileContext): Evaluator {switch (node.kind) {case 'Member': {const target = compileNode(node.target, cc);const name = node.name; // resolved oncereturn (ctx, focus, frame) => andThen(target(ctx, focus, frame), (input) => navigate(ctx, input, name));}case 'FunctionCall': {const fn = cc.registry.resolve(node.name, node.args.length, node.span); // lookup and arity check once// ...}// ...}}
- Done once, at compile time: function lookup and arity check; choosing the operator implementation; building literal values (
Decimal, dates, quantities); resolving type specifiers against the model; folding side-effect-free constant subtrees (for example-1or'a' & 'b'). EvalContextis created per evaluation. It holds the input (%context),%resource,%rootResource, variables, model, unit system, hooks, a snapshot ofnow, the timezone policy, the output mode, the evaluation limits, and a cancellation signal (T0021: sync and async).Frameholds the iteration variables$this,$indexand$total. Frames are passed as parameters and chained for nested lambdas, never stored globally. That keeps a compiled expression reentrant and safe for concurrent async evaluations.- Higher-order functions (
where,select,all,exists(criteria),repeat,iif, and lateraggregate,sort,defineVariable) receive their lambda arguments as compiled closures, not as evaluated values. The function definition declares which parameters are lambdas. - Errors carry the span of the node that failed. Spans are attached at operator and function-call boundaries, not on every node, to keep the hot path cheap.
- Generating JavaScript source (
new Function) was rejected: it breaks under a Content-Security-Policy withoutunsafe-evaland is harder to debug.
Invalid literal values
Invalid literal values are well-formed tokens whose value is not allowed: an out-of-range date component (@2015-02-30), a timezone on a Time literal (@T14:34:28Z), a Decimal literal beyond the Decimal size bound, or an Integer literal outside the 32-bit range (spec notes). The size bound is more than 10,000 significant digits, or more than 10,000 digits after the point; Decimal.parse() returns undefined for such a literal (T0004). These do not fail at compile time. The compiler emits a closure that raises FhirPathEvaluationError (eval/invalid-literal) when it is evaluated. The suite classifies these as execution errors (testLiteralTimeUTC and testLiteralTimeTimezoneOffset are invalid="execution"), and the error-class checks in T0022: conformance runner depend on that. The suite has no test for an oversized Decimal literal; the owner chose the same treatment for it (#69).
Type names that do not resolve
T0008 owns target resolution and error timing for is/as (operator and function forms); T0012 reuses the contract for ofType(), and T0018 supplies model-aware item types. See spec notes: unresolved type specifiers (#58).
- Resolve each type specifier once at compile time with T0003's
resolveTypeSpecifier(). Keep its pure API unchanged: it returns a type orundefined, never inspects input and never throws for an unknown type. Qualified names stay in their namespace; knowing a namespace does not make a nonexistent type valid. - Compile the outcome as a resolved target, a model-less resource-name target, or an unresolved target. An unresolved target produces a closure that raises
FhirPathEvaluationErrorwitheval/unknown-typeand the use-site span when that operator/function is evaluated. Add the evaluation error code in T0008. Neither ordinary compilation nor constant folding raises it early; an unevaluated lazy branch does not raise it. Validate the target before matching items, including when the input is empty. - With a model supplied, a name absent from both that model and System is an error.
System.Patient,FHIR.Booleanwith a FHIR model,Foo.Patient, and unqualifiedstring1are errors, not false/empty. Without a model, qualifiedFHIR.Patientis also an error; only the unqualified resource-name extension below applies. - A resolved target whose type does not match is an ordinary mismatch:
isreturns false,asreturns empty, andofType()discards the item. Existing subtype rules remain;ofType()additionally accepts the mapped System type of FHIR primitives (existing type rules). - Static analysis may warn with
semantic/unknown-type, but does not turn this execution failure into a compile failure, including in strict mode (T0024). The pinned phase-1testFHIRPathAsFunction23and24require execution errors. The conflicting positive assertion intestType22is handled only by a named suite exception when model-aware execution is wired in T0018, as spec notes explains; it grants no semantic exemption toSystem.Patientor any other unknown name.
Resource type names without a model
Model-less JSON evaluation is a Callis extension, separate from resolving System/model types (#59, spec notes). T0008 implements its item matcher for is/as, and T0012 reuses it for ofType().
- Without a model, an unqualified name first resolves against System. If it does not resolve, compile it as an exact resource-name target. No FHIR type registry, capitalization heuristic or scan of the input's resources is required;
resolveTypeSpecifier({ name: 'Patient' })still returnsundefined. - A resource-name target matches only an input object whose own
resourceTypeis a string exactly equal to that name, case-sensitively. Read it withObject.hasOwn; inherited properties, missing/non-string values, arrays, null and primitives do not match. A string value'Patient'is not a Patient resource. - Every such unqualified name is a valid resource-name target in this mode, even a misspelling or a name not present in the input. A nonmatch is false for singleton
isand empty foras/ofType(), not an unknown-type error. This intentional loss of typo detection is confined to model-less unqualified targets. Qualified unknown names always error. - There is no inferred inheritance: a Patient does not match
ResourceorDomainResource; those names match only literal equalresourceTypestrings. System names take precedence (StringmeansSystem.String, even on an object withresourceType: 'String'). Ordinary singleton/cardinality rules still apply; for a valid target, emptyisis false under 3.0.0, emptyas/ofType()is empty, and multi-itemis/asraiseseval/singleton-expected. - With a model supplied, never fall back to raw
resourceType, even for an untyped node: the provider and T0018 determine model item types. Do not equate a resource-name target with aModelTypeInfoor change the T0003 System/model subtype helpers. Reuse the own-property exact-name rule for the model-less root identifier so navigation and type tests agree (T0018 navigation).
Function library
interface FunctionDefinition {readonly name: string;readonly params: readonly ParamSpec[]; // { kind: 'value' | 'lambda' | 'type', optional?, variadic? }readonly returns?: TypeHint; // for the analyzerreadonly async?: boolean; // true if it may call host hooks// Any thenable: adopted in async mode, `eval/async-in-sync` in sync mode.readonly impl: (call: CallContext, input: Collection, args: readonly Arg[]) => Collection | PromiseLike<Collection>;}
- The registry maps names to definitions. Names are resolved at compile time; an unknown name is a semantic error.
createRegistry({ standard: true, fhir: true })is the default.registry.extend([...])adds user-defined functions. Library code builds its maps withObject.create(null).- The standard library is split into files that mirror the spec sections:
existence,filtering,subsetting,combining,conversion,string,math,tree,utility, plustypes(is(),as()) andlogic(not()) for the function forms in the operations sections. The FHIR functions live insrc/fhir/. - Operators are not in the registry, because the grammar fixes them. They share kernels (
equality,comparison,arithmetic) with the functions. - Each function file has a test file next to it containing the examples from its spec section.
Acceptance criteria
- End to end:
compile(expr).evaluate(json)works for navigation, literals of every type, the indexer,$this,$index, variables, Boolean logic, andis/aswith System types. - Each lambda helper is tested in sync mode, and in async mode using a test-only async function. A thenable in sync mode raises
eval/async-in-sync, including a cross-realm promise and a custom thenable. - Navigating to
__proto__,constructorortoStringreturns empty. - Unknown qualified targets (
System.Patient,Foo.Patient, and model-lessFHIR.Patient) compile, then raiseeval/unknown-typein bothisandasforms, even on empty input; valid System mismatches remain false/empty. A lazy branch that is not evaluated does not raise the error. - Without a model, on
{"contained": [{"resourceType": "Patient"}, {"resourceType": "Observation"}]},contained[0] is Patientis true,contained[1] is Patientis false, andcontained[1] as Observationreturns that item. Cover'Patient' is Patient, a misspelled target, System-name precedence, absent/non-string/inheritedresourceType, lack of inheritance, and valid-target empty/multiple input rules. - A compiled expression can be evaluated concurrently (two overlapping async evaluations) without interference.
- Once fixtures exist (T0023), the individual suite tests that use only T0008 features (navigation, literals, the indexer,
$this, variables, Boolean logic,is/ason System types) pass, for examplePatient.name[-1]and the literal parts oftestLiterals; the PR lists them. No whole group is expected to pass with T0008 alone, because the groups also use operators and functions:testIndexerneeds=and|(completed by T0010)testCollectionBooleanneedsiif()(completed by T0014)testDollarneedswhere,skip,substring,length,childrenand a semantic check (completed by T0013, with T0012, T0015, T0017 and T0024)