Framework for multiple environments

This commit is contained in:
greg 2017-12-21 03:11:56 -08:00
parent aa821e720a
commit 85aabed344

View File

@ -6,20 +6,35 @@ use std::str::Chars;
use std::collections::HashMap; use std::collections::HashMap;
pub struct EvaluatorState { pub struct EvaluatorState {
vars: HashMap<String, Sexp> binding_stack: Vec<HashMap<String, Sexp>>
} }
impl EvaluatorState { impl EvaluatorState {
fn new() -> EvaluatorState { fn new() -> EvaluatorState {
use self::Sexp::*;
let mut default_map = HashMap::new();
default_map.insert(format!("+"), Builtin("+".to_string()));
default_map.insert(format!("-"), Builtin("-".to_string()));
default_map.insert(format!("*"), Builtin("*".to_string()));
default_map.insert(format!("/"), Builtin("/".to_string()));
default_map.insert(format!("%"), Builtin("%".to_string()));
EvaluatorState { EvaluatorState {
vars: HashMap::new(), binding_stack: vec![default_map],
} }
} }
fn set_var(&mut self, var: String, value: Sexp) { fn set_var(&mut self, var: String, value: Sexp) {
self.vars.insert(var, value); let binding = self.binding_stack.last_mut().unwrap();
binding.insert(var, value);
} }
fn get_var(&self, var: &str) -> Option<&Sexp> { fn get_var(&self, var: &str) -> Option<&Sexp> {
self.vars.get(var) for bindings in self.binding_stack.iter().rev() {
match bindings.get(var) {
Some(x) => return Some(x),
None => (),
}
}
None
} }
} }