Added `undefined` error

pull/2/head
Mark 2023-07-31 16:43:30 -07:00
parent b913eb2cf0
commit e412c53124
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 10 additions and 2 deletions

View File

@ -43,7 +43,11 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (Li
Expression::Quantity(_, _) => None, Expression::Quantity(_, _) => None,
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) }, 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(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
Expression::Operator(_, _, _) => { eval_operator(g, context)? }, Expression::Operator(_, _, _) => { eval_operator(g, context)? },
}; };

View File

@ -10,7 +10,8 @@ pub enum EvalError {
TooBig, TooBig,
ZeroDivision, ZeroDivision,
IncompatibleUnit, IncompatibleUnit,
BadDefineName BadDefineName,
Undefined(String)
} }
@ -31,6 +32,9 @@ impl ToString for EvalError {
}, },
EvalError::BadDefineName => { EvalError::BadDefineName => {
String::from("Invalid variable name") String::from("Invalid variable name")
},
EvalError::Undefined(s) => {
format!("{} is undefined", s)
} }
} }
} }