daisy/src/evaluate/operator.rs

227 lines
6.3 KiB
Rust
Raw Normal View History

2023-07-28 15:01:36 -07:00
use crate::parser::LineLocation;
2023-06-11 13:53:45 -07:00
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-07-28 15:01:36 -07:00
pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Expression>, (LineLocation, EvalError)> {
let Expression::Operator(op_loc, op, args) = g else {panic!()};
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-07-28 15:01:36 -07:00
if let Expression::Variable(l, s) = &args[0] {
2023-06-17 22:15:58 -07:00
let r = context.push_var(s.clone(), b.clone());
2023-07-28 15:01:36 -07:00
if r.is_err() { return Err((*l, EvalError::BadDefineName)); }
2023-06-14 20:18:28 -07:00
return Ok(Some(b.clone()));
2023-07-28 15:01:36 -07:00
} else { return Err((args[0].get_linelocation(), EvalError::BadDefineName)); }
2023-06-14 20:18:28 -07:00
},
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-07-28 15:01:36 -07:00
if let Expression::Quantity(l, v) = args {
return Ok(Some(Expression::Quantity(*l, -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-07-28 15:01:36 -07:00
let mut loc: LineLocation;
if let Expression::Quantity(l, s) = &args[0] {
2023-06-11 13:53:45 -07:00
sum = s.clone();
2023-07-28 15:01:36 -07:00
loc = l.clone();
2023-06-14 20:18:28 -07:00
} else { return Ok(None); };
2023-06-11 13:53:45 -07:00
2023-07-28 15:01:36 -07:00
// Flag that is set to true if we find incompatible units.
// We don't stop right away because we need to add all linelocations
// to show a pretty error.
let mut incompatible_units = false;
2023-06-11 13:53:45 -07:00
let mut i: usize = 1;
while i < args.len() {
let j = &args[i];
2023-07-28 15:01:36 -07:00
if let Expression::Quantity(l, v) = j {
2023-06-11 13:53:45 -07:00
if !sum.unit.compatible_with(&v.unit) {
2023-07-28 15:01:36 -07:00
incompatible_units = true;
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
if !incompatible_units { sum += v.clone(); }
loc += *l;
} else {
if incompatible_units {
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
}
return Ok(None);
}
2023-06-11 13:53:45 -07:00
i += 1;
}
2023-07-28 15:01:36 -07:00
if incompatible_units {
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
}
return Ok(Some(Expression::Quantity(loc + *op_loc, 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];
2023-07-28 15:01:36 -07:00
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
return Ok(Some(Expression::Quantity(*la + *lb, a.clone() - b.clone())));
2023-06-17 19:08:05 -07:00
}
}
return Ok(None);
},
Operator::Divide |
Operator::DivideLong => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
2023-07-28 15:01:36 -07:00
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
if b.is_zero() { return Err((*la + *lb, EvalError::ZeroDivision)); }
return Ok(Some(Expression::Quantity(*la + *lb, a.clone() / b.clone())));
2023-06-17 19:08:05 -07:00
}
}
return Ok(None);
},
Operator::ImplicitMultiply |
2023-06-11 13:53:45 -07:00
Operator::Multiply => {
2023-07-28 15:01:36 -07:00
let mut prod: Quantity;
let mut loc: LineLocation;
if let Expression::Quantity(l, s) = &args[0] {
prod = s.clone();
loc = l.clone();
} else { return Ok(None); };
let mut i: usize = 1;
while i < args.len() {
let j = &args[i];
if let Expression::Quantity(l, v) = j {
2023-06-11 13:53:45 -07:00
prod *= v.clone();
2023-07-28 15:01:36 -07:00
loc += *l;
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-07-28 15:01:36 -07:00
i += 1;
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
return Ok(Some(Expression::Quantity(loc + *op_loc, 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-07-28 15:01:36 -07:00
if let Expression::Quantity(la, va) = a {
if let Expression::Quantity(lb, vb) = b {
2023-06-11 13:53:45 -07:00
if !(va.unitless() && vb.unitless()) {
2023-07-28 15:01:36 -07:00
return Err((*la + *lb + *op_loc, EvalError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
if vb <= &Quantity::new_rational(1f64).unwrap() { return Err((*la + *lb + *op_loc, EvalError::BadMath)); }
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, EvalError::BadMath)); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, EvalError::BadMath)); }
2023-06-11 13:53:45 -07:00
2023-07-28 15:01:36 -07:00
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, 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-07-28 15:01:36 -07:00
if let Expression::Quantity(la, va) = a {
if let Expression::Quantity(lb, vb) = b {
2023-06-11 13:53:45 -07:00
let n = va.clone().convert_to(vb.clone());
if n.is_none() {
2023-07-28 15:01:36 -07:00
return Err((*la + *lb + *op_loc, EvalError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
return Ok(Some(Expression::Quantity(*la + *lb, 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];
2023-07-28 15:01:36 -07:00
if let Expression::Quantity(l, v) = a {
if v.is_negative() { return Err((*l + *op_loc, EvalError::BadMath)); }
let p = v.pow(Quantity::new_rational_from_string("0.5").unwrap());
if p.is_nan() {return Err((*l + *op_loc, EvalError::BadMath));}
return Ok(Some(Expression::Quantity(*l, p)));
2023-06-17 19:08:05 -07:00
} 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-07-28 15:01:36 -07:00
if let Expression::Quantity(la, va) = a {
if let Expression::Quantity(lb, vb) = b {
2023-06-11 13:53:45 -07:00
if !vb.unitless() {
2023-07-28 15:01:36 -07:00
return Err((*lb, EvalError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
if va.is_zero() && vb.is_negative() {
2023-07-28 15:01:36 -07:00
return Err((*la + *lb + *op_loc, EvalError::ZeroDivision));
2023-06-11 13:53:45 -07:00
}
let p = va.pow(vb.clone());
2023-07-28 15:01:36 -07:00
if p.is_nan() {return Err((*la + *lb + *op_loc, EvalError::BadMath));}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, 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-07-28 15:01:36 -07:00
if let Expression::Quantity(l, v) = args {
2023-06-11 13:53:45 -07:00
if !v.unitless() {
2023-07-28 15:01:36 -07:00
return Err((*l + *op_loc, EvalError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
if !v.fract().is_zero() { return Err((*l + *op_loc, EvalError::BadMath)); }
if v > &Quantity::new_rational(50_000f64).unwrap() { return Err((*l + *op_loc, EvalError::TooBig)); }
2023-06-11 13:53:45 -07:00
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-07-28 15:01:36 -07:00
return Ok(Some(Expression::Quantity(*l + *op_loc, prod)));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
}
};
}