mirror of https://github.com/rm-dr/daisy
Added linelocation to all errors
parent
a489eec482
commit
241bb572a5
|
@ -59,24 +59,24 @@ fn do_expression(
|
|||
let Ok(g) = g else {panic!()};
|
||||
|
||||
|
||||
// Display parsed string
|
||||
write!(
|
||||
stdout, " {}{}=>{}{} {}\r\n",
|
||||
style::Bold, color::Fg(color::Magenta),
|
||||
style::Reset, color::Fg(color::Reset),
|
||||
g.to_string()
|
||||
).unwrap();
|
||||
|
||||
|
||||
// Evaluate expression
|
||||
#[cfg(debug_assertions)]
|
||||
RawTerminal::suspend_raw_mode(&stdout).unwrap();
|
||||
let g = evaluate(&g, context);
|
||||
let g_evaluated = evaluate(&g, context);
|
||||
#[cfg(debug_assertions)]
|
||||
RawTerminal::activate_raw_mode(&stdout).unwrap();
|
||||
|
||||
// Show output
|
||||
if let Ok(q) = g {
|
||||
if let Ok(q) = g_evaluated {
|
||||
// Display parsed string
|
||||
write!(
|
||||
stdout, " {}{}=>{}{} {}\r\n",
|
||||
style::Bold, color::Fg(color::Magenta),
|
||||
style::Reset, color::Fg(color::Reset),
|
||||
g.to_string()
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, "\n {}{}={} {}{}\r\n\n",
|
||||
style::Bold,
|
||||
|
@ -88,12 +88,22 @@ fn do_expression(
|
|||
return Ok(q);
|
||||
|
||||
} else {
|
||||
match g {
|
||||
match g_evaluated {
|
||||
Ok(_) => unreachable!(),
|
||||
|
||||
Err(EvalError::TooBig) => {
|
||||
Err((l, EvalError::TooBig)) => {
|
||||
write!(
|
||||
stdout, "\n {}{}Mathematical Error: {}Number too big{}\r\n\n",
|
||||
stdout, "{}{}{}{}{}{}\r\n",
|
||||
color::Fg(color::Red),
|
||||
style::Bold,
|
||||
" ".repeat(l.pos + 4),
|
||||
"^".repeat(l.len),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, " {}{}Mathematical Error: {}Number too big{}\r\n\n",
|
||||
style::Bold,
|
||||
color::Fg(color::Red),
|
||||
style::Reset,
|
||||
|
@ -101,9 +111,19 @@ fn do_expression(
|
|||
).unwrap();
|
||||
},
|
||||
|
||||
Err(EvalError::ZeroDivision) => {
|
||||
Err((l, EvalError::ZeroDivision)) => {
|
||||
write!(
|
||||
stdout, "\n {}{}Mathematical Error: {}Division by zero{}\r\n\n",
|
||||
stdout, "{}{}{}{}{}{}\r\n",
|
||||
color::Fg(color::Red),
|
||||
style::Bold,
|
||||
" ".repeat(l.pos + 4),
|
||||
"^".repeat(l.len),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, " {}{}Mathematical Error: {}Division by zero{}\r\n\n",
|
||||
style::Bold,
|
||||
color::Fg(color::Red),
|
||||
style::Reset,
|
||||
|
@ -111,9 +131,19 @@ fn do_expression(
|
|||
).unwrap();
|
||||
},
|
||||
|
||||
Err(EvalError::BadMath) => {
|
||||
Err((l, EvalError::BadMath)) => {
|
||||
write!(
|
||||
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",
|
||||
stdout, "{}{}{}{}{}{}\r\n",
|
||||
color::Fg(color::Red),
|
||||
style::Bold,
|
||||
" ".repeat(l.pos + 4),
|
||||
"^".repeat(l.len),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, " {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",
|
||||
style::Bold,
|
||||
color::Fg(color::Red),
|
||||
style::Reset,
|
||||
|
@ -121,9 +151,19 @@ fn do_expression(
|
|||
).unwrap();
|
||||
},
|
||||
|
||||
Err(EvalError::IncompatibleUnit) => {
|
||||
Err((l, EvalError::IncompatibleUnit)) => {
|
||||
write!(
|
||||
stdout, "\n {}{}Evaluation Error: {}Incompatible units{}\r\n\n",
|
||||
stdout, "{}{}{}{}{}{}\r\n",
|
||||
color::Fg(color::Red),
|
||||
style::Bold,
|
||||
" ".repeat(l.pos + 4),
|
||||
"^".repeat(l.len),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, " {}{}Evaluation Error: {}Incompatible units{}\r\n\n",
|
||||
style::Bold,
|
||||
color::Fg(color::Red),
|
||||
style::Reset,
|
||||
|
@ -131,9 +171,19 @@ fn do_expression(
|
|||
).unwrap();
|
||||
},
|
||||
|
||||
Err(EvalError::BadDefineName) => {
|
||||
Err((l, EvalError::BadDefineName)) => {
|
||||
write!(
|
||||
stdout, "\n {}{}Evaluation Error: {}Invalid variable name{}\r\n\n",
|
||||
stdout, "{}{}{}{}{}{}\r\n",
|
||||
color::Fg(color::Red),
|
||||
style::Bold,
|
||||
" ".repeat(l.pos + 4),
|
||||
"^".repeat(l.len),
|
||||
color::Fg(color::Reset),
|
||||
style::Reset,
|
||||
).unwrap();
|
||||
|
||||
write!(
|
||||
stdout, " {}{}Evaluation Error: {}Invalid variable name{}\r\n\n",
|
||||
style::Bold,
|
||||
color::Fg(color::Red),
|
||||
style::Reset,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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); }
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3,24 +3,26 @@ use crate::quantity::Quantity;
|
|||
|
||||
use super::Operator;
|
||||
use super::Constant;
|
||||
use super::super::LineLocation;
|
||||
|
||||
|
||||
/// Expressions represent logical objects in an expession.
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub enum Expression {
|
||||
Variable(String),
|
||||
Quantity(Quantity),
|
||||
Constant(Constant),
|
||||
Operator(Operator, VecDeque<Expression>),
|
||||
Variable(LineLocation, String),
|
||||
Quantity(LineLocation, Quantity),
|
||||
Constant(LineLocation, Constant),
|
||||
Operator(LineLocation, Operator, VecDeque<Expression>),
|
||||
}
|
||||
|
||||
impl ToString for Expression {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
Expression::Quantity(v) => v.to_string(),
|
||||
Expression::Constant(c) => c.to_string(),
|
||||
Expression::Variable(s) => s.clone(),
|
||||
Expression::Operator(o,a) => o.print(a)
|
||||
Expression::Quantity(_, v) => v.to_string(),
|
||||
Expression::Constant(_, c) => c.to_string(),
|
||||
Expression::Variable(_, s) => s.clone(),
|
||||
Expression::Operator(_, o,a) => o.print(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,16 +32,16 @@ impl Expression {
|
|||
// This sometimes leads to different--usually more verbose--behavior.
|
||||
pub fn to_string_outer(&self) -> String {
|
||||
match self {
|
||||
Expression::Quantity(v) => v.to_string_outer(),
|
||||
Expression::Constant(c) => c.to_string(),
|
||||
Expression::Variable(s) => s.clone(),
|
||||
Expression::Operator(o,a) => o.print(a)
|
||||
Expression::Quantity(_, v) => v.to_string_outer(),
|
||||
Expression::Constant(_, c) => c.to_string(),
|
||||
Expression::Variable(_, s) => s.clone(),
|
||||
Expression::Operator(_, o,a) => o.print(a)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_quantity(&self) -> bool {
|
||||
match self {
|
||||
Expression::Quantity(_) => true,
|
||||
Expression::Quantity(_,_) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +49,7 @@ impl Expression {
|
|||
#[inline(always)]
|
||||
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Expression>> {
|
||||
match self {
|
||||
Expression::Operator(_, ref mut a) => Some(a),
|
||||
Expression::Operator(_, _, ref mut a) => Some(a),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +57,7 @@ impl Expression {
|
|||
#[inline(always)]
|
||||
pub fn get_args(&self) -> Option<&VecDeque<Expression>> {
|
||||
match self {
|
||||
Expression::Operator(_, ref a) => Some(a),
|
||||
Expression::Operator(_, _, ref a) => Some(a),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
@ -83,4 +85,23 @@ impl Expression {
|
|||
}
|
||||
return Some(g);
|
||||
}
|
||||
|
||||
pub fn get_linelocation(&self) -> LineLocation {
|
||||
match self {
|
||||
Expression::Quantity(l, _)
|
||||
| Expression::Constant(l, _)
|
||||
| Expression::Variable(l, _)
|
||||
| Expression::Operator(l, _,_)
|
||||
=> { l.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_linelocation(&mut self, loc: &LineLocation) {
|
||||
match self {
|
||||
Expression::Quantity(l, _) => { *l = *loc },
|
||||
Expression::Constant(l, _) => { *l = *loc },
|
||||
Expression::Variable(l, _) => { *l = *loc },
|
||||
Expression::Operator(l, _,_) => { *l = *loc },
|
||||
}
|
||||
}
|
||||
}
|
|
@ -110,7 +110,7 @@ impl Operator {
|
|||
#[inline(always)]
|
||||
fn add_parens_to_arg(&self, arg: &Expression) -> String {
|
||||
let mut astr: String = arg.to_string();
|
||||
if let Expression::Operator(o,_) = arg {
|
||||
if let Expression::Operator(_, o,_) = arg {
|
||||
if o < self {
|
||||
astr = format!("({})", astr);
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ impl Operator {
|
|||
#[inline(always)]
|
||||
fn add_parens_to_arg_strict(&self, arg: &Expression) -> String {
|
||||
let mut astr: String = arg.to_string();
|
||||
if let Expression::Operator(o,_) = arg {
|
||||
if let Expression::Operator(_, o,_) = arg {
|
||||
if o <= self {
|
||||
astr = format!("({})", astr);
|
||||
}
|
||||
|
@ -220,15 +220,15 @@ impl Operator {
|
|||
// multiplied by a unit (like 10 m)
|
||||
// Times sign should stay in all other cases.
|
||||
let no_times = {
|
||||
if let Expression::Quantity(p) = a {
|
||||
if let Expression::Quantity(q) = b {
|
||||
if let Expression::Quantity(_, p) = a {
|
||||
if let Expression::Quantity(_, q) = b {
|
||||
p.unitless() && !q.unitless()
|
||||
} else {false}
|
||||
} else {false}
|
||||
};
|
||||
|
||||
if no_times {
|
||||
let Expression::Quantity(u) = b else {panic!()};
|
||||
let Expression::Quantity(_, u) = b else {panic!()};
|
||||
if u.unit.no_space() {
|
||||
return format!("{}{}",
|
||||
self.add_parens_to_arg_strict(a),
|
||||
|
@ -252,7 +252,7 @@ impl Operator {
|
|||
let a = &args[0];
|
||||
let b = &args[1];
|
||||
|
||||
if let Expression::Quantity(q) = a {
|
||||
if let Expression::Quantity(_, q) = a {
|
||||
if q.is_one() {
|
||||
return format!("{}⁻¹",
|
||||
self.add_parens_to_arg_strict(b)
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
use std::cmp;
|
||||
use std::ops::Add;
|
||||
use std::ops::AddAssign;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Specifies the location of a token in an input string.
|
||||
#[derive(Debug)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct LineLocation {
|
||||
pub pos: usize,
|
||||
pub len: usize
|
||||
}
|
||||
|
||||
impl LineLocation {
|
||||
pub fn zero(&self) -> bool {
|
||||
return self.pos == 0 && self.len == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for LineLocation {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.pos == other.pos &&
|
||||
self.len ==other.len
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for LineLocation {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
return self.pos.partial_cmp(&other.pos);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for LineLocation {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self::Output {
|
||||
if self.zero() { return other; }
|
||||
if other.zero() { return self; }
|
||||
|
||||
let start = cmp::min(self.pos, other.pos);
|
||||
let end = cmp::max(self.pos+self.len-1, other.pos+other.len-1);
|
||||
return LineLocation{
|
||||
pos: start,
|
||||
len: end - start+1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for LineLocation where {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
if self.zero() {*self = other}
|
||||
if other.zero() { return }
|
||||
|
||||
let start = cmp::min(self.pos, other.pos);
|
||||
let end = cmp::max(self.pos+self.len-1, other.pos+other.len-1);
|
||||
self.pos = start;
|
||||
self.len = end - start + 1;
|
||||
}
|
||||
}
|
|
@ -3,11 +3,11 @@ mod stage;
|
|||
mod token;
|
||||
mod parsererror;
|
||||
mod expression;
|
||||
mod linelocation;
|
||||
|
||||
use self::{
|
||||
token::Token,
|
||||
parsererror::ParserError,
|
||||
parsererror::LineLocation
|
||||
};
|
||||
|
||||
pub use self::{
|
||||
|
@ -15,6 +15,7 @@ pub use self::{
|
|||
expression::Constant,
|
||||
expression::Operator,
|
||||
expression::Function,
|
||||
linelocation::LineLocation,
|
||||
};
|
||||
|
||||
use crate::context::Context;
|
||||
|
@ -27,7 +28,6 @@ pub fn parse(
|
|||
let expressions = stage::tokenize(s);
|
||||
let (_, expressions) = stage::find_subs(expressions);
|
||||
let g = stage::groupify(expressions)?;
|
||||
|
||||
let g = stage::treeify(g, context)?;
|
||||
|
||||
return Ok(g);
|
||||
|
@ -50,11 +50,12 @@ pub fn substitute(
|
|||
|
||||
let l = s.chars().count();
|
||||
let expressions = stage::tokenize(s);
|
||||
let (subs, _) = stage::find_subs(expressions);
|
||||
let (mut subs, _) = stage::find_subs(expressions);
|
||||
let mut new_c = l - c;
|
||||
|
||||
for r in subs.iter() {
|
||||
// find_subs gives substitutions in reverse order.
|
||||
while subs.len() > 0 {
|
||||
let r = subs.pop_back().unwrap();
|
||||
// Apply substitutions in reverse order
|
||||
|
||||
if { // Don't substitute if our cursor is inside the substitution
|
||||
c >= r.0.pos &&
|
||||
|
|
|
@ -1,11 +1,3 @@
|
|||
/// Specifies the location of a token in an input string.
|
||||
#[derive(Debug)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct LineLocation {
|
||||
pub pos: usize,
|
||||
pub len: usize
|
||||
}
|
||||
|
||||
/// Types of parser errors.
|
||||
/// If we cannot parse a string, one of these is returned.
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -194,9 +194,12 @@ pub fn groupify(
|
|||
Token,
|
||||
(LineLocation, ParserError)
|
||||
> {
|
||||
|
||||
let last_linelocation = g.back().unwrap().get_line_location().clone();
|
||||
|
||||
// Vector of grouping levels
|
||||
let mut levels: Vec<(LineLocation, VecDeque<Token>)> = Vec::with_capacity(8);
|
||||
levels.push((LineLocation{pos: 0, len: 0}, VecDeque::with_capacity(8)));
|
||||
levels.push((LineLocation{pos: 0, len: last_linelocation.pos + last_linelocation.len}, VecDeque::with_capacity(8)));
|
||||
|
||||
// Makes sure parenthesis are matched
|
||||
let mut i_level = 0;
|
||||
|
@ -212,10 +215,7 @@ pub fn groupify(
|
|||
},
|
||||
|
||||
Token::GroupEnd(l) => {
|
||||
let l = LineLocation {
|
||||
pos: l_now.pos,
|
||||
len: l.len + l.pos - l_now.pos
|
||||
};
|
||||
let l = *l_now + l;
|
||||
|
||||
if i_level == 0 { return Err((l, ParserError::ExtraCloseParen)) }
|
||||
if v_now.len() == 0 { return Err((l, ParserError::EmptyGroup)) }
|
||||
|
@ -259,5 +259,5 @@ pub fn groupify(
|
|||
let (_, mut v) = levels.pop().unwrap();
|
||||
lookback(&mut v)?;
|
||||
|
||||
return Ok(Token::Group(LineLocation{pos:0, len:0}, v));
|
||||
return Ok(Token::Group(LineLocation{pos:0, len:last_linelocation.pos + last_linelocation.len}, v));
|
||||
}
|
|
@ -85,11 +85,8 @@ fn treeify_binary(
|
|||
} {
|
||||
return Ok(false);
|
||||
} else {
|
||||
let tl = *this.get_line_location();
|
||||
return Err((
|
||||
LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
|
||||
ParserError::Syntax
|
||||
));
|
||||
let tl = *this.get_line_location() + *l;
|
||||
return Err((tl, ParserError::Syntax));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,22 +123,33 @@ fn treeify_binary(
|
|||
let left_pre = g_inner.remove(i-1).unwrap();
|
||||
let this_pre = g_inner.remove(i-1).unwrap();
|
||||
let right_pre = g_inner.remove(i-1).unwrap();
|
||||
let left: Expression; let right: Expression;
|
||||
if let Token::Group(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_expression(context)?;}
|
||||
if let Token::Group(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_expression(context)?;}
|
||||
let mut left: Expression; let mut right: Expression;
|
||||
if let Token::Group(l, _) = right_pre {
|
||||
right = treeify(right_pre, context)?;
|
||||
right.set_linelocation(&(right.get_linelocation() + l));
|
||||
} else {
|
||||
right = right_pre.to_expression(context)?;
|
||||
}
|
||||
|
||||
let o = {
|
||||
let Token::Operator(_, s) = this_pre else {panic!()};
|
||||
if let Token::Group(l, _) = left_pre {
|
||||
left = treeify(left_pre, context)?;
|
||||
left.set_linelocation(&(left.get_linelocation() + l));
|
||||
} else {
|
||||
left = left_pre.to_expression(context)?;
|
||||
}
|
||||
|
||||
let (l, o) = {
|
||||
let Token::Operator(l, s) = this_pre else {panic!()};
|
||||
let o = Operator::from_string(&s);
|
||||
if o.is_none() { panic!() }
|
||||
o.unwrap()
|
||||
(l, o.unwrap())
|
||||
};
|
||||
|
||||
let mut new_token_args: VecDeque<Expression> = VecDeque::with_capacity(2);
|
||||
new_token_args.push_back(left);
|
||||
new_token_args.push_back(right);
|
||||
|
||||
g_inner.insert(i-1, Token::Container(Expression::Operator(o, new_token_args)));
|
||||
g_inner.insert(i-1, Token::Container(Expression::Operator(l, o, new_token_args)));
|
||||
|
||||
return Ok(true);
|
||||
} else {
|
||||
|
@ -204,12 +212,8 @@ fn treeify_unary(
|
|||
}
|
||||
|
||||
if let Token::Operator(l, _) = next {
|
||||
let tl = *this.get_line_location();
|
||||
return Err((
|
||||
LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
|
||||
ParserError::Syntax
|
||||
));
|
||||
|
||||
let tl = *this.get_line_location() + *l;
|
||||
return Err((tl, ParserError::Syntax));
|
||||
} else {
|
||||
|
||||
// This operator
|
||||
|
@ -239,28 +243,34 @@ fn treeify_unary(
|
|||
|
||||
if next_op.is_none() || this_op > next_op.unwrap() {
|
||||
let this_pre = g_inner.remove(i).unwrap();
|
||||
let next_pre: Token; let next: Expression;
|
||||
let next_pre: Token; let mut next: Expression;
|
||||
if left_associative {
|
||||
next_pre = g_inner.remove(i-1).unwrap();
|
||||
} else {
|
||||
next_pre = g_inner.remove(i).unwrap();
|
||||
}
|
||||
if let Token::Group(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_expression(context)? }
|
||||
if let Token::Group(l, _) = next_pre {
|
||||
next = treeify(next_pre, context)?;
|
||||
next.set_linelocation(&(next.get_linelocation() + l));
|
||||
} else {
|
||||
next = next_pre.to_expression(context)?;
|
||||
}
|
||||
|
||||
let o = {
|
||||
let Token::Operator(_, s) = this_pre else {panic!()};
|
||||
|
||||
let (l, o) = {
|
||||
let Token::Operator(l, s) = this_pre else {panic!()};
|
||||
let o = Operator::from_string(&s);
|
||||
if o.is_none() { panic!() }
|
||||
o.unwrap()
|
||||
(l, o.unwrap())
|
||||
};
|
||||
|
||||
let mut new_token_args: VecDeque<Expression> = VecDeque::with_capacity(3);
|
||||
new_token_args.push_back(next);
|
||||
|
||||
if left_associative {
|
||||
g_inner.insert(i-1, Token::Container(Expression::Operator(o, new_token_args)));
|
||||
g_inner.insert(i-1, Token::Container(Expression::Operator(l, o, new_token_args)));
|
||||
} else {
|
||||
g_inner.insert(i, Token::Container(Expression::Operator(o, new_token_args)));
|
||||
g_inner.insert(i, Token::Container(Expression::Operator(l, o, new_token_args)));
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
|
|
|
@ -74,20 +74,20 @@ impl Token {
|
|||
return Err((l, ParserError::BadNumber))
|
||||
}
|
||||
|
||||
return Ok(Expression::Quantity(r.unwrap()));
|
||||
return Ok(Expression::Quantity(l, r.unwrap()));
|
||||
},
|
||||
|
||||
Token::Word(_l, s) => {
|
||||
Token::Word(l, s) => {
|
||||
|
||||
let c = Constant::from_string(&s);
|
||||
if c.is_some() { return Ok(Expression::Constant(c.unwrap())); }
|
||||
if c.is_some() { return Ok(Expression::Constant(l, c.unwrap())); }
|
||||
|
||||
let c = Unit::from_string(&s);
|
||||
if c.is_some() { return Ok(Expression::Quantity(c.unwrap())); }
|
||||
if c.is_some() { return Ok(Expression::Quantity(l, c.unwrap())); }
|
||||
|
||||
let c = context.get_variable(&s);
|
||||
if c.is_some() { return Ok(Expression::Variable(s)); }
|
||||
return Ok(Expression::Variable(s));
|
||||
if c.is_some() { return Ok(Expression::Variable(l, s)); }
|
||||
return Ok(Expression::Variable(l, s));
|
||||
}
|
||||
|
||||
Token::Container(v) => { return Ok(v); }
|
||||
|
|
Loading…
Reference in New Issue