sui-eval 0.1.156

Clean-room Nix language evaluator — lazy tree-walker + bytecode VM with construction-guaranteed Lazy<T>
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Clean-room Nix language evaluator.
//!
//! Architecture:
//! Nix source → rnix parser (CST) → Evaluator (values)
//!
//! Parsing is delegated to the `rnix` crate (MIT).

use std::rc::Rc;

/// Core Nix builtins (90+ functions).
pub mod builtins;
/// Bidirectional conversion between bytecode VM values and tree-walker values.
pub mod convert;
/// Tree-walking evaluator using rnix's typed AST.
pub mod eval;
/// Content-addressed derivation path cache (redb-backed).
pub mod drv_cache;
/// Content-addressed evaluation cache (file hash + lock hash → result).
pub mod eval_cache;
/// Content-addressed input fetcher for flake.lock resolved inputs.
pub mod fetcher;
/// Native flake lock management — update, check, write.
pub mod flake_lock;
/// Pure-Rust git operations via gix/gitoxide (no CLI spawning, no C deps).
pub mod git;
/// Centralized path resolution (normalize, resolve relative, import).
pub mod path;
/// Source positions for `builtins.unsafeGetAttrPos` / `__curPos`.
pub mod pos;
/// Lightweight evaluation profiling counters.
pub mod perf;
/// ENV-RESOLVE M0 flag + per-source resolution-table plumbing (the
/// tree-walker's consume side of the `sui-resolve` side-table).
pub mod resolve_env;
/// Infinite recursion debugging tools (force chain, trace, depth limit, stats).
pub mod trace;
/// Nix value types, environments, thunks, and error types.
pub mod value;
/// Lazy evaluation primitives — making accidental eagerness impossible.
pub mod lazy;
/// Import-from-derivation: realize a derivation output mid-eval via a
/// binary-installed hook (the pure evaluator owns no build pipeline).
pub mod realize;

/// The normalized differential render (deep-forcing, error-propagating) the
/// sui↔sui differential + the `SUI_IR` shadow-eval latch byte-compare against
/// `eval_ir`. Must stay format-locked with `sui_ir::render`.
pub mod render;

/// Re-export flake lock types from sui-compat where they canonically live.
pub mod flake {
    pub use sui_compat::flake::*;
}

/// Evaluate a Nix expression string (convenience re-export).
pub use eval::{eval, eval_with_file};
/// Re-exported for ergonomic access from dependent crates.
pub use value::{EvalError, Value};

/// The evaluator trait — enables swapping evaluation strategies.
///
/// Implementations: tree-walking (current), bytecode VM (future),
/// delegation to external `nix eval` (fallback during transition).
pub trait Evaluator {
    /// Evaluate a Nix expression string.
    fn eval_expr(&self, input: &str) -> Result<Value, EvalError>;

    /// Evaluate a Nix file.
    fn eval_file(&self, path: &std::path::Path) -> Result<Value, EvalError>;
}

/// The default tree-walking evaluator.
pub struct TreeWalkEvaluator;

impl Evaluator for TreeWalkEvaluator {
    fn eval_expr(&self, input: &str) -> Result<Value, EvalError> {
        eval(input)
    }

    fn eval_file(&self, path: &std::path::Path) -> Result<Value, EvalError> {
        let source = std::fs::read_to_string(path)
            .map_err(|e| EvalError::IoError {
                context: format!("eval_file: {}", path.display()),
                message: e.to_string(),
            })?;
        let path_buf = path.to_path_buf();
        let _guard = eval::push_eval_file(path_buf.clone());
        eval::eval_with_file(&source, Some(path_buf))
    }
}

/// Bytecode VM evaluator — compiles to bytecode and executes on the stack VM.
///
/// Installs a flake resolver that delegates `builtins.getFlake` to the
/// tree-walker's [`builtins::evaluate_flake`], so the VM gets correct
/// flake input resolution for all input types (GitHub, path, indirect).
pub struct BytecodeEvaluator;

