1
//! Reflective / introspection special forms — `COMPILE`, `EVAL`,
2
//! `DESCRIBE`, `PP`, `APROPOS`, `DEFTEST`, `ASSERT-EQUAL`,
3
//! `RUN-TESTS`, `COVERAGE-DUMP`.
4
//!
5
//! Split by feature so each submodule stays under the ~500-line
6
//! CLAUDE.md guideline:
7
//! - [`compile_eval_forms`] — COMPILE + EVAL (handlers in both
8
//!   directions, plus their value-position counterparts).
9
//! - [`describe_pp`] — DESCRIBE + PP (both produce String summaries).
10
//! - [`apropos`] — symbol-table search returning a sorted quoted list.
11
//! - [`test_framework`] — DEFTEST + ASSERT-EQUAL + RUN-TESTS.
12
//! - [`coverage`] — COVERAGE-DUMP snapshot of native-fn references.
13

            
14
mod apropos;
15
mod compile_eval_forms;
16
mod coverage;
17
mod describe_pp;
18
mod folded;
19
pub(super) use folded::folded_form;
20
mod test_framework;
21

            
22
#[cfg(test)]
23
mod tests;
24

            
25
use crate::ast::{Expr, WasmType};
26
use crate::compiler::context::CompileContext;
27
use crate::compiler::emit::FunctionEmitter;
28
use crate::compiler::expr::compile_for_stack;
29
use crate::error::Result;
30
use crate::runtime::SymbolTable;
31

            
32
use super::SpecialFormSpec;
33

            
34
/// Compiles a constant-folded Expr result onto the wasm stack. PP / DESCRIBE /
35
/// RUN-TESTS return `Expr::String`; COMPILE / DEFTEST / APROPOS return a quoted
36
/// symbol or list, which lowers as the symbol or list cell it is.
37
14242
pub(in crate::compiler::special::compile_eval) fn compile_static_result_for_stack(
38
14242
    ctx: &mut CompileContext,
39
14242
    emit: &mut FunctionEmitter,
40
14242
    symbols: &mut SymbolTable,
41
14242
    result: &Expr,
42
14242
) -> Result<WasmType> {
43
14242
    compile_for_stack(ctx, emit, symbols, result)
44
14242
}
45

            
46
/// Compiles a `stack`-producing reflective form for effect position by
47
/// running its OWN stack compiler and dropping the result — never its
48
/// `compile` fallback, which is written for VALUE position and calls
49
/// `compile_expr`/`serialize_stack_to_output` (each of these forms is
50
/// nested inside something else in effect position, e.g. `(progn (eval
51
/// '(+ 1 2)) 5)`; using `compile` there would spuriously push the eval'd
52
/// value onto the RPC output stream ahead of the program's real result).
53
11076
fn compile_for_effect_via_stack(
54
11076
    stack_fn: super::StackFn,
55
11076
    ctx: &mut CompileContext,
56
11076
    emit: &mut FunctionEmitter,
57
11076
    symbols: &mut SymbolTable,
58
11076
    args: &[Expr],
59
11076
) -> Result<()> {
60
11076
    stack_fn(ctx, emit, symbols, args)?;
61
11073
    emit.drop_value();
62
11073
    Ok(())
63
11076
}
64

            
65
/// COMPILE has no runtime effect of its own (its "compiled" marker just
66
/// round-trips — see `compile_eval_forms`'s module doc): validate the
67
/// argument (surfacing e.g. an undefined-symbol error) and drop the
68
/// resulting marker, never compiling or serializing it. Unlike the other
69
/// reflective forms, COMPILE's `stack` path (`compile_compile_form_for_stack`)
70
/// isn't reusable here for effect position for free — see EVAL below for
71
/// why routing an ARBITRARY resolved form through a stack compiler is
72
/// unsafe in general; COMPILE happens to need no compile at all.
73
fn compile_compile_form_for_effect(
74
    _ctx: &mut CompileContext,
75
    _emit: &mut FunctionEmitter,
76
    symbols: &mut SymbolTable,
77
    args: &[Expr],
78
) -> Result<()> {
79
    compile_eval_forms::compile_form(symbols, args)?;
80
    Ok(())
81
}
82

            
83
/// EVAL resolves its argument to arbitrary code and re-runs the compiler on
84
/// it — that code can be ANY special form, including one with no `stack`
85
/// producer at all (e.g. `(eval '(defun x (a) (+ 1 a)))`, a plain
86
/// definition). `compile_eval_form_for_stack` demands a genuine stack value
87
/// and errors on those; the value-position `compile_eval_form` handles them
88
/// via `compile_expr`, but `compile_expr` unconditionally serializes a
89
/// value-producing result to the RPC output stream (right for EVAL's own
90
/// top-level/value use, wrong here — the result is being discarded). Effect
91
/// position instead re-dispatches the resolved code through
92
/// `expr::compile_for_effect` directly: the SAME "compile this form for its
93
/// effects, dropping any value, no output" contract this module's own
94
/// dispatcher already provides for every other special form.
95
1064
fn compile_eval_form_for_effect(
96
1064
    ctx: &mut CompileContext,
97
1064
    emit: &mut FunctionEmitter,
98
1064
    symbols: &mut SymbolTable,
99
1064
    args: &[Expr],
100
1064
) -> Result<()> {
101
1064
    if args.len() != 1 {
102
        return Err(crate::error::Error::Arity {
103
            name: "eval".to_string(),
104
            expected: 1,
105
            actual: args.len(),
106
        });
107
1064
    }
108
1064
    let inner = folded::folded_eval_form(symbols, &args[0])?;
109
1052
    crate::compiler::expr::compile_for_effect(ctx, emit, symbols, &inner)
110
1064
}
111

            
112
208
fn compile_describe_for_effect(
113
208
    ctx: &mut CompileContext,
114
208
    emit: &mut FunctionEmitter,
115
208
    symbols: &mut SymbolTable,
116
208
    args: &[Expr],
117
208
) -> Result<()> {
118
208
    compile_for_effect_via_stack(
119
208
        describe_pp::compile_describe_for_stack,
120
208
        ctx,
121
208
        emit,
122
208
        symbols,
123
208
        args,
124
208
    )
125
208
}
126

            
127
1664
fn compile_pp_for_effect(
128
1664
    ctx: &mut CompileContext,
129
1664
    emit: &mut FunctionEmitter,
130
1664
    symbols: &mut SymbolTable,
131
1664
    args: &[Expr],
132
1664
) -> Result<()> {
133
1664
    compile_for_effect_via_stack(describe_pp::compile_pp_for_stack, ctx, emit, symbols, args)
134
1664
}
135

            
136
217
fn compile_apropos_for_effect(
137
217
    ctx: &mut CompileContext,
138
217
    emit: &mut FunctionEmitter,
139
217
    symbols: &mut SymbolTable,
140
217
    args: &[Expr],
141
217
) -> Result<()> {
142
217
    compile_for_effect_via_stack(apropos::compile_apropos_for_stack, ctx, emit, symbols, args)
143
217
}
144

            
145
8779
fn compile_deftest_for_effect(
146
8779
    ctx: &mut CompileContext,
147
8779
    emit: &mut FunctionEmitter,
148
8779
    symbols: &mut SymbolTable,
149
8779
    args: &[Expr],
150
8779
) -> Result<()> {
151
8779
    compile_for_effect_via_stack(
152
8779
        test_framework::compile_deftest_for_stack,
153
8779
        ctx,
154
8779
        emit,
155
8779
        symbols,
156
8779
        args,
157
8779
    )
158
8779
}
159

            
160
fn compile_assert_equal_for_effect(
161
    ctx: &mut CompileContext,
162
    emit: &mut FunctionEmitter,
163
    symbols: &mut SymbolTable,
164
    args: &[Expr],
165
) -> Result<()> {
166
    compile_for_effect_via_stack(
167
        test_framework::compile_assert_equal_for_stack,
168
        ctx,
169
        emit,
170
        symbols,
171
        args,
172
    )
173
}
174

            
175
fn compile_run_tests_for_effect(
176
    ctx: &mut CompileContext,
177
    emit: &mut FunctionEmitter,
178
    symbols: &mut SymbolTable,
179
    args: &[Expr],
180
) -> Result<()> {
181
    compile_for_effect_via_stack(
182
        test_framework::compile_run_tests_for_stack,
183
        ctx,
184
        emit,
185
        symbols,
186
        args,
187
    )
188
}
189

            
190
208
fn compile_coverage_dump_for_effect(
191
208
    ctx: &mut CompileContext,
192
208
    emit: &mut FunctionEmitter,
193
208
    symbols: &mut SymbolTable,
194
208
    args: &[Expr],
195
208
) -> Result<()> {
196
208
    compile_for_effect_via_stack(
197
208
        coverage::compile_coverage_dump_for_stack,
198
208
        ctx,
199
208
        emit,
200
208
        symbols,
201
208
        args,
202
208
    )
203
208
}
204

            
205
pub(super) const FORMS: &[SpecialFormSpec] = &[
206
    SpecialFormSpec {
207
        name: "COMPILE",
208
        eval: compile_eval_forms::compile_form,
209
        compile: compile_eval_forms::compile_compile_form,
210
        stack: Some(compile_eval_forms::compile_compile_form_for_stack),
211
        effect: Some(compile_compile_form_for_effect),
212
    },
213
    SpecialFormSpec {
214
        name: "EVAL",
215
        eval: compile_eval_forms::eval_form,
216
        compile: compile_eval_forms::compile_eval_form,
217
        stack: Some(compile_eval_forms::compile_eval_form_for_stack),
218
        effect: Some(compile_eval_form_for_effect),
219
    },
220
    SpecialFormSpec {
221
        name: "DESCRIBE",
222
        eval: describe_pp::describe,
223
        compile: describe_pp::compile_describe,
224
        stack: Some(describe_pp::compile_describe_for_stack),
225
        effect: Some(compile_describe_for_effect),
226
    },
227
    SpecialFormSpec {
228
        name: "PP",
229
        eval: describe_pp::pp,
230
        compile: describe_pp::compile_pp,
231
        stack: Some(describe_pp::compile_pp_for_stack),
232
        effect: Some(compile_pp_for_effect),
233
    },
234
    SpecialFormSpec {
235
        name: "APROPOS",
236
        eval: apropos::apropos,
237
        compile: apropos::compile_apropos,
238
        stack: Some(apropos::compile_apropos_for_stack),
239
        effect: Some(compile_apropos_for_effect),
240
    },
241
    SpecialFormSpec {
242
        name: "DEFTEST",
243
        eval: test_framework::deftest,
244
        compile: test_framework::compile_deftest,
245
        stack: Some(test_framework::compile_deftest_for_stack),
246
        effect: Some(compile_deftest_for_effect),
247
    },
248
    SpecialFormSpec {
249
        name: "ASSERT-EQUAL",
250
        eval: test_framework::assert_equal,
251
        compile: test_framework::compile_assert_equal,
252
        stack: Some(test_framework::compile_assert_equal_for_stack),
253
        effect: Some(compile_assert_equal_for_effect),
254
    },
255
    SpecialFormSpec {
256
        name: "RUN-TESTS",
257
        eval: test_framework::run_tests,
258
        compile: test_framework::compile_run_tests,
259
        stack: Some(test_framework::compile_run_tests_for_stack),
260
        effect: Some(compile_run_tests_for_effect),
261
    },
262
    SpecialFormSpec {
263
        name: "COVERAGE-DUMP",
264
        eval: coverage::coverage_dump,
265
        compile: coverage::compile_coverage_dump,
266
        stack: Some(coverage::compile_coverage_dump_for_stack),
267
        effect: Some(compile_coverage_dump_for_effect),
268
    },
269
];