Log
03b5e6481c37
feat(core): typed formal parameters in function literals (rule 56, ADR-35)
public
feat(core): typed formal parameters in function literals (rule 56, ADR-35)
Extend the grammar to accept FEEL grammar rule 56 typed formal parameters
(`function (a: number) body`). A `FormalParam` rule replaces the bare
`Comma<Name>` in the two `FnDef` productions — it parses `name` or
`name: Type`, discarding the annotation via the existing `InstanceType`
grammar. `Expr::FnDef` and the evaluator are unchanged; the type is only
enforced at the grammar level (this slice binds by name only).
Add unit tests: single and multiple typed params, mixed typed/bare,
generic function type annotations, closures with typed params, three-level
currying with typed params.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
modified
crates/feel-core/src/feel.lalrpop
+17 −4
@@ -97,13 +97,15 @@
97 97 Expr::Quantified { kind: Quantifier::Some, bindings: bs, predicate: Box::new(p) }, 98 98 "every" <bs:Comma<IterCtx>> "satisfies" <p:TextualExpr> => 99 99 Expr::Quantified { kind: Quantifier::Every, bindings: bs, predicate: Box::new(p) }, 100 - // A FEEL function literal `function(a, b) body` (ADR-23). Like `if`/`for`, the 101 - // body is a full TextualExpr, so it extends to the end of the sub-expression. 100 + // A FEEL function literal `function(a, b) body` (ADR-23/ADR-35). Like `if`/`for`, 101 + // the body is a full TextualExpr, so it extends to the end of the sub-expression. 102 102 // `function` is a keyword token, so `function(` is unambiguously a literal (not 103 - // a call). Params are bare names (no type annotations in this slice). 103 + // a call). Each formal parameter is a bare name or an optionally typed name 104 + // `name: Type` (FEEL grammar rule 56, ADR-35); the type annotation is parsed and 105 + // discarded — this slice binds by name only, and arity is `params.len()`. 104 106 "function" "(" ")" <b:TextualExpr> => 105 107 Expr::FnDef { params: vec![], body: Box::new(b) }, 106 - "function" "(" <ps:Comma<Name>> ")" <b:TextualExpr> => 108 + "function" "(" <ps:Comma<FormalParam>> ")" <b:TextualExpr> => 107 109 Expr::FnDef { params: ps, body: Box::new(b) }, 108 110 Disjunction, 109 111 };
@@ -224,6 +226,17 @@
224 226 Arg: ArgEntry = { 225 227 <n:Name> ":" <v:Expr> => ArgEntry::Named(n, v), 226 228 <e:Expr> => ArgEntry::Pos(e), 229 +}; 230 + 231 +// One formal parameter in a function literal (FEEL grammar rule 56, ADR-35). 232 +// Either a bare name (`a`) or a typed name (`a: number`, `fn1: function<number>->number`). 233 +// The type annotation is parsed via the existing `InstanceType` grammar and discarded 234 +// — this slice binds arguments by name only; arity is already `params.len()`. 235 +// LR(1)-clean: after `Name`, a lookahead `:` shifts into the typed branch while 236 +// `,`/`)` reduce to the bare branch (mirrors the `Arg` rule's one-token decision). 237 +FormalParam: String = { 238 + <n:Name> => n, 239 + <n:Name> ":" InstanceType => n, 227 240 }; 228 241 229 242 Primary: Expr = {
modified
crates/feel-core/src/lib.rs
+46 −0
@@ -2342,6 +2342,52 @@
2342 2342 ); 2343 2343 } 2344 2344 2345 + // --- typed formal parameters (FEEL rule 56, ADR-35) ---------------------- // 2346 + 2347 + #[ test ] 2348 + fn typed_formal_params_parse_and_eval () { 2349 + // A typed param `a: number` is semantically identical to bare `a` — the type 2350 + // annotation is parsed and discarded in this slice (ADR-35). 2351 + assert_eq! ( 2352 + ev(" {f: function (a: number) 1 + a, r: f(2)}.r " ), 2353 + Value::Number(3.0 ) 2354 + ); 2355 + // Multiple typed params. 2356 + assert_eq! ( 2357 + ev(" {f: function (a: number, b: number) a * b, r: f(3, 4)}.r " ), 2358 + Value::Number(12.0 ) 2359 + ); 2360 + // Mixed typed and bare params — the parser accepts both in the same list. 2361 + assert_eq! ( 2362 + ev(" {f: function (a: number, b) a + b, r: f(10, 5)}.r " ), 2363 + Value::Number(15.0 ) 2364 + ); 2365 + // A typed param with a generic type `function<number> -> number` (rule 56). 2366 + assert_eq! ( 2367 + ev(" {bkm: function (fn1: function<number> -> number) fn1(3), r: bkm(function (x: number) x + 1)}.r " ), 2368 + Value::Number(4.0 ) 2369 + ); 2370 + } 2371 + 2372 + #[ test ] 2373 + fn typed_params_closure_and_currying () { 2374 + // Typed params work with closures (closed-over var `base`). 2375 + assert_eq! ( 2376 + ev(" {base: 10, f: function (a: number) a * base, r: f(5)}.r " ), 2377 + Value::Number(50.0 ) 2378 + ); 2379 + // Curried closures: outer typed param `a` captured in inner lambda. 2380 + assert_eq! ( 2381 + ev(" {outer: function (a: number) function (b: number) a * b, step1: outer(3), r: step1(4)}.r " ), 2382 + Value::Number(12.0 ) 2383 + ); 2384 + // Three-level currying. 2385 + assert_eq! ( 2386 + ev(" {f: function (a: number) function (b: number) function (c: number) a + b + c, s1: f(1), s2: s1(2), r: s2(3)}.r " ), 2387 + Value::Number(6.0 ) 2388 + ); 2389 + } 2390 + 2345 2391 // --- host callables (ForeignFunction) ------------------------------------ // 2346 2392 2347 2393 /// A host function that sums all of its numeric arguments (positional + named),