mirror of
https://github.com/rm-dr/daisy
synced 2025-10-10 20:42:37 -07:00
Added linelocation to all errors
This commit is contained in:
@ -1,12 +1,15 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::parser::LineLocation;
|
||||
use crate::quantity::Quantity;
|
||||
use crate::parser::Operator;
|
||||
use crate::parser::Expression;
|
||||
use super::EvalError;
|
||||
use crate::context::Context;
|
||||
|
||||
pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut Context) -> Result<Option<Expression>, EvalError> {
|
||||
|
||||
pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Expression>, (LineLocation, EvalError)> {
|
||||
|
||||
let Expression::Operator(op_loc, op, args) = g else {panic!()};
|
||||
|
||||
match op {
|
||||
Operator::Function(_) => unreachable!("Functions are handled seperately."),
|
||||
|
||||
@ -14,42 +17,61 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
if args.len() != 2 { panic!() };
|
||||
let b = &args[1];
|
||||
|
||||
if let Expression::Variable(s) = &args[0] {
|
||||
if let Expression::Variable(l, s) = &args[0] {
|
||||
let r = context.push_var(s.clone(), b.clone());
|
||||
if r.is_err() { return Err(EvalError::BadDefineName); }
|
||||
if r.is_err() { return Err((*l, EvalError::BadDefineName)); }
|
||||
return Ok(Some(b.clone()));
|
||||
} else { return Err(EvalError::BadDefineName); }
|
||||
} else { return Err((args[0].get_linelocation(), EvalError::BadDefineName)); }
|
||||
},
|
||||
|
||||
Operator::Negative => {
|
||||
if args.len() != 1 { panic!() };
|
||||
let args = &args[0];
|
||||
|
||||
if let Expression::Quantity(v) = args {
|
||||
return Ok(Some(Expression::Quantity(-v.clone())));
|
||||
if let Expression::Quantity(l, v) = args {
|
||||
return Ok(Some(Expression::Quantity(*l, -v.clone())));
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
|
||||
Operator::Add => {
|
||||
let mut sum: Quantity;
|
||||
if let Expression::Quantity(s) = &args[0] {
|
||||
let mut loc: LineLocation;
|
||||
if let Expression::Quantity(l, s) = &args[0] {
|
||||
sum = s.clone();
|
||||
loc = l.clone();
|
||||
} else { return Ok(None); };
|
||||
|
||||
|
||||
// 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(v) = j {
|
||||
if let Expression::Quantity(l, v) = j {
|
||||
|
||||
if !sum.unit.compatible_with(&v.unit) {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
incompatible_units = true;
|
||||
}
|
||||
|
||||
sum += v.clone();
|
||||
} else { return Ok(None); }
|
||||
if !incompatible_units { sum += v.clone(); }
|
||||
loc += *l;
|
||||
} else {
|
||||
if incompatible_units {
|
||||
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(sum)));
|
||||
|
||||
if incompatible_units {
|
||||
return Err((loc + *op_loc, EvalError::IncompatibleUnit));
|
||||
}
|
||||
|
||||
return Ok(Some(Expression::Quantity(loc + *op_loc, sum)));
|
||||
},
|
||||
|
||||
Operator::Subtract => {
|
||||
@ -57,9 +79,9 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
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())));
|
||||
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())));
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,10 +95,10 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
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())));
|
||||
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())));
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,14 +107,23 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
|
||||
Operator::ImplicitMultiply |
|
||||
Operator::Multiply => {
|
||||
let mut prod = Quantity::new_rational(1f64).unwrap();
|
||||
for i in args.iter() {
|
||||
let j = i;
|
||||
if let Expression::Quantity(v) = j {
|
||||
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 {
|
||||
prod *= v.clone();
|
||||
loc += *l;
|
||||
} else { return Ok(None); }
|
||||
i += 1;
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(prod)));
|
||||
return Ok(Some(Expression::Quantity(loc + *op_loc, prod)));
|
||||
},
|
||||
|
||||
Operator::ModuloLong
|
||||
@ -101,18 +132,18 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
if let Expression::Quantity(la, va) = a {
|
||||
if let Expression::Quantity(lb, vb) = b {
|
||||
|
||||
if !(va.unitless() && vb.unitless()) {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
return Err((*la + *lb + *op_loc, 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); }
|
||||
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)); }
|
||||
|
||||
return Ok(Some(Expression::Quantity(va.clone() % vb.clone())));
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, va.clone() % vb.clone())));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -122,13 +153,13 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
if let Expression::Quantity(la, va) = a {
|
||||
if let Expression::Quantity(lb, vb) = b {
|
||||
let n = va.clone().convert_to(vb.clone());
|
||||
if n.is_none() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
return Err((*la + *lb + *op_loc, EvalError::IncompatibleUnit));
|
||||
}
|
||||
return Ok(Some(Expression::Quantity(n.unwrap())));
|
||||
return Ok(Some(Expression::Quantity(*la + *lb, n.unwrap())));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -138,11 +169,11 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
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)));
|
||||
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)));
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
|
||||
@ -151,20 +182,20 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Expression::Quantity(va) = a {
|
||||
if let Expression::Quantity(vb) = b {
|
||||
if let Expression::Quantity(la, va) = a {
|
||||
if let Expression::Quantity(lb, vb) = b {
|
||||
|
||||
if !vb.unitless() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
return Err((*lb, EvalError::IncompatibleUnit));
|
||||
}
|
||||
|
||||
if va.is_zero() && vb.is_negative() {
|
||||
return Err(EvalError::ZeroDivision);
|
||||
return Err((*la + *lb + *op_loc, EvalError::ZeroDivision));
|
||||
}
|
||||
|
||||
let p = va.pow(vb.clone());
|
||||
if p.is_nan() {return Err(EvalError::BadMath);}
|
||||
return Ok(Some(Expression::Quantity(p)));
|
||||
if p.is_nan() {return Err((*la + *lb + *op_loc, EvalError::BadMath));}
|
||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, p)));
|
||||
} else { return Ok(None); }
|
||||
} else { return Ok(None); }
|
||||
},
|
||||
@ -173,14 +204,14 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
if args.len() != 1 {panic!()};
|
||||
let args = &args[0];
|
||||
|
||||
if let Expression::Quantity(v) = args {
|
||||
if let Expression::Quantity(l, v) = args {
|
||||
|
||||
if !v.unitless() {
|
||||
return Err(EvalError::IncompatibleUnit);
|
||||
return Err((*l + *op_loc, EvalError::IncompatibleUnit));
|
||||
}
|
||||
|
||||
if !v.fract().is_zero() { return Err(EvalError::BadMath); }
|
||||
if v > &Quantity::new_rational(50_000f64).unwrap() { return Err(EvalError::TooBig); }
|
||||
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)); }
|
||||
|
||||
let mut prod = Quantity::new_rational(1f64).unwrap();
|
||||
let mut u = v.clone();
|
||||
@ -189,7 +220,7 @@ pub fn eval_operator(op: &Operator, args: &VecDeque<Expression>, context: &mut C
|
||||
u = u - Quantity::new_rational(1f64).unwrap();
|
||||
}
|
||||
|
||||
return Ok(Some(Expression::Quantity(prod)));
|
||||
return Ok(Some(Expression::Quantity(*l + *op_loc, prod)));
|
||||
} else { return Ok(None); }
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user