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) } } }