impl BytecodeEvaluator {
    /// Run a bytecode evaluation with tree-walker bridges installed.
    ///
    /// Installs two bridges before running the VM:
    /// 1. **Flake resolver** — delegates `builtins.getFlake` to the tree-walker
    /// 2. **Builtin bridge** — delegates missing builtins (getEnv, match, split,
    ///    fromTOML, genericClosure, etc.) to tree-walker implementations
    fn eval_with_flake_resolver(input: &str) -> Result<Value, EvalError> {
        // Install flake resolver: tree-walker evaluate_flake → StringKeyedValue
        let _flake_guard = sui_bytecode::set_flake_resolver(Box::new(|flake_ref: &str| {
            let flake_dir = if flake_ref.starts_with('/') || flake_ref.starts_with('.') {
                std::path::PathBuf::from(flake_ref)
            } else if let Some(path) = flake_ref.strip_prefix("path:") {
                std::path::PathBuf::from(path)
            } else {
                return Err(format!("unsupported flake reference: {flake_ref}"));
            };

            let result = builtins::evaluate_flake(&flake_dir)
                .map_err(|e| e.to_string())?;

            // Convert tree-walker Value → StringKeyedValue for the VM.
            Ok(eval_to_string_keyed(&result))
        }));

        // Install builtin bridge: VM builtins → tree-walker builtins
        let _bridge_guard = sui_bytecode::set_builtin_bridge(Box::new(
            |name: &str, args: Vec<sui_bytecode::StringKeyedValue>| {
                // Special case: __import — the VM compiler couldn't handle
                // this file, so fall back to the tree-walker evaluator.
                if name == "__import" {
                    let path_str = match &args[0] {
                        sui_bytecode::StringKeyedValue::Path(p)
                        | sui_bytecode::StringKeyedValue::String(p) => p.clone(),
                        _ => return Err("__import: expected path or string argument".to_string()),
                    };
                    let path = std::path::Path::new(&path_str);
                    let source = std::fs::read_to_string(path)
                        .map_err(|e| format!("__import: {}: {e}", path.display()))?;
                    let path_buf = path.to_path_buf();
                    let _guard = eval::push_eval_file(path_buf.clone());
                    let result = eval::eval_with_file(&source, Some(path_buf))
                        .map_err(|e| e.to_string())?;
                    // Force the top-level result before converting — if the
                    // tree-walker returned a thunk, the VM would see
                    // "expected set, got thunk" when accessing attrs.
                    let forced = eval::force_value(&result)
                        .map_err(|e| e.to_string())?;
                    return Ok(eval_to_string_keyed(&forced));
                }

                // Convert StringKeyedValue args → tree-walker Value
                let eval_args: Vec<Value> = args
                    .iter()
                    .map(|a| convert::string_keyed_to_eval(a))
                    .collect();

                // Call the tree-walker builtin
                let result = builtins::call_builtin_by_name(name, &eval_args)
                    .map_err(|e| e.to_string())?;

                // Force the result before converting — builtins may return
                // thunks that the VM cannot handle directly.
                let forced = eval::force_value(&result)
                    .map_err(|e| e.to_string())?;

                // Convert tree-walker Value → StringKeyedValue
                Ok(eval_to_string_keyed(&forced))
            },
        ));

        match sui_bytecode::eval_full(input) {
            Ok(result) => Ok(convert::string_keyed_to_eval(&result.to_string_keyed())),
            Err(sui_bytecode::EvalError::Compile(c)) => {
                // Compilation failed — fall back to tree-walker entirely.
                eprintln!("[sui-vm] top-level compile fallback: {c}");
                eval::eval(input)
            }
            Err(sui_bytecode::EvalError::Runtime(r)) => {
                // Runtime error — fall back to tree-walker entirely.
                // This handles VM bugs (GetLocal slot mismatch, etc.)
                // that don't affect correctness of the tree-walker.
                eprintln!("[sui-vm] top-level runtime fallback: {r}");
                eval::eval(input)
            }
        }
    }
}

