1
//! Shared lowering for a const-folded list-native result at value position.
2
//!
3
//! CAR/CDR/REVERSE/CONS/APPEND all const-fold a constant (or quoted) list
4
//! argument on the eval path; the folded result may be a self-evaluating atom, a
5
//! quoted tail, or a bare datum (`Symbol` / `List` / `Cons`) extracted from a
6
//! quoted source. A bare datum is DATA, not code, so it is re-quoted and lowered
7
//! by `compile_for_stack`'s quoted-datum arm — the one place a datum becomes its
8
//! printed `StringRef`.
9

            
10
use crate::ast::{Expr, WasmType};
11
use crate::compiler::context::CompileContext;
12
use crate::compiler::emit::FunctionEmitter;
13
use crate::compiler::expr::compile_for_stack;
14
use crate::error::Result;
15
use crate::runtime::SymbolTable;
16

            
17
/// Whether a const-folded result is quoted DATA (must render as a datum string)
18
/// rather than code/an atom: a bare `Symbol` / `List` / `Cons` extracted from a
19
/// quoted source, or a `Quote`-wrapped value.
20
#[must_use]
21
2158
pub(super) fn is_datum_result(result: &Expr) -> bool {
22
747
    matches!(
23
2158
        result,
24
        Expr::Symbol(_) | Expr::List(_) | Expr::Cons(_, _) | Expr::Quote(_)
25
    )
26
2158
}
27

            
28
/// Lowers a const-folded list-native result to a single clean stack value.
29
2324
pub(super) fn compile_folded_to_stack(
30
2324
    ctx: &mut CompileContext,
31
2324
    emit: &mut FunctionEmitter,
32
2324
    symbols: &mut SymbolTable,
33
2324
    folded: Expr,
34
2324
) -> Result<WasmType> {
35
2324
    match folded {
36
332
        datum @ (Expr::Symbol(_) | Expr::List(_) | Expr::Cons(_, _)) => {
37
332
            compile_for_stack(ctx, emit, symbols, &Expr::Quote(Box::new(datum)))
38
        }
39
1992
        other => compile_for_stack(ctx, emit, symbols, &other),
40
    }
41
2324
}