mirror of https://github.com/rm-dr/daisy
Fixed variable parse order
parent
1671bbbade
commit
41bf1954a1
|
@ -13,19 +13,29 @@ impl Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_hist(&mut self, t: Expression) { self.history.push(t); }
|
pub fn push_hist(&mut self, t: Expression) { self.history.push(t); }
|
||||||
pub fn push_var(&mut self, s: String, t: Expression) { self.variables.insert(s, t); }
|
pub fn push_var(&mut self, s: String, t: Expression) -> Result<(), ()> {
|
||||||
|
if self.valid_varible(&s) {
|
||||||
|
self.variables.insert(s, t);
|
||||||
|
return Ok(());
|
||||||
|
} else { return Err(()); }
|
||||||
|
}
|
||||||
pub fn del_var(&mut self, s: &String) { self.variables.remove(s); }
|
pub fn del_var(&mut self, s: &String) { self.variables.remove(s); }
|
||||||
|
|
||||||
pub fn get_variable(&self, s: &String) -> Option<Expression> {
|
pub fn get_variable(&self, s: &String) -> Option<Expression> {
|
||||||
|
|
||||||
let v: Option<&Expression>;
|
let v: Option<&Expression>;
|
||||||
|
|
||||||
if s == "ans" {
|
if s == "ans" {
|
||||||
v = self.history.last();
|
v = self.history.last();
|
||||||
} else {
|
} else {
|
||||||
v = self.variables.get(s);
|
v = self.variables.get(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.is_some() { Some(v.unwrap().clone()) } else { None }
|
if v.is_some() { Some(v.unwrap().clone()) } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn valid_varible(&self, s: &str) -> bool {
|
||||||
|
return match s {
|
||||||
|
"ans" => false,
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,14 +59,16 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, Eva
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Move down the tree
|
// Move down the tree
|
||||||
coords.push(0);
|
|
||||||
|
|
||||||
let n = root.get_at_coords(&coords[..]);
|
// Don't evaluate the first argument of a define.
|
||||||
if let Some(n) = n {
|
// This prevents variables from being expanded before a re-assignment.
|
||||||
if let Expression::Operator(Operator::Define, _) = n {
|
if let Expression::Operator(Operator::Define, _) = g {
|
||||||
*coords.last_mut().unwrap() += 1;
|
*coords.last_mut().unwrap() += 1;
|
||||||
|
coords.push(0);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
coords.push(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,8 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||||
let b = &args[1];
|
let b = &args[1];
|
||||||
|
|
||||||
if let Expression::Variable(s) = &args[0] {
|
if let Expression::Variable(s) = &args[0] {
|
||||||
context.push_var(s.clone(), b.clone());
|
let r = context.push_var(s.clone(), b.clone());
|
||||||
|
if r.is_err() { return Err(EvalError::BadDefineName); }
|
||||||
return Ok(Some(b.clone()));
|
return Ok(Some(b.clone()));
|
||||||
} else { return Err(EvalError::BadDefineName); }
|
} else { return Err(EvalError::BadDefineName); }
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue