Added variable re-evaluation

pull/2/head
Mark 2023-08-02 12:49:14 -07:00
parent 62a98afb87
commit 2d69e73897
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 17 additions and 11 deletions

View File

@ -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`

View File

@ -38,14 +38,21 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (Li
(coords.len() != 0 && (*coords.last().unwrap() >= 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<Expression, (Li
new.set_linelocation(&g.get_linelocation());
}
*g = new;
} else {
// Always move up if we couldn't evaluate this node.
move_up = true;
}
if move_up {
// Move up the tree
coords.pop();
if coords.len() != 0 {
*coords.last_mut().unwrap() += 1;
} else { break; }
}
} else {
// Move down the tree