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
24342488
    pub fn new(name: impl Into<String>, kind: SymbolKind) -> Self {
33
24342488
        Self {
34
24342488
            name: name.into(),
35
24342488
            kind,
36
24342488
            value: None,
37
24342488
            function: None,
38
24342488
            doc: None,
39
24342488
            properties: HashMap::new(),
40
24342488
        }
41
24342488
    }
42

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

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

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

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

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

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

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

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

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

            
89
    #[must_use]
90
570
    pub fn doc(&self) -> Option<&str> {
91
570
        self.doc.as_deref()
92
570
    }
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
}