1
use std::collections::HashMap;
2

            
3
use crate::ast::Expr;
4

            
5
use super::entry::{Symbol, SymbolKind};
6

            
7
#[derive(Debug, Clone, Default)]
8
pub struct SymbolTable {
9
    pub(super) symbols: HashMap<String, Symbol>,
10
    pub(super) struct_fields: HashMap<String, Vec<String>>,
11
    /// Registered nomiscript tests, in declaration order. Populated
12
    /// by `DEFTEST`, drained by `RUN-TESTS`. Each entry is
13
    /// `(name, body)` where `body` is the test's wrapped (BEGIN ...)
14
    /// form. Lives on the table so each compiler/evaluator instance
15
    /// gets its own isolated registry — no global state.
16
    pub(super) tests: Vec<(String, Expr)>,
17
    /// Per-native-fn compile-time reference counter. Bumped each time
18
    /// the compiler emits a `call $<host-fn-idx>` for a registered
19
    /// `HostFnSpec`. Read by `(coverage-dump)` to certify the test
20
    /// surface reaches every native. Compile-time count (not runtime
21
    /// hits) — sufficient for the parity contract "every native has
22
    /// at least one test that compiles against it"; runtime hit
23
    /// counts via a wasm-global counter array land in a follow-up.
24
    pub(super) native_coverage: HashMap<String, u32>,
25
    /// Current macro-expansion nesting depth. Bumped on entry to each
26
    /// `expand_macro` (which clones the table for the expansion body), so
27
    /// the count accumulates down a chain of nested expansions and a
28
    /// self-referential macro is caught before it overflows the stack.
29
    /// Lives on the table because every expansion site threads
30
    /// `&mut SymbolTable`; a top-level form starts at 0.
31
    pub(super) macro_depth: usize,
32
    /// Names of defuns currently being inlined on the eval path, in entry
33
    /// order. The eval-side `call_lambda` walks a defun body inline for
34
    /// constant folding; a recursive defun whose recursion doesn't fold to a
35
    /// base case (e.g. a runtime-arg call) would otherwise recurse the
36
    /// compiler's native stack forever. A name already on this stack signals
37
    /// re-entry, so the call site short-circuits to a runtime placeholder
38
    /// (handing the real lowering to the codegen monomorph path) rather than
39
    /// inlining again. Threaded on the table because the eval path only ever
40
    /// carries `&mut SymbolTable`.
41
    pub(super) inline_stack: Vec<String>,
42
    /// Result `WasmType` of each emitted closure, keyed by `ClosureSigId`'s raw
43
    /// id. A closure value carries only `WasmType::Closure(sig)` — the sig's
44
    /// param/result types live on the codegen `CompileContext`, invisible to the
45
    /// eval surface. The closure-emit sites (which have the context) record the
46
    /// result here so the ctx-less eval paths — FOLD's accumulator-type probe,
47
    /// MAP/FILTER element typing, binding-local sizing — predict a HOF result
48
    /// type that AGREES with what `compile_*_to_stack` emits (which reads the
49
    /// real sig). Without it, eval guesses from the seed and drifts from
50
    /// codegen, mis-sizing a local (invalid wasm). Carried on the table because
51
    /// the eval recursion only ever threads `&mut SymbolTable`.
52
    pub(super) closure_results: HashMap<u32, crate::ast::WasmType>,
53
}
54

            
55
/// Hard ceiling on nested macro expansion. A self-reproducing macro
56
/// (`(defmacro m () '(m))`) would otherwise recurse until the compiler's
57
/// native stack overflows; this turns that into a structured compile
58
/// error. Each level clones the symbol table and re-enters the full
59
/// eval/compile recursion, so the per-level native-stack cost is high —
60
/// `64` (matching `MAX_STATIC_LOOP_ITERS`) stays comfortably within a
61
/// worker-thread stack while far exceeding any legitimate macro nesting.
62
pub const MAX_MACRO_EXPANSION_DEPTH: usize = 64;
63

            
64
/// Hard ceiling on nested eval-time function inlining. The eval path walks a
65
/// defun body inline for constant folding; a recursive defun whose recursion
66
/// doesn't fold to a base case (a runtime-arg call, or genuinely unbounded
67
/// recursion) would otherwise recurse the compiler's native stack until it
68
/// overflows. This turns that into a structured compile error.
69
///
70
/// Set conservatively below the macro ceiling: an inline level is far heavier
71
/// on the native stack than a macro-expansion level (it clones the table,
72
/// binds params, and re-enters the full `eval_value` recursion through
73
/// `if`/arithmetic/etc.), and compilation is not guaranteed to run on a large
74
/// stack — empirically a default ~2 MiB thread overflows around ~64 inline
75
/// levels. `32` leaves roughly a 2× native-stack margin while still allowing
76
/// far deeper const-folded recursion than any realistic accounting script
77
/// uses (the deepest legitimate cases are a handful of levels).
78
pub const MAX_INLINE_DEPTH: usize = 32;
79

            
80
impl SymbolTable {
81
    #[must_use]
82
82924
    pub fn new() -> Self {
83
82924
        Self::default()
84
82924
    }
85

            
86
    /// Enter one macro-expansion level, erroring once the depth ceiling is
87
    /// crossed. Bumped before a macro's expansion is re-evaluated (where a
88
    /// self-referential macro would otherwise recurse forever) and undone
89
    /// by [`Self::exit_macro_expansion`] once that subtree finishes, so
90
    /// sibling expansions don't accumulate. Turns a non-terminating macro
91
    /// into a structured compile error instead of a native stack overflow.
92
25326
    pub(crate) fn enter_macro_expansion(&mut self) -> crate::error::Result<()> {
93
25326
        if self.macro_depth >= MAX_MACRO_EXPANSION_DEPTH {
94
136
            return Err(crate::error::Error::Compile(format!(
95
136
                "macro expansion exceeded depth {MAX_MACRO_EXPANSION_DEPTH} \
96
136
                 (recursive or non-terminating macro?)"
97
136
            )));
98
25190
        }
99
25190
        self.macro_depth += 1;
100
25190
        Ok(())
101
25326
    }
102

            
103
25190
    pub(crate) fn exit_macro_expansion(&mut self) {
104
25190
        self.macro_depth = self.macro_depth.saturating_sub(1);
105
25190
    }
106

            
107
    /// Whether `name` is already being inlined further up the eval-time call
108
    /// chain — i.e. inlining it again would be (mutual) recursion.
109
    #[must_use]
110
21278
    pub(crate) fn is_inlining(&self, name: &str) -> bool {
111
21278
        self.inline_stack.iter().any(|n| n == name)
112
21278
    }
113

            
114
    /// Mark `name` as entering an eval-time inline walk. Paired with
115
    /// [`Self::exit_inline`]; the depth ceiling turns a deeply-nested
116
    /// (or unbounded) inline recursion into a structured compile error
117
    /// instead of a native stack overflow — the eval-path analogue of
118
    /// [`Self::enter_macro_expansion`].
119
17130
    pub(crate) fn enter_inline(&mut self, name: &str) -> crate::error::Result<()> {
120
17130
        if self.inline_stack.len() >= MAX_INLINE_DEPTH {
121
            return Err(crate::error::Error::Compile(format!(
122
                "function inlining exceeded depth {MAX_INLINE_DEPTH} \
123
                 (recursive or non-terminating call to '{name}'?)"
124
            )));
125
17130
        }
126
17130
        self.inline_stack.push(name.to_string());
127
17130
        Ok(())
128
17130
    }
129

            
130
17130
    pub(crate) fn exit_inline(&mut self) {
131
17130
        self.inline_stack.pop();
132
17130
    }
133

            
134
    /// Records the result `WasmType` for a closure signature id (see
135
    /// [`Self::closure_results`]). Called by the closure-emit sites, which hold
136
    /// the `CompileContext` and so know the sig's true result type.
137
2448
    pub fn record_closure_result(
138
2448
        &mut self,
139
2448
        sig: crate::ast::ClosureSigId,
140
2448
        result: crate::ast::WasmType,
141
2448
    ) {
142
2448
        self.closure_results.insert(sig.0, result);
143
2448
    }
144

            
145
    /// The recorded result `WasmType` for a closure signature id, or `None` if
146
    /// the closure wasn't emitted through a recording site (e.g. a closure
147
    /// reaching the eval surface from a path that doesn't record).
148
1564
    pub fn closure_result(&self, sig: crate::ast::ClosureSigId) -> Option<crate::ast::WasmType> {
149
1564
        self.closure_results.get(&sig.0).copied()
150
1564
    }
151

            
152
    #[must_use]
153
42674
    pub fn with_builtins() -> Self {
154
42674
        let mut table = Self::new();
155
42674
        table.register_builtins();
156
42674
        table.load_standard_library();
157
        // Re-register entity accessors after stdlib to override DEFSTRUCT-generated ones
158
42674
        table.register_entity_accessors();
159
        // Prelude loads last so its DEFUNs resolve the builtins + accessors above.
160
42674
        table.load_prelude();
161
42674
        table
162
42674
    }
163

            
164
    #[must_use]
165
36398
    pub fn with_builtins_for_wasm() -> Self {
166
36398
        let mut table = Self::new();
167
36398
        table.register_builtins();
168
36398
        table.load_standard_library();
169
36398
        table.register_entity_accessors();
170
36398
        table.load_prelude();
171
36398
        table
172
36398
    }
173

            
174
22490926
    pub fn define(&mut self, symbol: Symbol) {
175
22490926
        self.symbols.insert(symbol.name.clone(), symbol);
176
22490926
    }
177

            
178
    /// Register a named test (`DEFTEST`); `RUN-TESTS` reads it via [`tests`].
179
    /// Re-registering an existing name overwrites its body in place (matching
180
    /// [`define`]'s overwrite semantics) rather than appending, so the
181
    /// two-surface compile — which may evaluate a `DEFTEST` on both the eval
182
    /// and codegen passes — stays idempotent and never double-runs a test.
183
2998
    pub fn register_test(&mut self, name: impl Into<String>, body: Expr) {
184
2998
        let name = name.into();
185
2998
        match self
186
2998
            .tests
187
2998
            .iter_mut()
188
13266
            .find(|(existing, _)| *existing == name)
189
        {
190
1
            Some(entry) => entry.1 = body,
191
2997
            None => self.tests.push((name, body)),
192
        }
193
2998
    }
194

            
195
    /// Snapshot of registered tests in declaration order. Cloned so
196
    /// `RUN-TESTS` can iterate without holding a borrow on the table
197
    /// it needs to evaluate against.
198
    #[must_use]
199
685
    pub fn tests(&self) -> Vec<(String, Expr)> {
200
685
        self.tests.clone()
201
685
    }
202

            
203
    /// Records a compile-time reference to native fn `name`. Called
204
    /// from the compiler's `host_fn::compile_host_fn_for_*` whenever
205
    /// a wasm import call is emitted for `name`.
206
131989
    pub fn mark_native_referenced(&mut self, name: &str) {
207
131989
        *self.native_coverage.entry(name.to_string()).or_insert(0) += 1;
208
131989
    }
209

            
210
    /// Compile-time reference counts per native fn name. The map only
211
    /// contains names that were emitted at least once; callers that
212
    /// need a complete-vs-missing report should cross-reference
213
    /// against the registered `HostFnSpec` list.
214
    #[must_use]
215
204
    pub fn native_coverage(&self) -> &HashMap<String, u32> {
216
204
        &self.native_coverage
217
204
    }
218

            
219
    /// Pre-registers the lisp-side symbol for each host fn so the compiler's
220
    /// resolve_arg + native-dispatch path picks them up. The symbol's value
221
    /// slot carries a `WasmRuntime(result-type)` stand-in so callers that
222
    /// eval the host-fn form during compile-time analysis (CONS element
223
    /// inference, arithmetic dispatch type lookup, etc.) see the right
224
    /// result type without needing to actually run the host fn. The
225
    /// matching wasm import is registered separately by
226
    /// `CompileContext::new_eval_with_host_fns`.
227
12853
    pub fn register_host_fns(&mut self, host_fns: &[crate::host_fn::HostFnSpec]) {
228
475253
        for spec in host_fns {
229
475253
            let stand_in = match spec.result {
230
475049
                Some(ty) => Expr::WasmRuntime(ty),
231
204
                None => Expr::Nil,
232
            };
233
475253
            self.define(
234
475253
                Symbol::new(spec.nomi_name.clone(), SymbolKind::Native).with_value(stand_in),
235
            );
236
        }
237
12853
    }
238

            
239
    #[must_use]
240
1622998
    pub fn lookup(&self, name: &str) -> Option<&Symbol> {
241
1622998
        self.symbols.get(name)
242
1622998
    }
243

            
244
    /// Looks up a (possibly namespaced) name via its canonical key (ADR-0029).
245
    /// Resolution is strict and lexical: a qualified `(Some(ns), name)` looks
246
    /// up only `NS:NAME`, an unqualified `(None, name)` looks up only `NAME` —
247
    /// there is no cross-namespace fallback and no ambient "current namespace".
248
    /// Most call sites pass the already-canonical `Expr::Symbol` string straight
249
    /// to [`Self::lookup`]; this helper is for callers that hold the split parts.
250
    #[must_use]
251
3
    pub fn lookup_qualified(&self, namespace: Option<&str>, name: &str) -> Option<&Symbol> {
252
3
        self.lookup(&crate::ast::canonical_symbol(namespace, name))
253
3
    }
254

            
255
396892
    pub fn lookup_mut(&mut self, name: &str) -> Option<&mut Symbol> {
256
396892
        self.symbols.get_mut(name)
257
396892
    }
258

            
259
3552
    pub fn remove(&mut self, name: &str) -> Option<Symbol> {
260
3552
        self.symbols.remove(name)
261
3552
    }
262

            
263
    #[must_use]
264
1240
    pub fn contains(&self, name: &str) -> bool {
265
1240
        self.symbols.contains_key(name)
266
1240
    }
267

            
268
684
    pub fn iter(&self) -> impl Iterator<Item = (&String, &Symbol)> {
269
684
        self.symbols.iter()
270
684
    }
271

            
272
399508
    pub fn define_struct_fields(&mut self, name: impl Into<String>, fields: Vec<String>) {
273
399508
        self.struct_fields.insert(name.into(), fields);
274
399508
    }
275

            
276
    #[must_use]
277
204
    pub fn struct_fields(&self, name: &str) -> Option<&[String]> {
278
204
        self.struct_fields.get(name).map(Vec::as_slice)
279
204
    }
280
}