/// Convert a tree-walker `Value` to a `StringKeyedValue` (no interner needed).
///
/// Used by the flake resolver bridge to convert tree-walker results
/// into a format the bytecode VM can consume.
///
/// **Lazy thunk handling:** Tree-walker thunks are NOT eagerly forced.
/// Instead, they are wrapped in `StringKeyedValue::Thunk` with a callback
/// that forces the underlying tree-walker thunk on demand. This is critical
/// for `getFlake` performance: a typical flake has 100+ transitive input
/// thunks, and forcing them all would trigger git clones, recursive flake
/// resolution, and full evaluation of every dependency (10s+). By wrapping
/// lazily, only the inputs actually accessed by the expression are evaluated.
pub fn eval_to_string_keyed(val: &Value) -> sui_bytecode::StringKeyedValue {
    match val {
        Value::Null => sui_bytecode::StringKeyedValue::Null,
        Value::Bool(b) => sui_bytecode::StringKeyedValue::Bool(*b),
        Value::Int(n) => sui_bytecode::StringKeyedValue::Int(*n),
        Value::Float(f) => sui_bytecode::StringKeyedValue::Float(*f),
        Value::String(s) => sui_bytecode::StringKeyedValue::String(s.chars.to_string()),
        Value::Path(p) => sui_bytecode::StringKeyedValue::Path(p.to_string()),
        Value::List(items) => {
            sui_bytecode::StringKeyedValue::List(
                items.iter().map(eval_to_string_keyed).collect(),
            )
        }
        Value::Attrs(attrs) => {
            let mut map = std::collections::BTreeMap::new();
            for (k, v) in attrs.iter() {
                map.insert(k.clone(), eval_to_string_keyed(v));
            }
            sui_bytecode::StringKeyedValue::Attrs(map)
        }
        Value::Lambda(closure) => {
            // Wrap in Rc so the Fn closure captures a shared pointer
            // rather than an owned Closure.  Each invocation clones out
            // of the Rc (all Closure fields are refcounted, so O(1)),
            // but the capture itself is just an Rc bump — no redundant
            // outer+inner double-clone.
            let closure_rc = std::rc::Rc::new((**closure).clone());
            sui_bytecode::StringKeyedValue::Callable(std::rc::Rc::new(move |arg| {
                let eval_arg = convert::string_keyed_to_eval(&arg);
                let func = Value::Lambda(Rc::new((*closure_rc).clone()));
                let result = eval::apply(func, eval_arg)
                    .map_err(|e| e.to_string())?;
                let forced = eval::force_value(&result)
                    .map_err(|e| e.to_string())?;
                Ok(eval_to_string_keyed(&forced))
            }))
        }
        Value::Builtin(bf) => {
            // Same Rc pattern as Lambda above — BuiltinFn::clone() is
            // already cheap (static str + Rc bump on func), but the Rc
            // wrapper avoids the misleading double-clone.
            let bf_rc = std::rc::Rc::new((**bf).clone());
            sui_bytecode::StringKeyedValue::Callable(std::rc::Rc::new(move |arg| {
                let eval_arg = convert::string_keyed_to_eval(&arg);
                let func = Value::Builtin(Box::new((*bf_rc).clone()));
                let result = eval::apply(func, eval_arg)
                    .map_err(|e| e.to_string())?;
                let forced = eval::force_value(&result)
                    .map_err(|e| e.to_string())?;
                Ok(eval_to_string_keyed(&forced))
            }))
        }
        Value::Thunk(t) => {
            // Fast path: if already evaluated, convert the memoized value
            // without creating a thunk wrapper.
            if t.is_evaluated() {
                match t.force(&|e, env| eval::eval_expr(e, env)) {
                    Ok(v) => eval_to_string_keyed(&v),
                    Err(_) => sui_bytecode::StringKeyedValue::Null,
                }
            } else {
                // LAZY: Wrap the tree-walker thunk in a callback that
                // forces it on demand. The Rc clone is cheap and keeps
                // the thunk's memoization cell shared, so forcing once
                // caches the result for all subsequent accesses.
                let thunk_clone = t.clone();
                sui_bytecode::StringKeyedValue::Thunk(std::rc::Rc::new(move || {
                    let forced = thunk_clone
                        .force(&|e, env| eval::eval_expr(e, env))
                        .map_err(|e| e.to_string())?;
                    Ok(eval_to_string_keyed(&forced))
                }))
            }
        }
    }
}

impl Evaluator for BytecodeEvaluator {
    fn eval_expr(&self, input: &str) -> Result<Value, EvalError> {
        Self::eval_with_flake_resolver(input)
    }

