Lines
73.17 %
Functions
83.33 %
Branches
100 %
use crate::ast::{Expr, Program};
use crate::compiler::expr::{eval_value, format_expr, format_form};
use crate::error::{Error, Result};
use crate::runtime::{SymbolTable, Value};
pub fn eval_program(symbols: &mut SymbolTable, program: &Program) -> Result<Value> {
// Starts a fresh compile-time work budget (ADR-0034 / plan 0011 Phase
// 0) for THIS call. Without this, a caller that reuses one `SymbolTable`
// across many `eval_program` calls — e.g. the `nms` interpreter's REPL,
// which holds `self.symbols` for the whole session — would accumulate
// charges across every prior call and eventually reject a small,
// perfectly ordinary form once the historical total crossed
// `MAX_ELAB_WORK`, matching neither this call's own cost nor any
// reasonable per-call budget. `Compiler::compile_script` /
// `compile_eval` already reset per top-level compile for the same
// reason.
symbols.reset_budget();
crate::compiler::expr::reject_unindexable_literals(&program.exprs)?;
let Some((last, init)) = program.exprs.split_last() else {
return Ok(Value::Nil);
};
for expr in init {
eval_value(symbols, expr)?;
}
let value = eval_value(symbols, last)?;
expr_to_value(&value)
fn expr_to_value(expr: &Expr) -> Result<Value> {
match expr {
Expr::Nil => Ok(Value::Nil),
Expr::T => Ok(Value::T),
Expr::Number(n) => Ok(Value::Number(*n)),
Expr::String(s) => Ok(Value::String(s.clone())),
Expr::Symbol(s) => Ok(Value::Symbol(s.clone())),
Expr::Bytes(b) => Ok(Value::Bytes(b.clone())),
Expr::Datum(inner) => quoted_to_value(inner),
Expr::List(_) => Ok(Value::String(format_expr(expr))),
Expr::Lambda(_, _) => Ok(Value::String(format_expr(expr))),
Expr::RuntimeValue(val) => Ok(val.clone()),
_ => Err(Error::Compile(format!(
"unsupported expression: {}",
format_form(expr)
))),
fn quoted_to_value(inner: &Expr) -> Result<Value> {
match inner {
Expr::List(_) | Expr::Cons(_, _) => Ok(Value::String(format_expr(inner))),
"unsupported quoted expression: {}",
format_form(inner)
#[cfg(test)]
#[path = "eval_tests.rs"]
mod tests;