From e412c53124dce6d9dd9244865fde2a52076ea64b Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 31 Jul 2023 16:43:30 -0700 Subject: [PATCH] Added `undefined` error --- src/evaluate/evaluate.rs | 6 +++++- src/evaluate/mod.rs | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/evaluate/evaluate.rs b/src/evaluate/evaluate.rs index 54b2456..2484c5a 100644 --- a/src/evaluate/evaluate.rs +++ b/src/evaluate/evaluate.rs @@ -43,7 +43,11 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result None, Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) }, - Expression::Variable(_, s) => { context.get_variable(&s) }, + Expression::Variable(l, s) => { + let v = context.get_variable(&s); + if v.is_none() { return Err((*l, EvalError::Undefined(s.clone()))); } + v + }, Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) }, Expression::Operator(_, _, _) => { eval_operator(g, context)? }, }; diff --git a/src/evaluate/mod.rs b/src/evaluate/mod.rs index 59c305e..657d9b2 100644 --- a/src/evaluate/mod.rs +++ b/src/evaluate/mod.rs @@ -10,7 +10,8 @@ pub enum EvalError { TooBig, ZeroDivision, IncompatibleUnit, - BadDefineName + BadDefineName, + Undefined(String) } @@ -31,6 +32,9 @@ impl ToString for EvalError { }, EvalError::BadDefineName => { String::from("Invalid variable name") + }, + EvalError::Undefined(s) => { + format!("{} is undefined", s) } } }