shape_runtime/const_eval.rs
1//! Const evaluator for metadata() handlers.
2//!
3//! Phase 1.B (ADR-006 §2.7.4 audit-accuracy ruling): the pre-bulldozer
4//! evaluator decoded `&ValueWord`s via tag-bit dispatch (`as_f64()`,
5//! `as_bool()`, `is_heap()`, `vw_equals`, `is_truthy`, ...) and
6//! constructed values via `ValueWord::from_*`. After `ValueWord`'s
7//! deletion the entire body is a deferred Phase 2c rebuild — the
8//! kind-threaded const evaluator constructs `KindedSlot`s directly
9//! from `Literal::*` arms (the kind is statically known per arm) and
10//! dispatches operators on the slot bits + the carried `NativeKind`.
11//!
12//! There are no external callers of `ConstEvaluator` outside this
13//! crate (verified via cross-crate grep) so this stub does not block
14//! shape-vm or shape-jit. The file is preserved (rather than deleted)
15//! because `lib.rs:29` re-exports the module path; deletion would
16//! require coordination with the `lib.rs` module list.
17
18use shape_ast::ast::Expr;
19use shape_ast::error::{Result, ShapeError};
20use shape_value::KindedSlot;
21use std::collections::HashMap;
22
23/// Const evaluator for metadata() handlers.
24#[derive(Debug, Clone, Default)]
25pub struct ConstEvaluator {
26 /// Annotation parameters available during evaluation.
27 params: HashMap<String, KindedSlot>,
28}
29
30impl ConstEvaluator {
31 /// Create a new const evaluator with no parameters.
32 pub fn new() -> Self {
33 Self::default()
34 }
35
36 /// Create a const evaluator with annotation parameters.
37 pub fn with_params(params: HashMap<String, KindedSlot>) -> Self {
38 Self { params }
39 }
40
41 /// Add an annotation parameter to the scope.
42 pub fn add_param(&mut self, name: String, value: KindedSlot) {
43 self.params.insert(name, value);
44 }
45
46 /// Add an annotation parameter to the scope (alias kept for API
47 /// compatibility — both forms now take a `KindedSlot`).
48 pub fn add_param_nb(&mut self, name: String, value: KindedSlot) {
49 self.params.insert(name, value);
50 }
51
52 /// Evaluate an expression as a const value.
53 ///
54 /// Phase 1.B: the kind-threaded const evaluator is deferred to
55 /// Phase 2c. Until then, eval returns a deferred error rather than
56 /// silently produce wrong values.
57 pub fn eval(&self, _expr: &Expr) -> Result<KindedSlot> {
58 Err(ShapeError::RuntimeError {
59 message: "ConstEvaluator: pending Phase 2c kind-threaded rebuild — see ADR-006 §2.7.4".to_string(),
60 location: None,
61 })
62 }
63
64 /// Evaluate an expression as a const `KindedSlot` (alias kept for
65 /// API compatibility with the deleted `eval_nb` shape).
66 pub fn eval_as_nb(&self, expr: &Expr) -> Result<KindedSlot> {
67 self.eval(expr)
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 // Pre-bulldozer tests of the evaluator covered the literal /
74 // arithmetic / object / array / identifier paths. The kind-threaded
75 // rebuild reintroduces them in Phase 2c.
76}