daisy/src/evaluate/operator.rs

173 lines
4.5 KiB
Rust
Raw Normal View History

2023-06-11 13:53:45 -07:00
use std::collections::VecDeque;
use crate::quantity::Quantity;
use crate::parser::Operator;
2023-06-16 12:58:06 -07:00
use crate::parser::Expression;
2023-06-11 13:53:45 -07:00
use super::EvalError;
2023-06-14 20:18:28 -07:00
use crate::context::Context;
2023-06-11 13:53:45 -07:00
2023-06-16 12:58:06 -07:00
pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut Context) -> Result<Option<Expression>, EvalError> {
2023-06-11 13:53:45 -07:00
match op {
2023-06-11 14:24:43 -07:00
// Handled seperately in evaluate.rs
Operator::Function(_) |
// These are never evaluated,
// but are converted to one of the following instead.
2023-06-11 13:53:45 -07:00
Operator::ImplicitMultiply |
Operator::Sqrt |
Operator::Divide |
2023-06-14 08:59:06 -07:00
Operator::DivideLong |
2023-06-11 13:53:45 -07:00
Operator::Subtract => { panic!() }
2023-06-14 20:18:28 -07:00
Operator::Define => {
if args.len() != 2 { panic!() };
let b = &args[1];
2023-06-16 12:58:06 -07:00
if let Expression::Variable(s) = &args[0] {
2023-06-14 20:18:28 -07:00
context.push_var(s.clone(), b.clone());
return Ok(Some(b.clone()));
} else { return Err(EvalError::BadDefineName); }
},
2023-06-11 13:53:45 -07:00
Operator::Negative => {
2023-06-14 20:18:28 -07:00
if args.len() != 1 { panic!() };
2023-06-11 13:53:45 -07:00
let args = &args[0];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(v) = args {
return Ok(Some(Expression::Quantity(-v.clone())));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::Flip => {
2023-06-14 20:18:28 -07:00
if args.len() != 1 { panic!() };
2023-06-11 13:53:45 -07:00
let args = &args[0];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(v) = args {
2023-06-11 13:53:45 -07:00
if v.is_zero() { return Err(EvalError::ZeroDivision); }
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(
2023-06-11 13:53:45 -07:00
Quantity::new_rational(1f64).unwrap()/v.clone()
2023-06-14 20:18:28 -07:00
)));
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::Add => {
let mut sum: Quantity;
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(s) = &args[0] {
2023-06-11 13:53:45 -07:00
sum = s.clone();
2023-06-14 20:18:28 -07:00
} else { return Ok(None); };
2023-06-11 13:53:45 -07:00
let mut i: usize = 1;
while i < args.len() {
let j = &args[i];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(v) = j {
2023-06-11 13:53:45 -07:00
if !sum.unit.compatible_with(&v.unit) {
return Err(EvalError::IncompatibleUnit);
}
sum += v.clone();
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
i += 1;
}
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(sum)));
2023-06-11 13:53:45 -07:00
},
Operator::Multiply => {
let mut prod = Quantity::new_rational(1f64).unwrap();
for i in args.iter() {
let j = i;
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(v) = j {
2023-06-11 13:53:45 -07:00
prod *= v.clone();
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
}
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(prod)));
2023-06-11 13:53:45 -07:00
},
Operator::ModuloLong
| Operator::Modulo => {
2023-06-14 20:18:28 -07:00
if args.len() != 2 { panic!() };
2023-06-11 13:53:45 -07:00
let a = &args[0];
let b = &args[1];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(va) = a {
if let Expression::Quantity(vb) = b {
2023-06-11 13:53:45 -07:00
if !(va.unitless() && vb.unitless()) {
return Err(EvalError::IncompatibleUnit);
}
if vb <= &Quantity::new_rational(1f64).unwrap() { return Err(EvalError::BadMath); }
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err(EvalError::BadMath); }
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(va.clone() % vb.clone())));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::UnitConvert
=> {
2023-06-14 20:18:28 -07:00
if args.len() != 2 { panic!() };
2023-06-11 13:53:45 -07:00
let a = &args[0];
let b = &args[1];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(va) = a {
if let Expression::Quantity(vb) = b {
2023-06-11 13:53:45 -07:00
let n = va.clone().convert_to(vb.clone());
if n.is_none() {
return Err(EvalError::IncompatibleUnit);
}
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(n.unwrap())));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::Power => {
if args.len() != 2 {panic!()};
let a = &args[0];
let b = &args[1];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(va) = a {
if let Expression::Quantity(vb) = b {
2023-06-11 13:53:45 -07:00
if !vb.unitless() {
return Err(EvalError::IncompatibleUnit);
}
if va.is_zero() && vb.is_negative() {
return Err(EvalError::ZeroDivision);
}
let p = va.pow(vb.clone());
if p.is_nan() {return Err(EvalError::BadMath);}
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(p)));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::Factorial => {
if args.len() != 1 {panic!()};
let args = &args[0];
2023-06-16 12:58:06 -07:00
if let Expression::Quantity(v) = args {
2023-06-11 13:53:45 -07:00
if !v.unitless() {
return Err(EvalError::IncompatibleUnit);
}
if !v.fract().is_zero() { return Err(EvalError::BadMath); }
if v > &Quantity::new_rational(50_000f64).unwrap() { return Err(EvalError::TooBig); }
let mut prod = Quantity::new_rational(1f64).unwrap();
let mut u = v.clone();
while u > Quantity::new_rational(0f64).unwrap() {
prod *= u.clone();
u = u - Quantity::new_rational(1f64).unwrap();
}
2023-06-16 12:58:06 -07:00
return Ok(Some(Expression::Quantity(prod)));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
}
};
}