From 111657b567889763f55bfa4db5636b2ce9d2e95e Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 10 May 2018 22:23:42 -0700 Subject: [PATCH] Add generic stack data structure I'll want to use this soon enough --- schala-lang/src/lib.rs | 1 + schala-lang/src/util.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 schala-lang/src/util.rs diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index c67e47d..93a8b9d 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -17,6 +17,7 @@ macro_rules! bx { ($e:expr) => { Box::new($e) } } +mod util; mod builtin; mod tokenizing; mod parsing; diff --git a/schala-lang/src/util.rs b/schala-lang/src/util.rs new file mode 100644 index 0000000..2bf46eb --- /dev/null +++ b/schala-lang/src/util.rs @@ -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 +} + +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), + } + } +} +