diff --git a/TODO.md b/TODO.md index 7a69a8e..67bf891 100644 --- a/TODO.md +++ b/TODO.md @@ -1,12 +1,6 @@ ## Pre-release - Commands to list constants, units, and substitutions - list and delete variables - - Re-evaluate variables (a = q + 2, q = 3, a should evaluate to 5) - - - - - ## Parser - Better error when `sin = 2` diff --git a/src/evaluate/evaluate.rs b/src/evaluate/evaluate.rs index 2484c5a..c70a086 100644 --- a/src/evaluate/evaluate.rs +++ b/src/evaluate/evaluate.rs @@ -38,14 +38,21 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result= g.get_args().unwrap().len())) } { + // If true, move to the next node + let mut move_up = true; let new = match g { Expression::Quantity(_, _) => None, Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) }, Expression::Variable(l, s) => { + move_up = false; // Don't move up, re-evaluate let v = context.get_variable(&s); + + // Error if variable is undefined. + // Comment this to allow floating varables. if v.is_none() { return Err((*l, EvalError::Undefined(s.clone()))); } + v }, Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) }, @@ -60,14 +67,19 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result