Skip to main content

seqc/
builtins.rs

1//! Built-in word signatures for Seq
2//!
3//! Defines the stack effects for all runtime built-in operations.
4//!
5//! The signatures and docs are split across per-category sub-modules under
6//! `builtins/`, each exposing an `add_signatures(&mut sigs)` and an
7//! `add_docs(&mut docs)` helper. This file composes them into the public
8//! `builtin_signatures()` and `builtin_docs()` maps.
9
10use crate::types::Effect;
11use std::collections::HashMap;
12use std::sync::LazyLock;
13
14mod adt;
15mod arith;
16mod callable;
17mod concurrency;
18mod diagnostics;
19mod float;
20mod fs;
21mod io;
22mod list;
23mod macros;
24mod map;
25mod os;
26mod stack;
27mod tcp;
28mod text;
29
30#[cfg(test)]
31mod tests;
32
33/// Get the stack-effect signature for a built-in word.
34pub fn builtin_signature(name: &str) -> Option<Effect> {
35    BUILTIN_SIGNATURES.get(name).cloned()
36}
37
38/// Build the full map of built-in word signatures.
39///
40/// Clones the cached map so callers that wanted ownership (e.g. tests,
41/// `TypeChecker::register_external_words`) keep working unchanged.
42pub fn builtin_signatures() -> HashMap<String, Effect> {
43    BUILTIN_SIGNATURES.clone()
44}
45
46static BUILTIN_SIGNATURES: LazyLock<HashMap<String, Effect>> = LazyLock::new(|| {
47    let mut sigs = HashMap::new();
48    io::add_signatures(&mut sigs);
49    fs::add_signatures(&mut sigs);
50    arith::add_signatures(&mut sigs);
51    stack::add_signatures(&mut sigs);
52    concurrency::add_signatures(&mut sigs);
53    callable::add_signatures(&mut sigs);
54    tcp::add_signatures(&mut sigs);
55    os::add_signatures(&mut sigs);
56    text::add_signatures(&mut sigs);
57    adt::add_signatures(&mut sigs);
58    list::add_signatures(&mut sigs);
59    map::add_signatures(&mut sigs);
60    float::add_signatures(&mut sigs);
61    diagnostics::add_signatures(&mut sigs);
62    sigs
63});
64
65/// Get documentation for a built-in word.
66pub fn builtin_doc(name: &str) -> Option<&'static str> {
67    BUILTIN_DOCS.get(name).copied()
68}
69
70/// Get all built-in word documentation (cached with LazyLock for performance).
71pub fn builtin_docs() -> &'static HashMap<&'static str, &'static str> {
72    &BUILTIN_DOCS
73}
74
75static BUILTIN_DOCS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
76    let mut docs = HashMap::new();
77    io::add_docs(&mut docs);
78    fs::add_docs(&mut docs);
79    arith::add_docs(&mut docs);
80    stack::add_docs(&mut docs);
81    concurrency::add_docs(&mut docs);
82    callable::add_docs(&mut docs);
83    tcp::add_docs(&mut docs);
84    os::add_docs(&mut docs);
85    text::add_docs(&mut docs);
86    adt::add_docs(&mut docs);
87    list::add_docs(&mut docs);
88    map::add_docs(&mut docs);
89    float::add_docs(&mut docs);
90    diagnostics::add_docs(&mut docs);
91    docs
92});