Added linelocation to all errors

This commit is contained in:
2023-07-28 15:01:36 -07:00
parent a489eec482
commit 241bb572a5
12 changed files with 361 additions and 187 deletions

View File

@ -1,13 +1,13 @@
use crate::parser::Expression;
use crate::parser::Operator;
use crate::context::Context;
use crate::parser::LineLocation;
use super::operator::eval_operator;
use super::function::eval_function;
use super::EvalError;
pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, EvalError> {
pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (LineLocation, EvalError)> {
// Keeps track of our position in the expression tree.
// For example, the coordinates [0, 2, 1] are interpreted as follows:
@ -40,15 +40,23 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, Eva
let new = match g {
Expression::Quantity(_) => None,
Expression::Quantity(_, _) => None,
Expression::Constant(c) => { Some(evaluate(&c.value(), context).unwrap()) },
Expression::Variable(s) => { context.get_variable(&s) },
Expression::Operator(Operator::Function(f), v) => { Some(eval_function(&f, &v)?) },
Expression::Operator(o, v) => { eval_operator(&o, &v, context)? },
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) },
Expression::Variable(_, s) => { context.get_variable(&s) },
Expression::Operator(_, Operator::Function(_), _) => { Some(eval_function(g)?) },
Expression::Operator(_, _, _) => { eval_operator(g, context)? },
};
if new.is_some() { *g = new.unwrap()}
if let Some(mut new) = new {
if let Expression::Constant(_,_) = g {
// Fix constant line location.
// Constant expansion does not change the location of a value,
// but other operations might.
new.set_linelocation(&g.get_linelocation());
}
*g = new;
}
// Move up the tree
@ -62,11 +70,11 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, Eva
// Don't evaluate the first argument of a define.
// This prevents variables from being expanded before a re-assignment.
if let Expression::Operator(Operator::Define, _) = g {
*coords.last_mut().unwrap() += 1;
if let Expression::Operator(_, Operator::Define, _) = g {
*coords.last_mut().unwrap() += 1;
coords.push(0);
continue;
}
}
coords.push(0);
}

View File

@ -1,56 +1,58 @@
use std::collections::VecDeque;
use crate::parser::Expression;
use crate::parser::Function;
use crate::parser::Operator;
use crate::parser::LineLocation;
use super::EvalError;
pub fn eval_function(g: &Expression) -> Result<Expression, (LineLocation, EvalError)> {
let Expression::Operator(loc, Operator::Function(f), args) = g else {panic!()};
pub fn eval_function(f: &Function, args: &VecDeque<Expression>) -> Result<Expression, EvalError> {
if args.len() != 1 {panic!()};
let a = &args[0];
let Expression::Quantity(q) = a else {panic!()};
let Expression::Quantity(l, q) = a else {panic!()};
match f {
Function::NoUnit => { return Ok(Expression::Quantity(q.without_unit())); }
Function::ToBase => { return Ok(Expression::Quantity(q.convert_to_base())); }
Function::NoUnit => { return Ok(Expression::Quantity(*loc + *l, q.without_unit())); }
Function::ToBase => { return Ok(Expression::Quantity(*loc + *l, q.convert_to_base())); }
_ => {}
}
if !q.unitless() {
return Err(EvalError::IncompatibleUnit);
return Err((*loc + *l, EvalError::IncompatibleUnit));
}
match f {
Function::Abs => { return Ok(Expression::Quantity(q.abs())); },
Function::Floor => { return Ok(Expression::Quantity(q.floor())); },
Function::Ceil => { return Ok(Expression::Quantity(q.ceil())); },
Function::Round => { return Ok(Expression::Quantity(q.round())); },
Function::Abs => { return Ok(Expression::Quantity(*loc + *l, q.abs())); },
Function::Floor => { return Ok(Expression::Quantity(*loc + *l, q.floor())); },
Function::Ceil => { return Ok(Expression::Quantity(*loc + *l, q.ceil())); },
Function::Round => { return Ok(Expression::Quantity(*loc + *l, q.round())); },
Function::NaturalLog => { return Ok(Expression::Quantity(q.ln())); },
Function::TenLog => { return Ok(Expression::Quantity(q.log10())); },
Function::NaturalLog => { return Ok(Expression::Quantity(*loc + *l, q.ln())); },
Function::TenLog => { return Ok(Expression::Quantity(*loc + *l, q.log10())); },
Function::Sin => { return Ok(Expression::Quantity(q.sin())); },
Function::Cos => { return Ok(Expression::Quantity(q.cos())); },
Function::Tan => { return Ok(Expression::Quantity(q.tan())); },
Function::Asin => { return Ok(Expression::Quantity(q.asin())); },
Function::Acos => { return Ok(Expression::Quantity(q.acos())); },
Function::Atan => { return Ok(Expression::Quantity(q.atan())); },
Function::Sin => { return Ok(Expression::Quantity(*loc + *l, q.sin())); },
Function::Cos => { return Ok(Expression::Quantity(*loc + *l, q.cos())); },
Function::Tan => { return Ok(Expression::Quantity(*loc + *l, q.tan())); },
Function::Asin => { return Ok(Expression::Quantity(*loc + *l, q.asin())); },
Function::Acos => { return Ok(Expression::Quantity(*loc + *l, q.acos())); },
Function::Atan => { return Ok(Expression::Quantity(*loc + *l, q.atan())); },
Function::Csc => { return Ok(Expression::Quantity(q.csc())); },
Function::Sec => { return Ok(Expression::Quantity(q.sec())); },
Function::Cot => { return Ok(Expression::Quantity(q.cot())); },
Function::Csc => { return Ok(Expression::Quantity(*loc + *l, q.csc())); },
Function::Sec => { return Ok(Expression::Quantity(*loc + *l, q.sec())); },
Function::Cot => { return Ok(Expression::Quantity(*loc + *l, q.cot())); },
Function::Sinh => { return Ok(Expression::Quantity(q.sinh())); },
Function::Cosh => { return Ok(Expression::Quantity(q.cosh())); },
Function::Tanh => { return Ok(Expression::Quantity(q.tanh())); },
Function::Asinh => { return Ok(Expression::Quantity(q.asinh())); },
Function::Acosh => { return Ok(Expression::Quantity(q.acosh())); },
Function::Atanh => { return Ok(Expression::Quantity(q.atanh())); },
Function::Sinh => { return Ok(Expression::Quantity(*loc + *l, q.sinh())); },
Function::Cosh => { return Ok(Expression::Quantity(*loc + *l, q.cosh())); },
Function::Tanh => { return Ok(Expression::Quantity(*loc + *l, q.tanh())); },
Function::Asinh => { return Ok(Expression::Quantity(*loc + *l, q.asinh())); },
Function::Acosh => { return Ok(Expression::Quantity(*loc + *l, q.acosh())); },
Function::Atanh => { return Ok(Expression::Quantity(*loc + *l, q.atanh())); },
Function::Csch => { return Ok(Expression::Quantity(q.csch())); },
Function::Sech => { return Ok(Expression::Quantity(q.sech())); },
Function::Coth => { return Ok(Expression::Quantity(q.coth())); },
Function::Csch => { return Ok(Expression::Quantity(*loc + *l, q.csch())); },
Function::Sech => { return Ok(Expression::Quantity(*loc + *l, q.sech())); },
Function::Coth => { return Ok(Expression::Quantity(*loc + *l, q.coth())); },
Function::ToBase
| Function::NoUnit

View File

@ -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); }
}
};