mirror of
https://github.com/rm-dr/daisy
synced 2025-10-10 20:42:37 -07:00
Renamed token and pretoken
This commit is contained in:
@ -2,11 +2,11 @@ use std::collections::VecDeque;
|
||||
|
||||
use crate::quantity::Quantity;
|
||||
use crate::parser::Operator;
|
||||
use crate::parser::Token;
|
||||
use crate::parser::Expression;
|
||||
use super::EvalError;
|
||||
use crate::context::Context;
|
||||
|
||||
pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Context) -> Result<Option<Token>, EvalError> {
|
||||
pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut Context) -> Result<Option<Expression>, EvalError> {
|
||||
match op {
|
||||
|
||||
// Handled seperately in evaluate.rs
|
||||
@ -24,7 +24,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
if args.len() != 2 { panic!() };
|
||||
let b = &args[1];
|
||||
|
||||
if let Token::Variable(s) = &args[0] {
|
||||
if let Expression::Variable(s) = &args[0] {
|
||||
context.push_var(s.clone(), b.clone());
|
||||
return Ok(Some(b.clone()));
|
||||
} else { return Err(EvalError::BadDefineName); }
|
||||
@ -34,8 +34,8 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
if args.len() != 1 { panic!() };
|
||||
let args = &args[0];
|
||||
|
||||
if let Token::Quantity(v) = args {
|
||||
return Ok(Some(Token::Quantity(-v.clone())));
|
||||
if let Expression::Quantity(v) = args {
|
||||
return Ok(Some(Expression::Quantity(-v.clone())));
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
|
||||
@ -43,9 +43,9 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
if args.len() != 1 { panic!() };
|
||||
let args = &args[0];
|
||||
|
||||
if let Token::Quantity(v) = args {
|
||||
if let Expression::Quantity(v) = args {
|
||||
if v.is_zero() { return Err(EvalError::ZeroDivision); }
|
||||
return Ok(Some(Token::Quantity(
|
||||
return Ok(Some(Expression::Quantity(
|
||||
Quantity::new_rational(1f64).unwrap()/v.clone()
|
||||
)));
|
||||
} else { return Ok(None); }
|
||||
@ -53,14 +53,14 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
|
||||
Operator::Add => {
|
||||
let mut sum: Quantity;
|
||||
if let Token::Quantity(s) = &args[0] {
|
||||
if let Expression::Quantity(s) = &args[0] {
|
||||
sum = s.clone();
|
||||
} else { return Ok(None); };
|
||||
|
||||
let mut i: usize = 1;
|
||||
while i < args.len() {
|
||||
let j = &args[i];
|
||||
if let Token::Quantity(v) = j {
|
||||
if let Expression::Quantity(v) = j {
|
||||
|
||||
if !sum.unit.compatible_with(&v.unit) {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
@ -70,18 +70,18 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
} else { return Ok(None); }
|
||||
i += 1;
|
||||
}
|
||||
return Ok(Some(Token::Quantity(sum)));
|
||||
return Ok(Some(Expression::Quantity(sum)));
|
||||
},
|
||||
|
||||
Operator::Multiply => {
|
||||
let mut prod = Quantity::new_rational(1f64).unwrap();
|
||||
for i in args.iter() {
|
||||
let j = i;
|
||||
if let Token::Quantity(v) = j {
|
||||
if let Expression::Quantity(v) = j {
|
||||
prod *= v.clone();
|
||||
} else { return Ok(None); }
|
||||
}
|
||||
return Ok(Some(Token::Quantity(prod)));
|
||||
return Ok(Some(Expression::Quantity(prod)));
|
||||
},
|
||||
|
||||
Operator::ModuloLong
|
||||
@ -90,8 +90,8 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Token::Quantity(va) = a {
|
||||
if let Token::Quantity(vb) = b {
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
|
||||
if !(va.unitless() && vb.unitless()) {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
@ -101,7 +101,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
|
||||
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
|
||||
|
||||
return Ok(Some(Token::Quantity(va.clone() % vb.clone())));
|
||||
return Ok(Some(Expression::Quantity(va.clone() % vb.clone())));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -112,13 +112,13 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Token::Quantity(va) = a {
|
||||
if let Token::Quantity(vb) = b {
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
let n = va.clone().convert_to(vb.clone());
|
||||
if n.is_none() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
}
|
||||
return Ok(Some(Token::Quantity(n.unwrap())));
|
||||
return Ok(Some(Expression::Quantity(n.unwrap())));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -128,8 +128,8 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Token::Quantity(va) = a {
|
||||
if let Token::Quantity(vb) = b {
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
|
||||
if !vb.unitless() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
@ -141,7 +141,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
|
||||
let p = va.pow(vb.clone());
|
||||
if p.is_nan() {return Err(EvalError::BadMath);}
|
||||
return Ok(Some(Token::Quantity(p)));
|
||||
return Ok(Some(Expression::Quantity(p)));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -150,7 +150,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
if args.len() != 1 {panic!()};
|
||||
let args = &args[0];
|
||||
|
||||
if let Token::Quantity(v) = args {
|
||||
if let Expression::Quantity(v) = args {
|
||||
|
||||
if !v.unitless() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
@ -166,7 +166,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Token>, context: &mut Contex
|
||||
u = u - Quantity::new_rational(1f64).unwrap();
|
||||
}
|
||||
|
||||
return Ok(Some(Token::Quantity(prod)));
|
||||
return Ok(Some(Expression::Quantity(prod)));
|
||||
} else { return Ok(None); }
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user