Parameterize type of ScopeStack scope names

This commit is contained in:
Greg Shuflin 2021-10-21 19:22:11 -07:00
parent fb31687dea
commit 4ddcbc89ad

View File

@ -8,18 +8,18 @@ pub fn deref_optional_box<T>(x: &Option<Box<T>>) -> Option<&T> {
}
#[derive(Default, Debug)]
pub struct ScopeStack<'a, T: 'a, V: 'a> where T: Hash + Eq {
parent: Option<&'a ScopeStack<'a, T, V>>,
pub struct ScopeStack<'a, T: 'a, V: 'a, N=String> where T: Hash + Eq {
parent: Option<&'a ScopeStack<'a, T, V, N>>,
values: HashMap<T, V>,
scope_name: Option<String>
scope_name: Option<N>
}
impl<'a, T, V> ScopeStack<'a, T, V> where T: Hash + Eq {
pub fn new(name: Option<String>) -> ScopeStack<'a, T, V> where T: Hash + Eq {
impl<'a, T, V, N> ScopeStack<'a, T, V, N> where T: Hash + Eq {
pub fn new(scope_name: Option<N>) -> Self where T: Hash + Eq {
ScopeStack {
parent: None,
values: HashMap::new(),
scope_name: name
scope_name,
}
}
pub fn insert(&mut self, key: T, value: V) where T: Hash + Eq {
@ -33,15 +33,24 @@ impl<'a, T, V> ScopeStack<'a, T, V> where T: Hash + Eq {
}
}
pub fn new_scope(&'a self, name: Option<String>) -> ScopeStack<'a, T, V> where T: Hash + Eq {
pub fn new_scope(&'a self, scope_name: Option<N>) -> Self where T: Hash + Eq {
ScopeStack {
parent: Some(self),
values: HashMap::default(),
scope_name: name,
scope_name,
}
}
pub fn lookup_with_scope(&self, key: &T) -> Option<(&V, Option<&N>)> where T: Hash + Eq {
match (self.values.get(key), self.parent) {
(None, None) => None,
(None, Some(parent)) => parent.lookup_with_scope(key),
(Some(value), _) => Some((value, self.scope_name.as_ref()))
}
}
#[allow(dead_code)]
pub fn get_name(&self) -> Option<&String> {
pub fn get_name(&self) -> Option<&N> {
self.scope_name.as_ref()
}
}