daisy/src/evaluate/operator.rs

235 lines
6.4 KiB
Rust
Raw Normal View History

2023-08-03 14:39:53 -07:00
use std::collections::VecDeque;
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-14 20:18:28 -07:00
use crate::context::Context;
use crate::errors::DaisyError;
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, DaisyError)> {
2023-07-28 15:01:36 -07:00
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-08-04 21:33:42 -07:00
Operator::UserFunction(_) => unimplemented!(),
2023-08-03 14:39:53 -07:00
Operator::Tuple => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
let mut loc = *op_loc;
let mut vec: VecDeque<Expression> = VecDeque::new();
if let Expression::Tuple(l, v) = a {
loc += *l;
for i in v { vec.push_back(i.clone()) }
} else {
loc += a.get_linelocation();
vec.push_back(a.clone())
}
if let Expression::Tuple(l, v) = b {
loc += *l;
for i in v { vec.push_back(i.clone()) }
} else {
loc += b.get_linelocation();
vec.push_back(b.clone())
}
return Ok(Some(Expression::Tuple(loc, vec)))
}
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 {
2023-08-01 10:34:54 -07:00
return Ok(Some(Expression::Quantity(*l + *op_loc, -v.clone())));
2023-06-14 20:18:28 -07:00
} else { return Ok(None); }
2023-06-11 13:53:45 -07:00
},
Operator::Add => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
2023-06-11 13:53:45 -07:00
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
if !a.unit.compatible_with(&b.unit) {
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
a.convert_to_base().unit.to_string(),
b.convert_to_base().unit.to_string()
)
));
2023-07-28 15:01:36 -07:00
}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
2023-07-28 15:01:36 -07:00
}
2023-06-11 13:53:45 -07:00
}
2023-07-28 15:01:36 -07:00
return Ok(None);
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 {
if !a.unit.compatible_with(&b.unit) {
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
a.convert_to_base().unit.to_string(),
b.convert_to_base().unit.to_string()
)
));
}
2023-08-01 10:34:54 -07:00
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, 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 + *op_loc, DaisyError::ZeroDivision)); }
2023-08-01 10:34:54 -07:00
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, 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 => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
}
2023-06-11 13:53:45 -07:00
}
return Ok(None);
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()) {
return Err((*la + *lb + *op_loc, DaisyError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
if vb <= &Quantity::new_rational(1f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
if va.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::BadMath)); }
if vb.fract() != Quantity::new_rational(0f64).unwrap() { return Err((*la + *lb + *op_loc, DaisyError::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-08-01 09:43:47 -07:00
return Err((
*la + *lb + *op_loc,
DaisyError::IncompatibleUnits(
2023-08-01 09:43:47 -07:00
va.convert_to_base().unit.to_string(),
vb.convert_to_base().unit.to_string()
)
));
2023-06-11 13:53:45 -07:00
}
2023-08-01 10:34:54 -07:00
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, 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, DaisyError::BadMath)); }
2023-07-28 15:01:36 -07:00
let p = v.pow(Quantity::new_rational_from_string("0.5").unwrap());
if p.is_nan() {return Err((*l + *op_loc, DaisyError::BadMath));}
2023-07-28 15:01:36 -07:00
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() {
return Err((*lb, DaisyError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
if va.is_zero() && vb.is_negative() {
return Err((*la + *lb + *op_loc, DaisyError::ZeroDivision));
2023-06-11 13:53:45 -07:00
}
let p = va.pow(vb.clone());
if p.is_nan() {return Err((*la + *lb + *op_loc, DaisyError::BadMath));}
2023-07-28 15:01:36 -07:00
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() {
return Err((*l + *op_loc, DaisyError::IncompatibleUnit));
2023-06-11 13:53:45 -07:00
}
if !v.fract().is_zero() { return Err((*l + *op_loc, DaisyError::BadMath)); }
if v > &Quantity::new_rational(50_000f64).unwrap() { return Err((*l + *op_loc, DaisyError::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
}
};
}