summaryrefslogtreecommitdiff
path: root/src/semantic/environment.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/semantic/environment.rs')
-rw-r--r--src/semantic/environment.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/semantic/environment.rs b/src/semantic/environment.rs
new file mode 100644
index 0000000..fab16b3
--- /dev/null
+++ b/src/semantic/environment.rs
@@ -0,0 +1,33 @@
+use std::collections::HashMap;
+use std::hash::Hash;
+
+pub struct Environment<'a, T: 'a, U: 'a> {
+ pub parent: Option<&'a Environment<'a, T, U>>,
+ pub bindings: HashMap<T, U>,
+}
+
+impl<'a, T: 'a, U: 'a> Environment<'a, T, U>
+ where T: Eq + Hash {
+ pub fn new(parent: Option<&'a Environment<'a, T, U>>) -> Environment<'a, T, U> {
+ Environment {
+ parent: parent,
+ bindings: HashMap::new(),
+ }
+ }
+
+ pub fn add_binding(&mut self, key: T, value: U) {
+ self.bindings.insert(key, value);
+ }
+
+ pub fn lookup(&self, key: &T) -> Option<&U> {
+ if let Some(v) = self.bindings.get(key) {
+ Some(v)
+ }
+ else if let Some(p) = self.parent {
+ p.lookup(key)
+ }
+ else {
+ None
+ }
+ }
+}