Fixed incompatible messages when one unit is a scalar

newfloat
Mark 2023-08-20 16:33:34 -07:00
parent 6969d17cce
commit 4055c08217
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 52 additions and 12 deletions

View File

@ -75,12 +75,26 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a { if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b { if let Expression::Quantity(lb, b) = b {
if !a.unit.compatible_with(&b.unit) { if !a.unit.compatible_with(&b.unit) {
let a = a.convert_to_base().unit;
let b = b.convert_to_base().unit;
let a_s: String;
let b_s: String;
if a.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if b.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err(( return Err((
*la + *lb + *op_loc, *la + *lb + *op_loc,
DaisyError::IncompatibleUnits( DaisyError::IncompatibleUnits(a_s, b_s)
a.convert_to_base().unit.display(context),
b.convert_to_base().unit.display(context)
)
)); ));
} }
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone()))); return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
@ -98,12 +112,24 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a { if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b { if let Expression::Quantity(lb, b) = b {
if !a.unit.compatible_with(&b.unit) { if !a.unit.compatible_with(&b.unit) {
let a_s: String;
let b_s: String;
if a.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if b.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err(( return Err((
*la + *lb + *op_loc, *la + *lb + *op_loc,
DaisyError::IncompatibleUnits( DaisyError::IncompatibleUnits(a_s, b_s)
a.convert_to_base().unit.display(context),
b.convert_to_base().unit.display(context)
)
)); ));
} }
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone()))); return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
@ -181,12 +207,26 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
if let Expression::Quantity(lb, vb) = b { if let Expression::Quantity(lb, vb) = b {
let n = va.clone().convert_to(vb.clone()); let n = va.clone().convert_to(vb.clone());
if n.is_none() { if n.is_none() {
let va = va.convert_to_base().unit;
let vb = vb.convert_to_base().unit;
let a_s: String;
let b_s: String;
if va.unitless() {
a_s = String::from("scalar");
} else {
a_s = a.display(context);
}
if vb.unitless() {
b_s = String::from("scalar");
} else {
b_s = b.display(context);
}
return Err(( return Err((
*la + *lb + *op_loc, *la + *lb + *op_loc,
DaisyError::IncompatibleUnits( DaisyError::IncompatibleUnits(a_s, b_s)
va.convert_to_base().unit.display(context),
vb.convert_to_base().unit.display(context)
)
)); ));
} }
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap()))); return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, n.unwrap())));