Fixed variable parse order

This commit is contained in:
2023-06-17 22:15:58 -07:00
parent 1671bbbade
commit 41bf1954a1
3 changed files with 23 additions and 10 deletions

View File

@ -13,19 +13,29 @@ impl Context {
}
pub fn push_hist(&mut self, t: Expression) { self.history.push(t); }
pub fn push_var(&mut self, s: String, t: Expression) { self.variables.insert(s, 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<Expression> {
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
}
}
}