Lines
100 %
Functions
93.75 %
Branches
//! The registries a symbol table keeps beside its symbols: `DEFTEST` tests,
//! the native fns a compile referenced and the host fns it knows.
use std::collections::HashMap;
use crate::ast::Expr;
use super::entry::{Symbol, SymbolKind};
use super::table::SymbolTable;
impl SymbolTable {
/// Register a named test (`DEFTEST`); `RUN-TESTS` reads it via [`Self::tests`].
/// Re-registering an existing name overwrites its body in place (matching
/// [`Self::define`]'s overwrite semantics) rather than appending, so the
/// two-surface compile — which may evaluate a `DEFTEST` on both the eval
/// and codegen passes — stays idempotent and never double-runs a test.
pub fn register_test(&mut self, name: impl Into<String>, body: Expr) {
let name = name.into();
match self
.tests
.iter_mut()
.find(|(existing, _)| *existing == name)
{
Some(entry) => entry.1 = body,
None => self.tests.push((name, body)),
}
/// Snapshot of registered tests in declaration order. Cloned so
/// `RUN-TESTS` can iterate without holding a borrow on the table
/// it needs to evaluate against.
#[must_use]
pub fn tests(&self) -> Vec<(String, Expr)> {
self.tests.clone()
/// Records a compile-time reference to native fn `name`. Called
/// from the compiler's `host_fn::compile_host_fn_for_*` whenever
/// a wasm import call is emitted for `name`.
pub fn mark_native_referenced(&mut self, name: &str) {
*self.native_coverage.entry(name.to_string()).or_insert(0) += 1;
/// Compile-time reference counts per native fn name. The map only
/// contains names that were emitted at least once; callers that
/// need a complete-vs-missing report should cross-reference
/// against the registered `HostFnSpec` list.
pub fn native_coverage(&self) -> &HashMap<String, u32> {
&self.native_coverage
/// Pre-registers the lisp-side symbol for each host fn so the compiler's
/// resolve_arg + native-dispatch path picks them up. The symbol's value
/// slot carries a `WasmRuntime(result-type)` stand-in so callers that
/// eval the host-fn form during compile-time analysis (CONS element
/// inference, arithmetic dispatch type lookup, etc.) see the right
/// result type without needing to actually run the host fn. The
/// matching wasm import is registered separately by
/// `CompileContext::new_eval_with_host_fns`.
pub fn register_host_fns(&mut self, host_fns: &[crate::host_fn::HostFnSpec]) {
for spec in host_fns {
self.host_names.insert(spec.nomi_name.to_uppercase());
let stand_in = match spec.result {
Some(ty) => Expr::WasmRuntime(ty),
None => Expr::Nil,
};
self.define(
Symbol::new(spec.nomi_name.clone(), SymbolKind::Native).with_value(stand_in),
);
pub(crate) fn is_host_name(&self, name: &str) -> bool {
self.host_names.contains(&name.to_uppercase())