From 2d69e738970bd444e0d9ad6df4ebfc8f9ef97c27 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 2 Aug 2023 12:49:14 -0700 Subject: [PATCH] Added variable re-evaluation --- TODO.md | 6 ------ src/evaluate/evaluate.rs | 22 +++++++++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) 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