1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::rc_ref::RcRef;
use crate::vm::value::Value;
use crate::vm::value::Undefined;
use crate::vm::value::String;
use std::fmt;
use std::rc::{ Rc, };
use std::cell::{ Cell, Ref, RefMut, RefCell, };
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum SymbolKind {
Public,
Private,
}
pub type SymbolRegistryRef = RcRef<SymbolRegistry>;
#[derive(Debug)]
pub struct SymbolRegistry {
public: Vec<Vec<char>>,
private: Vec<Option<Vec<char>>>,
}
#[derive(Debug, Clone)]
pub struct Symbol {
kind: SymbolKind,
id: usize,
registry_ref: SymbolRegistryRef,
}
impl SymbolRegistry {
pub fn new() -> Self {
let public = vec![];
let private = vec![];
Self { public, private, }
}
pub fn len(&self, kind: SymbolKind) -> usize {
match kind {
SymbolKind::Public => self.public.len(),
SymbolKind::Private => self.private.len(),
}
}
pub fn find_public_description(&self, description: &[char]) -> Option<usize> {
self.public.iter().position(|item| &item[..] == description)
}
pub fn get_description(&self, kind: SymbolKind, id: usize) -> Option<&Vec<char>> {
match kind {
SymbolKind::Public => {
Some(&self.public[id])
},
SymbolKind::Private => {
match self.private[id] {
Some(ref desc) => Some(&desc),
None => None,
}
},
}
}
}
impl Symbol {
}
impl Symbol {
pub fn new(kind: SymbolKind, description: Option<Vec<char>>, registry_ref: &SymbolRegistryRef) -> Self {
match kind {
SymbolKind::Public => {
let mut registry = registry_ref.borrow_mut();
let description = description.expect("Must have a description!");
let id = match registry.find_public_description(&description) {
Some(pos) => pos,
None => {
registry.public.push(description);
registry.len(kind)
},
};
Self { kind: kind, id: id, registry_ref: registry_ref.clone() }
},
SymbolKind::Private => {
let mut registry = registry_ref.borrow_mut();
registry.private.push(description);
let id = registry.len(kind);
Self { kind: kind, id: id, registry_ref: registry_ref.clone() }
},
}
}
pub fn key_for(symbol: &Symbol) -> Option<Vec<char>> {
match symbol.kind {
SymbolKind::Public => {
symbol.description()
},
SymbolKind::Private => {
None
}
}
}
pub fn description(&self) -> Option<Vec<char>> {
match self.registry_ref.borrow().get_description(self.kind, self.id) {
Some(desc) => Some(desc.clone()),
None => None,
}
}
}
impl std::hash::Hash for Symbol {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.kind.hash(state);
self.id.hash(state);
}
}
impl PartialEq for Symbol {
fn eq(&self, other: &Symbol) -> bool {
(self.kind == other.kind) && (self.id == other.id)
}
}
impl Eq for Symbol { }
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.description() {
None => write!(f, "Symbol()"),
Some(desc) => write!(f, "Symbol({})", desc.iter().collect::<std::string::String>()),
}
}
}