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