    fn eval_file(&self, path: &std::path::Path) -> Result<Value, EvalError> {
        let source = std::fs::read_to_string(path)
            .map_err(|e| EvalError::IoError {
                context: format!("eval_file: {}", path.display()),
                message: e.to_string(),
            })?;
        self.eval_expr(&source)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct MockEvaluator(Result<Value, EvalError>);
    impl Evaluator for MockEvaluator {
        fn eval_expr(&self, _: &str) -> Result<Value, EvalError> {
            match &self.0 { Ok(v) => Ok(v.clone()), Err(_) => Err(EvalError::NotImplemented("mock".into())) }
        }
        fn eval_file(&self, _: &std::path::Path) -> Result<Value, EvalError> {
            self.eval_expr("")
        }
    }

    #[test]
    fn mock_evaluator_ok() {
        let e = MockEvaluator(Ok(Value::Int(42)));
        assert_eq!(e.eval_expr("anything").unwrap(), Value::Int(42));
    }

    #[test]
    fn mock_evaluator_err() {
        let e = MockEvaluator(Err(EvalError::NotImplemented("x".into())));
        assert!(e.eval_expr("anything").is_err());
    }

    #[test]
    fn tree_walk_evaluator() {
        let e = TreeWalkEvaluator;
        assert_eq!(e.eval_expr("1 + 2").unwrap(), Value::Int(3));
    }

    #[test]
    fn evaluator_trait_object_safe() {
        fn _assert(_: &dyn Evaluator) {}
    }

    // ── TreeWalkEvaluator through Evaluator trait ────────────

    #[test]
    fn tree_walk_eval_integer_arithmetic() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(e.eval_expr("2 + 3").unwrap(), Value::Int(5));
    }

