From d11c518721f18e6b9467e0b37ad353939d9ef238 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 21 Dec 2017 03:11:56 -0800 Subject: [PATCH] Framework for multiple environments --- src/rukka_lang/mod.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/rukka_lang/mod.rs b/src/rukka_lang/mod.rs index a925295..d66c783 100644 --- a/src/rukka_lang/mod.rs +++ b/src/rukka_lang/mod.rs @@ -6,20 +6,35 @@ use std::str::Chars; use std::collections::HashMap; pub struct EvaluatorState { - vars: HashMap + binding_stack: Vec> } impl 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 { - vars: HashMap::new(), + binding_stack: vec![default_map], } } 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> { - self.vars.get(var) + for bindings in self.binding_stack.iter().rev() { + match bindings.get(var) { + Some(x) => return Some(x), + None => (), + } + } + None } }