1
//! `DESCRIBE` and `PP` — symbol-introspection + pretty-print forms
2
//! that both produce a `String` summary.
3

            
4
use crate::ast::{Expr, WasmType};
5
use crate::compiler::context::CompileContext;
6
use crate::compiler::emit::FunctionEmitter;
7
use crate::compiler::expr::{compile_expr, eval_value, format_expr};
8
use crate::error::{Error, Result};
9
use crate::runtime::{SymbolKind, SymbolTable};
10

            
11
use super::compile_static_result_for_stack;
12
use super::folded::folded_symbol;
13

            
14
3132
pub(super) fn describe(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
15
3132
    if args.len() != 1 {
16
208
        return Err(Error::Arity {
17
208
            name: "DESCRIBE".to_string(),
18
208
            expected: 1,
19
208
            actual: args.len(),
20
208
        });
21
2924
    }
22
2924
    let name = match &args[0] {
23
624
        Expr::Symbol(s) => s.clone(),
24
2300
        operand => folded_symbol(symbols, "DESCRIBE", operand)?,
25
    };
26
2924
    let sym = symbols
27
2924
        .lookup(&name)
28
208
        .ok_or_else(|| Error::UndefinedSymbol(name.clone()))?;
29

            
30
2716
    let mut lines = Vec::new();
31
2716
    let kind_str = match sym.kind() {
32
208
        SymbolKind::Macro => "a macro",
33
2508
        _ if sym.function().is_some() => "a function",
34
416
        SymbolKind::Variable => "a variable",
35
624
        SymbolKind::Operator => "an operator",
36
220
        SymbolKind::Native => "a native function",
37
        SymbolKind::SpecialForm => "a special form",
38
        SymbolKind::Function => "a function",
39
    };
40
2716
    lines.push(format!("{name} is {kind_str}"));
41
2716
    if let Some(func) = sym.function() {
42
1456
        lines.push(format!("  Lambda: {}", format_expr(func)));
43
1260
    }
44
2716
    if let Some(val) = sym.value() {
45
416
        let type_name = match val {
46
            Expr::Nil => "Nil",
47
            Expr::T => "T",
48
416
            Expr::Number(_) => "Number",
49
            Expr::String(_) => "String",
50
            Expr::Symbol(_) => "Symbol",
51
            Expr::Lambda(_, _) => "Lambda",
52
            _ => "Compound",
53
        };
54
416
        lines.push(format!("  Value: {} ({})", format_expr(val), type_name));
55
2300
    }
56
2716
    if let Some(doc) = sym.doc() {
57
416
        lines.push(format!("  Documentation: \"{doc}\""));
58
2300
    }
59
2716
    Ok(Expr::String(lines.join("\n")))
60
3132
}
61

            
62
1876
pub(super) fn compile_describe(
63
1876
    ctx: &mut CompileContext,
64
1876
    emit: &mut FunctionEmitter,
65
1876
    symbols: &mut SymbolTable,
66
1876
    args: &[Expr],
67
1876
) -> Result<()> {
68
1876
    let result = describe(symbols, args)?;
69
1460
    compile_expr(ctx, emit, symbols, &result)
70
1876
}
71

            
72
/// `(pp v)` → a String containing the pretty-printed textual form
73
/// of `v`. The compile-side path constant-folds via [`format_expr`],
74
/// so the call returns an `Expr::String` directly. Runtime values
75
/// (`WasmRuntime` / `WasmLocal`) print as their type descriptor —
76
/// a runtime-side printer that walks the wasm value lands when the
77
/// host needs it; today the emacs/nms clients format the
78
/// `nomi-eval` return slot themselves.
79
3541
pub(super) fn pp(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
80
3541
    if args.len() != 1 {
81
418
        return Err(Error::Arity {
82
418
            name: "PP".to_string(),
83
418
            expected: 1,
84
418
            actual: args.len(),
85
418
        });
86
3123
    }
87
3123
    let resolved = eval_value(symbols, &args[0])?;
88
3123
    Ok(Expr::String(format_expr(&resolved)))
89
3541
}
90

            
91
1456
pub(super) fn compile_pp(
92
1456
    ctx: &mut CompileContext,
93
1456
    emit: &mut FunctionEmitter,
94
1456
    symbols: &mut SymbolTable,
95
1456
    args: &[Expr],
96
1456
) -> Result<()> {
97
1456
    let result = pp(symbols, args)?;
98
1040
    compile_expr(ctx, emit, symbols, &result)
99
1456
}
100

            
101
1253
pub(super) fn compile_describe_for_stack(
102
1253
    ctx: &mut CompileContext,
103
1253
    emit: &mut FunctionEmitter,
104
1253
    symbols: &mut SymbolTable,
105
1253
    args: &[Expr],
106
1253
) -> Result<WasmType> {
107
1253
    let result = describe(symbols, args)?;
108
1253
    compile_static_result_for_stack(ctx, emit, symbols, &result)
109
1253
}
110

            
111
2080
pub(super) fn compile_pp_for_stack(
112
2080
    ctx: &mut CompileContext,
113
2080
    emit: &mut FunctionEmitter,
114
2080
    symbols: &mut SymbolTable,
115
2080
    args: &[Expr],
116
2080
) -> Result<WasmType> {
117
2080
    let result = pp(symbols, args)?;
118
2080
    compile_static_result_for_stack(ctx, emit, symbols, &result)
119
2080
}