    #[test]
    fn tree_walk_eval_string_literal() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr(r#""hello world""#).unwrap(),
            Value::string("hello world"),
        );
    }

    #[test]
    fn tree_walk_eval_boolean() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(e.eval_expr("true && false").unwrap(), Value::Bool(false));
    }

    #[test]
    fn tree_walk_eval_if_else() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("if true then 42 else 0").unwrap(),
            Value::Int(42),
        );
    }

    #[test]
    fn tree_walk_eval_let_binding() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("let x = 10; in x * 2").unwrap(),
            Value::Int(20),
        );
    }

    #[test]
    fn tree_walk_eval_attrset() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let val = e.eval_expr("{ a = 1; b = 2; }.a").unwrap();
        assert_eq!(val, Value::Int(1));
    }

    #[test]
    fn tree_walk_eval_list() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let val = e.eval_expr("[1 2 3]").unwrap();
        assert_eq!(
            val,
            Value::list(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
        );
    }

    #[test]
    fn tree_walk_eval_lambda_application() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("(x: x + 1) 5").unwrap(),
            Value::Int(6),
        );
    }

    #[test]
    fn tree_walk_eval_builtin_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("builtins.length [1 2 3]").unwrap(),
            Value::Int(3),
        );
    }

    #[test]
    fn tree_walk_eval_parse_error_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_expr("let in");
        assert!(result.is_err());
    }

    #[test]
    fn tree_walk_eval_null_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(e.eval_expr("null").unwrap(), Value::Null);
    }

    #[test]
    fn tree_walk_eval_file_missing() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_file(std::path::Path::new("/nonexistent/file.nix"));
        assert!(result.is_err());
    }

    #[test]
    fn tree_walk_eval_string_interpolation_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr(r#"let name = "world"; in "hello ${name}""#).unwrap(),
            Value::string("hello world"),
        );
    }

    #[test]
    fn tree_walk_eval_comparison_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(e.eval_expr("3 > 2").unwrap(), Value::Bool(true));
        assert_eq!(e.eval_expr("1 == 1").unwrap(), Value::Bool(true));
    }

    #[test]
    fn tree_walk_eval_recursive_attrset_via_trait() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("rec { x = 1; y = x + 1; }.y").unwrap(),
            Value::Int(2),
        );
    }

    // ── Re-exports & convenience eval shim ─────────────────

    #[test]
    fn re_export_eval_function_works() {
        // The top-level `eval` re-export from `eval::eval` should be
        // identical to the inner function.
        assert_eq!(eval("1 + 1").unwrap(), Value::Int(2));
    }

    #[test]
    fn re_export_value_and_error_types_constructible() {
        let v: Value = Value::Int(7);
        let e: EvalError = EvalError::UndefinedVar("x".into());
        assert_eq!(v.type_name(), "int");
        assert!(e.to_string().contains("undefined"));
    }

    // ── flake re-export from sui-compat ────────────────────

    #[test]
    fn flake_module_re_exports_compat_types() {
        // Smoke test that the flake submodule re-export compiles. We
        // reference a known type from the sui_compat::flake module via
        // our own re-export. Whatever types live there must be reachable.
        // We use the path explicitly to force compilation of the import.
        #[allow(unused_imports)]
        use crate::flake::*;
        // The block is intentionally empty: success is "this compiled".
    }

    // ── TreeWalkEvaluator passes path through ──────────────

    #[test]
    fn tree_walk_eval_file_with_real_temp_file() {
        let dir = std::env::temp_dir().join("sui-eval-test-tree-walk");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("simple.nix");
        std::fs::write(&path, "1 + 2").unwrap();
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_file(&path).unwrap();
        assert_eq!(result, Value::Int(3));
        let _ = std::fs::remove_file(&path);
        let _ = std::fs::remove_dir(&dir);
    }

    #[test]
    fn tree_walk_eval_file_propagates_io_error_kind() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_file(std::path::Path::new("/nonexistent/never/exists.nix"));
        match result {
            Err(EvalError::IoError { context, .. }) => {
                assert!(context.contains("eval_file"));
            }
            other => panic!("expected IoError, got {other:?}"),
        }
    }

    #[test]
    fn tree_walk_eval_file_parse_error_propagates() {
        let dir = std::env::temp_dir().join("sui-eval-test-tw-parse");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("bad.nix");
        std::fs::write(&path, "let in").unwrap();
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_file(&path);
        assert!(result.is_err());
        let _ = std::fs::remove_file(&path);
        let _ = std::fs::remove_dir(&dir);
    }

    // ── Mock evaluator additional ──────────────────────────

    #[test]
    fn mock_evaluator_dispatched_via_trait_object() {
        let m: Box<dyn Evaluator> = Box::new(MockEvaluator(Ok(Value::Bool(true))));
        let r = m.eval_expr("anything").unwrap();
        assert_eq!(r, Value::Bool(true));
    }

    #[test]
    fn mock_evaluator_eval_file_routes_through_eval_expr() {
        let m = MockEvaluator(Ok(Value::Int(1)));
        let r = m.eval_file(std::path::Path::new("/dev/null"));
        assert_eq!(r.unwrap(), Value::Int(1));
    }

    // ── Through-trait coverage of more constructs ──────────

    #[test]
    fn tree_walk_eval_function_with_default_args() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(
            e.eval_expr("({a, b ? 10}: a + b) {a = 5;}").unwrap(),
            Value::Int(15),
        );
    }

    #[test]
    fn tree_walk_eval_with_throws_propagated() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_expr(r#"builtins.throw "boom""#);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.is_throw());
    }

    #[test]
    fn tree_walk_eval_assert_failure() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_expr("assert false; 42");
        assert!(matches!(result, Err(EvalError::AssertionFailed(_))));
    }

    #[test]
    fn tree_walk_eval_division_by_zero() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_expr("1 / 0");
        assert!(matches!(result, Err(EvalError::DivisionByZero)));
    }

    #[test]
    fn tree_walk_eval_undefined_variable() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let result = e.eval_expr("nonexistent_xyz");
        assert!(matches!(result, Err(EvalError::UndefinedVar(_))));
    }

    #[test]
    fn tree_walk_eval_path_literal() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let v = e.eval_expr("/tmp/x").unwrap();
        assert!(matches!(v, Value::Path(_)));
    }

    #[test]
    fn tree_walk_eval_float_literal() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        assert_eq!(e.eval_expr("3.14").unwrap(), Value::Float(3.14));
    }

    #[test]
    fn tree_walk_eval_lambda_returns_lambda() {
        let e: &dyn Evaluator = &TreeWalkEvaluator;
        let v = e.eval_expr("x: x").unwrap();
        assert!(matches!(v, Value::Lambda(_)));
    }
}