Skip to main content

nomiscript/runtime/symbol/
registry.rs

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
4use std::collections::HashMap;
5
6use crate::ast::Expr;
7
8use super::entry::{Symbol, SymbolKind};
9use super::table::SymbolTable;
10
11impl 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    pub fn register_test(&mut self, name: impl Into<String>, body: Expr) {
18        let name = name.into();
19        match self
20            .tests
21            .iter_mut()
22            .find(|(existing, _)| *existing == name)
23        {
24            Some(entry) => entry.1 = body,
25            None => self.tests.push((name, body)),
26        }
27    }
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    pub fn tests(&self) -> Vec<(String, Expr)> {
34        self.tests.clone()
35    }
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    pub fn mark_native_referenced(&mut self, name: &str) {
41        *self.native_coverage.entry(name.to_string()).or_insert(0) += 1;
42    }
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    pub fn native_coverage(&self) -> &HashMap<String, u32> {
50        &self.native_coverage
51    }
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    pub fn register_host_fns(&mut self, host_fns: &[crate::host_fn::HostFnSpec]) {
62        for spec in host_fns {
63            self.host_names.insert(spec.nomi_name.to_uppercase());
64            let stand_in = match spec.result {
65                Some(ty) => Expr::WasmRuntime(ty),
66                None => Expr::Nil,
67            };
68            self.define(
69                Symbol::new(spec.nomi_name.clone(), SymbolKind::Native).with_value(stand_in),
70            );
71        }
72    }
73
74    #[must_use]
75    pub(crate) fn is_host_name(&self, name: &str) -> bool {
76        self.host_names.contains(&name.to_uppercase())
77    }
78}