daisy/src/evaluate/operator.rs

196 lines
5.1 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-17 19:08:05 -07:00
Operator::Function(_) => unreachable!("Functions are handled seperately."),
2023-06-11 13:53:45 -07:00
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-17 22:15:58 -07:00
let r = context.push_var(s.clone(), b.clone());
if r.is_err() { return Err(EvalError::BadDefineName); }
2023-06-14 20:18:28 -07:00
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::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
},
2023-06-17 19:08:05 -07:00
Operator::Subtract => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
if let Expression::Quantity(a) = a {
if let Expression::Quantity(b) = b {
return Ok(Some(Expression::Quantity(a.clone() - b.clone())));
}
}
return Ok(None);
},
Operator::Divide |
Operator::DivideLong => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
if let Expression::Quantity(a) = a {
if let Expression::Quantity(b) = b {
if b.is_zero() { return Err(EvalError::ZeroDivision); }
return Ok(Some(Expression::Quantity(a.clone() / b.clone())));
}
}
return Ok(None);
},
Operator::ImplicitMultiply |
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
},
2023-06-17 19:08:05 -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
},
2023-06-17 19:08:05 -07:00
Operator::Sqrt => {
if args.len() != 1 { panic!() }
let a = &args[0];
if let Expression::Quantity(va) = a {
if va.is_negative() { return Err(EvalError::BadMath); }
let p = va.pow(Quantity::new_rational_from_string("0.5").unwrap());
if p.is_nan() {return Err(EvalError::BadMath);}
return Ok(Some(Expression::Quantity(p)));
} 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
}
};
}