From 41bf1954a10815c0dda74b3b0bd53130381b789c Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 17 Jun 2023 22:15:58 -0700 Subject: [PATCH] Fixed variable parse order --- src/context.rs | 18 ++++++++++++++---- src/evaluate/evaluate.rs | 12 +++++++----- src/evaluate/operator.rs | 3 ++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/context.rs b/src/context.rs index f6d3af1..f44d8be 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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 { - 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 + } + } + } diff --git a/src/evaluate/evaluate.rs b/src/evaluate/evaluate.rs index cc5cdb1..b6c699c 100644 --- a/src/evaluate/evaluate.rs +++ b/src/evaluate/evaluate.rs @@ -59,14 +59,16 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result, context: &mut C let b = &args[1]; if let Expression::Variable(s) = &args[0] { - context.push_var(s.clone(), b.clone()); + let r = context.push_var(s.clone(), b.clone()); + if r.is_err() { return Err(EvalError::BadDefineName); } return Ok(Some(b.clone())); } else { return Err(EvalError::BadDefineName); } },