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

            
13
830
pub(super) fn describe(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
14
830
    if args.len() != 1 {
15
83
        return Err(Error::Arity {
16
83
            name: "DESCRIBE".to_string(),
17
83
            expected: 1,
18
83
            actual: args.len(),
19
83
        });
20
747
    }
21
747
    let name = match &args[0] {
22
249
        Expr::Symbol(s) => s.clone(),
23
498
        Expr::Quote(inner) => match inner.as_ref() {
24
498
            Expr::Symbol(s) => s.clone(),
25
            _ => {
26
                return Err(Error::Compile(
27
                    "DESCRIBE: argument must be a symbol".to_string(),
28
                ));
29
            }
30
        },
31
        _ => {
32
            return Err(Error::Compile(
33
                "DESCRIBE: argument must be a symbol".to_string(),
34
            ));
35
        }
36
    };
37
747
    let sym = symbols
38
747
        .lookup(&name)
39
747
        .ok_or_else(|| Error::UndefinedSymbol(name.clone()))?;
40

            
41
664
    let mut lines = Vec::new();
42
664
    let kind_str = match sym.kind() {
43
83
        SymbolKind::Macro => "a macro",
44
581
        _ if sym.function().is_some() => "a function",
45
166
        SymbolKind::Variable => "a variable",
46
249
        SymbolKind::Operator => "an operator",
47
        SymbolKind::Native => "a native function",
48
        SymbolKind::SpecialForm => "a special form",
49
        SymbolKind::Function => "a function",
50
    };
51
664
    lines.push(format!("{name} is {kind_str}"));
52
664
    if let Some(func) = sym.function() {
53
249
        lines.push(format!("  Lambda: {}", format_expr(func)));
54
415
    }
55
664
    if let Some(val) = sym.value() {
56
166
        let type_name = match val {
57
            Expr::Nil => "Nil",
58
            Expr::T => "T",
59
166
            Expr::Number(_) => "Number",
60
            Expr::String(_) => "String",
61
            Expr::Symbol(_) => "Symbol",
62
            Expr::Lambda(_, _) => "Lambda",
63
            _ => "Compound",
64
        };
65
166
        lines.push(format!("  Value: {} ({})", format_expr(val), type_name));
66
498
    }
67
664
    if let Some(doc) = sym.doc() {
68
166
        lines.push(format!("  Documentation: \"{doc}\""));
69
498
    }
70
664
    Ok(Expr::String(lines.join("\n")))
71
830
}
72

            
73
664
pub(super) fn compile_describe(
74
664
    ctx: &mut CompileContext,
75
664
    emit: &mut FunctionEmitter,
76
664
    symbols: &mut SymbolTable,
77
664
    args: &[Expr],
78
664
) -> Result<()> {
79
664
    let result = describe(symbols, args)?;
80
498
    compile_expr(ctx, emit, symbols, &result)
81
664
}
82

            
83
/// `(pp v)` → a String containing the pretty-printed textual form
84
/// of `v`. The compile-side path constant-folds via [`format_expr`],
85
/// so the call returns an `Expr::String` directly. Runtime values
86
/// (`WasmRuntime` / `WasmLocal`) print as their type descriptor —
87
/// a runtime-side printer that walks the wasm value lands when the
88
/// host needs it; today the emacs/nms clients format the
89
/// `nomi-eval` return slot themselves.
90
1333
pub(super) fn pp(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
91
1333
    if args.len() != 1 {
92
168
        return Err(Error::Arity {
93
168
            name: "PP".to_string(),
94
168
            expected: 1,
95
168
            actual: args.len(),
96
168
        });
97
1165
    }
98
1165
    let resolved = eval_value(symbols, &args[0])?;
99
1165
    Ok(Expr::String(format_expr(&resolved)))
100
1333
}
101

            
102
581
pub(super) fn compile_pp(
103
581
    ctx: &mut CompileContext,
104
581
    emit: &mut FunctionEmitter,
105
581
    symbols: &mut SymbolTable,
106
581
    args: &[Expr],
107
581
) -> Result<()> {
108
581
    let result = pp(symbols, args)?;
109
415
    compile_expr(ctx, emit, symbols, &result)
110
581
}
111

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

            
122
166
pub(super) fn compile_pp_for_stack(
123
166
    ctx: &mut CompileContext,
124
166
    emit: &mut FunctionEmitter,
125
166
    symbols: &mut SymbolTable,
126
166
    args: &[Expr],
127
166
) -> Result<WasmType> {
128
166
    let result = pp(symbols, args)?;
129
166
    compile_static_result_for_stack(ctx, emit, symbols, &result)
130
166
}