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
680
pub(super) fn describe(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
14
680
    if args.len() != 1 {
15
68
        return Err(Error::Arity {
16
68
            name: "DESCRIBE".to_string(),
17
68
            expected: 1,
18
68
            actual: args.len(),
19
68
        });
20
612
    }
21
612
    let name = match &args[0] {
22
204
        Expr::Symbol(s) => s.clone(),
23
408
        Expr::Quote(inner) => match inner.as_ref() {
24
408
            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
612
    let sym = symbols
38
612
        .lookup(&name)
39
612
        .ok_or_else(|| Error::UndefinedSymbol(name.clone()))?;
40

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

            
73
544
pub(super) fn compile_describe(
74
544
    ctx: &mut CompileContext,
75
544
    emit: &mut FunctionEmitter,
76
544
    symbols: &mut SymbolTable,
77
544
    args: &[Expr],
78
544
) -> Result<()> {
79
544
    let result = describe(symbols, args)?;
80
408
    compile_expr(ctx, emit, symbols, &result)
81
544
}
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
1093
pub(super) fn pp(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
91
1093
    if args.len() != 1 {
92
138
        return Err(Error::Arity {
93
138
            name: "PP".to_string(),
94
138
            expected: 1,
95
138
            actual: args.len(),
96
138
        });
97
955
    }
98
955
    let resolved = eval_value(symbols, &args[0])?;
99
955
    Ok(Expr::String(format_expr(&resolved)))
100
1093
}
101

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

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

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