Added formattedtext and better error class

This commit is contained in:
2023-08-03 22:13:38 -07:00
parent 8076990a41
commit e67a5c4696
14 changed files with 522 additions and 532 deletions

View File

@ -2,12 +2,20 @@ use crate::parser::Expression;
use crate::parser::Operator;
use crate::context::Context;
use crate::parser::LineLocation;
use crate::errors::DaisyError;
use super::operator::eval_operator;
use super::function::eval_function;
use super::EvalError;
pub fn evaluate(t: &Expression, context: &mut Context, allow_incomplete: bool) -> Result<Expression, (LineLocation, EvalError)> {
pub fn evaluate(
t: &Expression,
context: &mut Context,
allow_incomplete: bool
) -> Result<
Expression,
(LineLocation, DaisyError)
> {
// Keeps track of our position in the expression tree.
// For example, the coordinates [0, 2, 1] are interpreted as follows:
@ -54,7 +62,7 @@ pub fn evaluate(t: &Expression, context: &mut Context, allow_incomplete: bool) -
// Error if variable is undefined.
// Comment this to allow floating varables.
if v.is_none() { return Err((*l, EvalError::Undefined(s.clone()))); }
if v.is_none() { return Err((*l, DaisyError::Undefined(s.clone()))); }
v
},
@ -76,7 +84,7 @@ pub fn evaluate(t: &Expression, context: &mut Context, allow_incomplete: bool) -
if let Expression::Quantity(_, _) = g {}
else {
let l = g.get_linelocation();
return Err((l, EvalError::EvaluationError))
return Err((l, DaisyError::EvaluationError))
}
}

View File

