Lines
100 %
Functions
33.33 %
Branches
//! Shared lowering for a const-folded list-native result at value position.
//!
//! CAR/CDR/REVERSE/CONS/APPEND all const-fold a constant (or quoted) list
//! argument on the eval path; the folded result may be a self-evaluating atom, a
//! quoted tail, or a bare datum (`Symbol` / `List` / `Cons`) extracted from a
//! quoted source. A bare datum is DATA, not code, so it is re-quoted and lowered
//! by `compile_for_stack`'s quoted-datum arm — the one place a datum becomes its
//! printed `StringRef`.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::compile_for_stack;
use crate::error::Result;
use crate::runtime::SymbolTable;
/// Whether a const-folded result is quoted DATA (must render as a datum string)
/// rather than code/an atom: a bare `Symbol` / `List` / `Cons` extracted from a
/// quoted source, or a `Quote`-wrapped value.
#[must_use]
pub(super) fn is_datum_result(result: &Expr) -> bool {
matches!(
result,
Expr::Symbol(_) | Expr::List(_) | Expr::Cons(_, _) | Expr::Quote(_)
)
}
/// Lowers a const-folded list-native result to a single clean stack value.
pub(super) fn compile_folded_to_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
folded: Expr,
) -> Result<WasmType> {
match folded {
datum @ (Expr::Symbol(_) | Expr::List(_) | Expr::Cons(_, _)) => {
compile_for_stack(ctx, emit, symbols, &Expr::Quote(Box::new(datum)))
other => compile_for_stack(ctx, emit, symbols, &other),