use crate::parser::Expression; use std::collections::HashMap; #[derive(Debug)] pub struct Context { history: Vec, variables: HashMap } impl Context { pub fn new() -> Context { Context{ history: Vec::new(), variables: HashMap::new() } } pub fn push_hist(&mut self, t: Expression) { self.history.push(t); } pub fn push_var(&mut self, s: String, t: Expression) -> Result<(), ()> { if self.valid_varible(&s) { self.variables.insert(s, t); return Ok(()); } else { return Err(()); } } pub fn del_var(&mut self, s: &String) { self.variables.remove(s); } pub fn get_variable(&self, s: &String) -> Option { let v: Option<&Expression>; if s == "ans" { v = self.history.last(); } else { v = self.variables.get(s); } if v.is_some() { Some(v.unwrap().clone()) } else { None } } pub fn valid_varible(&self, s: &str) -> bool { return match s { "ans" => false, _ => true } } pub fn get_variables(&self) -> &HashMap { return &self.variables } }