Skip to main content

eui_core/
cel.rs

1//! VAPP-191 (eApps Q2): expressions, the Rust twin of
2//! `packages/exponential-ui/src/cel.ts` (line by line; read its header for
3//! the language). A SUBSET of CEL with ONE number type (a double, like
4//! JSON), hand-written so both twins replay `fixtures/expressions.json`
5//! exactly: numbers, dates and error texts. No loops, no user functions, a
6//! length cap, no nested comprehensions and a step limit, so it always
7//! terminates. The functions are ONE table shared with the A2UI function
8//! form (`{"call": "mul", …}`, [`call_expression_function`]); dates run in
9//! the team zone (an offset per instant, [`ExpressionEnv::offset`]).
10
11use crate::dates::{day_from_number, day_number, days_in_month, format_day, iso_week, parse_day, weekday_of};
12use crate::json;
13use serde_json::{Map, Value};
14
15/// The cost caps (spec §4.2): characters (code points), nesting, steps.
16pub const MAX_LENGTH: usize = 1000;
17pub const MAX_DEPTH: usize = 32;
18pub const MAX_STEPS: usize = 10_000;
19
20/// Every unit `dateAdd` / `dateDiff` take; `startOf` / `endOf` take the calendar ones.
21pub const DATE_UNITS: &[&str] = &["second", "minute", "hour", "day", "week", "month", "quarter", "year"];
22pub const CALENDAR_UNITS: &[&str] = &["day", "week", "month", "quarter", "year"];
23/// The formats `formatDuration` writes.
24pub const DURATION_FORMATS: &[&str] = &["h:mm", "hh:mm"];
25
26/// VAPP-194 (E1): what calling a function does. Expressions are PURE: a
27/// binding, `visible`, a computed field or a query param never writes. A
28/// `write` changes records or the surface data, a `host` function reaches
29/// outside the surface (a toast, an MCP call, a link, another action); both
30/// run only as steps of a package action ([`crate::host::actions`]).
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum FunctionEffect {
33    Pure,
34    Write,
35    Host,
36}
37
38impl FunctionEffect {
39    /// `pure` | `write` | `host`.
40    pub fn as_str(self) -> &'static str {
41        match self {
42            FunctionEffect::Pure => "pure",
43            FunctionEffect::Write => "write",
44            FunctionEffect::Host => "host",
45        }
46    }
47}
48
49/// The functions that write (records, the surface data model).
50pub const WRITE_FUNCTIONS: &[&str] = &["set", "records.upsert", "records.delete", "records.batch"];
51/// The built-in host functions; every name under a host prefix is one too.
52pub const HOST_FUNCTIONS: &[&str] = &["openUrl", "bind"];
53pub const HOST_FUNCTION_PREFIXES: &[&str] = &["actions.", "harness.", "exponential.", "tools.", "upstream."];
54
55/// The effect of a function by name: the signature table's (every
56/// expression function, every bind-time function) is `pure`.
57pub fn function_effect(name: &str) -> FunctionEffect {
58    if WRITE_FUNCTIONS.contains(&name) {
59        return FunctionEffect::Write;
60    }
61    if HOST_FUNCTIONS.contains(&name) || HOST_FUNCTION_PREFIXES.iter().any(|p| name.starts_with(p)) {
62        return FunctionEffect::Host;
63    }
64    FunctionEffect::Pure
65}
66
67/// Why an effectful function is refused in a pure place.
68pub fn effect_words(name: &str, effect: FunctionEffect) -> String {
69    format!("{name} {}", if effect == FunctionEffect::Write { "writes" } else { "is a host function" })
70}
71
72/// The clock, the team zone and the step limit an evaluation runs with.
73#[derive(Clone, Copy, Default)]
74pub struct ExpressionEnv<'a> {
75    /// Epoch ms `today()` / `now()` read; `None` = the wall clock.
76    pub now: Option<f64>,
77    /// Minutes east of UTC at an instant; `None` = UTC.
78    pub offset: Option<&'a (dyn Fn(f64) -> i32 + Send + Sync)>,
79    /// The step limit (default [`MAX_STEPS`]).
80    pub max_steps: Option<usize>,
81}
82
83impl std::fmt::Debug for ExpressionEnv<'_> {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        f.debug_struct("ExpressionEnv").field("now", &self.now).field("max_steps", &self.max_steps).finish_non_exhaustive()
86    }
87}
88
89struct Env<'a> {
90    now: f64,
91    offset: Option<&'a (dyn Fn(f64) -> i32 + Send + Sync)>,
92}
93
94impl<'a> Env<'a> {
95    fn of(env: &ExpressionEnv<'a>) -> Env<'a> {
96        Env { now: env.now.unwrap_or_else(crate::format::now_ms).floor(), offset: env.offset }
97    }
98    fn off(&self, ms: f64) -> f64 {
99        self.offset.map_or(0.0, |o| f64::from(o(ms)))
100    }
101}
102
103type R<T> = Result<T, String>;
104
105// ---------------------------------------------------------------------------
106// Values
107// ---------------------------------------------------------------------------
108
109/// The runtime type name errors print.
110pub fn type_name(v: &Value) -> &'static str {
111    match v {
112        Value::Null => "null",
113        Value::Number(_) => "number",
114        Value::String(_) => "string",
115        Value::Bool(_) => "bool",
116        Value::Array(_) => "list",
117        Value::Object(_) => "map",
118    }
119}
120
121/// A finite result with `-0` as 0; anything else is out of range.
122fn num(v: f64) -> R<f64> {
123    if !v.is_finite() {
124        return Err("number out of range".into());
125    }
126    Ok(if v == 0.0 { 0.0 } else { v })
127}
128
129fn num_text(n: f64) -> String {
130    json::number_to_string(n)
131}
132
133fn deep_equal(a: &Value, b: &Value) -> bool {
134    match (a, b) {
135        (Value::Number(x), Value::Number(y)) => x.as_f64() == y.as_f64(),
136        (Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(a, b)| deep_equal(a, b)),
137        (Value::Object(x), Value::Object(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| deep_equal(v, w))),
138        _ => a == b,
139    }
140}
141
142/// Round half away from zero without `x + 0.5`.
143fn round_half_away(x: f64) -> f64 {
144    let a = x.abs();
145    let f = a.floor();
146    let r = if a - f >= 0.5 { f + 1.0 } else { f };
147    if x < 0.0 {
148        -r
149    } else {
150        r
151    }
152}
153
154fn utf16_cmp(a: &str, b: &str) -> std::cmp::Ordering {
155    a.encode_utf16().cmp(b.encode_utf16())
156}
157
158// ---------------------------------------------------------------------------
159// Dates (the team zone)
160// ---------------------------------------------------------------------------
161
162const DAY_MS: f64 = 86_400_000.0;
163
164fn unit_ms(unit: &str) -> Option<f64> {
165    match unit {
166        "second" => Some(1000.0),
167        "minute" => Some(60_000.0),
168        "hour" => Some(3_600_000.0),
169        _ => None,
170    }
171}
172
173#[derive(Clone, Copy)]
174enum Temporal {
175    Date(i64),
176    Ts(f64),
177}
178
179/// An ISO instant (`YYYY-MM-DDTHH:MM[:SS[.fff]][Z|±HH:MM]`, no offset = UTC)
180/// as epoch ms; `None` when it is not one.
181pub fn parse_timestamp(text: &str) -> Option<f64> {
182    let b = text.as_bytes();
183    let digit = |i: usize| b.get(i).is_some_and(u8::is_ascii_digit);
184    let int = |from: usize, n: usize| b[from..from + n].iter().fold(0i64, |acc, c| acc * 10 + i64::from(c - b'0'));
185    if b.len() < 16 || b[10] != b'T' || !(digit(11) && digit(12) && b[13] == b':' && digit(14) && digit(15)) {
186        return None;
187    }
188    let day = parse_day(&text[..10]).filter(|_| text.is_char_boundary(10))?;
189    if !(b[..10].iter().enumerate().all(|(i, c)| if i == 4 || i == 7 { *c == b'-' } else { c.is_ascii_digit() })) {
190        return None;
191    }
192    let (h, mi) = (int(11, 2), int(14, 2));
193    let mut i = 16;
194    let mut s = 0;
195    let mut ms = 0;
196    if b.get(i) == Some(&b':') {
197        if !(digit(i + 1) && digit(i + 2)) {
198            return None;
199        }
200        s = int(i + 1, 2);
201        i += 3;
202        if b.get(i) == Some(&b'.') {
203            let start = i + 1;
204            let mut end = start;
205            while digit(end) {
206                end += 1;
207            }
208            if end == start {
209                return None;
210            }
211            let frac: String = text[start..end.min(start + 3)].chars().collect();
212            ms = format!("{frac:0<3}").parse::<i64>().ok()?;
213            i = end;
214        }
215    }
216    let mut offset = 0;
217    match b.get(i) {
218        None => {}
219        Some(b'Z') if i + 1 == b.len() => {}
220        Some(c @ (b'+' | b'-')) if b.len() == i + 6 && digit(i + 1) && digit(i + 2) && b[i + 3] == b':' && digit(i + 4) && digit(i + 5) => {
221            let (oh, om) = (int(i + 1, 2), int(i + 4, 2));
222            if oh > 23 || om > 59 {
223                return None;
224            }
225            offset = (if *c == b'-' { -1 } else { 1 }) * (oh * 60 + om);
226        }
227        _ => return None,
228    }
229    if h > 23 || mi > 59 || s > 59 {
230        return None;
231    }
232    Some((day_number(day.y, day.m, day.d) as f64) * DAY_MS + (h * 3_600_000 + mi * 60_000 + s * 1000 + ms - offset * 60_000) as f64)
233}
234
235/// An instant as `YYYY-MM-DDTHH:MM:SS.sssZ` (JS `toISOString`), years 0..9999.
236pub fn format_timestamp(ms: f64) -> R<String> {
237    if !(ms >= MIN_DAY as f64 * DAY_MS && ms < (MAX_DAY + 1) as f64 * DAY_MS) {
238        return Err("date out of range".into());
239    }
240    let day = (ms / DAY_MS).floor();
241    let tod = (ms - day * DAY_MS) as i64;
242    let d = day_from_number(day as i64);
243    if d.y < 0 || d.y > 9999 {
244        return Err("date out of range".into());
245    }
246    Ok(format!("{}T{:02}:{:02}:{:02}.{:03}Z", format_day(d), tod / 3_600_000, (tod / 60_000) % 60, (tod / 1000) % 60, tod % 1000))
247}
248
249/// The days a date may be: 0000-01-01 ..= 9999-12-31 (checked BEFORE the
250/// calendar maths, so a huge day never overflows it).
251const MIN_DAY: i64 = -719_528;
252const MAX_DAY: i64 = 2_932_896;
253/// The most calendar steps (days) any `dateAdd` can take and stay in range.
254const MAX_CALENDAR_STEPS: f64 = 4_000_000.0;
255
256fn day_text(day: i64) -> R<String> {
257    if !(MIN_DAY..=MAX_DAY).contains(&day) {
258        return Err("date out of range".into());
259    }
260    let d = day_from_number(day);
261    if d.y < 0 || d.y > 9999 {
262        return Err("date out of range".into());
263    }
264    Ok(format_day(d))
265}
266
267/// The wall clock (as if UTC) of an instant in the zone.
268fn to_local(env: &Env, ms: f64) -> f64 {
269    ms + env.off(ms) * 60_000.0
270}
271
272/// The instant of a wall-clock time in the zone: a time in a DST gap moves
273/// forward by the gap; a repeated time takes its later (standard) instant.
274fn from_local(env: &Env, wall: f64) -> f64 {
275    let first = env.off(wall);
276    let utc = wall - first * 60_000.0;
277    let second = env.off(utc);
278    if second == first {
279        utc
280    } else {
281        wall - second * 60_000.0
282    }
283}
284
285fn temporal(f: &str, v: &Value) -> R<Temporal> {
286    let Value::String(s) = v else { return Err(format!("{f} needs a date, got {}", type_name(v))) };
287    if s.chars().count() == 10 {
288        if let Some(d) = parse_day(s) {
289            return Ok(Temporal::Date(day_number(d.y, d.m, d.d)));
290        }
291    }
292    parse_timestamp(s).map(Temporal::Ts).ok_or_else(|| format!("not a date: {s}"))
293}
294
295fn floor_day(wall: f64) -> i64 {
296    (wall / DAY_MS).floor() as i64
297}
298
299fn wall_of(env: &Env, t: Temporal) -> f64 {
300    match t {
301        Temporal::Date(day) => day as f64 * DAY_MS,
302        Temporal::Ts(ms) => to_local(env, ms),
303    }
304}
305
306fn unit_of<'a>(f: &str, v: &'a Value, units: &[&str]) -> R<&'a str> {
307    let Value::String(s) = v else { return Err(format!("{f} needs a string, got {}", type_name(v))) };
308    if !units.contains(&s.as_str()) {
309        return Err(format!("unknown unit {s}"));
310    }
311    Ok(s)
312}
313
314fn add_months(day: i64, months: i64) -> i64 {
315    let c = day_from_number(day);
316    let total = c.y * 12 + (c.m - 1) + months;
317    let y = total.div_euclid(12);
318    let m = total - y * 12 + 1;
319    day_number(y, m, c.d.min(days_in_month(y, m)))
320}
321
322fn add_calendar(day: i64, n: i64, unit: &str) -> i64 {
323    match unit {
324        "day" => day + n,
325        "week" => day + 7 * n,
326        "month" => add_months(day, n),
327        "quarter" => add_months(day, n * 3),
328        _ => add_months(day, n * 12),
329    }
330}
331
332fn start_day(day: i64, unit: &str) -> i64 {
333    let c = day_from_number(day);
334    match unit {
335        "day" => day,
336        "week" => {
337            let wd = match weekday_of(c.y, c.m, c.d) {
338                0 => 7,
339                n => n as i64,
340            };
341            day - (wd - 1)
342        }
343        "month" => day_number(c.y, c.m, 1),
344        "quarter" => day_number(c.y, (c.m - 1) / 3 * 3 + 1, 1),
345        _ => day_number(c.y, 1, 1),
346    }
347}
348
349fn number(f: &str, v: &Value) -> R<f64> {
350    match v {
351        Value::Number(n) => Ok(n.as_f64().unwrap_or(0.0)),
352        _ => Err(format!("{f} needs a number, got {}", type_name(v))),
353    }
354}
355
356fn whole_number(f: &str, v: &Value) -> R<f64> {
357    let n = number(f, v)?;
358    if n.fract() != 0.0 {
359        return Err(format!("{f} needs a whole number, got {}", num_text(n)));
360    }
361    Ok(n)
362}
363
364fn text<'a>(f: &str, v: &'a Value) -> R<&'a str> {
365    match v {
366        Value::String(s) => Ok(s),
367        _ => Err(format!("{f} needs a string, got {}", type_name(v))),
368    }
369}
370
371fn date_add(args: &[Value], env: &Env) -> R<Value> {
372    let t = temporal("dateAdd", &args[0])?;
373    let n = whole_number("dateAdd", &args[1])?;
374    let unit = unit_of("dateAdd", &args[2], DATE_UNITS)?;
375    let out = match t {
376        Temporal::Date(day) => {
377            if unit_ms(unit).is_some() {
378                return Err(format!("unit {unit} needs a timestamp"));
379            }
380            if !(n.abs() <= MAX_CALENDAR_STEPS) {
381                return Err("date out of range".into());
382            }
383            day_text(add_calendar(day, n as i64, unit))?
384        }
385        Temporal::Ts(ms) => match unit_ms(unit) {
386            Some(u) => format_timestamp(num(ms + n * u)?)?,
387            None => {
388                if !(n.abs() <= MAX_CALENDAR_STEPS) {
389                    return Err("date out of range".into());
390                }
391                let wall = to_local(env, ms);
392                let day = floor_day(wall);
393                let tod = wall - day as f64 * DAY_MS;
394                format_timestamp(from_local(env, add_calendar(day, n as i64, unit) as f64 * DAY_MS + tod))?
395            }
396        },
397    };
398    Ok(Value::String(out))
399}
400
401fn date_diff(args: &[Value], env: &Env) -> R<Value> {
402    let a = temporal("dateDiff", &args[0])?;
403    let b = temporal("dateDiff", &args[1])?;
404    let unit = unit_of("dateDiff", &args[2], DATE_UNITS)?;
405    if let Some(u) = unit_ms(unit) {
406        let instant = |t: Temporal| match t {
407            Temporal::Date(day) => from_local(env, day as f64 * DAY_MS),
408            Temporal::Ts(ms) => ms,
409        };
410        return Ok(json::number(num(((instant(b) - instant(a)) / u).trunc())?));
411    }
412    let (wa, wb) = (wall_of(env, a), wall_of(env, b));
413    if unit == "day" || unit == "week" {
414        let span = if unit == "day" { DAY_MS } else { 7.0 * DAY_MS };
415        return Ok(json::number(num(((wb - wa) / span).trunc())?));
416    }
417    let (da, db) = (floor_day(wa), floor_day(wb));
418    let (ca, cb) = (day_from_number(da), day_from_number(db));
419    let mut months = (cb.y - ca.y) * 12 + (cb.m - ca.m);
420    let rest_a = ca.d as f64 * DAY_MS + (wa - da as f64 * DAY_MS);
421    let rest_b = cb.d as f64 * DAY_MS + (wb - db as f64 * DAY_MS);
422    if months > 0 && rest_b < rest_a {
423        months -= 1;
424    } else if months < 0 && rest_b > rest_a {
425        months += 1;
426    }
427    let per = match unit {
428        "month" => 1.0,
429        "quarter" => 3.0,
430        _ => 12.0,
431    };
432    Ok(json::number(num((months as f64 / per).trunc())?))
433}
434
435fn start_end(f: &str, args: &[Value], env: &Env) -> R<Value> {
436    let t = temporal(f, &args[0])?;
437    let unit = unit_of(f, &args[1], CALENDAR_UNITS)?;
438    let day = match t {
439        Temporal::Date(day) => day,
440        Temporal::Ts(ms) => floor_day(to_local(env, ms)),
441    };
442    let is_date = matches!(t, Temporal::Date(_));
443    let out = if f == "startOf" {
444        let start = start_day(day, unit);
445        if is_date {
446            day_text(start)?
447        } else {
448            format_timestamp(from_local(env, start as f64 * DAY_MS))?
449        }
450    } else {
451        let next = add_calendar(start_day(day, unit), 1, unit);
452        if is_date {
453            day_text(next - 1)?
454        } else {
455            format_timestamp(from_local(env, next as f64 * DAY_MS) - 1.0)?
456        }
457    };
458    Ok(Value::String(out))
459}
460
461fn local_day(f: &str, v: &Value, env: &Env) -> R<crate::dates::CalendarDay> {
462    Ok(day_from_number(match temporal(f, v)? {
463        Temporal::Date(day) => day,
464        Temporal::Ts(ms) => floor_day(to_local(env, ms)),
465    }))
466}
467
468fn format_duration(args: &[Value]) -> R<Value> {
469    let h = number("formatDuration", &args[0])?;
470    let format = text("formatDuration", &args[1])?;
471    if !DURATION_FORMATS.contains(&format) {
472        return Err(format!("unknown duration format {format}"));
473    }
474    let total = round_half_away(h.abs() * 60.0);
475    if total > 1e15 {
476        return Err("number out of range".into());
477    }
478    let hours = (total / 60.0).floor() as i64;
479    let minutes = total as i64 - hours * 60;
480    let sign = if h < 0.0 && total > 0.0 { "-" } else { "" };
481    let hours = if format == "hh:mm" { format!("{hours:02}") } else { hours.to_string() };
482    Ok(Value::String(format!("{sign}{hours}:{minutes:02}")))
483}
484
485fn round(args: &[Value]) -> R<Value> {
486    let x = number("round", &args[0])?;
487    let digits = match args.get(1) {
488        None => 0.0,
489        Some(Value::Number(n)) => {
490            let d = n.as_f64().unwrap_or(f64::NAN);
491            if d.fract() != 0.0 || !(0.0..=10.0).contains(&d) {
492                return Err("round needs 0 to 10 digits".into());
493            }
494            d
495        }
496        Some(_) => return Err("round needs 0 to 10 digits".into()),
497    };
498    let mut p = 1.0;
499    for _ in 0..digits as usize {
500        p *= 10.0;
501    }
502    Ok(json::number(num(round_half_away(x * p) / p)?))
503}
504
505fn size(v: &Value) -> R<Value> {
506    Ok(json::number(match v {
507        Value::String(s) => s.chars().count() as f64,
508        Value::Array(a) => a.len() as f64,
509        Value::Object(m) => m.len() as f64,
510        _ => return Err(format!("size needs a list, string or map, got {}", type_name(v))),
511    }))
512}
513
514#[derive(Clone, Copy, PartialEq, Eq)]
515enum Param {
516    Number,
517    Text,
518    Temporal,
519    Sized,
520    Any,
521}
522
523struct Def {
524    params: &'static [Param],
525    min: usize,
526    variadic: bool,
527    args: &'static [&'static str],
528}
529
530use Param::{Any as A, Number as N, Sized as Z, Temporal as T, Text as S};
531
532/// THE function table (spec §4.2), positional for CEL, named (`args`) for
533/// the A2UI form. Generic only: no working days, no holidays.
534pub const FUNCTION_NAMES: &[&str] = &["mul", "div", "mod", "round", "floor", "ceil", "abs", "min", "max", "today", "now", "dateAdd", "dateDiff", "startOf", "endOf", "weekday", "isoWeek", "hours", "formatDuration", "size", "orElse"];
535
536/// The A2UI form's names in the bind table (`size` = the existing `len`;
537/// `expr` = the string form, `{expr, vars}`).
538pub const CALL_NAMES: &[&str] = &["mul", "div", "mod", "round", "floor", "ceil", "abs", "min", "max", "today", "now", "dateAdd", "dateDiff", "startOf", "endOf", "weekday", "isoWeek", "hours", "formatDuration", "orElse", "expr"];
539
540fn def(name: &str) -> Option<Def> {
541    let d = |params: &'static [Param], args: &'static [&'static str]| Def { params, min: params.len(), variadic: false, args };
542    Some(match name {
543        "mul" | "div" | "mod" => d(&[N, N], &["a", "b"]),
544        "round" => Def { params: &[N, N], min: 1, variadic: false, args: &["value", "digits"] },
545        "floor" | "ceil" | "abs" => d(&[N], &["value"]),
546        "min" | "max" => Def { params: &[N], min: 1, variadic: true, args: &["values"] },
547        "today" | "now" => d(&[], &[]),
548        "dateAdd" => d(&[T, N, S], &["date", "n", "unit"]),
549        "dateDiff" => d(&[T, T, S], &["a", "b", "unit"]),
550        "startOf" | "endOf" => d(&[T, S], &["date", "unit"]),
551        "weekday" | "isoWeek" => d(&[T], &["date"]),
552        "hours" => d(&[N], &["minutes"]),
553        "formatDuration" => d(&[N, S], &["hours", "format"]),
554        "size" => d(&[Z], &["value"]),
555        // VAPP-195: the value, or the default when it is missing (null).
556        "orElse" => d(&[A, A], &["value", "default"]),
557        _ => return None,
558    })
559}
560
561fn arity_error(name: &str, def: &Def, n: usize) -> Option<String> {
562    if def.variadic {
563        return (n < 1).then(|| format!("{name} takes at least 1 argument"));
564    }
565    let (min, max) = (def.min, def.params.len());
566    if n >= min && n <= max {
567        return None;
568    }
569    Some(if min == max { format!("{name} takes {min} argument{}", if min == 1 { "" } else { "s" }) } else { format!("{name} takes {min} or {max} arguments") })
570}
571
572fn run(name: &str, args: &[Value], env: &Env) -> R<Value> {
573    let n = |f: fn(f64) -> f64| -> R<Value> { Ok(json::number(num(f(number(name, &args[0])?))?)) };
574    match name {
575        "mul" => Ok(json::number(num(number("mul", &args[0])? * number("mul", &args[1])?)?)),
576        "div" => {
577            let (x, y) = (number("div", &args[0])?, number("div", &args[1])?);
578            if y == 0.0 {
579                return Err("division by zero".into());
580            }
581            Ok(json::number(num(x / y)?))
582        }
583        "mod" => {
584            let (x, y) = (number("mod", &args[0])?, number("mod", &args[1])?);
585            if y == 0.0 {
586                return Err("modulo by zero".into());
587            }
588            Ok(json::number(num(x % y)?))
589        }
590        "round" => round(args),
591        "floor" => n(f64::floor),
592        "ceil" => n(f64::ceil),
593        "abs" => n(f64::abs),
594        "min" | "max" => {
595            let mut out = if name == "min" { f64::INFINITY } else { f64::NEG_INFINITY };
596            for v in args {
597                let x = number(name, v)?;
598                out = if name == "min" { out.min(x) } else { out.max(x) };
599            }
600            Ok(json::number(num(out)?))
601        }
602        "today" => Ok(Value::String(day_text(floor_day(to_local(env, env.now)))?)),
603        "now" => Ok(Value::String(format_timestamp(env.now)?)),
604        "dateAdd" => date_add(args, env),
605        "dateDiff" => date_diff(args, env),
606        "startOf" | "endOf" => start_end(name, args, env),
607        "weekday" => {
608            let d = local_day("weekday", &args[0], env)?;
609            Ok(json::number(match weekday_of(d.y, d.m, d.d) {
610                0 => 7.0,
611                w => w as f64,
612            }))
613        }
614        "isoWeek" => {
615            let d = local_day("isoWeek", &args[0], env)?;
616            Ok(json::number(iso_week(d.y, d.m, d.d).1 as f64))
617        }
618        "hours" => Ok(json::number(num(number("hours", &args[0])? / 60.0)?)),
619        "formatDuration" => format_duration(args),
620        "size" => size(&args[0]),
621        "orElse" => Ok(if args[0].is_null() { args.get(1).cloned().unwrap_or(Value::Null) } else { args[0].clone() }),
622        _ => Err(format!("unknown function {name}")),
623    }
624}
625
626/// The A2UI function form `{call: name, args}` with its arguments resolved.
627/// Outer `None` = not an expression function; inner `None` = no value (an
628/// error, like every bind-time function). VAPP-195: a failed call shows its
629/// `default` (absent = no value).
630pub fn call_expression_function(name: &str, args: &Map<String, Value>, env: &ExpressionEnv) -> Option<Option<Value>> {
631    let fallback = || Some(args.get("default").cloned());
632    if name == "expr" {
633        let Some(Value::String(expr)) = args.get("expr") else { return fallback() };
634        let empty = Map::new();
635        let vars = args.get("vars").and_then(Value::as_object).unwrap_or(&empty);
636        return evaluate_expression(expr, vars, env).map_or_else(|_| fallback(), |v| Some(Some(v)));
637    }
638    if name == "size" {
639        return None;
640    }
641    let def = def(name)?;
642    let positional: Vec<Value> = if def.variadic {
643        args.get("values").and_then(Value::as_array).cloned().unwrap_or_default()
644    } else {
645        let mut list: Vec<Option<&Value>> = def.args.iter().map(|k| args.get(*k)).collect();
646        while list.len() > def.min && list.last().is_some_and(Option::is_none) {
647            list.pop();
648        }
649        list.into_iter().map(|v| v.cloned().unwrap_or(Value::Null)).collect()
650    };
651    if arity_error(name, &def, positional.len()).is_some() {
652        return fallback();
653    }
654    run(name, &positional, &Env::of(env)).map_or_else(|_| fallback(), |v| Some(Some(v)))
655}
656
657// ---------------------------------------------------------------------------
658// Parsing
659// ---------------------------------------------------------------------------
660
661/// A parsed expression (`at` = the code point it starts at).
662#[derive(Debug, Clone, PartialEq)]
663pub enum Node {
664    Lit(Value, usize),
665    Id(String, usize),
666    List(Vec<Node>, usize),
667    Field(Box<Node>, String, usize),
668    Index(Box<Node>, Box<Node>, usize),
669    Unary(char, Box<Node>, usize),
670    Bin(&'static str, Box<Node>, Box<Node>, usize),
671    Cond(Box<Node>, Box<Node>, Box<Node>, usize),
672    Call(String, Vec<Node>, usize),
673    Macro(String, Box<Node>, String, Box<Node>, usize),
674    /// VAPP-195: `has(a.b)`, CEL's presence test (the node is a `Field`).
675    Has(Box<Node>, usize),
676}
677
678#[derive(Debug, Clone, PartialEq)]
679enum Tok {
680    Num(f64),
681    Str(String),
682    Id(String),
683    Op(&'static str),
684    End,
685}
686
687#[derive(Debug, Clone)]
688struct Token {
689    t: Tok,
690    at: usize,
691    text: String,
692}
693
694const OPS2: &[&str] = &["==", "!=", "<=", ">=", "&&", "||"];
695const OPS1: &[&str] = &["+", "-", "*", "/", "%", "<", ">", "!", "(", ")", "[", "]", ",", ".", "?", ":"];
696
697fn is_digit(c: Option<&char>) -> bool {
698    c.is_some_and(char::is_ascii_digit)
699}
700fn is_id_start(c: Option<&char>) -> bool {
701    c.is_some_and(|c| c.is_ascii_alphabetic() || *c == '_')
702}
703fn is_id_part(c: Option<&char>) -> bool {
704    is_id_start(c) || is_digit(c)
705}
706
707fn tokenize(cs: &[char]) -> R<Vec<Token>> {
708    let mut out = Vec::new();
709    let mut i = 0;
710    while i < cs.len() {
711        let c = cs[i];
712        if matches!(c, ' ' | '\t' | '\n' | '\r') {
713            i += 1;
714            continue;
715        }
716        let at = i;
717        if c.is_ascii_digit() {
718            while is_digit(cs.get(i)) {
719                i += 1;
720            }
721            if cs.get(i) == Some(&'.') && is_digit(cs.get(i + 1)) {
722                i += 1;
723                while is_digit(cs.get(i)) {
724                    i += 1;
725                }
726            }
727            if matches!(cs.get(i), Some('e' | 'E')) && (is_digit(cs.get(i + 1)) || (matches!(cs.get(i + 1), Some('+' | '-')) && is_digit(cs.get(i + 2)))) {
728                i += 2;
729                while is_digit(cs.get(i)) {
730                    i += 1;
731                }
732            }
733            let text: String = cs[at..i].iter().collect();
734            let v = text.parse::<f64>().unwrap_or(f64::INFINITY);
735            out.push(Token { t: Tok::Num(v), at, text });
736            continue;
737        }
738        if c == '\'' || c == '"' {
739            i += 1;
740            let mut s = String::new();
741            loop {
742                let d = cs.get(i).copied();
743                match d {
744                    None | Some('\n') => return Err(format!("at {at}: unterminated string")),
745                    Some(d) if d == c => break,
746                    Some('\\') => {
747                        let e = cs.get(i + 1).copied();
748                        let mapped = match e {
749                            Some('\\') => '\\',
750                            Some('\'') => '\'',
751                            Some('"') => '"',
752                            Some('n') => '\n',
753                            Some('t') => '\t',
754                            Some('r') => '\r',
755                            _ => return Err(format!("at {i}: unknown escape \\{}", e.map(String::from).unwrap_or_default())),
756                        };
757                        s.push(mapped);
758                        i += 2;
759                    }
760                    Some(d) => {
761                        s.push(d);
762                        i += 1;
763                    }
764                }
765            }
766            i += 1;
767            out.push(Token { t: Tok::Str(s), at, text: cs[at..i].iter().collect() });
768            continue;
769        }
770        if is_id_start(Some(&c)) {
771            while is_id_part(cs.get(i)) {
772                i += 1;
773            }
774            let text: String = cs[at..i].iter().collect();
775            out.push(Token { t: Tok::Id(text.clone()), at, text });
776            continue;
777        }
778        let two: String = cs[i..(i + 2).min(cs.len())].iter().collect();
779        if let Some(op) = OPS2.iter().find(|o| **o == two) {
780            out.push(Token { t: Tok::Op(op), at, text: two });
781            i += 2;
782            continue;
783        }
784        if let Some(op) = OPS1.iter().find(|o| o.starts_with(c)) {
785            out.push(Token { t: Tok::Op(op), at, text: c.to_string() });
786            i += 1;
787            continue;
788        }
789        return Err(format!("at {at}: unexpected {c}"));
790    }
791    out.push(Token { t: Tok::End, at: cs.len(), text: "end".into() });
792    Ok(out)
793}
794
795const MACROS: &[&str] = &["map", "filter", "all", "exists"];
796
797struct Parser {
798    tokens: Vec<Token>,
799    i: usize,
800    depth: usize,
801    in_body: usize,
802}
803
804impl Parser {
805    fn peek(&self) -> &Token {
806        &self.tokens[self.i]
807    }
808    fn is_op(&self, v: &str) -> bool {
809        matches!(&self.peek().t, Tok::Op(o) if *o == v)
810    }
811    fn next(&mut self) -> Token {
812        let t = self.tokens[self.i].clone();
813        self.i += 1;
814        t
815    }
816    fn unexpected<T>(&self) -> R<T> {
817        let t = self.peek();
818        Err(format!("at {}: unexpected {}", t.at, t.text))
819    }
820    fn expect(&mut self, v: &str) -> R<Token> {
821        if !self.is_op(v) {
822            return self.unexpected();
823        }
824        Ok(self.next())
825    }
826    fn enter(&mut self, at: usize) -> R<()> {
827        self.depth += 1;
828        if self.depth > MAX_DEPTH {
829            return Err(format!("at {at}: expression is nested too deeply"));
830        }
831        Ok(())
832    }
833
834    fn parse_all(&mut self) -> R<Node> {
835        let node = self.expr()?;
836        if self.peek().t != Tok::End {
837            return self.unexpected();
838        }
839        Ok(node)
840    }
841
842    fn expr(&mut self) -> R<Node> {
843        crate::deep(|| self.expr_inner())
844    }
845
846    fn expr_inner(&mut self) -> R<Node> {
847        let at = self.peek().at;
848        self.enter(at)?;
849        let test = self.or()?;
850        let mut out = test;
851        if self.is_op("?") {
852            self.next();
853            let then = self.expr()?;
854            self.expect(":")?;
855            let otherwise = self.expr()?;
856            out = Node::Cond(Box::new(out), Box::new(then), Box::new(otherwise), at);
857        }
858        self.depth -= 1;
859        Ok(out)
860    }
861
862    fn binary(&mut self, ops: &[&'static str], operand: fn(&mut Parser) -> R<Node>) -> R<Node> {
863        let mut a = operand(self)?;
864        loop {
865            let t = self.peek().clone();
866            let Tok::Op(op) = t.t else { return Ok(a) };
867            let Some(op) = ops.iter().find(|o| **o == op) else { return Ok(a) };
868            self.next();
869            let b = operand(self)?;
870            a = Node::Bin(op, Box::new(a), Box::new(b), t.at);
871        }
872    }
873    fn or(&mut self) -> R<Node> {
874        self.binary(&["||"], Parser::and)
875    }
876    fn and(&mut self) -> R<Node> {
877        self.binary(&["&&"], Parser::rel)
878    }
879    fn rel(&mut self) -> R<Node> {
880        self.binary(&["==", "!=", "<", "<=", ">", ">="], Parser::add)
881    }
882    fn add(&mut self) -> R<Node> {
883        self.binary(&["+", "-"], Parser::mul)
884    }
885    fn mul(&mut self) -> R<Node> {
886        self.binary(&["*", "/", "%"], Parser::unary)
887    }
888
889    fn unary(&mut self) -> R<Node> {
890        let t = self.peek().clone();
891        if let Tok::Op(op @ ("!" | "-")) = t.t {
892            self.next();
893            self.enter(t.at)?;
894            let arg = crate::deep(|| self.unary())?;
895            self.depth -= 1;
896            return Ok(Node::Unary(op.chars().next().unwrap_or('!'), Box::new(arg), t.at));
897        }
898        self.postfix()
899    }
900
901    fn postfix(&mut self) -> R<Node> {
902        let mut node = self.primary()?;
903        loop {
904            if self.is_op(".") {
905                self.next();
906                let name = self.peek().clone();
907                let Tok::Id(field) = name.t.clone() else { return self.unexpected() };
908                self.next();
909                if self.is_op("(") {
910                    if !MACROS.contains(&field.as_str()) {
911                        if let Some(dotted) = dotted_name(&node) {
912                            let full = format!("{dotted}.{field}");
913                            let effect = function_effect(&full);
914                            if effect != FunctionEffect::Pure {
915                                return Err(format!("at {}: {}: an expression is pure, use an action step", root_at(&node), effect_words(&full, effect)));
916                            }
917                        }
918                        return Err(format!("at {}: unknown method {field}", name.at));
919                    }
920                    if self.in_body > 0 {
921                        return Err(format!("at {}: nested comprehensions are not allowed", name.at));
922                    }
923                    self.next();
924                    let v = self.peek().clone();
925                    let comma = matches!(self.tokens.get(self.i + 1).map(|t| &t.t), Some(Tok::Op(",")));
926                    let Tok::Id(var) = v.t.clone() else { return Err(format!("at {}: {field} needs a name first", v.at)) };
927                    if !comma {
928                        return Err(format!("at {}: {field} needs a name first", v.at));
929                    }
930                    self.next();
931                    self.next();
932                    self.in_body += 1;
933                    let body = self.expr()?;
934                    self.in_body -= 1;
935                    self.expect(")")?;
936                    node = Node::Macro(field, Box::new(node), var, Box::new(body), name.at);
937                } else {
938                    node = Node::Field(Box::new(node), field, name.at);
939                }
940                continue;
941            }
942            if self.is_op("[") {
943                let at = self.next().at;
944                let index = self.expr()?;
945                self.expect("]")?;
946                node = Node::Index(Box::new(node), Box::new(index), at);
947                continue;
948            }
949            return Ok(node);
950        }
951    }
952
953    fn list(&mut self, close: &str) -> R<Vec<Node>> {
954        let mut items = Vec::new();
955        if self.is_op(close) {
956            self.next();
957            return Ok(items);
958        }
959        loop {
960            items.push(self.expr()?);
961            if self.is_op(",") {
962                self.next();
963                if self.is_op(close) && close == "]" {
964                    self.next();
965                    return Ok(items);
966                }
967                continue;
968            }
969            self.expect(close)?;
970            return Ok(items);
971        }
972    }
973
974    fn primary(&mut self) -> R<Node> {
975        let t = self.peek().clone();
976        match t.t {
977            Tok::Num(v) => {
978                self.next();
979                if !v.is_finite() {
980                    return Err(format!("at {}: number out of range", t.at));
981                }
982                Ok(Node::Lit(json::number(v), t.at))
983            }
984            Tok::Str(s) => {
985                self.next();
986                Ok(Node::Lit(Value::String(s), t.at))
987            }
988            Tok::Id(name) => {
989                self.next();
990                match name.as_str() {
991                    "true" | "false" => return Ok(Node::Lit(Value::Bool(name == "true"), t.at)),
992                    "null" => return Ok(Node::Lit(Value::Null, t.at)),
993                    _ => {}
994                }
995                // VAPP-195: CEL's presence test, `has(a.b)`.
996                if name == "has" && self.is_op("(") {
997                    self.next();
998                    let arg = self.expr()?;
999                    self.expect(")")?;
1000                    if !matches!(arg, Node::Field(..)) {
1001                        let hint = match &arg {
1002                            Node::Id(name, _) => format!("; for a name write {name} != null"),
1003                            _ => String::new(),
1004                        };
1005                        return Err(format!("at {}: has needs a field: has(a.b){hint}", t.at));
1006                    }
1007                    return Ok(Node::Has(Box::new(arg), t.at));
1008                }
1009                if self.is_op("(") {
1010                    if def(&name).is_none() {
1011                        let effect = function_effect(&name);
1012                        if effect != FunctionEffect::Pure {
1013                            return Err(format!("at {}: {}: an expression is pure, use an action step", t.at, effect_words(&name, effect)));
1014                        }
1015                        return Err(format!("at {}: unknown function {name}", t.at));
1016                    }
1017                    self.next();
1018                    let args = self.list(")")?;
1019                    return Ok(Node::Call(name, args, t.at));
1020                }
1021                Ok(Node::Id(name, t.at))
1022            }
1023            Tok::Op("(") => {
1024                self.next();
1025                let inner = self.expr()?;
1026                self.expect(")")?;
1027                Ok(inner)
1028            }
1029            Tok::Op("[") => {
1030                self.next();
1031                Ok(Node::List(self.list("]")?, t.at))
1032            }
1033            _ => self.unexpected(),
1034        }
1035    }
1036}
1037
1038/// `a.b.c` as a name (a chain of plain names), else `None`.
1039fn dotted_name(node: &Node) -> Option<String> {
1040    match node {
1041        Node::Id(name, _) => Some(name.clone()),
1042        Node::Field(of, name, _) => dotted_name(of).map(|of| format!("{of}.{name}")),
1043        _ => None,
1044    }
1045}
1046
1047/// VAPP-214: the free names an expression reads, sorted (a macro's own
1048/// variable is not free inside its body).
1049pub fn expression_names(node: &Node) -> Vec<String> {
1050    fn walk(n: &Node, bound: &mut Vec<String>, out: &mut std::collections::BTreeSet<String>) {
1051        match n {
1052            Node::Lit(..) => {}
1053            Node::Id(name, _) => {
1054                if !bound.iter().any(|b| b == name) {
1055                    out.insert(name.clone());
1056                }
1057            }
1058            Node::List(items, _) | Node::Call(_, items, _) => items.iter().for_each(|x| walk(x, bound, out)),
1059            Node::Field(of, _, _) | Node::Has(of, _) | Node::Unary(_, of, _) => walk(of, bound, out),
1060            Node::Index(of, index, _) => {
1061                walk(of, bound, out);
1062                walk(index, bound, out);
1063            }
1064            Node::Bin(_, a, b, _) => {
1065                walk(a, bound, out);
1066                walk(b, bound, out);
1067            }
1068            Node::Cond(test, then, other, _) => {
1069                walk(test, bound, out);
1070                walk(then, bound, out);
1071                walk(other, bound, out);
1072            }
1073            Node::Macro(_, of, v, body, _) => {
1074                walk(of, bound, out);
1075                bound.push(v.clone());
1076                walk(body, bound, out);
1077                bound.pop();
1078            }
1079        }
1080    }
1081    let mut out = std::collections::BTreeSet::new();
1082    walk(node, &mut Vec::new(), &mut out);
1083    out.into_iter().collect()
1084}
1085
1086/// VAPP-195: a value's path as written (`a.b[0].c`, `m['k']`): names,
1087/// fields and literal indexes; `None` for anything else.
1088pub fn path_text(node: &Node) -> Option<String> {
1089    match node {
1090        Node::Id(name, _) => Some(name.clone()),
1091        Node::Field(of, name, _) => path_text(of).map(|of| format!("{of}.{name}")),
1092        Node::Index(of, index, _) => {
1093            let of = path_text(of)?;
1094            match literal(index) {
1095                Some(Value::Number(n)) => Some(format!("{of}[{}]", num_text(n.as_f64().unwrap_or(0.0)))),
1096                Some(Value::String(k)) => Some(format!("{of}['{k}']")),
1097                _ => None,
1098            }
1099        }
1100        _ => None,
1101    }
1102}
1103
1104/// The code point a node starts at (its own `at`).
1105fn node_at(node: &Node) -> usize {
1106    match node {
1107        Node::Lit(_, at) | Node::Id(_, at) | Node::List(_, at) | Node::Field(_, _, at) | Node::Index(_, _, at) | Node::Unary(_, _, at) | Node::Bin(_, _, _, at) | Node::Cond(_, _, _, at) | Node::Call(_, _, at) | Node::Macro(_, _, _, _, at) | Node::Has(_, at) => *at,
1108    }
1109}
1110
1111fn root_at(node: &Node) -> usize {
1112    match node {
1113        Node::Field(of, _, _) => root_at(of),
1114        _ => node_at(node),
1115    }
1116}
1117
1118/// Parse once (at package validation, or before evaluating): the tree, or
1119/// the first syntax / cost error (`at <code point>: <reason>`).
1120pub fn parse_expression(source: &str) -> Result<Node, String> {
1121    let cs: Vec<char> = source.chars().collect();
1122    if cs.len() > MAX_LENGTH {
1123        return Err(format!("expression is longer than {MAX_LENGTH} characters"));
1124    }
1125    Parser { tokens: tokenize(&cs)?, i: 0, depth: 0, in_body: 0 }.parse_all()
1126}
1127
1128// ---------------------------------------------------------------------------
1129// Evaluation
1130// ---------------------------------------------------------------------------
1131
1132struct Scope<'a> {
1133    name: &'a str,
1134    value: &'a Value,
1135    parent: Option<&'a Scope<'a>>,
1136}
1137
1138struct Evaluator<'v> {
1139    vars: &'v Map<String, Value>,
1140    env: Env<'v>,
1141    steps: usize,
1142    max_steps: usize,
1143}
1144
1145impl Evaluator<'_> {
1146    fn step(&mut self) -> R<()> {
1147        self.steps += 1;
1148        if self.steps > self.max_steps {
1149            return Err("step limit exceeded".into());
1150        }
1151        Ok(())
1152    }
1153
1154    fn ev(&mut self, node: &Node, scope: Option<&Scope>) -> R<Value> {
1155        crate::deep(|| self.ev_inner(node, scope))
1156    }
1157
1158    fn ev_inner(&mut self, node: &Node, scope: Option<&Scope>) -> R<Value> {
1159        self.step()?;
1160        match node {
1161            Node::Lit(v, _) => Ok(v.clone()),
1162            Node::List(items, _) => items.iter().map(|i| self.ev(i, scope)).collect::<R<Vec<_>>>().map(Value::Array),
1163            Node::Id(name, _) => {
1164                let mut s = scope;
1165                while let Some(sc) = s {
1166                    if sc.name == name {
1167                        return Ok(sc.value.clone());
1168                    }
1169                    s = sc.parent;
1170                }
1171                self.vars.get(name).cloned().ok_or_else(|| format!("unknown name {name}"))
1172            }
1173            Node::Field(of, name, _) => match self.ev(of, scope)? {
1174                Value::Null => Ok(Value::Null),
1175                Value::Object(m) => Ok(m.get(name).cloned().unwrap_or(Value::Null)),
1176                other => Err(format!("no field {name} on {}", type_name(&other))),
1177            },
1178            Node::Has(field, _) => {
1179                let Node::Field(receiver, name, _) = field.as_ref() else { return Err("has needs a field: has(a.b)".into()) };
1180                match self.ev(receiver, scope)? {
1181                    Value::Null => Ok(Value::Bool(false)),
1182                    Value::Object(m) => Ok(Value::Bool(m.get(name).is_some_and(|v| !v.is_null()))),
1183                    other => Err(format!("no field {name} on {}", type_name(&other))),
1184                }
1185            }
1186            Node::Index(of, index, _) => {
1187                let of = self.ev(of, scope)?;
1188                let index = self.ev(index, scope)?;
1189                match of {
1190                    Value::Array(list) => {
1191                        let Value::Number(n) = &index else { return Err(format!("a list index is a number, got {}", type_name(&index))) };
1192                        let n = n.as_f64().unwrap_or(0.0);
1193                        if n.fract() != 0.0 {
1194                            return Err(format!("a list index is a whole number, got {}", num_text(n)));
1195                        }
1196                        // VAPP-204: an index past either end is null, like a missing field.
1197                        if n < 0.0 || n >= list.len() as f64 {
1198                            return Ok(Value::Null);
1199                        }
1200                        Ok(list[n as usize].clone())
1201                    }
1202                    Value::Object(m) => {
1203                        let Value::String(k) = &index else { return Err(format!("a map key is a string, got {}", type_name(&index))) };
1204                        Ok(m.get(k).cloned().unwrap_or(Value::Null))
1205                    }
1206                    Value::Null => Ok(Value::Null),
1207                    other => Err(format!("cannot index {}", type_name(&other))),
1208                }
1209            }
1210            Node::Unary(op, arg, _) => {
1211                let v = self.ev(arg, scope)?;
1212                if *op == '-' {
1213                    let Value::Number(n) = &v else { return Err(format!("cannot apply - to {}", type_name(&v))) };
1214                    return Ok(json::number(num(-n.as_f64().unwrap_or(0.0))?));
1215                }
1216                let Value::Bool(b) = v else { return Err(format!("cannot apply ! to {}", type_name(&v))) };
1217                Ok(Value::Bool(!b))
1218            }
1219            Node::Cond(test, then, otherwise, _) => {
1220                let t = self.ev(test, scope)?;
1221                let Value::Bool(t) = t else { return Err(format!("cannot use {} as a condition", type_name(&t))) };
1222                self.ev(if t { then } else { otherwise }, scope)
1223            }
1224            Node::Bin(op, a, b, _) => self.binary(op, a, b, scope),
1225            Node::Call(name, args, _) => {
1226                let args = args.iter().map(|a| self.ev(a, scope)).collect::<R<Vec<_>>>()?;
1227                let d = def(name).ok_or_else(|| format!("unknown function {name}"))?;
1228                if let Some(e) = arity_error(name, &d, args.len()) {
1229                    return Err(e);
1230                }
1231                run(name, &args, &self.env)
1232            }
1233            Node::Macro(f, of, var, body, _) => {
1234                let of = self.ev(of, scope)?;
1235                let Value::Array(list) = of else { return Err(format!("{f} needs a list, got {}", type_name(&of))) };
1236                let mut out = Vec::new();
1237                for item in &list {
1238                    self.step()?;
1239                    let inner = Scope { name: var, value: item, parent: scope };
1240                    let v = self.ev(body, Some(&inner))?;
1241                    if f == "map" {
1242                        out.push(v);
1243                        continue;
1244                    }
1245                    let Value::Bool(b) = v else { return Err(format!("{f} needs a bool, got {}", type_name(&v))) };
1246                    match f.as_str() {
1247                        "filter" if b => out.push(item.clone()),
1248                        "all" if !b => return Ok(Value::Bool(false)),
1249                        "exists" if b => return Ok(Value::Bool(true)),
1250                        _ => {}
1251                    }
1252                }
1253                Ok(match f.as_str() {
1254                    "all" => Value::Bool(true),
1255                    "exists" => Value::Bool(false),
1256                    _ => Value::Array(out),
1257                })
1258            }
1259        }
1260    }
1261
1262    fn binary(&mut self, op: &str, a: &Node, b: &Node, scope: Option<&Scope>) -> R<Value> {
1263        if op == "&&" || op == "||" {
1264            let x = self.ev(a, scope)?;
1265            let Value::Bool(x) = x else { return Err(format!("cannot apply {op} to {}", type_name(&x))) };
1266            if (op == "&&" && !x) || (op == "||" && x) {
1267                return Ok(Value::Bool(x));
1268            }
1269            let y = self.ev(b, scope)?;
1270            let Value::Bool(_) = y else { return Err(format!("cannot apply {op} to {}", type_name(&y))) };
1271            return Ok(y);
1272        }
1273        let x = self.ev(a, scope)?;
1274        let y = self.ev(b, scope)?;
1275        match op {
1276            "==" => Ok(Value::Bool(deep_equal(&x, &y))),
1277            "!=" => Ok(Value::Bool(!deep_equal(&x, &y))),
1278            "<" | "<=" | ">" | ">=" => {
1279                let c = match (&x, &y) {
1280                    (Value::Number(p), Value::Number(q)) => p.as_f64().partial_cmp(&q.as_f64()).unwrap_or(std::cmp::Ordering::Equal),
1281                    (Value::String(p), Value::String(q)) => utf16_cmp(p, q),
1282                    _ => return Err(format!("cannot compare {} and {}", type_name(&x), type_name(&y))),
1283                };
1284                use std::cmp::Ordering::*;
1285                Ok(Value::Bool(match op {
1286                    "<" => c == Less,
1287                    "<=" => c != Greater,
1288                    ">" => c == Greater,
1289                    _ => c != Less,
1290                }))
1291            }
1292            "+" => match (x, y) {
1293                (Value::Number(p), Value::Number(q)) => Ok(json::number(num(p.as_f64().unwrap_or(0.0) + q.as_f64().unwrap_or(0.0))?)),
1294                (Value::String(p), Value::String(q)) => Ok(Value::String(p + &q)),
1295                (Value::Array(mut p), Value::Array(q)) => {
1296                    p.extend(q);
1297                    Ok(Value::Array(p))
1298                }
1299                (x, y) => Err(format!("cannot apply + to {} and {}", type_name(&x), type_name(&y))),
1300            },
1301            _ => {
1302                let (Value::Number(p), Value::Number(q)) = (&x, &y) else { return Err(format!("cannot apply {op} to {} and {}", type_name(&x), type_name(&y))) };
1303                let (p, q) = (p.as_f64().unwrap_or(0.0), q.as_f64().unwrap_or(0.0));
1304                match op {
1305                    "-" => Ok(json::number(num(p - q)?)),
1306                    "*" => Ok(json::number(num(p * q)?)),
1307                    _ => {
1308                        if q == 0.0 {
1309                            return Err(if op == "/" { "division by zero" } else { "modulo by zero" }.into());
1310                        }
1311                        Ok(json::number(num(if op == "/" { p / q } else { p % q })?))
1312                    }
1313                }
1314            }
1315        }
1316    }
1317}
1318
1319/// Evaluate an expression over `vars`: the value, or the error (a syntax,
1320/// type, range or cost error).
1321pub fn evaluate_expression(source: &str, vars: &Map<String, Value>, env: &ExpressionEnv) -> Result<Value, String> {
1322    let ast = parse_expression(source)?;
1323    evaluate_node(&ast, vars, env)
1324}
1325
1326/// Evaluate a parsed tree (see [`parse_expression`]).
1327pub fn evaluate_node<'a>(ast: &Node, vars: &'a Map<String, Value>, env: &ExpressionEnv<'a>) -> Result<Value, String> {
1328    let mut e = Evaluator { vars, env: Env::of(env), steps: 0, max_steps: env.max_steps.unwrap_or(MAX_STEPS) };
1329    e.ev(ast, None)
1330}
1331
1332/// VAPP-194: evaluate a parsed tree and count its steps (an action spends
1333/// one budget over all of its expressions): the result and the steps used.
1334pub fn evaluate_counted<'a>(ast: &Node, vars: &'a Map<String, Value>, env: &ExpressionEnv<'a>) -> (Result<Value, String>, usize) {
1335    let mut e = Evaluator { vars, env: Env::of(env), steps: 0, max_steps: env.max_steps.unwrap_or(MAX_STEPS) };
1336    let result = e.ev(ast, None);
1337    (result, e.steps)
1338}
1339
1340// ---------------------------------------------------------------------------
1341// Type checking (package validation)
1342// ---------------------------------------------------------------------------
1343
1344/// VAPP-195 (E2): a value that may be missing is `T?`, spelled `{"?": T}`
1345/// (a row never has a field named `?`).
1346pub fn is_optional(t: &Value) -> bool {
1347    matches!(t, Value::Object(m) if m.len() == 1 && m.contains_key("?"))
1348}
1349
1350/// `T?` (null and an optional stay as they are).
1351pub fn optional_type(t: Value) -> Value {
1352    if is_optional(&t) || is(&t, "null") {
1353        return t;
1354    }
1355    let mut m = Map::new();
1356    m.insert("?".into(), t);
1357    Value::Object(m)
1358}
1359
1360/// `T` of a `T?`.
1361pub fn required_type(t: &Value) -> Value {
1362    match t {
1363        Value::Object(m) if is_optional(t) => m["?"].clone(),
1364        _ => t.clone(),
1365    }
1366}
1367
1368/// The name a type prints as in an error: a list `list`, a row `map`, an
1369/// optional `T?`.
1370pub fn type_display(t: &Value) -> String {
1371    match t {
1372        Value::Object(m) if is_optional(t) => format!("{}?", type_display(&m["?"])),
1373        Value::String(s) => s.clone(),
1374        Value::Array(_) => "list".into(),
1375        _ => "map".into(),
1376    }
1377}
1378
1379fn is_row(t: &Value) -> bool {
1380    t.is_object() && !is_optional(t)
1381}
1382fn is_textual(t: &Value) -> bool {
1383    matches!(t.as_str(), Some("string" | "date" | "timestamp"))
1384}
1385fn loose(t: &Value) -> bool {
1386    matches!(t.as_str(), Some("dyn" | "null"))
1387}
1388fn is(t: &Value, name: &str) -> bool {
1389    t.as_str() == Some(name)
1390}
1391fn ty(name: &str) -> Value {
1392    Value::String(name.into())
1393}
1394
1395fn accepts(param: Param, t: &Value) -> bool {
1396    if param == Param::Any || loose(t) {
1397        return true;
1398    }
1399    match param {
1400        Param::Number => is(t, "number"),
1401        Param::Text | Param::Temporal => is_textual(t),
1402        Param::Sized => is(t, "string") || is(t, "map") || t.is_array() || is_row(t),
1403        Param::Any => true,
1404    }
1405}
1406
1407fn param_name(p: Param) -> &'static str {
1408    match p {
1409        Param::Number => "a number",
1410        Param::Text => "a string",
1411        Param::Temporal => "a date",
1412        Param::Sized => "a list, string or map",
1413        Param::Any => "a value",
1414    }
1415}
1416
1417struct TypeScope<'a> {
1418    name: &'a str,
1419    ty: Value,
1420    parent: Option<&'a TypeScope<'a>>,
1421}
1422
1423fn literal(node: &Node) -> Option<&Value> {
1424    match node {
1425        Node::Lit(v, _) => Some(v),
1426        _ => None,
1427    }
1428}
1429
1430/// The paths a test proves present when it is true (`yes`) / when it is
1431/// false (`no`): `has(p)`, `p != null`, `p == null`, through `&&`, `||`
1432/// and `!`.
1433fn guards(node: &Node) -> (Vec<String>, Vec<String>) {
1434    match node {
1435        Node::Has(field, _) => (path_text(field).into_iter().collect(), Vec::new()),
1436        Node::Bin(op @ ("==" | "!="), a, b, _) => {
1437            let side = if matches!(a.as_ref(), Node::Lit(Value::Null, _)) {
1438                Some(b)
1439            } else if matches!(b.as_ref(), Node::Lit(Value::Null, _)) {
1440                Some(a)
1441            } else {
1442                None
1443            };
1444            match side.and_then(|s| path_text(s)) {
1445                None => (Vec::new(), Vec::new()),
1446                Some(p) if *op == "!=" => (vec![p], Vec::new()),
1447                Some(p) => (Vec::new(), vec![p]),
1448            }
1449        }
1450        Node::Bin("&&", a, b, _) => {
1451            let (mut yes, _) = guards(a);
1452            yes.extend(guards(b).0);
1453            (yes, Vec::new())
1454        }
1455        Node::Bin("||", a, b, _) => {
1456            let (_, mut no) = guards(a);
1457            no.extend(guards(b).1);
1458            (Vec::new(), no)
1459        }
1460        Node::Unary('!', arg, _) => {
1461            let (yes, no) = guards(arg);
1462            (no, yes)
1463        }
1464        _ => (Vec::new(), Vec::new()),
1465    }
1466}
1467
1468/// An unguarded use of a value that may be missing.
1469fn missing<T>(node: &Node) -> R<T> {
1470    let p = path_text(node).unwrap_or_else(|| format!("the value at {}", node_at(node)));
1471    Err(format!("{p} may be missing: guard it with has(), a null check or orElse()"))
1472}
1473
1474struct Checker<'t> {
1475    types: &'t Map<String, Value>,
1476    /// The paths a guard proved present here (`has(r.f) ? r.f + 1 : 0`).
1477    present: std::cell::RefCell<Vec<String>>,
1478}
1479
1480impl Checker<'_> {
1481    /// Type `node` with `paths` proved present.
1482    fn within(&self, paths: Vec<String>, node: &Node, scope: Option<&TypeScope>) -> R<Value> {
1483        let before = self.present.borrow().clone();
1484        self.present.borrow_mut().extend(paths);
1485        let out = self.tc(node, scope);
1486        *self.present.borrow_mut() = before;
1487        out
1488    }
1489
1490    /// The type of a use that must not be missing.
1491    fn need(&self, node: &Node, scope: Option<&TypeScope>) -> R<Value> {
1492        let t = self.tc(node, scope)?;
1493        if is_optional(&t) {
1494            return missing(node);
1495        }
1496        Ok(t)
1497    }
1498
1499    fn tc(&self, node: &Node, scope: Option<&TypeScope>) -> R<Value> {
1500        let t = crate::deep(|| self.tc_inner(node, scope))?;
1501        let p = if matches!(node, Node::Id(..) | Node::Field(..) | Node::Index(..)) { path_text(node) } else { None };
1502        Ok(match p {
1503            Some(p) if self.present.borrow().contains(&p) => required_type(&t),
1504            _ => t,
1505        })
1506    }
1507
1508    fn tc_inner(&self, node: &Node, scope: Option<&TypeScope>) -> R<Value> {
1509        match node {
1510            Node::Lit(v, _) => Ok(ty(type_name(v))),
1511            Node::List(items, _) => {
1512                let items = items.iter().map(|i| self.tc(i, scope)).collect::<R<Vec<_>>>()?;
1513                Ok(Value::Array(vec![match items.first() {
1514                    None => ty("dyn"),
1515                    Some(first) if items.iter().all(|t| t == first) => first.clone(),
1516                    Some(_) => ty("dyn"),
1517                }]))
1518            }
1519            Node::Id(name, _) => {
1520                let mut s = scope;
1521                while let Some(sc) = s {
1522                    if sc.name == name {
1523                        return Ok(sc.ty.clone());
1524                    }
1525                    s = sc.parent;
1526                }
1527                self.types.get(name).cloned().ok_or_else(|| format!("unknown name {name}"))
1528            }
1529            Node::Field(..) | Node::Has(..) => {
1530                let has = matches!(node, Node::Has(..));
1531                let field = match node {
1532                    Node::Has(field, _) => field.as_ref(),
1533                    _ => node,
1534                };
1535                let Node::Field(of, name, _) = field else { return Err("has needs a field: has(a.b)".into()) };
1536                let raw = self.tc(of, scope)?;
1537                let of = required_type(&raw);
1538                // VAPP-204: a field of a map may be missing (`dyn?`).
1539                let t = if is(&of, "map") {
1540                    optional_type(ty("dyn"))
1541                } else if matches!(of.as_str(), Some("dyn" | "null")) {
1542                    ty("dyn")
1543                } else if let Value::Object(fields) = &of {
1544                    fields.get(name).cloned().ok_or_else(|| format!("unknown field {name}"))?
1545                } else {
1546                    return Err(format!("no field {name} on {}", type_display(&of)));
1547                };
1548                if has {
1549                    return Ok(ty("bool"));
1550                }
1551                Ok(if is_optional(&raw) { optional_type(t) } else { t })
1552            }
1553            Node::Index(of, index, _) => {
1554                let raw = self.tc(of, scope)?;
1555                let of_t = required_type(&raw);
1556                let index_t = self.need(index, scope)?;
1557                let wrap = |t: Value| if is_optional(&raw) { optional_type(t) } else { t };
1558                if let Value::Array(item) = &of_t {
1559                    if !loose(&index_t) && !is(&index_t, "number") {
1560                        return Err(format!("a list index is a number, got {}", type_display(&index_t)));
1561                    }
1562                    // VAPP-204: an index may be past the end (null).
1563                    return Ok(optional_type(item.first().cloned().unwrap_or_else(|| ty("dyn"))));
1564                }
1565                if is_row(&of_t) || is(&of_t, "map") {
1566                    if !loose(&index_t) && !is_textual(&index_t) {
1567                        return Err(format!("a map key is a string, got {}", type_display(&index_t)));
1568                    }
1569                    if let (Value::Object(fields), Some(Value::String(key))) = (&of_t, literal(index)) {
1570                        return fields.get(key).cloned().map(wrap).ok_or_else(|| format!("unknown field {key}"));
1571                    }
1572                    return Ok(optional_type(ty("dyn")));
1573                }
1574                if loose(&of_t) {
1575                    return Ok(wrap(ty("dyn")));
1576                }
1577                Err(format!("cannot index {}", type_display(&of_t)))
1578            }
1579            Node::Unary(op, arg, _) => {
1580                let t = self.need(arg, scope)?;
1581                let want = if *op == '-' { "number" } else { "bool" };
1582                if !loose(&t) && !is(&t, want) {
1583                    return Err(format!("cannot apply {op} to {}", type_display(&t)));
1584                }
1585                Ok(ty(want))
1586            }
1587            Node::Cond(test, then, otherwise, _) => {
1588                let t = self.need(test, scope)?;
1589                if !loose(&t) && !is(&t, "bool") {
1590                    return Err(format!("cannot use {} as a condition", type_display(&t)));
1591                }
1592                let (yes, no) = guards(test);
1593                let a = self.within(yes, then, scope)?;
1594                let b = self.within(no, otherwise, scope)?;
1595                Ok(if a == b {
1596                    a
1597                } else if is(&a, "null") {
1598                    optional_type(b)
1599                } else if is(&b, "null") {
1600                    optional_type(a)
1601                } else if required_type(&a) == required_type(&b) {
1602                    optional_type(required_type(&a))
1603                } else if is_optional(&a) || is_optional(&b) {
1604                    optional_type(ty("dyn"))
1605                } else {
1606                    ty("dyn")
1607                })
1608            }
1609            Node::Bin(op, a, b, _) => self.binary(op, a, b, scope),
1610            Node::Call(name, args, _) => self.call(name, args, scope),
1611            Node::Macro(f, of, var, body, _) => {
1612                let of = self.need(of, scope)?;
1613                if !of.is_array() && !loose(&of) {
1614                    return Err(format!("{f} needs a list, got {}", type_display(&of)));
1615                }
1616                let item = of.as_array().and_then(|a| a.first().cloned()).unwrap_or_else(|| ty("dyn"));
1617                let inner = TypeScope { name: var, ty: item, parent: scope };
1618                let body_t = self.tc(body, Some(&inner))?;
1619                if f == "map" {
1620                    return Ok(Value::Array(vec![body_t]));
1621                }
1622                if is_optional(&body_t) {
1623                    return missing(body);
1624                }
1625                if !loose(&body_t) && !is(&body_t, "bool") {
1626                    return Err(format!("{f} needs a bool, got {}", type_display(&body_t)));
1627                }
1628                Ok(if f == "filter" {
1629                    if of.is_array() {
1630                        of
1631                    } else {
1632                        Value::Array(vec![ty("dyn")])
1633                    }
1634                } else {
1635                    ty("bool")
1636                })
1637            }
1638        }
1639    }
1640
1641    fn binary(&self, op: &str, a: &Node, b: &Node, scope: Option<&TypeScope>) -> R<Value> {
1642        if op == "&&" || op == "||" {
1643            let x = self.need(a, scope)?;
1644            let (yes, no) = guards(a);
1645            let y = self.within(if op == "&&" { yes } else { no }, b, scope)?;
1646            if is_optional(&y) {
1647                return missing(b);
1648            }
1649            for t in [&x, &y] {
1650                if !loose(t) && !is(t, "bool") {
1651                    return Err(format!("cannot apply {op} to {}", type_display(t)));
1652                }
1653            }
1654            return Ok(ty("bool"));
1655        }
1656        if op == "==" || op == "!=" {
1657            // The null check: either side may be missing.
1658            let x = required_type(&self.tc(a, scope)?);
1659            let y = required_type(&self.tc(b, scope)?);
1660            let (dx, dy) = (type_display(&x), type_display(&y));
1661            let comparable = loose(&x) || loose(&y) || (is_textual(&x) && is_textual(&y)) || dx == dy;
1662            if !comparable {
1663                return Err(format!("cannot compare {dx} and {dy}"));
1664            }
1665            return Ok(ty("bool"));
1666        }
1667        let x = self.need(a, scope)?;
1668        let y = self.need(b, scope)?;
1669        let (dx, dy) = (type_display(&x), type_display(&y));
1670        let numeric = |t: &Value| is(t, "number") || loose(t);
1671        match op {
1672            "<" | "<=" | ">" | ">=" => {
1673                let ok = |t: &Value| loose(t) || is(t, "number") || is_textual(t);
1674                if !ok(&x) || !ok(&y) || (is(&x, "number") && is_textual(&y)) || (is(&y, "number") && is_textual(&x)) {
1675                    return Err(format!("cannot compare {dx} and {dy}"));
1676                }
1677                Ok(ty("bool"))
1678            }
1679            "+" => {
1680                if loose(&x) && loose(&y) {
1681                    return Ok(ty("dyn"));
1682                }
1683                if numeric(&x) && numeric(&y) {
1684                    return Ok(ty("number"));
1685                }
1686                if (is_textual(&x) || loose(&x)) && (is_textual(&y) || loose(&y)) {
1687                    return Ok(ty("string"));
1688                }
1689                if (x.is_array() || loose(&x)) && (y.is_array() || loose(&y)) {
1690                    return Ok(if x.is_array() && y.is_array() && x == y { x } else { Value::Array(vec![ty("dyn")]) });
1691                }
1692                Err(format!("cannot apply + to {dx} and {dy}"))
1693            }
1694            _ => {
1695                if !(numeric(&x) && numeric(&y)) {
1696                    return Err(format!("cannot apply {op} to {dx} and {dy}"));
1697                }
1698                if (op == "/" || op == "%") && literal(b).is_some_and(|v| v.as_f64() == Some(0.0)) {
1699                    return Err(if op == "/" { "division by zero" } else { "modulo by zero" }.into());
1700                }
1701                Ok(ty("number"))
1702            }
1703        }
1704    }
1705
1706    fn call(&self, name: &str, args: &[Node], scope: Option<&TypeScope>) -> R<Value> {
1707        let d = def(name).ok_or_else(|| format!("unknown function {name}"))?;
1708        if let Some(e) = arity_error(name, &d, args.len()) {
1709            return Err(e);
1710        }
1711        // orElse takes a value that may be missing; every other argument is used.
1712        let types = args.iter().enumerate().map(|(i, a)| if name == "orElse" && i == 0 { self.tc(a, scope) } else { self.need(a, scope) }).collect::<R<Vec<_>>>()?;
1713        for (i, t) in types.iter().enumerate() {
1714            let param = d.params[i.min(d.params.len() - 1)];
1715            if !accepts(param, &required_type(t)) {
1716                return Err(format!("{name} needs {}, got {}", param_name(param), type_display(t)));
1717            }
1718        }
1719        if name == "orElse" {
1720            let (x, y) = (required_type(&types[0]), &types[1]);
1721            if !loose(&x) && !loose(y) && !(is_textual(&x) && is_textual(y)) && type_display(&x) != type_display(y) {
1722                return Err(format!("orElse needs a {} default, got {}", type_display(&x), type_display(y)));
1723            }
1724        }
1725        let lit = |i: usize| args.get(i).and_then(literal);
1726        let unit_at = match name {
1727            "dateAdd" | "dateDiff" => Some(2),
1728            "startOf" | "endOf" => Some(1),
1729            _ => None,
1730        };
1731        if let Some(Value::String(unit)) = unit_at.and_then(lit) {
1732            let units = if unit_at == Some(2) { DATE_UNITS } else { CALENDAR_UNITS };
1733            if !units.contains(&unit.as_str()) {
1734                return Err(format!("unknown unit {unit}"));
1735            }
1736            if name == "dateAdd" && is(&types[0], "date") && unit_ms(unit).is_some() {
1737                return Err(format!("unit {unit} needs a timestamp"));
1738            }
1739        }
1740        if name == "dateAdd" {
1741            if let Some(n) = lit(1).and_then(Value::as_f64) {
1742                if n.fract() != 0.0 {
1743                    return Err(format!("dateAdd needs a whole number, got {}", num_text(n)));
1744                }
1745            }
1746        }
1747        if name == "round" && args.len() > 1 {
1748            if let Some(d) = lit(1).and_then(Value::as_f64) {
1749                if d.fract() != 0.0 || !(0.0..=10.0).contains(&d) {
1750                    return Err("round needs 0 to 10 digits".into());
1751                }
1752            }
1753        }
1754        if name == "div" && lit(1).and_then(Value::as_f64) == Some(0.0) {
1755            return Err("division by zero".into());
1756        }
1757        if name == "mod" && lit(1).and_then(Value::as_f64) == Some(0.0) {
1758            return Err("modulo by zero".into());
1759        }
1760        if name == "formatDuration" {
1761            if let Some(Value::String(f)) = lit(1) {
1762                if !DURATION_FORMATS.contains(&f.as_str()) {
1763                    return Err(format!("unknown duration format {f}"));
1764                }
1765            }
1766        }
1767        Ok(match name {
1768            "dateAdd" | "startOf" | "endOf" => {
1769                if is(&types[0], "date") || is(&types[0], "timestamp") {
1770                    types[0].clone()
1771                } else {
1772                    ty("dyn")
1773                }
1774            }
1775            "today" => ty("date"),
1776            "now" => ty("timestamp"),
1777            "formatDuration" => ty("string"),
1778            "orElse" => {
1779                let x = required_type(&types[0]);
1780                if is(&x, "null") {
1781                    required_type(&types[1])
1782                } else {
1783                    x
1784                }
1785            }
1786            _ => ty("number"),
1787        })
1788    }
1789}
1790
1791/// Type-check an expression against the names it may read (a kind's
1792/// fields, a binding's value): the type, or the error (the parse error, or
1793/// the first type error). Types are spelled as JSON (`"number"`, `[T]`, a
1794/// row object, `{"?": T}` for a value that may be missing).
1795pub fn check_expression(source: &str, types: &Map<String, Value>) -> Result<Value, String> {
1796    let ast = parse_expression(source)?;
1797    Checker { types, present: std::cell::RefCell::new(Vec::new()) }.tc(&ast, None)
1798}