Skip to main content

nomiscript/runtime/symbol/
entry.rs

1use std::collections::HashMap;
2
3use crate::ast::Expr;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub 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)]
22pub 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
31impl Symbol {
32    pub fn new(name: impl Into<String>, kind: SymbolKind) -> Self {
33        Self {
34            name: name.into(),
35            kind,
36            value: None,
37            function: None,
38            doc: None,
39            properties: HashMap::new(),
40        }
41    }
42
43    #[must_use]
44    pub fn with_value(mut self, value: Expr) -> Self {
45        self.value = Some(value);
46        self
47    }
48
49    #[must_use]
50    pub fn name(&self) -> &str {
51        &self.name
52    }
53
54    #[must_use]
55    pub fn kind(&self) -> SymbolKind {
56        self.kind
57    }
58
59    #[must_use]
60    pub fn value(&self) -> Option<&Expr> {
61        self.value.as_ref()
62    }
63
64    pub fn set_value(&mut self, value: Expr) {
65        self.value = Some(value);
66    }
67
68    #[must_use]
69    pub fn with_function(mut self, func: Expr) -> Self {
70        self.function = Some(func);
71        self
72    }
73
74    #[must_use]
75    pub fn function(&self) -> Option<&Expr> {
76        self.function.as_ref()
77    }
78
79    pub fn set_function(&mut self, func: Expr) {
80        self.function = Some(func);
81    }
82
83    #[must_use]
84    pub fn with_doc(mut self, doc: impl Into<String>) -> Self {
85        self.doc = Some(doc.into());
86        self
87    }
88
89    #[must_use]
90    pub fn doc(&self) -> Option<&str> {
91        self.doc.as_deref()
92    }
93
94    pub fn set_doc(&mut self, doc: impl Into<String>) {
95        self.doc = Some(doc.into());
96    }
97
98    #[must_use]
99    pub fn get_property(&self, key: &str) -> Option<&Expr> {
100        self.properties.get(key)
101    }
102
103    pub fn set_property(&mut self, key: impl Into<String>, value: Expr) {
104        self.properties.insert(key.into(), value);
105    }
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}