1
//! `LET` / `LET*` eval-only handlers plus `eval_body` — the body
2
//! sequencing shared by every binding form's tail. `eval_body` is the
3
//! piece that walks the body for accumulator setf chains so the
4
//! promoted runtime type matches the codegen path's `PairElement`.
5

            
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::binder_key::Surface;
8
use crate::compiler::expr::eval_value;
9
use crate::compiler::special::control::ends_live_code;
10
use crate::error::Result;
11
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
12

            
13
use super::dynamic::{dynamic_home, reject_missed_specials};
14
use super::infer::{infer_runtime_type_with_body, promote_assigned_binding, sibling_scope};
15
use super::parse::{reject_duplicate_names, split_let};
16
use super::shared::{star_scope, unescaped};
17

            
18
3526633
pub(super) fn let_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
19
3526633
    let (bindings, keys, body) = split_let(symbols, Surface::Eval, "LET", false, args)?;
20
3526424
    reject_duplicate_names("variable", "LET", bindings.iter().map(|(name, _)| name))?;
21
3591412
    reject_missed_specials(symbols, bindings.iter().map(|(name, _)| name.as_str()))?;
22
3526424
    let inits: Vec<Expr> = bindings
23
3526424
        .iter()
24
3638797
        .map(|(_, init)| {
25
3638797
            init.as_ref()
26
3638797
                .map_or(Ok(Expr::Nil), |e| eval_value(symbols, e))
27
3638797
        })
28
46163
        .collect::<Result<_>>()?;
29
3480261
    let sizing = sibling_scope(symbols, &bindings, inits.iter().cloned());
30
3480261
    let resolved: Vec<(String, Option<Expr>)> = bindings
31
3480261
        .into_iter()
32
3480261
        .zip(&keys)
33
3480261
        .zip(inits)
34
3592634
        .map(|(((name, _), key), raw)| {
35
3592634
            if dynamic_home(symbols, &name).is_some() {
36
22982
                return Ok((name, None));
37
3569652
            }
38
            // A let-var the body mutates classifies as runtime, as in codegen.
39
3569652
            let table = sizing.as_ref().unwrap_or(symbols);
40
3569652
            let val = promote_assigned_binding(body, &name, as_binding_datum(raw), key, table)?;
41
3569652
            Ok((name, Some(val)))
42
3592634
        })
43
        .collect::<Result<_>>()?;
44
3592634
    let names: Vec<String> = resolved.iter().map(|(name, _)| name.clone()).collect();
45
3480261
    symbols.push_scope();
46
3480261
    for (name, val) in resolved {
47
3592634
        if let Some(val) = val {
48
3569652
            symbols.define_scoped(Symbol::new(&name, SymbolKind::Variable).with_value(val));
49
22982
        }
50
    }
51
3480261
    let value = eval_body(symbols, body).map(|value| unescaped(symbols, &names, value));
52
3480261
    symbols.pop_scope()?;
53
3480261
    value
54
3526633
}
55

            
56
12560
pub(super) fn let_star(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
57
12560
    let (bindings, keys, body) = split_let(symbols, Surface::Eval, "LET*", true, args)?;
58
17915
    reject_missed_specials(symbols, bindings.iter().map(|(name, _)| name.as_str()))?;
59
12560
    symbols.push_scope();
60
17921
    let names: Vec<String> = bindings.iter().map(|(name, _)| name.clone()).collect();
61
12560
    let value = bindings
62
12560
        .iter()
63
12560
        .enumerate()
64
12560
        .zip(&keys)
65
17921
        .try_for_each(|((at, (name, init)), key)| {
66
17921
            let val = match init {
67
17921
                Some(expr) => as_binding_datum(eval_value(symbols, expr)?),
68
                None => Expr::Nil,
69
            };
70
17704
            let scope: Vec<Expr> = star_scope(&bindings, at, body, true)
71
17704
                .into_iter()
72
17704
                .cloned()
73
17704
                .collect();
74
17704
            let val = promote_assigned_binding(&scope, name, val, key, symbols)?;
75
17704
            symbols.define_scoped(Symbol::new(name, SymbolKind::Variable).with_value(val));
76
17704
            Ok(())
77
17921
        })
78
12343
        .and_then(|()| eval_body(symbols, body))
79
11501
        .map(|value| unescaped(symbols, &names, value));
80
12560
    symbols.pop_scope()?;
81
12560
    value
82
12560
}
83

            
84
/// A binding holds data: a bare symbol an init evaluates to is bound quoted,
85
/// so a read of the variable is that symbol and never a read of the variable
86
/// it names (0014 obligation (f)).
87
3592107
pub(super) fn as_binding_datum(value: Expr) -> Expr {
88
3592107
    match value {
89
215
        symbol @ Expr::Symbol(_) => Expr::quoted(symbol),
90
3591892
        other => other,
91
    }
92
3592107
}
93

            
94
4147231
pub(in crate::compiler) fn eval_body(symbols: &mut SymbolTable, body: &[Expr]) -> Result<Expr> {
95
4147231
    let dead = symbols.is_dead_code();
96
4147231
    let result = eval_body_forms(symbols, body);
97
4147231
    symbols.set_dead_code(dead);
98
4147231
    result
99
4147231
}
100

            
101
4147231
fn eval_body_forms(symbols: &mut SymbolTable, body: &[Expr]) -> Result<Expr> {
102
4147231
    let Some((result_expr, init)) = body.split_last() else {
103
44454
        return Ok(Expr::Nil);
104
    };
105
4102777
    let mut has_runtime = false;
106
4102777
    for expr in init {
107
384046
        let exits = symbols.measurements().exits();
108
384046
        let val = eval_value(symbols, expr)?;
109
373466
        if val.is_wasm_runtime() {
110
239729
            has_runtime = true;
111
133737
        }
112
373466
        if ends_live_code(symbols, expr, exits)? {
113
253
            symbols.set_dead_code(true);
114
373213
        }
115
    }
116
4092197
    let result = eval_value(symbols, result_expr)?;
117
988128
    if has_runtime && !result.is_wasm_runtime() {
118
        // An accumulator tail takes the cons car's element type, as codegen does.
119
53714
        let ty: WasmType = infer_runtime_type_with_body(&result, result_expr, body, symbols);
120
53714
        return Ok(Expr::WasmRuntime(ty));
121
934414
    }
122
934414
    Ok(result)
123
4147231
}