1
//! The registries a symbol table keeps beside its symbols: `DEFTEST` tests,
2
//! the native fns a compile referenced and the host fns it knows.
3

            
4
use std::collections::HashMap;
5

            
6
use crate::ast::Expr;
7

            
8
use super::entry::{Symbol, SymbolKind};
9
use super::table::SymbolTable;
10

            
11
impl SymbolTable {
12
    /// Register a named test (`DEFTEST`); `RUN-TESTS` reads it via [`Self::tests`].
13
    /// Re-registering an existing name overwrites its body in place (matching
14
    /// [`Self::define`]'s overwrite semantics) rather than appending, so the
15
    /// two-surface compile — which may evaluate a `DEFTEST` on both the eval
16
    /// and codegen passes — stays idempotent and never double-runs a test.
17
11707
    pub fn register_test(&mut self, name: impl Into<String>, body: Expr) {
18
11707
        let name = name.into();
19
11707
        match self
20
11707
            .tests
21
11707
            .iter_mut()
22
67666
            .find(|(existing, _)| *existing == name)
23
        {
24
1
            Some(entry) => entry.1 = body,
25
11706
            None => self.tests.push((name, body)),
26
        }
27
11707
    }
28

            
29
    /// Snapshot of registered tests in declaration order. Cloned so
30
    /// `RUN-TESTS` can iterate without holding a borrow on the table
31
    /// it needs to evaluate against.
32
    #[must_use]
33
2305
    pub fn tests(&self) -> Vec<(String, Expr)> {
34
2305
        self.tests.clone()
35
2305
    }
36

            
37
    /// Records a compile-time reference to native fn `name`. Called
38
    /// from the compiler's `host_fn::compile_host_fn_for_*` whenever
39
    /// a wasm import call is emitted for `name`.
40
1583520
    pub fn mark_native_referenced(&mut self, name: &str) {
41
1583520
        *self.native_coverage.entry(name.to_string()).or_insert(0) += 1;
42
1583520
    }
43

            
44
    /// Compile-time reference counts per native fn name. The map only
45
    /// contains names that were emitted at least once; callers that
46
    /// need a complete-vs-missing report should cross-reference
47
    /// against the registered `HostFnSpec` list.
48
    #[must_use]
49
832
    pub fn native_coverage(&self) -> &HashMap<String, u32> {
50
832
        &self.native_coverage
51
832
    }
52

            
53
    /// Pre-registers the lisp-side symbol for each host fn so the compiler's
54
    /// resolve_arg + native-dispatch path picks them up. The symbol's value
55
    /// slot carries a `WasmRuntime(result-type)` stand-in so callers that
56
    /// eval the host-fn form during compile-time analysis (CONS element
57
    /// inference, arithmetic dispatch type lookup, etc.) see the right
58
    /// result type without needing to actually run the host fn. The
59
    /// matching wasm import is registered separately by
60
    /// `CompileContext::new_eval_with_host_fns`.
61
393313
    pub fn register_host_fns(&mut self, host_fns: &[crate::host_fn::HostFnSpec]) {
62
393313
        for spec in host_fns {
63
24844621
            self.host_names.insert(spec.nomi_name.to_uppercase());
64
24844621
            let stand_in = match spec.result {
65
24843996
                Some(ty) => Expr::WasmRuntime(ty),
66
625
                None => Expr::Nil,
67
            };
68
24844621
            self.define(
69
24844621
                Symbol::new(spec.nomi_name.clone(), SymbolKind::Native).with_value(stand_in),
70
24844621
            );
71
        }
72
393313
    }
73

            
74
    #[must_use]
75
26192625
    pub(crate) fn is_host_name(&self, name: &str) -> bool {
76
26192625
        self.host_names.contains(&name.to_uppercase())
77
26192625
    }
78
}