Lines
96.15 %
Functions
100 %
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: a list as its `$pair` chain, a
//! symbol as its printed `StringRef`.
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::{compile_expr, compile_for_stack, serialize_stack_to_output};
use crate::error::Result;
use crate::runtime::SymbolTable;
/// Whether a const-folded result is quoted DATA (must lower as a datum)
/// rather than code/an atom: a bare `Symbol` / `List` / `Cons` extracted from a
/// quoted source, or a `Datum` value.
#[must_use]
pub(super) fn is_datum_result(result: &Expr) -> bool {
matches!(
result,
Expr::Symbol(_) | Expr::List(_) | Expr::Cons(_, _) | Expr::Datum(_)
)
}
/// 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::quoted(datum))
other => compile_for_stack(ctx, emit, symbols, &other),
/// Emits a const-folded list-native result to the output: a datum renders as
/// DATA, an atom lowers as itself.
pub(super) fn emit_folded_output(
) -> Result<()> {
if is_datum_result(&folded) {
let ty = compile_folded_to_stack(ctx, emit, symbols, folded)?;
return serialize_stack_to_output(ctx, emit, ty);
compile_expr(ctx, emit, symbols, &folded)