mirror of https://github.com/rm-dr/daisy
Added `undefined` error
parent
b913eb2cf0
commit
e412c53124
|
@ -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)? },
|
||||||
};
|
};
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue