1
//! Built-in registrations: operators, special forms, native fns,
2
//! entity-type/context constants, compiler-generated natives (entity and
3
//! typed-entity accessors), and the MAP-family macros (MAPCAR / MAPC).
4

            
5
use crate::ast::{Expr, LambdaParams, Number};
6

            
7
use super::entry::{Symbol, SymbolKind};
8
use super::table::SymbolTable;
9

            
10
impl SymbolTable {
11
1532510
    pub fn register_builtins(&mut self) {
12
        // Name lists tangle from doc/scripting/builtin_reference.org
13
        // into builtins_generated.rs — the org registry tables are
14
        // the single source of truth. Adding a builtin = one org row.
15
1532510
        for op in super::builtins_generated::OPERATORS {
16
16857610
            self.define(Symbol::new(*op, SymbolKind::Operator));
17
        }
18
1532510
        for form in super::builtins_generated::SPECIAL_FORMS {
19
70495460
            self.define(Symbol::new(*form, SymbolKind::SpecialForm));
20
        }
21
1532510
        for native in super::builtins_generated::NATIVES {
22
67430440
            self.define(Symbol::new(*native, SymbolKind::Native));
23
        }
24

            
25
1532510
        self.register_entity_constants();
26
1532510
        self.register_compiler_natives();
27

            
28
1532510
        self.add_map_family_macros();
29
1532510
    }
30

            
31
1532510
    fn register_entity_constants(&mut self) {
32
        use scripting_format::{ContextType, EntityType};
33

            
34
1532510
        let entity_types: &[(&str, EntityType)] = &[
35
1532510
            ("+ENTITY-TRANSACTION+", EntityType::Transaction),
36
1532510
            ("+ENTITY-SPLIT+", EntityType::Split),
37
1532510
            ("+ENTITY-TAG+", EntityType::Tag),
38
1532510
            ("+ENTITY-ACCOUNT+", EntityType::Account),
39
1532510
            ("+ENTITY-COMMODITY+", EntityType::Commodity),
40
1532510
        ];
41
1532510
        for (name, ty) in entity_types {
42
7662550
            self.define_special(
43
7662550
                Symbol::new(*name, SymbolKind::Variable)
44
7662550
                    .with_value(Expr::Number(Number::Index(i64::from(*ty as u8)))),
45
7662550
            );
46
        }
47

            
48
1532510
        let context_types: &[(&str, ContextType)] = &[
49
1532510
            ("+CONTEXT-CREATE+", ContextType::EntityCreate),
50
1532510
            ("+CONTEXT-UPDATE+", ContextType::EntityUpdate),
51
1532510
            ("+CONTEXT-DELETE+", ContextType::EntityDelete),
52
1532510
            ("+CONTEXT-BATCH+", ContextType::BatchProcess),
53
1532510
        ];
54
1532510
        for (name, ct) in context_types {
55
6130040
            self.define_special(
56
6130040
                Symbol::new(*name, SymbolKind::Variable)
57
6130040
                    .with_value(Expr::Number(Number::Index(i64::from(*ct as u8)))),
58
6130040
            );
59
        }
60
1532510
    }
61

            
62
3065020
    pub(super) fn register_compiler_natives(&mut self) {
63
3065020
        for name in crate::compiler::native_names() {
64
324892120
            if !is_listed_builtin(name) {
65
156316020
                self.define(Symbol::new(name, SymbolKind::Native));
66
168576100
            }
67
        }
68
3065020
    }
69

            
70
1532510
    fn add_map_family_macros(&mut self) {
71
1532510
        let mapcar_params = LambdaParams {
72
1532510
            required: vec!["func".to_string(), "list".to_string()],
73
1532510
            optional: Vec::new(),
74
1532510
            rest: Some("lists".to_string()),
75
1532510
            key: Vec::new(),
76
1532510
            aux: Vec::new(),
77
1532510
        };
78
        // Backquoted so the argument FORMS splice into the expansion unevaluated;
79
        // evaluating them here would bake a runtime list's placeholder into it.
80
1532510
        let mapcar_body = map_call_template(vec![
81
1532510
            unquote("func"),
82
1532510
            unquote("list"),
83
1532510
            Expr::UnquoteSplicing(Box::new(Expr::Symbol("lists".to_string()))),
84
1532510
        ]);
85
1532510
        let mapcar_lambda = Expr::Lambda(mapcar_params, Box::new(mapcar_body));
86
1532510
        self.define(Symbol::new("MAPCAR", SymbolKind::Macro).with_function(mapcar_lambda));
87

            
88
1532510
        let mapc_params = LambdaParams::simple(vec!["func".to_string(), "list".to_string()]);
89
1532510
        let mapc_body = mapc_template();
90
1532510
        let mapc_lambda = Expr::Lambda(mapc_params, Box::new(mapc_body));
91
1532510
        self.define(Symbol::new("MAPC", SymbolKind::Macro).with_function(mapc_lambda));
92
1532510
    }
93
}
94

            
95
/// Names the builtin reference lists keep their listed kind.
96
324892120
fn is_listed_builtin(name: &str) -> bool {
97
324892120
    [
98
324892120
        super::builtins_generated::OPERATORS,
99
324892120
        super::builtins_generated::SPECIAL_FORMS,
100
324892120
        super::builtins_generated::NATIVES,
101
324892120
    ]
102
324892120
    .iter()
103
907245920
    .any(|names| names.contains(&name))
104
324892120
}
105

            
106
/// `` `(map ,@args) `` — a MAP call template for the map-family macros.
107
1532510
fn map_call_template(args: Vec<Expr>) -> Expr {
108
1532510
    let mut call = vec![Expr::symbol("MAP")];
109
1532510
    call.extend(args);
110
1532510
    Expr::Quasiquote(Box::new(Expr::List(call)))
111
1532510
}
112

            
113
/// `` `(let ((<l> ,list)) (map ,func <l>) <l>) ``: MAPC answers its list
114
/// argument, evaluated once. `<l>` has a lowercase name with a space, which
115
/// the reader never produces, so it cannot capture a variable `func` reads.
116
1532510
fn mapc_template() -> Expr {
117
4597530
    let list = || Expr::Symbol("mapc list".to_string());
118
1532510
    Expr::Quasiquote(Box::new(Expr::List(vec![
119
1532510
        Expr::symbol("LET"),
120
1532510
        Expr::List(vec![Expr::List(vec![list(), unquote("list")])]),
121
1532510
        Expr::List(vec![Expr::symbol("MAP"), unquote("func"), list()]),
122
1532510
        list(),
123
1532510
    ])))
124
1532510
}
125

            
126
6130040
fn unquote(param: &str) -> Expr {
127
6130040
    Expr::Unquote(Box::new(Expr::Symbol(param.to_string())))
128
6130040
}