1
use std::collections::HashMap;
2

            
3
use crate::ast::Expr;
4

            
5
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6
pub enum SymbolKind {
7
    /// Built-in operators like +, -, *, /
8
    Operator,
9
    /// Native functions implemented in the runtime
10
    Native,
11
    /// User-defined functions
12
    Function,
13
    /// Variables and constants
14
    Variable,
15
    /// Special forms like if, let, define, lambda
16
    SpecialForm,
17
    /// User-defined macros
18
    Macro,
19
}
20

            
21
#[derive(Debug, Clone, PartialEq)]
22
pub struct Symbol {
23
    pub(super) name: String,
24
    pub(super) kind: SymbolKind,
25
    pub(super) value: Option<Expr>,
26
    pub(super) function: Option<Expr>,
27
    pub(super) doc: Option<String>,
28
    pub(super) properties: HashMap<String, Expr>,
29
}
30

            
31
impl Symbol {
32
22490997
    pub fn new(name: impl Into<String>, kind: SymbolKind) -> Self {
33
22490997
        Self {
34
22490997
            name: name.into(),
35
22490997
            kind,
36
22490997
            value: None,
37
22490997
            function: None,
38
22490997
            doc: None,
39
22490997
            properties: HashMap::new(),
40
22490997
        }
41
22490997
    }
42

            
43
    #[must_use]
44
1345819
    pub fn with_value(mut self, value: Expr) -> Self {
45
1345819
        self.value = Some(value);
46
1345819
        self
47
1345819
    }
48

            
49
    #[must_use]
50
69
    pub fn name(&self) -> &str {
51
69
        &self.name
52
69
    }
53

            
54
    #[must_use]
55
1464054
    pub fn kind(&self) -> SymbolKind {
56
1464054
        self.kind
57
1464054
    }
58

            
59
    #[must_use]
60
1307176
    pub fn value(&self) -> Option<&Expr> {
61
1307176
        self.value.as_ref()
62
1307176
    }
63

            
64
55624
    pub fn set_value(&mut self, value: Expr) {
65
55624
        self.value = Some(value);
66
55624
    }
67

            
68
    #[must_use]
69
6358436
    pub fn with_function(mut self, func: Expr) -> Self {
70
6358436
        self.function = Some(func);
71
6358436
        self
72
6358436
    }
73

            
74
    #[must_use]
75
1345943
    pub fn function(&self) -> Option<&Expr> {
76
1345943
        self.function.as_ref()
77
1345943
    }
78

            
79
272
    pub fn set_function(&mut self, func: Expr) {
80
272
        self.function = Some(func);
81
272
    }
82

            
83
    #[must_use]
84
477
    pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
85
477
        self.doc = Some(doc.into());
86
477
        self
87
477
    }
88

            
89
    #[must_use]
90
546
    pub fn doc(&self) -> Option<&str> {
91
546
        self.doc.as_deref()
92
546
    }
93

            
94
    pub fn set_doc(&mut self, doc: impl Into<String>) {
95
        self.doc = Some(doc.into());
96
    }
97

            
98
    #[must_use]
99
3
    pub fn get_property(&self, key: &str) -> Option<&Expr> {
100
3
        self.properties.get(key)
101
3
    }
102

            
103
2
    pub fn set_property(&mut self, key: impl Into<String>, value: Expr) {
104
2
        self.properties.insert(key.into(), value);
105
2
    }
106

            
107
    pub fn remove_property(&mut self, key: &str) -> Option<Expr> {
108
        self.properties.remove(key)
109
    }
110

            
111
    #[must_use]
112
    pub fn properties(&self) -> &HashMap<String, Expr> {
113
        &self.properties
114
    }
115
}