1
use crate::ast::{Expr, Program};
2
use crate::compiler::expr::{eval_value, format_expr, format_form};
3
use crate::error::{Error, Result};
4
use crate::runtime::{SymbolTable, Value};
5

            
6
13936
pub fn eval_program(symbols: &mut SymbolTable, program: &Program) -> Result<Value> {
7
    // Starts a fresh compile-time work budget (ADR-0034 / plan 0011 Phase
8
    // 0) for THIS call. Without this, a caller that reuses one `SymbolTable`
9
    // across many `eval_program` calls — e.g. the `nms` interpreter's REPL,
10
    // which holds `self.symbols` for the whole session — would accumulate
11
    // charges across every prior call and eventually reject a small,
12
    // perfectly ordinary form once the historical total crossed
13
    // `MAX_ELAB_WORK`, matching neither this call's own cost nor any
14
    // reasonable per-call budget. `Compiler::compile_script` /
15
    // `compile_eval` already reset per top-level compile for the same
16
    // reason.
17
13936
    symbols.reset_budget();
18
13936
    crate::compiler::expr::reject_unindexable_literals(&program.exprs)?;
19
13936
    let Some((last, init)) = program.exprs.split_last() else {
20
        return Ok(Value::Nil);
21
    };
22
13936
    for expr in init {
23
1664
        eval_value(symbols, expr)?;
24
    }
25
13936
    let value = eval_value(symbols, last)?;
26
11856
    expr_to_value(&value)
27
13936
}
28

            
29
11857
fn expr_to_value(expr: &Expr) -> Result<Value> {
30
11857
    match expr {
31
1664
        Expr::Nil => Ok(Value::Nil),
32
832
        Expr::T => Ok(Value::T),
33
4992
        Expr::Number(n) => Ok(Value::Number(*n)),
34
624
        Expr::String(s) => Ok(Value::String(s.clone())),
35
        Expr::Symbol(s) => Ok(Value::Symbol(s.clone())),
36
        Expr::Bytes(b) => Ok(Value::Bytes(b.clone())),
37
3536
        Expr::Datum(inner) => quoted_to_value(inner),
38
        Expr::List(_) => Ok(Value::String(format_expr(expr))),
39
        Expr::Lambda(_, _) => Ok(Value::String(format_expr(expr))),
40
        Expr::RuntimeValue(val) => Ok(val.clone()),
41
209
        _ => Err(Error::Compile(format!(
42
209
            "unsupported expression: {}",
43
209
            format_form(expr)
44
209
        ))),
45
    }
46
11857
}
47

            
48
3537
fn quoted_to_value(inner: &Expr) -> Result<Value> {
49
3537
    match inner {
50
        Expr::Nil => Ok(Value::Nil),
51
        Expr::T => Ok(Value::T),
52
        Expr::Number(n) => Ok(Value::Number(*n)),
53
        Expr::String(s) => Ok(Value::String(s.clone())),
54
208
        Expr::Symbol(s) => Ok(Value::Symbol(s.clone())),
55
        Expr::Bytes(b) => Ok(Value::Bytes(b.clone())),
56
3328
        Expr::List(_) | Expr::Cons(_, _) => Ok(Value::String(format_expr(inner))),
57
1
        _ => Err(Error::Compile(format!(
58
1
            "unsupported quoted expression: {}",
59
1
            format_form(inner)
60
1
        ))),
61
    }
62
3537
}
63

            
64
#[cfg(test)]
65
#[path = "eval_tests.rs"]
66
mod tests;