Lines
67.69 %
Functions
59.09 %
Branches
100 %
//! Reflective / introspection special forms — `COMPILE`, `EVAL`,
//! `DESCRIBE`, `PP`, `APROPOS`, `DEFTEST`, `ASSERT-EQUAL`,
//! `RUN-TESTS`, `COVERAGE-DUMP`.
//!
//! Split by feature so each submodule stays under the ~500-line
//! CLAUDE.md guideline:
//! - [`compile_eval_forms`] — COMPILE + EVAL (handlers in both
//! directions, plus their value-position counterparts).
//! - [`describe_pp`] — DESCRIBE + PP (both produce String summaries).
//! - [`apropos`] — symbol-table search returning a sorted quoted list.
//! - [`test_framework`] — DEFTEST + ASSERT-EQUAL + RUN-TESTS.
//! - [`coverage`] — COVERAGE-DUMP snapshot of native-fn references.
mod apropos;
mod compile_eval_forms;
mod coverage;
mod describe_pp;
mod folded;
pub(super) use folded::folded_form;
mod test_framework;
#[cfg(test)]
mod tests;
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;
use super::SpecialFormSpec;
/// Compiles a constant-folded Expr result onto the wasm stack. PP / DESCRIBE /
/// RUN-TESTS return `Expr::String`; COMPILE / DEFTEST / APROPOS return a quoted
/// symbol or list, which lowers as the symbol or list cell it is.
pub(in crate::compiler::special::compile_eval) fn compile_static_result_for_stack(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
result: &Expr,
) -> Result<WasmType> {
compile_for_stack(ctx, emit, symbols, result)
}
/// Compiles a `stack`-producing reflective form for effect position by
/// running its OWN stack compiler and dropping the result — never its
/// `compile` fallback, which is written for VALUE position and calls
/// `compile_expr`/`serialize_stack_to_output` (each of these forms is
/// nested inside something else in effect position, e.g. `(progn (eval
/// '(+ 1 2)) 5)`; using `compile` there would spuriously push the eval'd
/// value onto the RPC output stream ahead of the program's real result).
fn compile_for_effect_via_stack(
stack_fn: super::StackFn,
args: &[Expr],
) -> Result<()> {
stack_fn(ctx, emit, symbols, args)?;
emit.drop_value();
Ok(())
/// COMPILE has no runtime effect of its own (its "compiled" marker just
/// round-trips — see `compile_eval_forms`'s module doc): validate the
/// argument (surfacing e.g. an undefined-symbol error) and drop the
/// resulting marker, never compiling or serializing it. Unlike the other
/// reflective forms, COMPILE's `stack` path (`compile_compile_form_for_stack`)
/// isn't reusable here for effect position for free — see EVAL below for
/// why routing an ARBITRARY resolved form through a stack compiler is
/// unsafe in general; COMPILE happens to need no compile at all.
fn compile_compile_form_for_effect(
_ctx: &mut CompileContext,
_emit: &mut FunctionEmitter,
compile_eval_forms::compile_form(symbols, args)?;
/// EVAL resolves its argument to arbitrary code and re-runs the compiler on
/// it — that code can be ANY special form, including one with no `stack`
/// producer at all (e.g. `(eval '(defun x (a) (+ 1 a)))`, a plain
/// definition). `compile_eval_form_for_stack` demands a genuine stack value
/// and errors on those; the value-position `compile_eval_form` handles them
/// via `compile_expr`, but `compile_expr` unconditionally serializes a
/// value-producing result to the RPC output stream (right for EVAL's own
/// top-level/value use, wrong here — the result is being discarded). Effect
/// position instead re-dispatches the resolved code through
/// `expr::compile_for_effect` directly: the SAME "compile this form for its
/// effects, dropping any value, no output" contract this module's own
/// dispatcher already provides for every other special form.
fn compile_eval_form_for_effect(
if args.len() != 1 {
return Err(crate::error::Error::Arity {
name: "eval".to_string(),
expected: 1,
actual: args.len(),
});
let inner = folded::folded_eval_form(symbols, &args[0])?;
crate::compiler::expr::compile_for_effect(ctx, emit, symbols, &inner)
fn compile_describe_for_effect(
compile_for_effect_via_stack(
describe_pp::compile_describe_for_stack,
ctx,
emit,
symbols,
args,
)
fn compile_pp_for_effect(
compile_for_effect_via_stack(describe_pp::compile_pp_for_stack, ctx, emit, symbols, args)
fn compile_apropos_for_effect(
compile_for_effect_via_stack(apropos::compile_apropos_for_stack, ctx, emit, symbols, args)
fn compile_deftest_for_effect(
test_framework::compile_deftest_for_stack,
fn compile_assert_equal_for_effect(
test_framework::compile_assert_equal_for_stack,
fn compile_run_tests_for_effect(
test_framework::compile_run_tests_for_stack,
fn compile_coverage_dump_for_effect(
coverage::compile_coverage_dump_for_stack,
pub(super) const FORMS: &[SpecialFormSpec] = &[
SpecialFormSpec {
name: "COMPILE",
eval: compile_eval_forms::compile_form,
compile: compile_eval_forms::compile_compile_form,
stack: Some(compile_eval_forms::compile_compile_form_for_stack),
effect: Some(compile_compile_form_for_effect),
},
name: "EVAL",
eval: compile_eval_forms::eval_form,
compile: compile_eval_forms::compile_eval_form,
stack: Some(compile_eval_forms::compile_eval_form_for_stack),
effect: Some(compile_eval_form_for_effect),
name: "DESCRIBE",
eval: describe_pp::describe,
compile: describe_pp::compile_describe,
stack: Some(describe_pp::compile_describe_for_stack),
effect: Some(compile_describe_for_effect),
name: "PP",
eval: describe_pp::pp,
compile: describe_pp::compile_pp,
stack: Some(describe_pp::compile_pp_for_stack),
effect: Some(compile_pp_for_effect),
name: "APROPOS",
eval: apropos::apropos,
compile: apropos::compile_apropos,
stack: Some(apropos::compile_apropos_for_stack),
effect: Some(compile_apropos_for_effect),
name: "DEFTEST",
eval: test_framework::deftest,
compile: test_framework::compile_deftest,
stack: Some(test_framework::compile_deftest_for_stack),
effect: Some(compile_deftest_for_effect),
name: "ASSERT-EQUAL",
eval: test_framework::assert_equal,
compile: test_framework::compile_assert_equal,
stack: Some(test_framework::compile_assert_equal_for_stack),
effect: Some(compile_assert_equal_for_effect),
name: "RUN-TESTS",
eval: test_framework::run_tests,
compile: test_framework::compile_run_tests,
stack: Some(test_framework::compile_run_tests_for_stack),
effect: Some(compile_run_tests_for_effect),
name: "COVERAGE-DUMP",
eval: coverage::coverage_dump,
compile: coverage::compile_coverage_dump,
stack: Some(coverage::compile_coverage_dump_for_stack),
effect: Some(compile_coverage_dump_for_effect),
];