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

@ -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 },
}
}
}

View File

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