Add generic stack data structure

I'll want to use this soon enough
This commit is contained in:
greg 2018-05-10 22:23:42 -07:00
parent c5e8d3e080
commit 111657b567
2 changed files with 24 additions and 0 deletions

View File

@ -17,6 +17,7 @@ macro_rules! bx {
($e:expr) => { Box::new($e) }
}
mod util;
mod builtin;
mod tokenizing;
mod parsing;

23
schala-lang/src/util.rs Normal file
View File

@ -0,0 +1,23 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::cmp::Eq;
#[derive(Default, Debug)]
pub struct StateStack<'a, T: 'a, V: 'a> where T: Hash + Eq {
parent: Option<&'a StateStack<'a, T, V>>,
values: HashMap<T, V>
}
impl<'a, T, V> StateStack<'a, T, V> where T: Hash + Eq {
pub fn insert(&mut self, key: T, value: V) where T: Hash + Eq {
self.values.insert(key, value);
}
pub fn lookup(&self, key: &T) -> Option<&V> where T: Hash + Eq {
match (self.values.get(key), self.parent) {
(None, None) => None,
(None, Some(parent)) => parent.lookup(key),
(Some(value), _) => Some(value),
}
}
}