@ -6,7 +6,7 @@ use crate::quantity::FreeUnit;
use crate::quantity::WholeUnit;
use crate::quantity::Quantity;
use crate::quantity::Scalar;
use super::EvalError;
use crate::errors::DaisyError;
// If unitless, do nothing
@ -26,7 +26,7 @@ fn to_radians(q: Quantity) -> Result<Quantity, ()> {
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalError)> {
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, DaisyError)> {
let Expression::Operator(loc, Operator::Function(f), args) = g else {unreachable!()};
@ -37,7 +37,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
if let Expression::Tuple(l, v) = a {
return Err((
*l + *loc,
EvalError::BadArguments(f.to_string(), 1, v.len())
DaisyError::BadArguments(f.to_string(), 1, v.len())
))
};
@ -51,102 +51,102 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
Function::Abs => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.abs()));
},
Function::Floor => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.floor()));
},
Function::Ceil => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.ceil()));
},
Function::Round => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.round()));
},
Function::NaturalLog => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.ln()));
},
Function::TenLog => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.log10()));
},
Function::Sin => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sin()));
},
Function::Cos => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cos()));
},
Function::Tan => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.tan()));
},
Function::Csc => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.csc()));
},
Function::Sec => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sec()));
},
Function::Cot => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cot()));
},
Function::Sinh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sinh()));
},
Function::Cosh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.cosh()));
},
Function::Tanh => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.tanh()));
},
Function::Csch => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.csch()));
},
Function::Sech => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.sech()));
},
Function::Coth => {
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, EvalError::IncompatibleUnit)); };
let Ok(q) = to_radians(q.clone()) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)); };
return Ok(Expression::Quantity(*loc + *l, q.coth()));
},
Function::Asin => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.asin()));
},
Function::Acos => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.acos()));
},
Function::Atan => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.atan()));
},
Function::Asinh => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.asinh()));
},
Function::Acosh => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.acosh()));
},
Function::Atanh => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
return Ok(Expression::Quantity(*loc + *l, q.atanh()));
},
@ -155,7 +155,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
Function::ToCelsius => {
let mut k = Quantity::new_rational(1f64).unwrap();
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, EvalError::IncompatibleUnit)) };
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)) };
let mut r = q.without_unit();
r += Quantity::new_rational(-273.15f64).unwrap();
@ -165,7 +165,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
Function::ToFahrenheit => {
let mut k = Quantity::new_rational(1f64).unwrap();
k.insert_unit(FreeUnit::from_whole(WholeUnit::Kelvin), Scalar::new_rational(1f64).unwrap());
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, EvalError::IncompatibleUnit)) };
let Some(q) = q.convert_to(k) else { return Err((*loc + *l, DaisyError::IncompatibleUnit)) };
let mut r = q.without_unit();
r *= Quantity::new_rational_from_frac(9i64, 5i64).unwrap();
@ -175,7 +175,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
return Ok(Expression::Quantity(*loc + *l, r));
},
Function::FromCelsius => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
let mut r = Quantity::new_rational(273.15f64).unwrap();
r += q.clone();
@ -184,7 +184,7 @@ pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalEr
return Ok(Expression::Quantity(*loc + *l, r));
},
Function::FromFahrenheit => {
if !q.unitless() { return Err((*loc + *l, EvalError::IncompatibleUnit));}
if !q.unitless() { return Err((*loc + *l, DaisyError::IncompatibleUnit));}
let mut r = q.clone();
r += Quantity::new_rational(459.67).unwrap();

View File

@ -2,50 +2,4 @@ mod operator;
mod function;
mod evaluate;
pub use self::evaluate::evaluate;
#[derive(Debug)]
pub enum EvalError {
BadMath,
TooBig,
ZeroDivision,
IncompatibleUnit,
IncompatibleUnits(String, String),
Undefined(String),
EvaluationError,
BadArguments(String, usize, usize)
}
impl ToString for EvalError {
fn to_string(&self) -> String {
match self {
EvalError::BadMath => {
String::from("Failed to evaluate expression")
},
EvalError::TooBig => {
String::from("Number too big")
},
EvalError::ZeroDivision => {
String::from("Division by zero")
},
EvalError::IncompatibleUnit => {
String::from("Incompatible unit")
},
EvalError::IncompatibleUnits(a, b) => {
format!("Incompatible units ({a} and {b})")
},
EvalError::Undefined(s) => {
format!("{s} is undefined")
},
EvalError::EvaluationError => {
String::from("Could not evaluate")
},
EvalError::BadArguments(s, want, got) => {
format!("{s} takes {want} argument{}, got {got}",
if *want == 1 {""} else {"s"},
)
}
}
}
}
pub use self::evaluate::evaluate;

View File

@ -3,11 +3,11 @@ use crate::parser::LineLocation;
use crate::quantity::Quantity;
use crate::parser::Operator;
use crate::parser::Expression;
use super::EvalError;
use crate::context::Context;
use crate::errors::DaisyError;
pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Expression>, (LineLocation, EvalError)> {
pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Expression>, (LineLocation, DaisyError)> {
let Expression::Operator(op_loc, op, args) = g else {panic!()};
@ -49,44 +49,26 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
},
Operator::Add => {
let mut sum: Quantity;
let mut loc: LineLocation;
if let Expression::Quantity(l, s) = &args[0] {
sum = s.clone();
loc = *l;
} else { return Ok(None); };
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
// 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;
let mut i: usize = 1;
while i < args.len() {
let j = &args[i];
if let Expression::Quantity(l, v) = j {
if !sum.unit.compatible_with(&v.unit) {
incompatible_units = true;
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()
)
));
}
if !incompatible_units { sum += v.clone(); }
loc += *l;
} else {
if incompatible_units {
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
}
return Ok(None);
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() + b.clone())));
}
i += 1;
}
if incompatible_units {
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
}
return Ok(Some(Expression::Quantity(loc + *op_loc, sum)));
return Ok(None);
},
Operator::Subtract => {
@ -96,6 +78,15 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
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()
)
));
}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() - b.clone())));
}
}
@ -112,7 +103,7 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
if b.is_zero() { return Err((*la + *lb + *op_loc, EvalError::ZeroDivision)); }
if b.is_zero() { return Err((*la + *lb + *op_loc, DaisyError::ZeroDivision)); }
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() / b.clone())));
}
}
@ -122,23 +113,17 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
Operator::ImplicitMultiply |
Operator::Multiply => {
let mut prod: Quantity;
let mut loc: LineLocation;
if let Expression::Quantity(l, s) = &args[0] {
prod = s.clone();
loc = *l;
} else { return Ok(None); };
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
let mut i: usize = 1;
while i < args.len() {
let j = &args[i];
if let Expression::Quantity(l, v) = j {
prod *= v.clone();
loc += *l;
} else { return Ok(None); }
i += 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())));
}
}
return Ok(Some(Expression::Quantity(loc + *op_loc, prod)));
return Ok(None);
},
Operator::ModuloLong
@ -151,12 +136,12 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
if let Expression::Quantity(lb, vb) = b {
if !(va.unitless() && vb.unitless()) {
return Err((*la + *lb + *op_loc, EvalError::IncompatibleUnit));
return Err((*la + *lb + *op_loc, DaisyError::IncompatibleUnit));
}
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)); }
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)); }
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, va.clone() % vb.clone())));
} else { return Ok(None); }
@ -174,7 +159,7 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
if n.is_none() {
return Err((
*la + *lb + *op_loc,
EvalError::IncompatibleUnits(
DaisyError::IncompatibleUnits(
va.convert_to_base().unit.to_string(),
vb.convert_to_base().unit.to_string()
)
@ -191,9 +176,9 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
let a = &args[0];
if let Expression::Quantity(l, v) = a {
if v.is_negative() { return Err((*l + *op_loc, EvalError::BadMath)); }
if v.is_negative() { return Err((*l + *op_loc, DaisyError::BadMath)); }
let p = v.pow(Quantity::new_rational_from_string("0.5").unwrap());
if p.is_nan() {return Err((*l + *op_loc, EvalError::BadMath));}
if p.is_nan() {return Err((*l + *op_loc, DaisyError::BadMath));}
return Ok(Some(Expression::Quantity(*l, p)));
} else { return Ok(None); }
},
@ -207,15 +192,15 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
if let Expression::Quantity(lb, vb) = b {
if !vb.unitless() {
return Err((*lb, EvalError::IncompatibleUnit));
return Err((*lb, DaisyError::IncompatibleUnit));
}
if va.is_zero() && vb.is_negative() {
return Err((*la + *lb + *op_loc, EvalError::ZeroDivision));
return Err((*la + *lb + *op_loc, DaisyError::ZeroDivision));
}
let p = va.pow(vb.clone());
if p.is_nan() {return Err((*la + *lb + *op_loc, EvalError::BadMath));}
if p.is_nan() {return Err((*la + *lb + *op_loc, DaisyError::BadMath));}
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, p)));
} else { return Ok(None); }
} else { return Ok(None); }
@ -228,11 +213,11 @@ pub fn eval_operator(g: &Expression, _context: &mut Context) -> Result<Option<Ex
if let Expression::Quantity(l, v) = args {
if !v.unitless() {
return Err((*l + *op_loc, EvalError::IncompatibleUnit));
return Err((*l + *op_loc, DaisyError::IncompatibleUnit));
}
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)); }
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)); }
let mut prod = Quantity::new_rational(1f64).unwrap();
let mut u = v.clone();