Lines
97.83 %
Functions
100 %
Branches
//! `LET` / `LET*` eval-only handlers plus `eval_body` — the body
//! sequencing shared by every binding form's tail. `eval_body` is the
//! piece that walks the body for accumulator setf chains so the
//! promoted runtime type matches the codegen path's `PairElement`.
use crate::ast::{Expr, WasmType};
use crate::compiler::binder_key::Surface;
use crate::compiler::expr::eval_value;
use crate::compiler::special::control::ends_live_code;
use crate::error::Result;
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
use super::dynamic::{dynamic_home, reject_missed_specials};
use super::infer::{infer_runtime_type_with_body, promote_assigned_binding, sibling_scope};
use super::parse::{reject_duplicate_names, split_let};
use super::shared::{star_scope, unescaped};
pub(super) fn let_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
let (bindings, keys, body) = split_let(symbols, Surface::Eval, "LET", false, args)?;
reject_duplicate_names("variable", "LET", bindings.iter().map(|(name, _)| name))?;
reject_missed_specials(symbols, bindings.iter().map(|(name, _)| name.as_str()))?;
let inits: Vec<Expr> = bindings
.iter()
.map(|(_, init)| {
init.as_ref()
.map_or(Ok(Expr::Nil), |e| eval_value(symbols, e))
})
.collect::<Result<_>>()?;
let sizing = sibling_scope(symbols, &bindings, inits.iter().cloned());
let resolved: Vec<(String, Option<Expr>)> = bindings
.into_iter()
.zip(&keys)
.zip(inits)
.map(|(((name, _), key), raw)| {
if dynamic_home(symbols, &name).is_some() {
return Ok((name, None));
}
// A let-var the body mutates classifies as runtime, as in codegen.
let table = sizing.as_ref().unwrap_or(symbols);
let val = promote_assigned_binding(body, &name, as_binding_datum(raw), key, table)?;
Ok((name, Some(val)))
let names: Vec<String> = resolved.iter().map(|(name, _)| name.clone()).collect();
symbols.push_scope();
for (name, val) in resolved {
if let Some(val) = val {
symbols.define_scoped(Symbol::new(&name, SymbolKind::Variable).with_value(val));
let value = eval_body(symbols, body).map(|value| unescaped(symbols, &names, value));
symbols.pop_scope()?;
value
pub(super) fn let_star(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
let (bindings, keys, body) = split_let(symbols, Surface::Eval, "LET*", true, args)?;
let names: Vec<String> = bindings.iter().map(|(name, _)| name.clone()).collect();
let value = bindings
.enumerate()
.try_for_each(|((at, (name, init)), key)| {
let val = match init {
Some(expr) => as_binding_datum(eval_value(symbols, expr)?),
None => Expr::Nil,
};
let scope: Vec<Expr> = star_scope(&bindings, at, body, true)
.cloned()
.collect();
let val = promote_assigned_binding(&scope, name, val, key, symbols)?;
symbols.define_scoped(Symbol::new(name, SymbolKind::Variable).with_value(val));
Ok(())
.and_then(|()| eval_body(symbols, body))
.map(|value| unescaped(symbols, &names, value));
/// A binding holds data: a bare symbol an init evaluates to is bound quoted,
/// so a read of the variable is that symbol and never a read of the variable
/// it names (0014 obligation (f)).
pub(super) fn as_binding_datum(value: Expr) -> Expr {
match value {
symbol @ Expr::Symbol(_) => Expr::quoted(symbol),
other => other,
pub(in crate::compiler) fn eval_body(symbols: &mut SymbolTable, body: &[Expr]) -> Result<Expr> {
let dead = symbols.is_dead_code();
let result = eval_body_forms(symbols, body);
symbols.set_dead_code(dead);
result
fn eval_body_forms(symbols: &mut SymbolTable, body: &[Expr]) -> Result<Expr> {
let Some((result_expr, init)) = body.split_last() else {
return Ok(Expr::Nil);
let mut has_runtime = false;
for expr in init {
let exits = symbols.measurements().exits();
let val = eval_value(symbols, expr)?;
if val.is_wasm_runtime() {
has_runtime = true;
if ends_live_code(symbols, expr, exits)? {
symbols.set_dead_code(true);
let result = eval_value(symbols, result_expr)?;
if has_runtime && !result.is_wasm_runtime() {
// An accumulator tail takes the cons car's element type, as codegen does.
let ty: WasmType = infer_runtime_type_with_body(&result, result_expr, body, symbols);
return Ok(Expr::WasmRuntime(ty));
Ok(result)