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;
29mod udp;
30
31#[cfg(test)]
32mod tests;
33
34type AddSigsFn = fn(&mut HashMap<String, Effect>);
35type AddDocsFn = fn(&mut HashMap<&'static str, &'static str>);
36
37/// Single source of truth for the built-in category sub-modules.
38///
39/// Adding a new sub-module requires exactly one row here; the signature map,
40/// the doc map, and `builtin_categories()` all iterate over this list, so
41/// they cannot drift apart.
42const CATEGORIES: &[(&str, AddSigsFn, AddDocsFn)] = &[
43    ("io", io::add_signatures, io::add_docs),
44    ("fs", fs::add_signatures, fs::add_docs),
45    ("arith", arith::add_signatures, arith::add_docs),
46    ("stack", stack::add_signatures, stack::add_docs),
47    (
48        "concurrency",
49        concurrency::add_signatures,
50        concurrency::add_docs,
51    ),
52    ("callable", callable::add_signatures, callable::add_docs),
53    ("tcp", tcp::add_signatures, tcp::add_docs),
54    ("udp", udp::add_signatures, udp::add_docs),
55    ("os", os::add_signatures, os::add_docs),
56    ("text", text::add_signatures, text::add_docs),
57    ("adt", adt::add_signatures, adt::add_docs),
58    ("list", list::add_signatures, list::add_docs),
59    ("map", map::add_signatures, map::add_docs),
60    ("float", float::add_signatures, float::add_docs),
61    (
62        "diagnostics",
63        diagnostics::add_signatures,
64        diagnostics::add_docs,
65    ),
66];
67
68/// Get the stack-effect signature for a built-in word.
69pub fn builtin_signature(name: &str) -> Option<Effect> {
70    BUILTIN_SIGNATURES.get(name).cloned()
71}
72
73/// Build the full map of built-in word signatures.
74///
75/// Clones the cached map so callers that wanted ownership (e.g. tests,
76/// `TypeChecker::register_external_words`) keep working unchanged.
77pub fn builtin_signatures() -> HashMap<String, Effect> {
78    BUILTIN_SIGNATURES.clone()
79}
80
81static BUILTIN_SIGNATURES: LazyLock<HashMap<String, Effect>> = LazyLock::new(|| {
82    let mut sigs = HashMap::new();
83    for (_, add_sigs, _) in CATEGORIES {
84        add_sigs(&mut sigs);
85    }
86    sigs
87});
88
89/// Get documentation for a built-in word.
90pub fn builtin_doc(name: &str) -> Option<&'static str> {
91    BUILTIN_DOCS.get(name).copied()
92}
93
94/// Built-in words grouped by their category sub-module, in registration order.
95///
96/// Each entry is `(category_name, sorted_word_names)`. Useful for clients
97/// that want to render a categorised reference (e.g. a quick-help screen).
98pub fn builtin_categories() -> Vec<(&'static str, Vec<String>)> {
99    CATEGORIES
100        .iter()
101        .map(|(name, add_sigs, _)| {
102            let mut sigs = HashMap::new();
103            add_sigs(&mut sigs);
104            let mut words: Vec<String> = sigs.into_keys().collect();
105            words.sort();
106            (*name, words)
107        })
108        .collect()
109}
110
111/// Get all built-in word documentation (cached with LazyLock for performance).
112pub fn builtin_docs() -> &'static HashMap<&'static str, &'static str> {
113    &BUILTIN_DOCS
114}
115
116static BUILTIN_DOCS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
117    let mut docs = HashMap::new();
118    for (_, _, add_docs) in CATEGORIES {
119        add_docs(&mut docs);
120    }
121    docs
122});