From 16970b51e5f9d2c220de699c7417f65f4f209d4c Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 9 Jul 2025 13:40:44 +0200 Subject: [PATCH 1/6] clippy: run cargo clippy --- src/configuration/loader.rs | 21 +- src/exact_math/rationals.rs | 30 +-- src/exact_math/symbolic.rs | 3 +- src/functions/add.rs | 317 +++++++++++----------------- src/functions/divide.rs | 279 +++++++++---------------- src/functions/expo.rs | 8 +- src/functions/function.rs | 6 +- src/functions/minus.rs | 247 +++++++++------------- src/functions/mult.rs | 269 +++++++++--------------- src/interpreting/interpreter.rs | 44 ++-- src/interpreting/stdlib.rs | 360 ++++++++++++++++---------------- src/lexing/lexer.rs | 15 +- src/main.rs | 14 +- src/parsing/ast.rs | 51 +++-- src/parsing/parser.rs | 6 +- src/utils/matrix_utils.rs | 51 +++-- src/utils/plot_utils.rs | 18 +- 17 files changed, 725 insertions(+), 1014 deletions(-) diff --git a/src/configuration/loader.rs b/src/configuration/loader.rs index 12560f96..75f751dd 100644 --- a/src/configuration/loader.rs +++ b/src/configuration/loader.rs @@ -90,20 +90,11 @@ pub fn load_rgb_color(str: &str) -> (u8, u8, u8) { let gd = u8::from_str_radix(second, 16); let bd = u8::from_str_radix(last, 16); - let r = match rd { - Ok(c) => c, - Err(_) => 0xFF, - }; - - let g = match gd { - Ok(c) => c, - Err(_) => 0xFF, - }; - - let b = match bd { - Ok(c) => c, - Err(_) => 0xFF, - }; + let r = rd.unwrap_or(0xFF); + + let g = gd.unwrap_or(0xFF); + + let b = bd.unwrap_or(0xFF); (r, g, b) } @@ -123,7 +114,7 @@ pub fn load_color(string: String) -> Color { if str.unwrap().len() < 6 { Color::Cyan } else { - let (r, g, b) = load_rgb_color(&str.unwrap()); + let (r, g, b) = load_rgb_color(str.unwrap()); if r == 0xFF && g == 0xFF && b == 0xFF { Color::Cyan } else { diff --git a/src/exact_math/rationals.rs b/src/exact_math/rationals.rs index 4c6e03e2..458cfdaa 100644 --- a/src/exact_math/rationals.rs +++ b/src/exact_math/rationals.rs @@ -22,7 +22,7 @@ impl Rationals { } pub fn approx(self) -> f64 { - return self.over as f64 / self.under as f64; + self.over as f64 / self.under as f64 } pub fn rationalize(f: f64) -> Self { @@ -31,11 +31,11 @@ impl Rationals { } pub fn is_null(self) -> bool { - return self.over == 0; + self.over == 0 } pub fn opposite(self) -> Self { - Rationals::new(self.under, -1 * self.over) + Rationals::new(self.under, -self.over) } pub fn invert(self) -> Result { @@ -68,7 +68,7 @@ impl Rationals { } if i1 == 0 && i2 == 0 { - return Rationals { under: 0, over: 0 }; + Rationals { under: 0, over: 0 } } else if i1 == 0 { return Rationals { under: 1, over: 0 }; } else if i2 == 0 { @@ -113,21 +113,21 @@ impl Display for Rationals { impl PartialEq for Rationals { fn eq(&self, other: &Self) -> bool { if self.under == other.under { - return self.over == other.over; + self.over == other.over } else { let i1 = self.put_to_denominator(other.under); let i2 = other.put_to_denominator(self.under); - return i1.over == i2.over; + i1.over == i2.over } } fn ne(&self, other: &Self) -> bool { - return !self.eq(other); + !self.eq(other) } } impl PartialOrd for Rationals { fn partial_cmp(&self, other: &Self) -> Option { - if !(self.under == other.under) { + if self.under != other.under { if self.over == other.over { return Some(std::cmp::Ordering::Equal); } @@ -155,10 +155,10 @@ impl PartialOrd for Rationals { } fn le(&self, other: &Self) -> bool { - return self < other || self == other; + self <= other } fn ge(&self, other: &Self) -> bool { - return self > other || self == other; + self >= other } } @@ -178,14 +178,14 @@ impl ops::Add for Rationals { impl ops::Sub for Rationals { type Output = Rationals; fn sub(self, rhs: Self) -> Self::Output { - return self + Rationals::new(rhs.under, -rhs.over); + self + Rationals::new(rhs.under, -rhs.over) } } impl ops::Mul for Rationals { type Output = Rationals; fn mul(self, rhs: Self) -> Self::Output { - return Rationals::new(self.under * rhs.under, self.over * rhs.over).reduce(); + Rationals::new(self.under * rhs.under, self.over * rhs.over).reduce() } } @@ -195,7 +195,7 @@ impl ops::Div for Rationals { let l = self.under * rhs.over; let rs = self.over * rhs.under; let r = Rationals::new(l, rs); - return r.reduce(); + r.reduce() } } @@ -214,14 +214,14 @@ mod test { pub fn test_equality_simple() { let r1 = Rationals::new(2, 5); let r2 = Rationals::new(2, 3); - assert_eq!(r1 > r2, true); + assert!(r1 > r2); } #[test] pub fn test_equality_hard() { let f1 = Rationals::new(2, 3); let f2 = Rationals::new(3, 1); - assert_eq!(f2 < f1, true); + assert!(f2 < f1); } #[test] diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index 81e6f56f..22adc19e 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -81,8 +81,7 @@ mod test { 0, 0, 0, 0, 0, ]; - let _ = v - .into_iter() + v.into_iter() .map(|f| size(&f)) .zip(should) .for_each(|(x, y)| assert_eq!(x, y)); diff --git a/src/functions/add.rs b/src/functions/add.rs index 4f554560..cabbb5d4 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -18,14 +18,14 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Int(_) | Float(_) | Rational(_) | Bool(_) => { let mut s = vec.clone(); s.push(p.clone()); - InterpreterVector(Box::from(s.clone())) + InterpreterVector(s.clone()) } Null => InterpreterVector(vec.clone()), InterpreterVector(vec2) => { let mut res = Vec::new(); vec.clone() .into_iter() - .zip(vec2.clone().into_iter()) + .zip(*vec2.clone()) .map(|(x, y)| add(x.clone(), y.clone(), ram)) .for_each(|s| res.push(s)); InterpreterVector(Box::from(res)) @@ -44,11 +44,11 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Null, InterpreterVector(vec)) => InterpreterVector(vec.clone()), (Int(v), Null) => Int(*v), (Float(f), Null) => Float(*f), - (Rational(s), Null) => Rational(s.clone()), - (Null, Rational(s)) => Rational(s.clone()), - (Rational(s), Rational(s2)) => Rational(s.clone() + s2.clone()), - (Rational(s), Int(i)) => Rational(s.clone() + Rationals::new(1, *i)), - (Int(i), Rational(s)) => Rational(s.clone() + Rationals::new(1, *i)), + (Rational(s), Null) => Rational(*s), + (Null, Rational(s)) => Rational(*s), + (Rational(s), Rational(s2)) => Rational(*s + *s2), + (Rational(s), Int(i)) => Rational(*s + Rationals::new(1, *i)), + (Int(i), Rational(s)) => Rational(*s + Rationals::new(1, *i)), (Rational(s), Float(f)) => Float(s.approx() + f), (Float(f), Rational(s)) => Float(f + s.approx()), (Int(v), Int(v2)) => Int(v + v2), @@ -84,27 +84,19 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Some(_) => apply_operator(Identifier(s.clone()), Null, ram, add), }, (Rational(r), Identifier(ss)) => match ram { - None => Plus( - Box::from(Rational(r.clone())), - Box::from(Identifier(ss.clone())), - ), - Some(_) => { - apply_operator_reverse(Rational(r.clone()), Identifier(ss.clone()), ram, add) - } + None => Plus(Box::from(Rational(*r)), Box::from(Identifier(ss.clone()))), + Some(_) => apply_operator_reverse(Rational(*r), Identifier(ss.clone()), ram, add), }, (Identifier(ss), Rational(r)) => match ram { - None => Plus( - Box::from(Identifier(ss.clone())), - Box::from(Rational(r.clone())), - ), - Some(_) => apply_operator(Identifier(ss.clone()), Rational(r.clone()), ram, add), + None => Plus(Box::from(Identifier(ss.clone())), Box::from(Rational(*r))), + Some(_) => apply_operator(Identifier(ss.clone()), Rational(*r), ram, add), }, (Identifier(s), Null) => match ram { None => Identifier(s.clone()), Some(_) => apply_operator(Identifier(s.clone()), Null, ram, add), }, (Int(i), Identifier(s)) => match ram { - None => Plus(Box::from(Identifier(s.clone())), Box::from(Int(i.clone()))), + None => Plus(Box::from(Identifier(s.clone())), Box::from(Int(*i))), Some(_) => apply_operator(Identifier(s.clone()), Int(*i), ram, add), }, (Identifier(s), Float(i)) => match ram { @@ -233,14 +225,8 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { } (Plus(s1, s2), Rational(r)) => { - let first = Plus( - Box::from(add(*s1.clone(), Rational(r.clone()), ram)), - s2.clone(), - ); - let second = Plus( - s1.clone(), - Box::from(add(*s2.clone(), Rational(r.clone()), ram)), - ); + let first = Plus(Box::from(add(*s1.clone(), Rational(*r), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Rational(*r), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -251,14 +237,8 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { } (Rational(r), Plus(s1, s2)) => { - let first = Plus( - Box::from(add(*s1.clone(), Rational(r.clone()), ram)), - s2.clone(), - ); - let second = Plus( - s1.clone(), - Box::from(add(*s2.clone(), Rational(r.clone()), ram)), - ); + let first = Plus(Box::from(add(*s1.clone(), Rational(*r), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Rational(*r), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -338,11 +318,11 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Mul(s1, s2), Rational(r)) => Plus( Box::from(Mul(s1.clone(), s2.clone())), - Box::from(Rational(r.clone())), + Box::from(Rational(*r)), ), (Rational(r), Mul(s1, s2)) => Plus( - Box::from(Rational(r.clone())), + Box::from(Rational(*r)), Box::from(Mul(s1.clone(), s2.clone())), ), @@ -375,8 +355,8 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(add(*x.clone(), *x1.clone(), ram)), *y, z.clone()) } else { Plus( - Box::from(Var(x.clone(), y.clone(), z.clone())), - Box::from(Var(x1.clone(), y1.clone(), z1.clone())), + Box::from(Var(x.clone(), *y, z.clone())), + Box::from(Var(x1.clone(), *y1, z1.clone())), ) } } @@ -386,7 +366,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(add(*x.clone(), Int(1), ram)), *y, z.clone()) } else { Plus( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), *y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -397,7 +377,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(add(*x.clone(), Int(1), ram)), *y, z.clone()) } else { Plus( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), *y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -422,178 +402,125 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { ), (Rational(r), Var(x, y, z)) => Plus( - Box::from(Rational(r.clone())), + Box::from(Rational(*r)), Box::from(Var(x.clone(), *y, z.clone())), ), (Var(x, y, z), Rational(r)) => Plus( Box::from(Var(x.clone(), *y, z.clone())), - Box::from(Rational(r.clone())), + Box::from(Rational(*r)), ), - (Var(x, y, z), Div(s1, s2)) => { - let first = Div( - Box::from(add( - mult(Var(x.clone(), *y, z.clone()), *s2.clone(), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Var(x, y, z), Div(s1, s2)) => Div( + Box::from(add( + mult(Var(x.clone(), *y, z.clone()), *s2.clone(), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Div(s1, s2), Var(x, y, z)) => { - let first = Div( - Box::from(add( - mult(Var(x.clone(), *y, z.clone()), *s2.clone(), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Var(x, y, z)) => Div( + Box::from(add( + mult(Var(x.clone(), *y, z.clone()), *s2.clone(), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Mul(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(add( - mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - *s3.clone(), - ram, - )), - s4.clone(), - ); - first - } + (Mul(s1, s2), Div(s3, s4)) => Div( + Box::from(add( + mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + *s3.clone(), + ram, + )), + s4.clone(), + ), - (Div(s1, s2), Mul(s3, s4)) => { - let first = Div( - Box::from(add( - mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Mul(s3, s4)) => Div( + Box::from(add( + mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Div(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(add( - mult(*s1.clone(), *s4.clone(), ram), - mult(*s2.clone(), *s3.clone(), ram), - ram, - )), - Box::from(mult(*s2.clone(), *s4.clone(), ram)), - ); - first - } + (Div(s1, s2), Div(s3, s4)) => Div( + Box::from(add( + mult(*s1.clone(), *s4.clone(), ram), + mult(*s2.clone(), *s3.clone(), ram), + ram, + )), + Box::from(mult(*s2.clone(), *s4.clone(), ram)), + ), - (Div(s1, s2), Identifier(s)) => { - let first = Div( - Box::from(add( - mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Identifier(s)) => Div( + Box::from(add( + mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Identifier(s), Div(s1, s2)) => { - let first = Div( - Box::from(add( - mult(Var(Box::from(Int(1)), 1, s.clone()), *s1.clone(), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Identifier(s), Div(s1, s2)) => Div( + Box::from(add( + mult(Var(Box::from(Int(1)), 1, s.clone()), *s1.clone(), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Div(s1, s2), Int(i)) => { - let first = Div( - Box::from(add(mult(*s2.clone(), Int(*i), ram), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Int(i)) => Div( + Box::from(add(mult(*s2.clone(), Int(*i), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Int(i), Div(s1, s2)) => { - let first = Div( - Box::from(add(mult(Int(*i), *s2.clone(), ram), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Int(i), Div(s1, s2)) => Div( + Box::from(add(mult(Int(*i), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Div(s1, s2), Float(f)) => { - let first = Div( - Box::from(add(mult(*s2.clone(), Float(*f), ram), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Float(f)) => Div( + Box::from(add(mult(*s2.clone(), Float(*f), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Float(f), Div(s1, s2)) => { - let first = Div( - Box::from(add(mult(Float(*f), *s2.clone(), ram), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Float(f), Div(s1, s2)) => Div( + Box::from(add(mult(Float(*f), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Div(s1, s2), Rational(r)) => { - let first = Div( - Box::from(add( - mult(*s2.clone(), Rational(r.clone()), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Rational(r)) => Div( + Box::from(add(mult(*s2.clone(), Rational(*r), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Rational(r), Div(s1, s2)) => { - let first = Div( - Box::from(add( - mult(Rational(r.clone()), *s2.clone(), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Rational(r), Div(s1, s2)) => Div( + Box::from(add(mult(Rational(*r), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ), - (Div(s1, s2), Plus(s3, s4)) => { - let first = Div( - Box::from(add( - *s1.clone(), - mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Plus(s3, s4)) => Div( + Box::from(add( + *s1.clone(), + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + )), + s2.clone(), + ), - (Plus(s3, s4), Div(s1, s2)) => { - let first = Div( - Box::from(add( - mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - *s1.clone(), - ram, - )), - s1.clone(), - ); - first - } + (Plus(s3, s4), Div(s1, s2)) => Div( + Box::from(add( + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + *s1.clone(), + ram, + )), + s1.clone(), + ), (Null, Div(s1, s2)) => Div(s1.clone(), s2.clone()), @@ -614,7 +541,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Call(x, y), Rational(i)) => Plus( Box::from(Call(x.clone(), y.clone())), - Box::from(Rational(i.clone())), + Box::from(Rational(*i)), ), (Int(i), Call(x, y)) => Plus(Box::from(Call(x.clone(), y.clone())), Box::from(Int(*i))), @@ -623,7 +550,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Rational(i), Call(x, y)) => Plus( Box::from(Call(x.clone(), y.clone())), - Box::from(Rational(i.clone())), + Box::from(Rational(*i)), ), (Call(x, y), Identifier(a)) => Plus( diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 406f7305..631344ff 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -125,81 +125,54 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { None => Bool(b), Some(_) => apply_operator(Identifier(s), Bool(b), ram, divide), }, - (Plus(s1, s2), Plus(s3, s4)) => { - let first = add( - divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - ram, - ); - first - } - (Plus(s1, s2), Identifier(s3)) => { - let first = add( - divide(*s1.clone(), Var(Box::from(Int(1)), 1, s3.clone()), ram), - divide(*s2.clone(), Var(Box::from(Int(1)), 1, s3.clone()), ram), - ram, - ); - first - } + (Plus(s1, s2), Plus(s3, s4)) => add( + divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + ), + (Plus(s1, s2), Identifier(s3)) => add( + divide(*s1.clone(), Var(Box::from(Int(1)), 1, s3.clone()), ram), + divide(*s2.clone(), Var(Box::from(Int(1)), 1, s3.clone()), ram), + ram, + ), - (Identifier(s3), Plus(s1, s2)) => { - let first = Div( - Box::from(Var(Box::new(Int(1)), 1, s3.clone())), - Box::from(add(*s1.clone(), *s2.clone(), ram)), - ); - first - } - (Int(i), Plus(s1, s2)) => { - let first = Div( - Box::from(Int(i)), - Box::from(add(*s1.clone(), *s2.clone(), ram)), - ); - first - } + (Identifier(s3), Plus(s1, s2)) => Div( + Box::from(Var(Box::new(Int(1)), 1, s3.clone())), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ), + (Int(i), Plus(s1, s2)) => Div( + Box::from(Int(i)), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ), - (Plus(s1, s2), Int(i)) => { - let first = add( - divide(*s1.clone(), Int(i), ram), - divide(*s2.clone(), Int(i), ram), - ram, - ); - first - } + (Plus(s1, s2), Int(i)) => add( + divide(*s1.clone(), Int(i), ram), + divide(*s2.clone(), Int(i), ram), + ram, + ), - (Float(f), Plus(s1, s2)) => { - let first = Div( - Box::from(Float(f)), - Box::from(add(*s1.clone(), *s2.clone(), ram)), - ); - first - } + (Float(f), Plus(s1, s2)) => Div( + Box::from(Float(f)), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ), - (Plus(s1, s2), Float(f)) => { - let first = add( - divide(*s1.clone(), Float(f), ram), - divide(*s2.clone(), Float(f), ram), - ram, - ); - first - } - (Rational(r), Plus(s1, s2)) => { - let first = Div( - Box::from(Rational(r)), - Box::from(add(*s1.clone(), *s2.clone(), ram)), - ); - first - } + (Plus(s1, s2), Float(f)) => add( + divide(*s1.clone(), Float(f), ram), + divide(*s2.clone(), Float(f), ram), + ram, + ), + (Rational(r), Plus(s1, s2)) => Div( + Box::from(Rational(r)), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ), - (Plus(s1, s2), Rational(r)) => { - let first = add( - divide(*s1.clone(), Rational(r), ram), - divide(*s2.clone(), Rational(r), ram), - ram, - ); - first - } + (Plus(s1, s2), Rational(r)) => add( + divide(*s1.clone(), Rational(r), ram), + divide(*s2.clone(), Rational(r), ram), + ram, + ), (Var(x, y, z), Plus(s1, s2)) => Div( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(add(*s1.clone(), *s2.clone(), ram)), ), @@ -440,14 +413,11 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { } } - (Plus(s3, s4), Mul(s1, s2)) => { - let first = add( - divide(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - divide(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - ram, - ); - first - } + (Plus(s3, s4), Mul(s1, s2)) => add( + divide(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + divide(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + ram, + ), (Null, Mul(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), @@ -458,10 +428,10 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(divide(*x.clone(), *x1.clone(), ram)), y - y1, z) } else { Div( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var( Box::from(divide(Int(1), *x1.clone(), ram)), - y1.clone(), + y1, z1.clone(), )), ) @@ -470,10 +440,10 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Var(x, y, z), Identifier(s)) => { if z == s { - Var(Box::from(x.clone()), y - 1, z) + Var(x.clone(), y - 1, z) } else { Div( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -485,11 +455,7 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { } else { Div( Box::from(Var(Box::from(Int(1)), 1, s.clone())), - Box::from(Var( - Box::from(mult(Int(1), *x.clone(), ram)), - y.clone(), - z.clone(), - )), + Box::from(Var(Box::from(mult(Int(1), *x.clone(), ram)), y, z.clone())), ) } } @@ -516,113 +482,68 @@ pub fn divide(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { z.clone(), ), - (Var(x, y, z), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram)), - s1.clone(), - ); - first - } + (Var(x, y, z), Div(s1, s2)) => Div( + Box::from(mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram)), + s1.clone(), + ), - (Div(s1, s2), Var(x, y, z)) => { - let first = Div( - s1.clone(), - Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), - ); - first - } + (Div(s1, s2), Var(x, y, z)) => Div( + s1.clone(), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), + ), - (Mul(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), - s3.clone(), - ); - first - } + (Mul(s1, s2), Div(s3, s4)) => Div( + Box::from(mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), + s3.clone(), + ), - (Div(s1, s2), Mul(s3, s4)) => { - let first = Div( - s1.clone(), - Box::from(mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram)), - ); - first - } + (Div(s1, s2), Mul(s3, s4)) => Div( + s1.clone(), + Box::from(mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram)), + ), - (Div(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(mult(*s1.clone(), *s4.clone(), ram)), - Box::from(mult(*s2.clone(), *s3.clone(), ram)), - ); - first - } + (Div(s1, s2), Div(s3, s4)) => Div( + Box::from(mult(*s1.clone(), *s4.clone(), ram)), + Box::from(mult(*s2.clone(), *s3.clone(), ram)), + ), - (Div(s1, s2), Identifier(s)) => { - let first = Div( - s1.clone(), - Box::from(mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), - ); - first - } + (Div(s1, s2), Identifier(s)) => Div( + s1.clone(), + Box::from(mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), + ), - (Identifier(s), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram)), - s1.clone(), - ); - first - } + (Identifier(s), Div(s1, s2)) => Div( + Box::from(mult(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram)), + s1.clone(), + ), - (Div(s1, s2), Int(i)) => { - let first = Div(s1.clone(), Box::from(mult(*s2.clone(), Int(i), ram))); - first - } + (Div(s1, s2), Int(i)) => Div(s1.clone(), Box::from(mult(*s2.clone(), Int(i), ram))), - (Int(i), Div(s1, s2)) => { - let first = Div(Box::from(mult(Int(i), *s2.clone(), ram)), s1.clone()); - first - } + (Int(i), Div(s1, s2)) => Div(Box::from(mult(Int(i), *s2.clone(), ram)), s1.clone()), - (Div(s1, s2), Float(f)) => { - let first = Div(s1.clone(), Box::from(mult(*s2.clone(), Float(f), ram))); - first - } + (Div(s1, s2), Float(f)) => Div(s1.clone(), Box::from(mult(*s2.clone(), Float(f), ram))), - (Float(f), Div(s1, s2)) => { - let first = Div(Box::from(mult(Float(f), *s2.clone(), ram)), s1.clone()); - first - } + (Float(f), Div(s1, s2)) => Div(Box::from(mult(Float(f), *s2.clone(), ram)), s1.clone()), - (Div(s1, s2), Rational(r)) => { - let first = Div( - Box::from(mult(*s1.clone(), Int(r.under), ram)), - Box::from(mult(*s2.clone(), Int(r.over), ram)), - ); - first - } + (Div(s1, s2), Rational(r)) => Div( + Box::from(mult(*s1.clone(), Int(r.under), ram)), + Box::from(mult(*s2.clone(), Int(r.over), ram)), + ), - (Rational(r), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Int(r.under), *s2.clone(), ram)), - Box::from(mult(*s1.clone(), Int(r.over), ram)), - ); - first - } + (Rational(r), Div(s1, s2)) => Div( + Box::from(mult(Int(r.under), *s2.clone(), ram)), + Box::from(mult(*s1.clone(), Int(r.over), ram)), + ), - (Div(s1, s2), Plus(s3, s4)) => { - let first = Div( - Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Plus(s3, s4)) => Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ), - (Plus(s3, s4), Div(s1, s2)) => { - let first = Div( - Box::from(mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), - s1.clone(), - ); - first - } + (Plus(s3, s4), Div(s1, s2)) => Div( + Box::from(mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s1.clone(), + ), (Null, Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), diff --git a/src/functions/expo.rs b/src/functions/expo.rs index 9debb671..afadef61 100644 --- a/src/functions/expo.rs +++ b/src/functions/expo.rs @@ -16,8 +16,8 @@ pub fn expo(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Float(v), Float(f)) => Float(v.powf(f)), (Float(v), Int(i1)) => Float(v.powf(i1 as f64)), - (Rational(s), Null) => Rational(s.clone()), - (Null, Rational(s)) => Rational(s.clone()), + (Rational(s), Null) => Rational(s), + (Null, Rational(s)) => Rational(s), (Rational(s), Rational(s2)) => Float(s.approx().powf(s2.approx())), (Rational(s), Int(i)) => Float(s.approx().powf(i as f64)), (Int(i), Rational(s)) => Float((i as f64).powf(s.approx())), @@ -49,9 +49,7 @@ pub fn expo(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Rational(s), Identifier(ss)) => match ram { None => Rational(s), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, expo) - } + Some(_) => apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, expo), }, (Identifier(ss), Rational(s)) => match ram { None => Rational(s), diff --git a/src/functions/function.rs b/src/functions/function.rs index 75bbb583..98903a04 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -15,7 +15,7 @@ pub fn apply_operator( Identifier(ref s) => s, _ => "", }; - if s == "" { + if s.is_empty() { return Null; } match ram { @@ -40,7 +40,7 @@ pub fn apply_operator_reverse( Identifier(ref s) => s, _ => "", }; - if s == "" { + if s.is_empty() { return Null; } match ram { @@ -591,7 +591,7 @@ pub fn select(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { pub fn concat(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { match (i, i2) { (InterpreterVector(v), InterpreterVector(v2)) => { - Parameters::InterpreterVector(vec![*v, *v2].concat().into()) + Parameters::InterpreterVector([*v, *v2].concat().into()) } (Str(s), Str(s2)) => Parameters::Str((s + s2.as_str()).to_string()), (Identifier(s), InterpreterVector(v)) => match ram { diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 69c56680..a34ace7b 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -45,7 +45,7 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (InterpreterVector(vec), InterpreterVector(vec2)) => { let mut res = Vec::new(); vec.into_iter() - .zip(vec2.into_iter()) + .zip(*vec2) .map(|(x, y)| minus(x.clone(), y.clone(), ram)) .for_each(|z| res.push(z)); InterpreterVector(Box::from(res)) @@ -411,10 +411,10 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) } else { Plus( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var( Box::from(minus(Int(0), *x1.clone(), ram)), - y1.clone(), + y1, z1.clone(), )), ) @@ -426,7 +426,7 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(minus(*x.clone(), Int(1), ram)), y, z) } else { Plus( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var(Box::from(Int(-1)), 1, s.clone())), ) } @@ -437,11 +437,7 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(minus(Int(1), *x.clone(), ram)), y, z) } else { Plus( - Box::from(Var( - Box::from(minus(Int(0), *x.clone(), ram)), - y.clone(), - z.clone(), - )), + Box::from(Var(Box::from(minus(Int(0), *x.clone(), ram)), y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -470,158 +466,113 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Plus(Box::from(Var(x, y, z)), Box::from(Rational(r.opposite()))) } - (Var(x, y, z), Div(s1, s2)) => { - let first = Div( - Box::from(minus( - mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram), - *s1.clone(), - ram, - )), - s2.clone(), - ); - first - } + (Var(x, y, z), Div(s1, s2)) => Div( + Box::from(minus( + mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Div(s1, s2), Var(x, y, z)) => { - let first = Div( - Box::from(minus( - *s1.clone(), - mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Var(x, y, z)) => Div( + Box::from(minus( + *s1.clone(), + mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram), + ram, + )), + s2.clone(), + ), - (Mul(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(minus( - mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - *s3.clone(), - ram, - )), - s4.clone(), - ); - first - } + (Mul(s1, s2), Div(s3, s4)) => Div( + Box::from(minus( + mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + *s3.clone(), + ram, + )), + s4.clone(), + ), - (Div(s1, s2), Mul(s3, s4)) => { - let first = Div( - Box::from(minus( - *s3.clone(), - mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - ram, - )), - s4.clone(), - ); - first - } + (Div(s1, s2), Mul(s3, s4)) => Div( + Box::from(minus( + *s3.clone(), + mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + ram, + )), + s4.clone(), + ), - (Div(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(minus( - mult(*s1.clone(), *s4.clone(), ram), - mult(*s2.clone(), *s3.clone(), ram), - ram, - )), - Box::from(mult(*s2.clone(), *s4.clone(), ram)), - ); - first - } + (Div(s1, s2), Div(s3, s4)) => Div( + Box::from(minus( + mult(*s1.clone(), *s4.clone(), ram), + mult(*s2.clone(), *s3.clone(), ram), + ram, + )), + Box::from(mult(*s2.clone(), *s4.clone(), ram)), + ), - (Div(s1, s2), Identifier(s)) => { - let first = Div( - Box::from(minus( - *s1.clone(), - mult( - *s2.clone(), - Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), + (Div(s1, s2), Identifier(s)) => Div( + Box::from(minus( + *s1.clone(), + mult( + *s2.clone(), + Var(Box::from(Parameters::Int(1)), 1, s.clone()), ram, - )), - s2.clone(), - ); - first - } + ), + ram, + )), + s2.clone(), + ), - (Identifier(s), Div(s1, s2)) => { - let first = Div( - Box::from(minus( - mult( - *s2.clone(), - Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), - *s1.clone(), + (Identifier(s), Div(s1, s2)) => Div( + Box::from(minus( + mult( + *s2.clone(), + Var(Box::from(Parameters::Int(1)), 1, s.clone()), ram, - )), - s2.clone(), - ); - first - } + ), + *s1.clone(), + ram, + )), + s2.clone(), + ), - (Div(s1, s2), Int(i)) => { - let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); - first - } + (Div(s1, s2), Int(i)) => Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()), - (Int(i), Div(s1, s2)) => { - let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); - first - } + (Int(i), Div(s1, s2)) => Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()), - (Div(s1, s2), Float(f)) => { - let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); - first - } + (Div(s1, s2), Float(f)) => Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()), - (Float(f), Div(s1, s2)) => { - let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); - first - } + (Float(f), Div(s1, s2)) => Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()), - (Div(s1, s2), Rational(r)) => { - let first = Div( - Box::from(minus(*s1.clone(), mult(*s2.clone(), Rational(r), ram), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Rational(r)) => Div( + Box::from(minus(*s1.clone(), mult(*s2.clone(), Rational(r), ram), ram)), + s2.clone(), + ), - (Rational(r), Div(s1, s2)) => { - let first = Div( - Box::from(minus(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Rational(r), Div(s1, s2)) => Div( + Box::from(minus(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ), //x/y - a+b = x - y(a+b)/y - (Div(s1, s2), Plus(s3, s4)) => { - let first = Div( - Box::from(minus( - *s1.clone(), - mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - ram, - )), - s2.clone(), - ); - first - } + (Div(s1, s2), Plus(s3, s4)) => Div( + Box::from(minus( + *s1.clone(), + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + )), + s2.clone(), + ), - (Plus(s3, s4), Div(s1, s2)) => { - let first = Div( - Box::from(minus( - mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - *s1.clone(), - ram, - )), - s1.clone(), - ); - first - } + (Plus(s3, s4), Div(s1, s2)) => Div( + Box::from(minus( + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + *s1.clone(), + ram, + )), + s1.clone(), + ), (Null, Div(s1, s2)) => divide( minus(Parameters::Int(0), *s1.clone(), ram), @@ -736,10 +687,10 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Mul(a, b), Call(x, y)) => Plus( Box::from(Mul(a.clone(), b.clone())), - Box::from(Box::from(Mul( + Box::from(Mul( Box::from(Int(-1)), Box::from(Call(x.clone(), y.clone())), - ))), + )), ), (Call(x, y), Div(a, b)) => Plus( @@ -749,10 +700,10 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Div(a, b), Call(x, y)) => Plus( Box::from(Div(a.clone(), b.clone())), - Box::from(Box::from(Mul( + Box::from(Mul( Box::from(Int(-1)), Box::from(Call(x.clone(), y.clone())), - ))), + )), ), _ => Identifier("Those two values are incompatible with the - operator".to_string()), } diff --git a/src/functions/mult.rs b/src/functions/mult.rs index a3f94b18..9c562f40 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -20,8 +20,8 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Float(v), Float(f)) => Float(v * f), (Float(v), Int(i1)) => Float(v * (i1 as f64)), - (Rational(s), Null) => Rational(s.clone()), - (Null, Rational(s)) => Rational(s.clone()), + (Rational(s), Null) => Rational(s), + (Null, Rational(s)) => Rational(s), (Rational(s), Rational(s2)) => Rational(s * s2), (Rational(s), Int(i)) => Rational(s * Rationals::new(1, i)), (Int(i), Rational(s)) => Rational(s * Rationals::new(1, i)), @@ -82,7 +82,7 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { let mut sum = Null; (*vec) .into_iter() - .zip(vec2.into_iter()) + .zip(*vec2) .map(|(a, b)| mult(a.clone(), b.clone(), ram)) .for_each(|x| sum = add(sum.clone(), x, ram)); @@ -96,7 +96,7 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { let mut res = Vec::new(); - if matrix_result.len() == 0 { + if matrix_result.is_empty() { return Null; } @@ -213,75 +213,51 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { first } } - (Plus(s1, s2), Identifier(s3)) => { - let first = Plus( - Box::from(mult( - *s1.clone(), - Var(Box::from(Int(1)), 1, s3.clone()), - ram, - )), - Box::from(mult( - *s2.clone(), - Var(Box::from(Int(1)), 1, s3.clone()), - ram, - )), - ); - first - } - (Identifier(s3), Plus(s1, s2)) => { - let first = Plus( - Box::from(mult(Var(Box::new(Int(1)), 1, s3.clone()), *s1.clone(), ram)), - Box::from(mult(Var(Box::new(Int(1)), 1, s3.clone()), *s2.clone(), ram)), - ); - first - } + (Plus(s1, s2), Identifier(s3)) => Plus( + Box::from(mult( + *s1.clone(), + Var(Box::from(Int(1)), 1, s3.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Var(Box::from(Int(1)), 1, s3.clone()), + ram, + )), + ), + (Identifier(s3), Plus(s1, s2)) => Plus( + Box::from(mult(Var(Box::new(Int(1)), 1, s3.clone()), *s1.clone(), ram)), + Box::from(mult(Var(Box::new(Int(1)), 1, s3.clone()), *s2.clone(), ram)), + ), - (Int(i), Plus(s1, s2)) => { - let first = Plus( - Box::from(mult(Int(i), *s1.clone(), ram)), - Box::from(mult(Int(i), *s2.clone(), ram)), - ); - first - } + (Int(i), Plus(s1, s2)) => Plus( + Box::from(mult(Int(i), *s1.clone(), ram)), + Box::from(mult(Int(i), *s2.clone(), ram)), + ), - (Plus(s1, s2), Int(i)) => { - let first = Plus( - Box::from(mult(*s1.clone(), Int(i), ram)), - Box::from(mult(*s2.clone(), Int(i), ram)), - ); - first - } + (Plus(s1, s2), Int(i)) => Plus( + Box::from(mult(*s1.clone(), Int(i), ram)), + Box::from(mult(*s2.clone(), Int(i), ram)), + ), - (Float(f), Plus(s1, s2)) => { - let first = Plus( - Box::from(mult(Float(f), *s1.clone(), ram)), - Box::from(mult(Float(f), *s2.clone(), ram)), - ); - first - } + (Float(f), Plus(s1, s2)) => Plus( + Box::from(mult(Float(f), *s1.clone(), ram)), + Box::from(mult(Float(f), *s2.clone(), ram)), + ), - (Plus(s1, s2), Float(f)) => { - let first = Plus( - Box::from(mult(*s1.clone(), Float(f), ram)), - Box::from(mult(*s2.clone(), Float(f), ram)), - ); - first - } - (Rational(r), Plus(s1, s2)) => { - let first = Plus( - Box::from(mult(Rational(r), *s1.clone(), ram)), - Box::from(mult(Rational(r), *s2.clone(), ram)), - ); - first - } + (Plus(s1, s2), Float(f)) => Plus( + Box::from(mult(*s1.clone(), Float(f), ram)), + Box::from(mult(*s2.clone(), Float(f), ram)), + ), + (Rational(r), Plus(s1, s2)) => Plus( + Box::from(mult(Rational(r), *s1.clone(), ram)), + Box::from(mult(Rational(r), *s2.clone(), ram)), + ), - (Plus(s1, s2), Rational(r)) => { - let first = Plus( - Box::from(mult(*s1.clone(), Rational(r), ram)), - Box::from(mult(*s2.clone(), Rational(r), ram)), - ); - first - } + (Plus(s1, s2), Rational(r)) => Plus( + Box::from(mult(*s1.clone(), Rational(r), ram)), + Box::from(mult(*s2.clone(), Rational(r), ram)), + ), (Var(x, y, z), Plus(s1, s2)) => Plus( Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), @@ -398,10 +374,10 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) } else { Mul( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var( Box::from(mult(Int(1), *x1.clone(), ram)), - y1.clone(), + y1, z1.clone(), )), ) @@ -410,10 +386,10 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Var(x, y, z), Identifier(s)) => { if z == s { - Var(Box::from(x.clone()), y + 1, z) + Var(x.clone(), y + 1, z) } else { Mul( - Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x.clone(), y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -421,14 +397,10 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { (Identifier(s), Var(x, y, z)) => { if z == s { - Var(Box::from(x.clone()), y + 1, z) + Var(x.clone(), y + 1, z) } else { Mul( - Box::from(Var( - Box::from(mult(Int(1), *x.clone(), ram)), - y.clone(), - z.clone(), - )), + Box::from(Var(Box::from(mult(Int(1), *x.clone(), ram)), y, z.clone())), Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } @@ -450,113 +422,68 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { Var(Box::from(mult(*x.clone(), Rational(r), ram)), y, z.clone()) } - (Var(x, y, z), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Var(x.clone(), y, z.clone()), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Var(x, y, z), Div(s1, s2)) => Div( + Box::from(mult(Var(x.clone(), y, z.clone()), *s1.clone(), ram)), + s2.clone(), + ), - (Div(s1, s2), Var(x, y, z)) => { - let first = Div( - Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Var(x, y, z)) => Div( + Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), + s2.clone(), + ), - (Mul(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(mult(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), - s4.clone(), - ); - first - } + (Mul(s1, s2), Div(s3, s4)) => Div( + Box::from(mult(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), + s4.clone(), + ), - (Div(s1, s2), Mul(s3, s4)) => { - let first = Div( - Box::from(mult(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Mul(s3, s4)) => Div( + Box::from(mult(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ), - (Div(s1, s2), Div(s3, s4)) => { - let first = Div( - Box::from(mult(*s1.clone(), *s3.clone(), ram)), - Box::from(mult(*s2.clone(), *s4.clone(), ram)), - ); - first - } + (Div(s1, s2), Div(s3, s4)) => Div( + Box::from(mult(*s1.clone(), *s3.clone(), ram)), + Box::from(mult(*s2.clone(), *s4.clone(), ram)), + ), - (Div(s1, s2), Identifier(s)) => { - let first = Div( - Box::from(mult(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Identifier(s)) => Div( + Box::from(mult(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), + s2.clone(), + ), - (Identifier(s), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Var(Box::from(Int(1)), 1, s.clone()), *s1.clone(), ram)), - s2.clone(), - ); - first - } + (Identifier(s), Div(s1, s2)) => Div( + Box::from(mult(Var(Box::from(Int(1)), 1, s.clone()), *s1.clone(), ram)), + s2.clone(), + ), - (Div(s1, s2), Int(i)) => { - let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); - first - } + (Div(s1, s2), Int(i)) => Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()), - (Int(i), Div(s1, s2)) => { - let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); - first - } + (Int(i), Div(s1, s2)) => Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()), - (Div(s1, s2), Float(f)) => { - let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); - first - } + (Div(s1, s2), Float(f)) => Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()), - (Float(f), Div(s1, s2)) => { - let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); - first - } + (Float(f), Div(s1, s2)) => Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()), - (Div(s1, s2), Rational(r)) => { - let first = Div( - Box::from(mult(*s1.clone(), Int(r.over), ram)), - Box::from(mult(*s2.clone(), Int(r.under), ram)), - ); - first - } + (Div(s1, s2), Rational(r)) => Div( + Box::from(mult(*s1.clone(), Int(r.over), ram)), + Box::from(mult(*s2.clone(), Int(r.under), ram)), + ), - (Rational(r), Div(s1, s2)) => { - let first = Div( - Box::from(mult(Int(r.over), *s1.clone(), ram)), - Box::from(mult(*s2.clone(), Int(r.under), ram)), - ); - first - } + (Rational(r), Div(s1, s2)) => Div( + Box::from(mult(Int(r.over), *s1.clone(), ram)), + Box::from(mult(*s2.clone(), Int(r.under), ram)), + ), - (Div(s1, s2), Plus(s3, s4)) => { - let first = Div( - Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), - s2.clone(), - ); - first - } + (Div(s1, s2), Plus(s3, s4)) => Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ), - (Plus(s3, s4), Div(s1, s2)) => { - let first = Div( - Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), - s2.clone(), - ); - first - } + (Plus(s3, s4), Div(s1, s2)) => Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ), (Null, Div(s1, s2)) => Div(s1.clone(), s2.clone()), diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index dc855acd..3b02473a 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -18,25 +18,25 @@ pub fn interpret(ast: &Ast, mut ram: &mut Ram, mut function: &mut Functions) -> left: l, right: r, } => { - let param1 = interpret(l, &mut ram, &mut function); - let param2 = interpret(r, &mut ram, &mut function); + let param1 = interpret(l, ram, function); + let param2 = interpret(r, ram, function); let last = match v { - Parameters::PlusOperation => add(param1, param2, Some(&ram)), - Parameters::MinusOperation => minus(param1, param2, Some(&ram)), - Parameters::MultiplicationOperation => mult(param1, param2, Some(&ram)), - Parameters::DivideOperation => divide(param1, param2, Some(&ram)), - Parameters::ExpoOperation => expo(param1, param2, Some(&ram)), - Parameters::Equal => equal(param1, param2, Some(&ram)), - Parameters::Not => not(param1, param2, Some(&ram)), - Parameters::GreaterOperation => greater(param1, param2, Some(&ram)), - Parameters::GreaterOrEqualOperation => greater_or_equal(param1, param2, Some(&ram)), - Parameters::LesserOperation => lesser(param1, param2, Some(&ram)), - Parameters::LesserOrEqualOperation => lesser_or_equal(param1, param2, Some(&ram)), - Parameters::AndOperation => and(param1, param2, Some(&ram)), - Parameters::OrOperation => or(param1, param2, Some(&ram)), - Parameters::SelectionOperation => select(param1, param2, Some(&ram)), - Parameters::ConcatOperation => concat(param1, param2, Some(&ram)), - Parameters::Rational(s) => Parameters::Rational(s.clone()), + Parameters::PlusOperation => add(param1, param2, Some(ram)), + Parameters::MinusOperation => minus(param1, param2, Some(ram)), + Parameters::MultiplicationOperation => mult(param1, param2, Some(ram)), + Parameters::DivideOperation => divide(param1, param2, Some(ram)), + Parameters::ExpoOperation => expo(param1, param2, Some(ram)), + Parameters::Equal => equal(param1, param2, Some(ram)), + Parameters::Not => not(param1, param2, Some(ram)), + Parameters::GreaterOperation => greater(param1, param2, Some(ram)), + Parameters::GreaterOrEqualOperation => greater_or_equal(param1, param2, Some(ram)), + Parameters::LesserOperation => lesser(param1, param2, Some(ram)), + Parameters::LesserOrEqualOperation => lesser_or_equal(param1, param2, Some(ram)), + Parameters::AndOperation => and(param1, param2, Some(ram)), + Parameters::OrOperation => or(param1, param2, Some(ram)), + Parameters::SelectionOperation => select(param1, param2, Some(ram)), + Parameters::ConcatOperation => concat(param1, param2, Some(ram)), + Parameters::Rational(s) => Parameters::Rational(*s), Parameters::Str(s) => Parameters::Str(s.to_string()), Parameters::Assign => match *(l.clone()) { Ast::Call { name: n, lst: list } => { @@ -69,7 +69,7 @@ pub fn interpret(ast: &Ast, mut ram: &mut Ram, mut function: &mut Functions) -> }; let (a, b) = assign(p1, param2.clone()); - if a != "".to_string() { + if a != *"" { if ram.contains_key(&a) { ram.remove(&a); } @@ -104,9 +104,9 @@ pub fn interpret(ast: &Ast, mut ram: &mut Ram, mut function: &mut Functions) -> } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), *y, z.clone()), - Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), - Parameters::Mul(x, y) => mult(*x.clone(), *y.clone(), Some(&ram)), - Parameters::Div(x, y) => divide(*x.clone(), *y.clone(), Some(&ram)), + Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(ram)), + Parameters::Mul(x, y) => mult(*x.clone(), *y.clone(), Some(ram)), + Parameters::Div(x, y) => divide(*x.clone(), *y.clone(), Some(ram)), Parameters::Call(x, y) => { exec(x.clone(), vec![*y.clone()], Some(ram), Some(function)) } diff --git a/src/interpreting/stdlib.rs b/src/interpreting/stdlib.rs index 62ca24e1..837e99a1 100644 --- a/src/interpreting/stdlib.rs +++ b/src/interpreting/stdlib.rs @@ -80,10 +80,11 @@ pub fn exec(s: String, lst: Vec, ram: Ram, functions: Functions) -> value: v, left: _l, right: _r, - } => match v { - Identifier(s) => names.push(s.clone()), - _ => (), - }, + } => { + if let Identifier(s) = v { + names.push(s.clone()) + } + } } } names @@ -114,7 +115,7 @@ pub fn print(p: &Vec) -> Parameters { } pub fn split_string(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } let str = match p.first() { @@ -129,7 +130,7 @@ pub fn split_string(p: &Vec, ram: &Ram) -> Parameters { }, _ => "", }; - if str == "" { + if str.is_empty() { return Null; } let separator = match p.get(1) { @@ -145,7 +146,7 @@ pub fn split_string(p: &Vec, ram: &Ram) -> Parameters { _ => "", }; - if separator == "" { + if separator.is_empty() { InterpreterVector( str.chars() .map(|f| Str(f.to_string())) @@ -163,7 +164,7 @@ pub fn split_string(p: &Vec, ram: &Ram) -> Parameters { } pub fn join_string(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } let delimiter = match p.last() { @@ -199,7 +200,7 @@ pub fn join_string(p: &Vec, ram: &Ram) -> Parameters { .to_string()) } pub fn cos(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -212,12 +213,12 @@ pub fn cos(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.cos()) } @@ -227,9 +228,9 @@ pub fn cos(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.cos()) } @@ -286,7 +287,7 @@ pub fn cos(p: &Vec, ram: &Ram) -> Parameters { } pub fn sin(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -299,12 +300,12 @@ pub fn sin(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.sin()) } @@ -314,9 +315,9 @@ pub fn sin(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.sin()) } @@ -373,7 +374,7 @@ pub fn sin(p: &Vec, ram: &Ram) -> Parameters { } pub fn tan(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -386,12 +387,12 @@ pub fn tan(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.tan()) } @@ -401,9 +402,9 @@ pub fn tan(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.tan()) } @@ -461,7 +462,7 @@ pub fn tan(p: &Vec, ram: &Ram) -> Parameters { } pub fn cosh(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -474,12 +475,12 @@ pub fn cosh(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.cosh()) } @@ -489,9 +490,9 @@ pub fn cosh(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.cosh()) } @@ -549,7 +550,7 @@ pub fn cosh(p: &Vec, ram: &Ram) -> Parameters { } pub fn sinh(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -562,12 +563,12 @@ pub fn sinh(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.sinh()) } @@ -577,9 +578,9 @@ pub fn sinh(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.sinh()) } @@ -637,7 +638,7 @@ pub fn sinh(p: &Vec, ram: &Ram) -> Parameters { } pub fn tanh(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -650,12 +651,12 @@ pub fn tanh(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = if degrees { - ((*i).clone() as f64) * (PI / 180.0) + ((*i) as f64) * (PI / 180.0) } else { - (*i).clone() as f64 + (*i) as f64 }; Float(fs.tanh()) } @@ -665,9 +666,9 @@ pub fn tanh(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { let fs = if degrees { - s.clone().approx() * PI / 180.0 + (*s).approx() * PI / 180.0 } else { - s.clone().approx() + (*s).approx() }; Float(fs.tanh()) } @@ -725,7 +726,7 @@ pub fn tanh(p: &Vec, ram: &Ram) -> Parameters { } pub fn acos(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -738,7 +739,7 @@ pub fn acos(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; Float(if degrees { @@ -753,9 +754,9 @@ pub fn acos(p: &Vec, ram: &Ram) -> Parameters { f.acos() }), Rational(s) => Parameters::Float(if degrees { - s.clone().approx().acos() * 180.0 / PI + (*s).approx().acos() * 180.0 / PI } else { - s.clone().approx().acos() + (*s).approx().acos() }), InterpreterVector(vec) => { @@ -811,7 +812,7 @@ pub fn acos(p: &Vec, ram: &Ram) -> Parameters { } pub fn asin(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -824,7 +825,7 @@ pub fn asin(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; Float(if degrees { @@ -840,9 +841,9 @@ pub fn asin(p: &Vec, ram: &Ram) -> Parameters { }), Rational(s) => Parameters::Float(if degrees { - s.clone().approx().asin() * (180.0 / PI) + (*s).approx().asin() * (180.0 / PI) } else { - s.clone().approx().asin() + (*s).approx().asin() }), InterpreterVector(vec) => { @@ -898,7 +899,7 @@ pub fn asin(p: &Vec, ram: &Ram) -> Parameters { } pub fn atan(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -911,7 +912,7 @@ pub fn atan(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; Float(if degrees { @@ -927,9 +928,9 @@ pub fn atan(p: &Vec, ram: &Ram) -> Parameters { }), Rational(s) => Parameters::Float(if degrees { - s.clone().approx().atan() * (180.0 / PI) + (*s).approx().atan() * (180.0 / PI) } else { - s.clone().approx().atan() + (*s).approx().atan() }), InterpreterVector(vec) => { @@ -985,7 +986,7 @@ pub fn atan(p: &Vec, ram: &Ram) -> Parameters { } pub fn exp(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -1006,7 +1007,7 @@ pub fn exp(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; if plus { @@ -1024,9 +1025,9 @@ pub fn exp(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { if plus { - Float(ln.powf(s.clone().approx())) + Float(ln.powf((*s).approx())) } else { - Float(s.clone().approx().exp()) + Float((*s).approx().exp()) } } @@ -1073,7 +1074,7 @@ pub fn exp(p: &Vec, ram: &Ram) -> Parameters { } pub fn ln(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -1094,7 +1095,7 @@ pub fn ln(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; if plus { @@ -1113,9 +1114,9 @@ pub fn ln(p: &Vec, ram: &Ram) -> Parameters { Rational(s) => { if plus { - Float(s.clone().approx().log(sln)) + Float((*s).approx().log(sln)) } else { - Float(s.clone().approx().ln()) + Float((*s).approx().ln()) } } @@ -1162,7 +1163,7 @@ pub fn ln(p: &Vec, ram: &Ram) -> Parameters { } pub fn sqrt(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -1183,7 +1184,7 @@ pub fn sqrt(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; if plus { @@ -1201,9 +1202,9 @@ pub fn sqrt(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { if plus { - Float(s.clone().approx().powf(1.0 / sln)) + Float((*s).approx().powf(1.0 / sln)) } else { - Float(s.clone().approx().sqrt()) + Float((*s).approx().sqrt()) } } @@ -1221,9 +1222,9 @@ pub fn sqrt(p: &Vec, ram: &Ram) -> Parameters { f.sqrt() })), Rational(s) => res.push(Parameters::Float(if plus { - s.clone().approx().powf(1.0 / sln) + s.approx().powf(1.0 / sln) } else { - s.clone().approx().sqrt() + s.approx().sqrt() })), Identifier(s) => match ram { None => (), @@ -1264,11 +1265,11 @@ pub fn fact(n: i64) -> i64 { } pub fn factorial(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int(fact(*i)), Float(f) => Parameters::Int(fact(*f as i64)), Identifier(s) => match ram { @@ -1283,14 +1284,14 @@ pub fn factorial(p: &Vec, ram: &Ram) -> Parameters { } pub fn abs(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int(i.abs()), Float(f) => Parameters::Float(f.abs()), - Rational(s) => Parameters::Rational(s.clone().abs()), + Rational(s) => Parameters::Rational((*s).abs()), Identifier(s) => match ram { None => Identifier("This variable is not initialized yet".to_string()), Some(ref t) => match t.get(s.as_str()) { @@ -1303,11 +1304,11 @@ pub fn abs(p: &Vec, ram: &Ram) -> Parameters { } pub fn ceil(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Float((*i as f64).ceil()), Float(f) => Parameters::Float(f.ceil()), Identifier(s) => match ram { @@ -1322,11 +1323,11 @@ pub fn ceil(p: &Vec, ram: &Ram) -> Parameters { } pub fn floor(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Float((*i as f64).floor()), Float(f) => Parameters::Float(f.floor()), Identifier(s) => match ram { @@ -1341,7 +1342,7 @@ pub fn floor(p: &Vec, ram: &Ram) -> Parameters { } pub fn round(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } @@ -1362,7 +1363,7 @@ pub fn round(p: &Vec, ram: &Ram) -> Parameters { } } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => { let fs: f64 = (*i) as f64; if plus { @@ -1380,9 +1381,9 @@ pub fn round(p: &Vec, ram: &Ram) -> Parameters { } Rational(s) => { if plus { - Float((s.clone().approx() * 10.0_f64.powf(sln).round()) / (10.0_f64.powf(sln))) + Float(((*s).approx() * 10.0_f64.powf(sln).round()) / (10.0_f64.powf(sln))) } else { - Float(s.clone().approx().round()) + Float((*s).approx().round()) } } Identifier(s) => match ram { @@ -1397,11 +1398,11 @@ pub fn round(p: &Vec, ram: &Ram) -> Parameters { } pub fn norm(p: &Vec, ram: &Ram, function: &Functions) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int((*i).abs()), Float(f) => Parameters::Float((*f).abs()), InterpreterVector(lst) => { @@ -1431,14 +1432,14 @@ pub fn norm(p: &Vec, ram: &Ram, function: &Functions) -> Parameters } pub fn transpose_vectors(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int((*i).abs()), Float(f) => Parameters::Float((*f).abs()), - Rational(s) => Parameters::Rational(s.clone().abs()), + Rational(s) => Parameters::Rational((*s).abs()), InterpreterVector(lst) => { let r = vec![*(lst.clone())]; let transposed = transpose(r); @@ -1464,14 +1465,14 @@ pub fn transpose_vectors(p: &Vec, ram: &Ram) -> Parameters { } pub fn transpose_matrices(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int((*i).abs()), Float(f) => Parameters::Float((*f).abs()), - Rational(s) => Parameters::Rational(s.clone().abs()), + Rational(s) => Parameters::Rational((*s).abs()), InterpreterVector(lst) => { let mut res1 = Vec::new(); let mut is_matrix = true; @@ -1509,14 +1510,14 @@ pub fn transpose_matrices(p: &Vec, ram: &Ram) -> Parameters { } pub fn det_matrix(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int((*i).abs()), Float(f) => Parameters::Float((*f).abs()), - Rational(s) => Parameters::Rational(s.clone().abs()), + Rational(s) => Parameters::Rational((*s).abs()), InterpreterVector(lst) => { let mut res1 = Vec::new(); let mut is_matrix = true; @@ -1561,14 +1562,14 @@ pub fn det_matrix(p: &Vec, ram: &Ram) -> Parameters { } pub fn inverse_matrix(p: &Vec, ram: &Ram) -> Parameters { - if p.len() < 1 { + if p.is_empty() { return Null; } - match p.get(0).unwrap() { + match p.first().unwrap() { Int(i) => Parameters::Int((*i).abs()), Float(f) => Parameters::Float((*f).abs()), - Rational(s) => Parameters::Rational(s.clone().abs()), + Rational(s) => Parameters::Rational((*s).abs()), InterpreterVector(lst) => { let mut res1 = Vec::new(); let mut is_matrix = true; @@ -1615,7 +1616,7 @@ pub fn inverse_matrix(p: &Vec, ram: &Ram) -> Parameters { "@Determinant is zero, matrix is not invertible".to_string(), ) } - Rational(s) if s.clone().is_null() => { + Rational(s) if s.is_null() => { return Identifier( "@Determinant is zero, matrix is not invertible".to_string(), ) @@ -1649,7 +1650,7 @@ pub fn diff(p: &Vec, ram: &Ram, function: &Functions) -> Parameters Err(_) => load_config(Config::default()).general_color, }; - if p.len() == 0 { + if p.is_empty() { let m = color.paint("Usage: diff "); println!("{m}"); return Null; @@ -1852,7 +1853,7 @@ pub fn plot_fn( Err(_) => load_config(Config::default()).general_color, }; - if p.len() == 0 { + if p.is_empty() { let m = color.paint(" > plot(): displays help\n > plot(f): plot f\n > plot(f,title,xlabel,ylabel): plot f with title,xlabel,ylabel\n > plot(f,mode): plot f with the mode=LINE|LINEMARKS|MARKS(default)\n > plot(f,title,xlabel,ylabel,mode): plot f with title,xlabel,ylabel and mode\n > plot(f,start,end,step,mode): plot f between start and end with steps and mode\n > plot(f,start,end,step,title,xlabel,ylabel,mode): combines\n"); println!("{m}"); return Null; @@ -1947,7 +1948,7 @@ pub fn plot_fn( Some(p) => match p { Float(f) => start = *f, Int(i) => start = *i as f64, - Rational(s) => start = s.clone().approx(), + Rational(s) => start = (*s).approx(), InterpreterVector(vec) => second_vector = Some(&**vec), Identifier(s) if ram.as_ref().unwrap().contains_key(s) => { @@ -1974,7 +1975,7 @@ pub fn plot_fn( Some(p) => match p { Float(f) => end = *f, Int(i) => end = *i as f64, - Rational(s) => end = s.clone().approx(), + Rational(s) => end = (*s).approx(), Identifier(s) if ram.as_ref().unwrap().contains_key(s) => { match ram.as_ref().unwrap().get(s) { @@ -1990,7 +1991,7 @@ pub fn plot_fn( "line" => mode = "line", "linemarks" => mode = "linemarks", _ => { - if title == "".to_string() { + if title == *"" { title = s.to_string() } else { xlabel = s.to_string() @@ -2006,7 +2007,7 @@ pub fn plot_fn( Some(p) => match p { Float(f) => steps = *f, Int(i) => steps = *i as f64, - Rational(s) => steps = s.clone().approx(), + Rational(s) => steps = (*s).approx(), Identifier(s) if ram.as_ref().unwrap().contains_key(s) => { match ram.as_ref().unwrap().get(s) { @@ -2020,9 +2021,9 @@ pub fn plot_fn( "line" => mode = "line", "linemarks" => mode = "linemarks", _ => { - if title == "".to_string() { + if title == *"" { title = s.to_string() - } else if xlabel == "".to_string() { + } else if xlabel == *"" { xlabel = s.to_string() } else { ylabel = s.to_string() @@ -2035,86 +2036,90 @@ pub fn plot_fn( match p.get(4) { None => (), - Some(p) => match p { - Str(s) => match s.to_lowercase().as_str() { - "marks" => mode = "marks", - "line" => mode = "line", - "linemarks" => mode = "linemarks", - _ => { - if title == "".to_string() { - title = s.to_string() - } else if xlabel == "".to_string() { - xlabel = s.to_string() - } else { - ylabel = s.to_string() + Some(p) => { + if let Str(s) = p { + match s.to_lowercase().as_str() { + "marks" => mode = "marks", + "line" => mode = "line", + "linemarks" => mode = "linemarks", + _ => { + if title == *"" { + title = s.to_string() + } else if xlabel == *"" { + xlabel = s.to_string() + } else { + ylabel = s.to_string() + } } } - }, - _ => (), - }, + } + } } match p.get(5) { None => (), - Some(p) => match p { - Str(s) => match s.to_lowercase().as_str() { - "marks" => mode = "marks", - "line" => mode = "line", - "linemarks" => mode = "linemarks", - _ => { - if title == "".to_string() { - title = s.to_string() - } else if xlabel == "".to_string() { - xlabel = s.to_string() - } else { - ylabel = s.to_string() + Some(p) => { + if let Str(s) = p { + match s.to_lowercase().as_str() { + "marks" => mode = "marks", + "line" => mode = "line", + "linemarks" => mode = "linemarks", + _ => { + if title == *"" { + title = s.to_string() + } else if xlabel == *"" { + xlabel = s.to_string() + } else { + ylabel = s.to_string() + } } } - }, - _ => (), - }, + } + } } match p.get(6) { None => (), - Some(p) => match p { - Str(s) => match s.to_lowercase().as_str() { - "marks" => mode = "marks", - "line" => mode = "line", - "linemarks" => mode = "linemarks", - _ => { - if title == "".to_string() { - title = s.to_string() - } else if xlabel == "".to_string() { - xlabel = s.to_string() - } else { - ylabel = s.to_string() + Some(p) => { + if let Str(s) = p { + match s.to_lowercase().as_str() { + "marks" => mode = "marks", + "line" => mode = "line", + "linemarks" => mode = "linemarks", + _ => { + if title == *"" { + title = s.to_string() + } else if xlabel == *"" { + xlabel = s.to_string() + } else { + ylabel = s.to_string() + } } } - }, - _ => (), - }, + } + } } match p.get(7) { None => (), - Some(p) => match p { - Str(s) => match s.to_lowercase().as_str() { - "marks" => mode = "marks", - "line" => mode = "line", - "linemarks" => mode = "linemarks", - _ => { - if title == "".to_string() { - title = s.to_string() - } else if xlabel == "".to_string() { - xlabel = s.to_string() - } else if ylabel == "".to_string() { - ylabel = s.to_string() + Some(p) => { + if let Str(s) = p { + match s.to_lowercase().as_str() { + "marks" => mode = "marks", + "line" => mode = "line", + "linemarks" => mode = "linemarks", + _ => { + if title == *"" { + title = s.to_string() + } else if xlabel == *"" { + xlabel = s.to_string() + } else if ylabel == *"" { + ylabel = s.to_string() + } } } - }, - _ => (), - }, + } + } } let st = start; @@ -2136,7 +2141,7 @@ pub fn plot_fn( sram.insert("e".to_string(), Float(E)); while start <= end { x.push(start); - if &fd == "" { + if fd.is_empty() { let p = f(&vec![Float(start)], ram); y.push(match p { Float(f) => f, @@ -2157,10 +2162,11 @@ pub fn plot_fn( value: v, left: _l, right: _r, - } => match v { - Identifier(s) => names.push(s.clone()), - _ => (), - }, + } => { + if let Identifier(s) = v { + names.push(s.clone()) + } + } } } names @@ -2181,14 +2187,14 @@ pub fn plot_fn( } else { match first_vector { Some(t) => { - t.into_iter().for_each(|j| match j { + t.iter().for_each(|j| match j { Int(i) => x.push(*i as f64), Float(f) => x.push(*f), - Rational(s) => x.push(s.clone().approx()), + Rational(s) => x.push((*s).approx()), Identifier(s) => match ram.as_ref().unwrap().get(s) { Some(Int(i)) => x.push(*i as f64), Some(Float(f)) => x.push(*f), - Some(Rational(r)) => x.push(r.clone().approx()), + Some(Rational(r)) => x.push((*r).approx()), _ => (), }, _ => (), @@ -2199,14 +2205,14 @@ pub fn plot_fn( match second_vector { Some(t) => { - t.into_iter().for_each(|j| match j { + t.iter().for_each(|j| match j { Int(i) => y.push(*i as f64), Float(f) => y.push(*f), - Rational(r) => y.push(r.clone().approx()), + Rational(r) => y.push((*r).approx()), Identifier(s) => match ram.as_ref().unwrap().get(s) { Some(Int(i)) => y.push(*i as f64), Some(Float(f)) => y.push(*f), - Some(Rational(r)) => y.push(r.clone().approx()), + Some(Rational(r)) => y.push((*r).approx()), _ => (), }, _ => (), diff --git a/src/lexing/lexer.rs b/src/lexing/lexer.rs index 1d2945c1..4bf1260a 100644 --- a/src/lexing/lexer.rs +++ b/src/lexing/lexer.rs @@ -95,12 +95,9 @@ fn lex_float( ) -> (f64, usize) { current_pos += 1; let current_char_options = chars.get(current_pos); - let current_char = match current_char_options { - Some(t) => t, - None => &'0', - }; + let current_char = current_char_options.unwrap_or(&'0'); let (a, b) = lex_raddix(*current_char, chars, current_pos, len); - let f = f64::from_str(&*(whole_side.to_string().as_str().to_owned() + "." + a.as_str())); + let f = f64::from_str(&(whole_side.to_string().as_str().to_owned() + "." + a.as_str())); if f.is_err() { return (f64::NAN, b); } @@ -119,14 +116,14 @@ pub fn lex(input: String) -> Vec { let length = input.len(); while current_pos < input.len() { let peeking_char = chars.get(current_pos); - let current_character: char; - match peeking_char { + + let current_character: char = match peeking_char { None => { current_pos += 1; continue; } - Some(t) => current_character = t.clone(), - } + Some(t) => *t, + }; if !is_an_allowed_char(current_character) { current_pos += 1; continue; diff --git a/src/main.rs b/src/main.rs index 5a693498..3aab4d8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -247,7 +247,7 @@ fn set_config(config: Config, args: &mut SplitWhitespace) -> (String, Option (String, Option) { @@ -264,7 +264,7 @@ fn reload_config() -> (String, Option) { } fn show_help_config() -> (String, Option) { - (format!("Config help, \n > config show: show config \n > config set: set config \n > config reload: reload config \n > config reset: reset config\n\n"),None) + ("Config help, \n > config show: show config \n > config set: set config \n > config reload: reload config \n > config reset: reset config\n\n".to_string(),None) } fn handle_config(line: &str, config: Config) -> (String, Option) { @@ -272,7 +272,7 @@ fn handle_config(line: &str, config: Config) -> (String, Option) { None => show_help_config(), Some(t) => { let mut w = t.split_whitespace(); - match w.nth(0) { + match w.next() { None => show_help_config(), Some("set") => set_config(config, &mut w.clone()), Some("reload") => reload_config(), @@ -295,19 +295,19 @@ fn main() { a.push(line.unwrap().to_string()); } } else { - args.nth(0); + args.next(); args.for_each(|f| a.push(f)); } let arg_final = a.join(""); if arg_final == "-h" || arg_final == "--help" { println!("-----Help Calc-----"); - println!(""); + println!(); println!("mini-calc > launch the mini-calc REPL"); println!("mini-calc [arg] > compute non interactively"); println!("mini-calc -h || --help > open this help"); println!("mini-calc -u || --update > update the binary"); - println!(""); + println!(); println!("------Help Calc-----"); exit(0); } @@ -372,7 +372,7 @@ fn main() { }); let message = &loaded.greeting_message; - println!("{}", message.to_string()); + println!("{}", message); let interface = Interface::new("calc").unwrap(); let style = &loaded.clone().prompt_style; diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 6b098606..fffbe1a4 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -98,11 +98,11 @@ pub fn int_to_superscript_string(i: i64) -> String { let string_int = i.to_string(); string_int .split("") - .map(|x| digit_to_superscript_char(x)) + .map(digit_to_superscript_char) .for_each(|f| vec.push(f)); let i = vec.join(""); - if i == "⁰".to_string() { + if i == *"⁰" { "error".to_string() } else if i == "¹" { "".to_string() @@ -166,7 +166,7 @@ impl Display for Ast { Ast::Call { name: v, lst: s } => { let mut vs = Vec::new(); s.iter().for_each(|x1| vs.push(x1.to_string())); - write!(f, "{}({})", v, vs.join(",").to_string()) + write!(f, "{}({})", v, vs.join(",")) } Ast::Conditional { condition, @@ -195,22 +195,20 @@ impl Parameters { Identifier(s) => { if s.starts_with("@") { match s.strip_prefix("@") { - None => format!(""), + None => String::new(), Some(c) => { format!("{} {}", Color::Purple.paint("Error:"), Color::Red.paint(c)) } } + } else if ram.is_none() { + self.to_string() } else { - if ram.is_none() { - return self.to_string(); - } else { - match ram.as_mut().unwrap().get(s) { - None => s.to_string(), - Some(t) => t.clone().pretty_print( - Some(ram.as_mut().unwrap()), - Some(function.as_mut().unwrap()), - ), - } + match ram.as_mut().unwrap().get(s) { + None => s.to_string(), + Some(t) => t.clone().pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ), } } } @@ -236,14 +234,14 @@ impl Parameters { "" } } - Float(f) if f >= 1.0 - 1e-10 && f <= 1.0 + 1e-10 => { + Float(f) if (1.0 - 1e-10..=1.0 + 1e-10).contains(&f) => { if division { "1" } else { "" } } - Rational(r) if r.clone() == Rationals::new(1, 1) => { + Rational(r) if r == Rationals::new(1, 1) => { if division { "1" } else { @@ -257,14 +255,14 @@ impl Parameters { "-" } } - Float(f) if f >= -1.0 - 1e-10 && f <= -1.0 + 1e-10 => { + Float(f) if (-1.0 - 1e-10..=-1.0 + 1e-10).contains(&f) => { if division { "-1" } else { "" } } - Rational(r) if r.clone() == Rationals::new(-1, 1) => { + Rational(r) if r == Rationals::new(-1, 1) => { if division { "-1" } else { @@ -317,8 +315,8 @@ impl Parameters { match y_printed.chars().nth(0) { Some('-') => format!("({}{})", x_printed, y_printed), _ => { - if y_printed == "0".to_string() { - format!("{}", x_printed) + if y_printed == *"0" { + x_printed.to_string() } else { format!("({})+({})", x_printed, y_printed) } @@ -355,12 +353,11 @@ impl Parameters { * ------------- */ let mut matrix = false; - if vec.len() == 0 { - return format!(""); + if vec.is_empty() { + return String::new(); } - match lst.first().unwrap() { - Parameters::InterpreterVector(_) => matrix = true, - _ => (), + if let Parameters::InterpreterVector(_) = lst.first().unwrap() { + matrix = true } if !matrix { format!("|{}|", vec.join(" ")) @@ -470,7 +467,7 @@ impl Parameters { } else { format!( "{}: {} = {}", - Color::Cyan.paint(format!("{}", s.clone())), + Color::Cyan.paint(s.clone().to_string()), Color::Yellow.paint("ident"), Color::Yellow.paint(self.pretty_print(ram, function)) ) @@ -567,7 +564,7 @@ impl Parameters { let param = t.get(&s); match param { None => Parameters::Null, - Some(t) => t.clone().abs(ram.as_deref()), + Some(t) => t.clone().abs(ram), } } }, diff --git a/src/parsing/parser.rs b/src/parsing/parser.rs index fc443d02..3a9efcb3 100644 --- a/src/parsing/parser.rs +++ b/src/parsing/parser.rs @@ -68,7 +68,7 @@ impl CalcParser<'_> { } pub fn consume(&mut self) -> Token { self.look_ahead(0); - if self.read.len() == 0 { + if self.read.is_empty() { return Null; } self.read.remove(0) @@ -79,12 +79,12 @@ impl CalcParser<'_> { if token.to_token_type() != expected { return false; } - return true; + true } pub fn consume_expected(&mut self, expected: TokenType) -> Token { self.look_ahead(0); - if self.read.len() == 0 { + if self.read.is_empty() { return Null; } match self.read.remove(0) { diff --git a/src/utils/matrix_utils.rs b/src/utils/matrix_utils.rs index 2729face..4450bc4b 100644 --- a/src/utils/matrix_utils.rs +++ b/src/utils/matrix_utils.rs @@ -43,10 +43,10 @@ pub fn mult_matrix(a: Matrix, b: Matrix, ram: ORam) -> M let intermediary = mult( a.get(i).unwrap().get(k).unwrap().clone(), b.get(k).unwrap().get(j).unwrap().clone(), - ram.as_deref(), + ram, ); - sum = add(sum, intermediary, ram.as_deref()) + sum = add(sum, intermediary, ram) } s.push(sum); @@ -78,13 +78,10 @@ pub fn lup_decompose( i_max = i; for k in i..n { - abs_a = ((a[k])[i]).clone().abs(ram.as_deref()); - match greater(abs_a.clone(), max_a.clone(), ram.as_deref()) { - Parameters::Bool(true) => { - max_a = (abs_a).clone(); - i_max = k; - } - _ => (), + abs_a = ((a[k])[i]).clone().abs(ram); + if let Parameters::Bool(true) = greater(abs_a.clone(), max_a.clone(), ram) { + max_a = (abs_a).clone(); + i_max = k; } } @@ -107,21 +104,21 @@ pub fn lup_decompose( (a)[i] = (a)[i_max].clone(); (a)[i_max] = ptr.clone(); - (p)[n] = add((p)[n].clone(), Parameters::Int(1), ram.as_deref()); + (p)[n] = add((p)[n].clone(), Parameters::Int(1), ram); } for j in (i + 1)..n { - (a)[j][i] = divide((a)[j][i].clone(), (a)[i][i].clone(), ram.as_deref()); + (a)[j][i] = divide((a)[j][i].clone(), (a)[i][i].clone(), ram); for k in (i + 1)..n { (a)[j][k] = minus( (a)[j][k].clone(), - mult((a)[j][i].clone(), (a)[i][k].clone(), ram.as_deref()), - ram.as_deref(), + mult((a)[j][i].clone(), (a)[i][k].clone(), ram), + ram, ) } } } - return 1; + 1 } pub fn lup_determinant( @@ -130,9 +127,9 @@ pub fn lup_determinant( n: usize, ram: ORam, ) -> Parameters { - let mut det: Parameters = (&a[0][0]).clone(); + let mut det: Parameters = a[0][0].clone(); for i in 1..n { - det = mult(det.clone(), (&a[i][i]).clone(), ram.as_deref()) + det = mult(det.clone(), a[i][i].clone(), ram) } match p[n] { @@ -140,14 +137,14 @@ pub fn lup_determinant( if (i - (n as i64)) % 2 == 0 { det } else { - minus(Parameters::Int(0), det, ram.as_deref()) + minus(Parameters::Int(0), det, ram) } } Parameters::Float(f) => { if (f - (n as f64)) % 2.0 == 0.0 { det } else { - minus(Parameters::Float(0.0), det, ram.as_deref()) + minus(Parameters::Float(0.0), det, ram) } } _ => Parameters::Float(f64::NAN), @@ -203,9 +200,9 @@ pub fn lup_invert( }; for k in 0..i { ia[i][j] = minus( - (&ia[i][j]).clone(), - mult((&a[i][k]).clone(), (&ia[k][j]).clone(), ram.as_deref()), - ram.as_deref(), + ia[i][j].clone(), + mult(a[i][k].clone(), ia[k][j].clone(), ram), + ram, ); } } @@ -213,12 +210,12 @@ pub fn lup_invert( for i in (0..n).rev() { for k in i + 1..n { ia[i][j] = minus( - (&ia[i][j]).clone(), - mult((&a[i][k]).clone(), (&ia[k][j]).clone(), ram.as_deref()), - ram.as_deref(), + ia[i][j].clone(), + mult(a[i][k].clone(), ia[k][j].clone(), ram), + ram, ) } - ia[i][j] = divide((&ia[i][j]).clone(), (&a[i][i]).clone(), ram.as_deref()); + ia[i][j] = divide(ia[i][j].clone(), a[i][i].clone(), ram); } } } @@ -255,11 +252,11 @@ mod test { let mut b = vec![Parameters::Int(0); 4]; - let _ = lup_decompose(&mut a, &mut b, 3 as usize, None); + let _ = lup_decompose(&mut a, &mut b, 3_usize, None); println!("{:?}/{:?}", &a, &b); - let det = lup_determinant(&mut a, &mut b, 3 as usize, None); + let det = lup_determinant(&mut a, &mut b, 3_usize, None); println!("{:?}", det); assert_eq!( diff --git a/src/utils/plot_utils.rs b/src/utils/plot_utils.rs index c53e7a82..593d8913 100644 --- a/src/utils/plot_utils.rs +++ b/src/utils/plot_utils.rs @@ -7,13 +7,13 @@ pub fn computes_lines( title: String, xlabel: String, ylabel: String, -) -> () { +) { let mut bitmap = vec![vec![' '; 100]; 30]; let mut ymin = f64::MAX; let mut ymax = f64::MIN; - y.into_iter().for_each(|y| { + y.iter().for_each(|y| { if y > &ymax { ymax = *y } @@ -32,7 +32,7 @@ pub fn computes_lines( y_scale = 1.0; } - let z = x.into_iter().zip(y).map(|(x, y)| { + let z = x.iter().zip(y).map(|(x, y)| { ( ((*x - start) / x_scale) as usize, ((*y - ymin) / y_scale) as usize, @@ -53,9 +53,9 @@ pub fn computes_lines( print!("{char}"); } - println!(""); + println!(); - if &title != "" { + if !title.is_empty() { let left_padding = (104 - title.len()) / 2; let right_padding = (104 - title.len()) - left_padding; for _ in 0..left_padding { @@ -66,7 +66,7 @@ pub fn computes_lines( print!("*") } - println!(""); + println!(); } let size = ylabel.len(); @@ -117,7 +117,7 @@ pub fn computes_lines( for y in 0..xs.len() { print!("{}", xs[y]); } - print!("*\n"); + println!("*"); } print!("* |"); @@ -141,7 +141,7 @@ pub fn computes_lines( } println!("{:.2} *", end); - if &xlabel != "" { + if !xlabel.is_empty() { let first = 104 / 2 - xlabel.len(); let last = 104 - first - xlabel.len(); for _ in 0..first { @@ -157,5 +157,5 @@ pub fn computes_lines( } } - println!(""); + println!(); } From 6b1e715919e6961fc151772e7b2db05a7aba1521 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 27 Jul 2025 11:27:04 +0200 Subject: [PATCH 2/6] clippy: correct clippy in lexer test --- src/lexing/lexer.rs | 57 +++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/src/lexing/lexer.rs b/src/lexing/lexer.rs index 4bf1260a..638980af 100644 --- a/src/lexing/lexer.rs +++ b/src/lexing/lexer.rs @@ -339,137 +339,112 @@ mod tests { #[test] fn lex_plus() { - let mut expected = Vec::new(); - expected.push(OPE(PLUS)); + let expected = vec![OPE(PLUS)]; let result = lex("+".to_string()); assert_eq!(result, expected) } #[test] fn lex_minus() { - let mut expected = Vec::new(); - expected.push(OPE(MINUS)); + let expected = vec![OPE(MINUS)]; let result = lex("-".to_string()); assert_eq!(result, expected) } #[test] fn lex_mult() { - let mut expected = Vec::new(); - expected.push(OPE(MULTIPLICATION)); + let expected = vec![OPE(MULTIPLICATION)]; let result = lex("*".to_string()); assert_eq!(result, expected) } #[test] fn lex_divide() { - let mut expected = Vec::new(); - expected.push(OPE(DIVIDE)); + let expected = vec![OPE(DIVIDE)]; let result = lex("/".to_string()); assert_eq!(result, expected) } #[test] fn lex_operators() { - let mut expected = Vec::new(); - expected.push(OPE(PLUS)); - expected.push(OPE(MULTIPLICATION)); - expected.push(OPE(MINUS)); - expected.push(OPE(DIVIDE)); + let expected = vec![OPE(PLUS), OPE(MULTIPLICATION), OPE(MINUS), OPE(DIVIDE)]; let result = lex("+*-/".to_string()); assert_eq!(result, expected) } #[test] fn lex_lpar() { - let mut expected = Vec::new(); - expected.push(LPAR); + let expected = vec![LPAR]; let result = lex("(".to_string()); assert_eq!(result, expected) } #[test] fn lex_rpar() { - let mut expected = Vec::new(); - expected.push(RPAR); + let expected = vec![RPAR]; let result = lex(")".to_string()); assert_eq!(result, expected) } #[test] fn lex_equal() { - let mut expected = Vec::new(); - expected.push(EQUAL); + let expected = vec![EQUAL]; let result = lex("=".to_string()); assert_eq!(result, expected); } #[test] fn lex_tokens() { - let mut expected = Vec::new(); - expected.push(LPAR); - expected.push(RPAR); - expected.push(EQUAL); + let expected = vec![LPAR, RPAR, EQUAL]; let result = lex("()=".to_string()); assert_eq!(result, expected) } #[test] fn lex_simple_int() { - let mut expected = Vec::new(); - expected.push(INT(1)); + let expected = vec![INT(1)]; let result = lex("1".to_string()); assert_eq!(result, expected); } #[test] fn lex_complex_int() { - let mut expected = Vec::new(); - expected.push(INT(100)); + let expected = vec![INT(100)]; let result = lex("100".to_string()); assert_eq!(result, expected); } #[test] fn lex_simple_string() { - let mut expected = Vec::new(); - expected.push(IDENTIFIER("test".to_string())); + let expected = vec![IDENTIFIER("test".to_string())]; let result = lex("test".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_operation() { - let mut expected = Vec::new(); - expected.push(INT(1)); - expected.push(OPE(PLUS)); - expected.push(INT(1)); + let expected = vec![INT(1), OPE(PLUS), INT(1)]; let result = lex("1 + 1".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_equality() { - let mut expected = Vec::new(); - expected.push(IDENTIFIER("var1".to_string())); - expected.push(EQUAL); - expected.push(INT(100)); + let expected = vec![IDENTIFIER("var1".to_string()), EQUAL, INT(100)]; let result = lex("var1 = 100".to_string()); assert_eq!(result, expected) } #[test] fn test_simple_float() { - let mut expected = Vec::new(); - expected.push(FLOAT(0.14)); + let expected = vec![FLOAT(0.14)]; let result = lex("0.14".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_float() { - let mut expected = Vec::new(); - expected.push(FLOAT(314.05)); + let expected = vec![FLOAT(314.05)]; let result = lex("314.05".to_string()); assert_eq!(result, expected) } From 10cb01dcc67f41f088cc2ccf6dfc0b151f844720 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 27 Jul 2025 11:40:31 +0200 Subject: [PATCH 3/6] clippy: correct clippy in plot utils --- src/interpreting/stdlib.rs | 2 +- src/main.rs | 8 ++++---- src/utils/plot_utils.rs | 17 ++++++----------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/interpreting/stdlib.rs b/src/interpreting/stdlib.rs index 837e99a1..3db8168d 100644 --- a/src/interpreting/stdlib.rs +++ b/src/interpreting/stdlib.rs @@ -2246,7 +2246,7 @@ pub fn plot_fn( if !terminal { f.show().unwrap(); } else { - computes_lines(&x, &y, st, end, steps, title, xlabel, ylabel); + computes_lines(&x, &y, st, end, title, xlabel, ylabel); } Null } diff --git a/src/main.rs b/src/main.rs index 3aab4d8b..dc16bb38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -593,18 +593,18 @@ impl Completer for CalcCompleter { Some(co) } - Some("toggle_float") => match words.next() { - _ => { + Some("toggle_float") => { + words.next(); + { let mut co = Vec::new(); for cmd in TOGGLE_FLOAT_CMD { if cmd.starts_with(word) { co.push(Completion::simple(cmd.to_string())); } } - Some(co) } - }, + } Some("config") => match words.next() { None => { diff --git a/src/utils/plot_utils.rs b/src/utils/plot_utils.rs index 593d8913..6ee5bc84 100644 --- a/src/utils/plot_utils.rs +++ b/src/utils/plot_utils.rs @@ -1,9 +1,8 @@ pub fn computes_lines( - x: &Vec, - y: &Vec, + x: &[f64], + y: &[f64], start: f64, end: f64, - _steps: f64, title: String, xlabel: String, ylabel: String, @@ -94,15 +93,11 @@ pub fn computes_lines( for s in string_ymax.replace("-", "|").chars().rev() { y_sized.push(s); } - for _ in (lsize)..(30 / 2) { - y_sized.push(' '); - } + y_sized.extend(std::iter::repeat_n(' ', 15 - (lsize))); for s in ymiddle.replace("-", "|").chars().rev() { y_sized.push(s); } - for _ in ymiddle_size..(30 / 2 - lminsize) { - y_sized.push(' '); - } + y_sized.extend(std::iter::repeat_n(' ', 15 - lminsize - ymiddle_size)); for s in lmin_string.replace("-", "|").chars().rev() { y_sized.push(s); } @@ -114,8 +109,8 @@ pub fn computes_lines( print!("{}", iter_y_sized.next().unwrap()); print!("|"); let xs = &bitmap[x]; - for y in 0..xs.len() { - print!("{}", xs[y]); + for item in xs { + print!("{}", item); } println!("*"); } From b9dcde210dd598317fe6c5a46e1a5b5897c96a68 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 27 Jul 2025 11:45:37 +0200 Subject: [PATCH 4/6] clippy: correct clippy in matrix utils --- src/utils/matrix_utils.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils/matrix_utils.rs b/src/utils/matrix_utils.rs index 4450bc4b..cdc7248b 100644 --- a/src/utils/matrix_utils.rs +++ b/src/utils/matrix_utils.rs @@ -77,7 +77,7 @@ pub fn lup_decompose( max_a = Parameters::Float(0.0); i_max = i; - for k in i..n { + for (k, _) in a.iter().enumerate().take(n).skip(i) { abs_a = ((a[k])[i]).clone().abs(ram); if let Parameters::Bool(true) = greater(abs_a.clone(), max_a.clone(), ram) { max_a = (abs_a).clone(); @@ -123,12 +123,12 @@ pub fn lup_decompose( pub fn lup_determinant( a: &mut Matrix, - p: &mut Vec, + p: &mut [Parameters], n: usize, ram: ORam, ) -> Parameters { let mut det: Parameters = a[0][0].clone(); - for i in 1..n { + for (i, _) in a.iter().enumerate().take(n).skip(1) { det = mult(det.clone(), a[i][i].clone(), ram) } @@ -153,7 +153,7 @@ pub fn lup_determinant( pub fn lup_invert( a: &mut Matrix, - p: &mut Vec, + p: &mut [Parameters], n: usize, ia: &mut Matrix, ram: ORam, From 9896ffecacc9d5944ebc215c955adf7894c1cb22 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 27 Jul 2025 12:04:24 +0200 Subject: [PATCH 5/6] clippy: correct clippy for Box> --- src/functions/add.rs | 4 +-- src/functions/function.rs | 2 +- src/functions/minus.rs | 8 ++--- src/functions/mult.rs | 14 ++++----- src/interpreting/interpreter.rs | 2 +- src/interpreting/stdlib.rs | 42 ++++++++++++------------- src/parsing/ast.rs | 6 ++-- src/parsing/parselets/infix_parselet.rs | 7 ++--- src/parsing/parser.rs | 18 +++++------ 9 files changed, 48 insertions(+), 55 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index cabbb5d4..380d4838 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -25,10 +25,10 @@ pub fn add(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { let mut res = Vec::new(); vec.clone() .into_iter() - .zip(*vec2.clone()) + .zip(vec2.clone()) .map(|(x, y)| add(x.clone(), y.clone(), ram)) .for_each(|s| res.push(s)); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Null, diff --git a/src/functions/function.rs b/src/functions/function.rs index 98903a04..121aed40 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -591,7 +591,7 @@ pub fn select(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { pub fn concat(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { match (i, i2) { (InterpreterVector(v), InterpreterVector(v2)) => { - Parameters::InterpreterVector([*v, *v2].concat().into()) + Parameters::InterpreterVector([v, v2].concat().into()) } (Str(s), Str(s2)) => Parameters::Str((s + s2.as_str()).to_string()), (Identifier(s), InterpreterVector(v)) => match ram { diff --git a/src/functions/minus.rs b/src/functions/minus.rs index a34ace7b..42266008 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -31,7 +31,7 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { vec.into_iter() .map(|x| minus(Null, x.clone(), ram)) .for_each(|z| res.push(z)); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } (Null, InterpreterVector(vec)) => { @@ -39,16 +39,16 @@ pub fn minus(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { vec.into_iter() .map(|x| minus(Null, x.clone(), ram)) .for_each(|z| res.push(z)); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } (InterpreterVector(vec), InterpreterVector(vec2)) => { let mut res = Vec::new(); vec.into_iter() - .zip(*vec2) + .zip(vec2) .map(|(x, y)| minus(x.clone(), y.clone(), ram)) .for_each(|z| res.push(z)); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } (Int(v), Float(f)) => Float((v as f64) - f), (Float(v), Float(f)) => Float(v - f), diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 9c562f40..db530c75 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -34,28 +34,28 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { vec.into_iter() .map(|x| mult(x.clone(), Int(v), ram)) .for_each(|x| result.push(x)); - InterpreterVector(Box::from(result)) + InterpreterVector(result) } (Int(v), InterpreterVector(vec)) => { let mut result = Vec::new(); vec.into_iter() .map(|x| mult(x.clone(), Int(v), ram)) .for_each(|x| result.push(x)); - InterpreterVector(Box::from(result)) + InterpreterVector(result) } (InterpreterVector(vec), Float(v)) => { let mut result = Vec::new(); vec.into_iter() .map(|x| mult(x.clone(), Float(v), ram)) .for_each(|x| result.push(x)); - InterpreterVector(Box::from(result)) + InterpreterVector(result) } (Float(v), InterpreterVector(vec)) => { let mut result = Vec::new(); vec.into_iter() .map(|x| mult(x.clone(), Float(v), ram)) .for_each(|x| result.push(x)); - InterpreterVector(Box::from(result)) + InterpreterVector(result) } (InterpreterVector(vec), InterpreterVector(vec2)) => { @@ -82,7 +82,7 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { let mut sum = Null; (*vec) .into_iter() - .zip(*vec2) + .zip(vec2) .map(|(a, b)| mult(a.clone(), b.clone(), ram)) .for_each(|x| sum = add(sum.clone(), x, ram)); @@ -102,9 +102,9 @@ pub fn mult(i: Parameters, i2: Parameters, ram: ORam) -> Parameters { matrix_result .into_iter() - .for_each(|x| res.push(InterpreterVector(Box::from(x)))); + .for_each(|x| res.push(InterpreterVector(x))); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } } diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 3b02473a..c4cb5623 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -100,7 +100,7 @@ pub fn interpret(ast: &Ast, mut ram: &mut Ram, mut function: &mut Functions) -> .into_iter() .map(|a| interpret(&a, ram, function)) .for_each(|s| vec.push(s)); - Parameters::InterpreterVector(Box::from(vec)) + Parameters::InterpreterVector(vec) } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), *y, z.clone()), diff --git a/src/interpreting/stdlib.rs b/src/interpreting/stdlib.rs index 3db8168d..5eeef4ea 100644 --- a/src/interpreting/stdlib.rs +++ b/src/interpreting/stdlib.rs @@ -267,7 +267,7 @@ pub fn cos(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("cos".to_string(), Box::from(Identifier(s.clone()))), @@ -354,7 +354,7 @@ pub fn sin(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("sin".to_string(), Box::from(Identifier(s.clone()))), @@ -442,7 +442,7 @@ pub fn tan(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("tan".to_string(), Box::from(Identifier(s.clone()))), @@ -530,7 +530,7 @@ pub fn cosh(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("cosh".to_string(), Box::from(Identifier(s.clone()))), @@ -618,7 +618,7 @@ pub fn sinh(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("sinh".to_string(), Box::from(Identifier(s.clone()))), @@ -706,7 +706,7 @@ pub fn tanh(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("tanh".to_string(), Box::from(Identifier(s.clone()))), @@ -792,7 +792,7 @@ pub fn acos(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("acos".to_string(), Box::from(Identifier(s.clone()))), @@ -879,7 +879,7 @@ pub fn asin(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("asin".to_string(), Box::from(Identifier(s.clone()))), @@ -966,7 +966,7 @@ pub fn atan(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("atan".to_string(), Box::from(Identifier(s.clone()))), @@ -1060,7 +1060,7 @@ pub fn exp(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("exp".to_string(), Box::from(Identifier(s.clone()))), @@ -1149,7 +1149,7 @@ pub fn ln(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("ln".to_string(), Box::from(Identifier(s.clone()))), @@ -1241,7 +1241,7 @@ pub fn sqrt(p: &Vec, ram: &Ram) -> Parameters { }, _ => (), }); - InterpreterVector(Box::from(res)) + InterpreterVector(res) } Identifier(s) => match ram { None => Call("sqrt".to_string(), Box::from(Identifier(s.clone()))), @@ -1441,17 +1441,17 @@ pub fn transpose_vectors(p: &Vec, ram: &Ram) -> Parameters { Float(f) => Parameters::Float((*f).abs()), Rational(s) => Parameters::Rational((*s).abs()), InterpreterVector(lst) => { - let r = vec![*(lst.clone())]; + let r = vec![(lst.clone())]; let transposed = transpose(r); let mut result = Vec::new(); transposed .into_iter() - .map(|v| InterpreterVector(Box::from(v))) + .map(|v| InterpreterVector(v)) .for_each(|v| result.push(v)); - InterpreterVector(Box::from(result)) + InterpreterVector(result) } Identifier(s) => match ram { None => Identifier("This variable is not initialized yet".to_string()), @@ -1494,8 +1494,8 @@ pub fn transpose_matrices(p: &Vec, ram: &Ram) -> Parameters { matrix_result .into_iter() - .for_each(|x| result.push(InterpreterVector(Box::from(x)))); - InterpreterVector(Box::from(result)) + .for_each(|x| result.push(InterpreterVector(x))); + InterpreterVector(result) } Identifier(s) => match ram { @@ -1583,7 +1583,7 @@ pub fn inverse_matrix(p: &Vec, ram: &Ram) -> Parameters { }); if !is_matrix { - return InterpreterVector(Box::from(res1)); + return InterpreterVector(res1); } let mut p = Vec::new(); @@ -1626,9 +1626,9 @@ pub fn inverse_matrix(p: &Vec, ram: &Ram) -> Parameters { lup_invert(&mut res, &mut p, n, &mut vec_ia, ram.as_deref()); let mut resd = Vec::new(); for i in 0..n { - resd.push(InterpreterVector(Box::new(vec_ia[i].clone()))); + resd.push(InterpreterVector(vec_ia[i].clone())); } - InterpreterVector(Box::new(resd)) + InterpreterVector(resd) } } } @@ -2246,7 +2246,7 @@ pub fn plot_fn( if !terminal { f.show().unwrap(); } else { - computes_lines(&x, &y, st, end, title, xlabel, ylabel); + computes_lines(&x, &y, st, end, title, xlabel, ylabel) } Null } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index fffbe1a4..2ca1a2f7 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -41,7 +41,7 @@ pub enum Parameters { Null, ExpoOperation, Vector(Box>), - InterpreterVector(Box>), + InterpreterVector(Vec), Var(Box, i64, String), Plus(Box, Box), Mul(Box, Box), @@ -277,9 +277,7 @@ impl Parameters { first_attach, separator, z, - if l == "¹" { - "" - } else if l == "⁻¹" { + if l == "¹" || l == "⁻¹" { "" } else { e.as_str() diff --git a/src/parsing/parselets/infix_parselet.rs b/src/parsing/parselets/infix_parselet.rs index 6c4ef40f..c834543c 100644 --- a/src/parsing/parselets/infix_parselet.rs +++ b/src/parsing/parselets/infix_parselet.rs @@ -73,13 +73,10 @@ impl InfixParselet for CallParselet { let name = match left { Ast::Nil => "", Ast::Node { - value: v, + value: Parameters::Identifier(s), left: _left, right: _right, - } => match v { - Parameters::Identifier(s) => s.as_str(), - _ => "", - }, + } => s.as_str(), _ => "", }; diff --git a/src/parsing/parser.rs b/src/parsing/parser.rs index 3a9efcb3..e0b7c38a 100644 --- a/src/parsing/parser.rs +++ b/src/parsing/parser.rs @@ -21,7 +21,7 @@ pub struct CalcParser<'a> { read: Vec, } -pub fn init_calc_parser(input: &Vec) -> CalcParser { +pub fn init_calc_parser(input: &[Token]) -> CalcParser { CalcParser { tokens: input.iter(), read: Vec::new(), @@ -87,15 +87,13 @@ impl CalcParser<'_> { if self.read.is_empty() { return Null; } - match self.read.remove(0) { - t => { - if t.to_token_type() == expected { - t - } else { - println!("error!"); - Null - } - } + let t = self.read.remove(0); + + if t.to_token_type() == expected { + t + } else { + println!("error!"); + Null } } From 77fe50b53d2d7ad866574e88ef93b0cf5b2ffb79 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 27 Jul 2025 12:14:39 +0200 Subject: [PATCH 6/6] clippy: correct all MAJ on enums --- src/lexing/lexer.rs | 134 ++++---- src/lexing/token.rs | 378 +++++++++++------------ src/parsing/ast.rs | 42 +-- src/parsing/parselets/infix_parselet.rs | 12 +- src/parsing/parselets/prefix_parselet.rs | 30 +- src/parsing/parser.rs | 108 +++---- 6 files changed, 352 insertions(+), 352 deletions(-) diff --git a/src/lexing/lexer.rs b/src/lexing/lexer.rs index 638980af..39b934b8 100644 --- a/src/lexing/lexer.rs +++ b/src/lexing/lexer.rs @@ -131,91 +131,91 @@ pub fn lex(input: String) -> Vec { match current_character { '+' => match vec.pop() { - Some(Token::OPE(PLUS)) => { - vec.push(Token::OPE(ConcatOperation)); + Some(Token::Ope(Plus)) => { + vec.push(Token::Ope(ConcatOperation)); current_pos += 1; } Some(p) => { vec.push(p); - vec.push(Token::OPE(PLUS)); + vec.push(Token::Ope(Plus)); current_pos += 1; } None => { - vec.push(Token::OPE(PLUS)); + vec.push(Token::Ope(Plus)); current_pos += 1; } }, '-' => { - vec.push(Token::OPE(MINUS)); + vec.push(Token::Ope(Minus)); current_pos += 1 } '*' => { - vec.push(Token::OPE(MULTIPLICATION)); + vec.push(Token::Ope(Multiplication)); current_pos += 1 } '/' => { - vec.push(Token::OPE(DIVIDE)); + vec.push(Token::Ope(Divide)); current_pos += 1 } ')' => { - vec.push(Token::RPAR); + vec.push(Token::Rpar); current_pos += 1 } '(' => { - vec.push(Token::LPAR); + vec.push(Token::Lpar); current_pos += 1 } '{' => { - vec.push(Token::LSB); + vec.push(Token::Lsb); current_pos += 1 } '}' => { - vec.push(Token::RSB); + vec.push(Token::Rsb); current_pos += 1 } '>' => { - vec.push(Token::OPE(GreaterThan)); + vec.push(Token::Ope(GreaterThan)); current_pos += 1 } '<' => { - vec.push(Token::OPE(LesserThan)); + vec.push(Token::Ope(LesserThan)); current_pos += 1 } '"' => { - vec.push(Token::QUOTE); + vec.push(Token::Quote); quote_i += 1; current_pos += 1 } ';' => { - vec.push(Token::IGNORE); + vec.push(Token::Ignore); current_pos += 1 } '=' => match vec.pop() { - Some(Token::EQUAL) => { - vec.push(Token::OPE(EQUALITY)); + Some(Token::Equal) => { + vec.push(Token::Ope(Equality)); current_pos += 1 } - Some(Token::OPE(LesserThan)) => { - vec.push(Token::OPE(LesserOrEqual)); + Some(Token::Ope(LesserThan)) => { + vec.push(Token::Ope(LesserOrEqual)); current_pos += 1; } - Some(Token::OPE(GreaterThan)) => { - vec.push(Token::OPE(GreaterOrEqual)); + Some(Token::Ope(GreaterThan)) => { + vec.push(Token::Ope(GreaterOrEqual)); current_pos += 1; } Some(p) => { vec.push(p); - vec.push(Token::EQUAL); + vec.push(Token::Equal); current_pos += 1 } None => { - vec.push(Token::EQUAL); + vec.push(Token::Equal); current_pos += 1 } }, '&' => match vec.pop() { Some(Token::PreAnd) => { - vec.push(Token::OPE(And)); + vec.push(Token::Ope(And)); current_pos += 1; } Some(p) => { @@ -230,7 +230,7 @@ pub fn lex(input: String) -> Vec { }, '|' => match vec.pop() { Some(Token::PreOr) => { - vec.push(Token::OPE(Or)); + vec.push(Token::Ope(Or)); current_pos += 1; } Some(p) => { @@ -244,33 +244,33 @@ pub fn lex(input: String) -> Vec { } }, '^' => { - vec.push(Token::OPE(EXPO)); + vec.push(Token::Ope(Expo)); current_pos += 1 } ',' => { - vec.push(Token::COMMA); + vec.push(Token::Comma); current_pos += 1 } '!' => { - vec.push(Token::OPE(NOT)); + vec.push(Token::Ope(NOT)); current_pos += 1 } ']' => { - vec.push(Token::RBRACKET); + vec.push(Token::Rbracket); current_pos += 1 } '[' => { - vec.push(Token::LBRACKET); + vec.push(Token::Lbracket); current_pos += 1 } ' ' => { if quote_i % 2 == 1 { - vec.push(Token::WHITESPACE); + vec.push(Token::Whitespace); } current_pos += 1 } '.' => { - vec.push(Token::OPE(Selection)); + vec.push(Token::Ope(Selection)); current_pos += 1 } ch => { @@ -283,14 +283,14 @@ pub fn lex(input: String) -> Vec { if *char == '.' { let (a1, b1) = lex_float(a, &mut chars, current_pos, length); current_pos = b1; - vec.push(Token::FLOAT(a1)) + vec.push(Token::Float(a1)) } else { - vec.push(Token::INT(a)); + vec.push(Token::Int(a)); current_pos = b; } } None => { - vec.push(Token::INT(a)); + vec.push(Token::Int(a)); current_pos = b; } } @@ -299,27 +299,27 @@ pub fn lex(input: String) -> Vec { let (a, b) = lex_string(current_character, &mut chars, current_pos, length); current_pos = b; match a.as_str() { - "false" => vec.push(Token::BOOL(false)), - "true" => vec.push(Token::BOOL(true)), - "or" => vec.push(Token::OPE(Or)), - "and" => vec.push(Token::OPE(And)), - "geq" => vec.push(Token::OPE(GreaterOrEqual)), - "leq" => vec.push(Token::OPE(LesserOrEqual)), - "lt" => vec.push(Token::OPE(LesserThan)), - "gt" => vec.push(Token::OPE(GreaterThan)), - "eq" => vec.push(Token::OPE(EQUALITY)), - "if" => vec.push(Token::IF), - "then" => vec.push(Token::THEN), - "else" => vec.push(Token::ELSE), - "while" => vec.push(Token::WHILE), - "do" => vec.push(Token::DO), - _ => vec.push(Token::IDENTIFIER(a)), + "false" => vec.push(Token::Bool(false)), + "true" => vec.push(Token::Bool(true)), + "or" => vec.push(Token::Ope(Or)), + "and" => vec.push(Token::Ope(And)), + "geq" => vec.push(Token::Ope(GreaterOrEqual)), + "leq" => vec.push(Token::Ope(LesserOrEqual)), + "lt" => vec.push(Token::Ope(LesserThan)), + "gt" => vec.push(Token::Ope(GreaterThan)), + "eq" => vec.push(Token::Ope(Equality)), + "if" => vec.push(Token::If), + "then" => vec.push(Token::Then), + "else" => vec.push(Token::Else), + "while" => vec.push(Token::While), + "do" => vec.push(Token::Do), + _ => vec.push(Token::Identifier(a)), } } if ch == '.' { let (a, b) = lex_float(0, &mut chars, current_pos, length); current_pos = b; - vec.push(Token::FLOAT(a)) + vec.push(Token::Float(a)) } } } @@ -339,112 +339,112 @@ mod tests { #[test] fn lex_plus() { - let expected = vec![OPE(PLUS)]; + let expected = vec![Ope(Plus)]; let result = lex("+".to_string()); assert_eq!(result, expected) } #[test] fn lex_minus() { - let expected = vec![OPE(MINUS)]; + let expected = vec![Ope(Minus)]; let result = lex("-".to_string()); assert_eq!(result, expected) } #[test] fn lex_mult() { - let expected = vec![OPE(MULTIPLICATION)]; + let expected = vec![Ope(Multiplication)]; let result = lex("*".to_string()); assert_eq!(result, expected) } #[test] fn lex_divide() { - let expected = vec![OPE(DIVIDE)]; + let expected = vec![Ope(Divide)]; let result = lex("/".to_string()); assert_eq!(result, expected) } #[test] fn lex_operators() { - let expected = vec![OPE(PLUS), OPE(MULTIPLICATION), OPE(MINUS), OPE(DIVIDE)]; + let expected = vec![Ope(Plus), Ope(Multiplication), Ope(Minus), Ope(Divide)]; let result = lex("+*-/".to_string()); assert_eq!(result, expected) } #[test] fn lex_lpar() { - let expected = vec![LPAR]; + let expected = vec![Lpar]; let result = lex("(".to_string()); assert_eq!(result, expected) } #[test] fn lex_rpar() { - let expected = vec![RPAR]; + let expected = vec![Rpar]; let result = lex(")".to_string()); assert_eq!(result, expected) } #[test] fn lex_equal() { - let expected = vec![EQUAL]; + let expected = vec![Equal]; let result = lex("=".to_string()); assert_eq!(result, expected); } #[test] fn lex_tokens() { - let expected = vec![LPAR, RPAR, EQUAL]; + let expected = vec![Lpar, Rpar, Equal]; let result = lex("()=".to_string()); assert_eq!(result, expected) } #[test] fn lex_simple_int() { - let expected = vec![INT(1)]; + let expected = vec![Int(1)]; let result = lex("1".to_string()); assert_eq!(result, expected); } #[test] fn lex_complex_int() { - let expected = vec![INT(100)]; + let expected = vec![Int(100)]; let result = lex("100".to_string()); assert_eq!(result, expected); } #[test] fn lex_simple_string() { - let expected = vec![IDENTIFIER("test".to_string())]; + let expected = vec![Identifier("test".to_string())]; let result = lex("test".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_operation() { - let expected = vec![INT(1), OPE(PLUS), INT(1)]; + let expected = vec![Int(1), Ope(Plus), Int(1)]; let result = lex("1 + 1".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_equality() { - let expected = vec![IDENTIFIER("var1".to_string()), EQUAL, INT(100)]; + let expected = vec![Identifier("var1".to_string()), Equal, Int(100)]; let result = lex("var1 = 100".to_string()); assert_eq!(result, expected) } #[test] fn test_simple_float() { - let expected = vec![FLOAT(0.14)]; + let expected = vec![Float(0.14)]; let result = lex("0.14".to_string()); assert_eq!(result, expected); } #[test] fn test_complex_float() { - let expected = vec![FLOAT(314.05)]; + let expected = vec![Float(314.05)]; let result = lex("314.05".to_string()); assert_eq!(result, expected) } diff --git a/src/lexing/token.rs b/src/lexing/token.rs index eca11c21..8e016717 100644 --- a/src/lexing/token.rs +++ b/src/lexing/token.rs @@ -2,12 +2,12 @@ use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, PartialEq)] pub enum Operator { - PLUS, - MINUS, - MULTIPLICATION, - DIVIDE, - EXPO, - EQUALITY, + Plus, + Minus, + Multiplication, + Divide, + Expo, + Equality, GreaterThan, LesserThan, GreaterOrEqual, @@ -21,100 +21,100 @@ pub enum Operator { #[derive(Debug, Clone, PartialEq)] pub enum Token { - OPE(Operator), - IDENTIFIER(String), - INT(i64), - FLOAT(f64), - BOOL(bool), - EQUAL, - RPAR, - LPAR, - RBRACKET, - LBRACKET, - COMMA, + Ope(Operator), + Identifier(String), + Int(i64), + Float(f64), + Bool(bool), + Equal, + Rpar, + Lpar, + Rbracket, + Lbracket, + Comma, Null, - QUOTE, - WHITESPACE, + Quote, + Whitespace, PreAnd, PreOr, - IF, - THEN, - ELSE, - WHILE, - DO, - IGNORE, - RSB, - LSB, + If, + Then, + Else, + While, + Do, + Ignore, + Rsb, + Lsb, } #[derive(Debug, Clone, PartialEq, Hash, Eq)] pub enum TokenType { - PLUS, - MINUS, - MULTIPLICATION, - DIVIDE, - IDENTIFIER, - INT, - FLOAT, - EQUAL, - EQUALITY, - GREATER, - LESSER, - GREATEREQ, - OR, - AND, - LESSEREQ, - NOT, - BOOL, - RPAR, - LPAR, - RBRACKET, - LBRACKET, + Plus, + Minus, + Multiplication, + Divide, + Identifier, + Int, + Float, + Equal, + Equality, + Greater, + Lesser, + GreaterEq, + Or, + And, + LesserEq, + Not, + Bool, + Rpar, + Lpar, + Rbracket, + Lbracket, Null, - COMMA, - WHITESPACE, - EXPO, - QUOTE, - IF, - THEN, - ELSE, - WHILE, - DO, - IGNORE, - RSB, - LSB, - SELECTION, - CONCAT, + Comma, + Whitespace, + Expo, + Quote, + If, + Then, + Else, + While, + Do, + Ignore, + Rsb, + Lsb, + Selection, + Concat, } pub enum Precedence { - IGNORE = 5, - IFTHENELSE = 6, - WHILE = 7, - PREFIX = 9, - ASSIGNMENT = 10, + Ignore = 5, + IfThenElse = 6, + While = 7, + Prefix = 9, + Assignment = 10, // TODO: maybe rename this - CONDITIONAL = 20, - SELECTION = 25, - CONCAT = 26, - MINUS = 30, - SUM = 40, - DIVIDE = 45, - PRODUCT = 50, - EXPONENT = 60, - CALL = 100, + Conditional = 20, + Selection = 25, + Concat = 26, + Minus = 30, + Sum = 40, + Divide = 45, + Product = 50, + Exponent = 60, + Call = 100, } impl Display for Operator { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - Operator::PLUS => write!(f, "+"), - Operator::MINUS => write!(f, "-"), - Operator::DIVIDE => write!(f, "/"), - Operator::MULTIPLICATION => write!(f, "*"), - Operator::EXPO => write!(f, "^"), - Operator::EQUALITY => write!(f, "=="), + Operator::Plus => write!(f, "+"), + Operator::Minus => write!(f, "-"), + Operator::Divide => write!(f, "/"), + Operator::Multiplication => write!(f, "*"), + Operator::Expo => write!(f, "^"), + Operator::Equality => write!(f, "=="), Operator::GreaterOrEqual => write!(f, ">="), Operator::GreaterThan => write!(f, ">"), Operator::LesserOrEqual => write!(f, "<="), @@ -131,30 +131,30 @@ impl Display for Operator { impl Display for Token { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { - Token::LPAR => write!(f, "("), - Token::RPAR => write!(f, ")"), - Token::EQUAL => write!(f, "="), - Token::FLOAT(i) => write!(f, "{}", i), - Token::INT(i) => write!(f, "{}", i), - Token::IDENTIFIER(s) => write!(f, "{}", s), - Token::OPE(s) => write!(f, "{}", s), - Token::COMMA => write!(f, ","), + Token::Lpar => write!(f, "("), + Token::Rpar => write!(f, ")"), + Token::Equal => write!(f, "="), + Token::Float(i) => write!(f, "{}", i), + Token::Int(i) => write!(f, "{}", i), + Token::Identifier(s) => write!(f, "{}", s), + Token::Ope(s) => write!(f, "{}", s), + Token::Comma => write!(f, ","), Token::Null => write!(f, "Null"), - Token::BOOL(b) => write!(f, "{b}"), + Token::Bool(b) => write!(f, "{b}"), Token::PreAnd => write!(f, ""), Token::PreOr => write!(f, ""), - Token::RBRACKET => write!(f, "]"), - Token::LBRACKET => write!(f, "["), - Token::QUOTE => write!(f, "\""), - Token::WHITESPACE => write!(f, " "), - Token::IF => write!(f, "if"), - Token::THEN => write!(f, "then"), - Token::ELSE => write!(f, "else"), - Token::WHILE => write!(f, "while"), - Token::DO => write!(f, "do"), - Token::IGNORE => write!(f, "!"), - Token::RSB => write!(f, "{{"), - Token::LSB => write!(f, "}}"), + Token::Rbracket => write!(f, "]"), + Token::Lbracket => write!(f, "["), + Token::Quote => write!(f, "\""), + Token::Whitespace => write!(f, " "), + Token::If => write!(f, "if"), + Token::Then => write!(f, "then"), + Token::Else => write!(f, "else"), + Token::While => write!(f, "while"), + Token::Do => write!(f, "do"), + Token::Ignore => write!(f, "!"), + Token::Rsb => write!(f, "{{"), + Token::Lsb => write!(f, "}}"), } } } @@ -162,44 +162,44 @@ impl Display for Token { impl Token { pub fn to_token_type(&self) -> TokenType { match &self { - Token::OPE(p) => match p { - Operator::PLUS => TokenType::PLUS, - Operator::MINUS => TokenType::MINUS, - Operator::MULTIPLICATION => TokenType::MULTIPLICATION, - Operator::DIVIDE => TokenType::DIVIDE, - Operator::EXPO => TokenType::EXPO, - Operator::EQUALITY => TokenType::EQUALITY, - Operator::GreaterThan => TokenType::GREATER, - Operator::GreaterOrEqual => TokenType::GREATEREQ, - Operator::LesserThan => TokenType::LESSER, - Operator::LesserOrEqual => TokenType::LESSEREQ, - Operator::NOT => TokenType::NOT, - Operator::And => TokenType::AND, - Operator::Or => TokenType::OR, - Operator::Selection => TokenType::SELECTION, - Operator::ConcatOperation => TokenType::CONCAT, + Token::Ope(p) => match p { + Operator::Plus => TokenType::Plus, + Operator::Minus => TokenType::Minus, + Operator::Multiplication => TokenType::Multiplication, + Operator::Divide => TokenType::Divide, + Operator::Expo => TokenType::Expo, + Operator::Equality => TokenType::Equality, + Operator::GreaterThan => TokenType::Greater, + Operator::GreaterOrEqual => TokenType::GreaterEq, + Operator::LesserThan => TokenType::Lesser, + Operator::LesserOrEqual => TokenType::LesserEq, + Operator::NOT => TokenType::Not, + Operator::And => TokenType::And, + Operator::Or => TokenType::Or, + Operator::Selection => TokenType::Selection, + Operator::ConcatOperation => TokenType::Concat, }, - Token::IDENTIFIER(_) => TokenType::IDENTIFIER, - Token::INT(_) => TokenType::INT, - Token::FLOAT(_) => TokenType::FLOAT, - Token::EQUAL => TokenType::EQUAL, - Token::RPAR => TokenType::RPAR, - Token::LPAR => TokenType::LPAR, - Token::COMMA => TokenType::COMMA, + Token::Identifier(_) => TokenType::Identifier, + Token::Int(_) => TokenType::Int, + Token::Float(_) => TokenType::Float, + Token::Equal => TokenType::Equal, + Token::Rpar => TokenType::Rpar, + Token::Lpar => TokenType::Lpar, + Token::Comma => TokenType::Comma, Token::Null => TokenType::Null, - Token::BOOL(_) => TokenType::BOOL, - Token::LBRACKET => TokenType::LBRACKET, - Token::RBRACKET => TokenType::RBRACKET, - Token::QUOTE => TokenType::QUOTE, - Token::WHITESPACE => TokenType::WHITESPACE, - Token::IF => TokenType::IF, - Token::ELSE => TokenType::ELSE, - Token::THEN => TokenType::THEN, - Token::WHILE => TokenType::WHILE, - Token::DO => TokenType::DO, - Token::IGNORE => TokenType::IGNORE, - Token::LSB => TokenType::LSB, - Token::RSB => TokenType::RSB, + Token::Bool(_) => TokenType::Bool, + Token::Lbracket => TokenType::Lbracket, + Token::Rbracket => TokenType::Rbracket, + Token::Quote => TokenType::Quote, + Token::Whitespace => TokenType::Whitespace, + Token::If => TokenType::If, + Token::Else => TokenType::Else, + Token::Then => TokenType::Then, + Token::While => TokenType::While, + Token::Do => TokenType::Do, + Token::Ignore => TokenType::Ignore, + Token::Lsb => TokenType::Lsb, + Token::Rsb => TokenType::Rsb, _ => TokenType::Null, } } @@ -210,141 +210,141 @@ mod test { #[test] fn test_token_type_operators_plus() { - let expected = TokenType::PLUS; - let value = Token::OPE(super::Operator::PLUS).to_token_type(); + let expected = TokenType::Plus; + let value = Token::Ope(super::Operator::Plus).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_minus() { - let expected = TokenType::MINUS; - let value = Token::OPE(super::Operator::MINUS).to_token_type(); + let expected = TokenType::Minus; + let value = Token::Ope(super::Operator::Minus).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_mult() { - let expected = TokenType::MULTIPLICATION; - let value = Token::OPE(super::Operator::MULTIPLICATION).to_token_type(); + let expected = TokenType::Multiplication; + let value = Token::Ope(super::Operator::Multiplication).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_divide() { - let expected = TokenType::DIVIDE; - let value = Token::OPE(super::Operator::DIVIDE).to_token_type(); + let expected = TokenType::Divide; + let value = Token::Ope(super::Operator::Divide).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_expo() { - let expected = TokenType::EXPO; - let value = Token::OPE(super::Operator::EXPO).to_token_type(); + let expected = TokenType::Expo; + let value = Token::Ope(super::Operator::Expo).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_equality() { - let expected = TokenType::EQUALITY; - let value = Token::OPE(super::Operator::EQUALITY).to_token_type(); + let expected = TokenType::Equality; + let value = Token::Ope(super::Operator::Equality).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_greater() { - let expected = TokenType::GREATER; - let value = Token::OPE(super::Operator::GreaterThan).to_token_type(); + let expected = TokenType::Greater; + let value = Token::Ope(super::Operator::GreaterThan).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_lesser() { - let expected = TokenType::LESSER; - let value = Token::OPE(super::Operator::LesserThan).to_token_type(); + let expected = TokenType::Lesser; + let value = Token::Ope(super::Operator::LesserThan).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_greaterq() { - let expected = TokenType::GREATEREQ; - let value = Token::OPE(super::Operator::GreaterOrEqual).to_token_type(); + let expected = TokenType::GreaterEq; + let value = Token::Ope(super::Operator::GreaterOrEqual).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_lesserq() { - let expected = TokenType::LESSEREQ; - let value = Token::OPE(super::Operator::LesserOrEqual).to_token_type(); + let expected = TokenType::LesserEq; + let value = Token::Ope(super::Operator::LesserOrEqual).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_and() { - let expected = TokenType::AND; - let value = Token::OPE(super::Operator::And).to_token_type(); + let expected = TokenType::And; + let value = Token::Ope(super::Operator::And).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_or() { - let expected = TokenType::OR; - let value = Token::OPE(super::Operator::Or).to_token_type(); + let expected = TokenType::Or; + let value = Token::Ope(super::Operator::Or).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_operators_not() { - let expected = TokenType::NOT; - let value = Token::OPE(super::Operator::NOT).to_token_type(); + let expected = TokenType::Not; + let value = Token::Ope(super::Operator::NOT).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_identifier() { - let expected = TokenType::IDENTIFIER; - let value = Token::IDENTIFIER("s".to_string()).to_token_type(); + let expected = TokenType::Identifier; + let value = Token::Identifier("s".to_string()).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_int() { - let expected = TokenType::INT; - let value = Token::INT(0).to_token_type(); + let expected = TokenType::Int; + let value = Token::Int(0).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_float() { - let expected = TokenType::FLOAT; - let value = Token::FLOAT(0.0).to_token_type(); + let expected = TokenType::Float; + let value = Token::Float(0.0).to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_equal() { - let expected = TokenType::EQUAL; - let value = Token::EQUAL.to_token_type(); + let expected = TokenType::Equal; + let value = Token::Equal.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_lpar() { - let expected = TokenType::LPAR; - let value = Token::LPAR.to_token_type(); + let expected = TokenType::Lpar; + let value = Token::Lpar.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_rpar() { - let expected = TokenType::RPAR; - let value = Token::RPAR.to_token_type(); + let expected = TokenType::Rpar; + let value = Token::Rpar.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_bool() { - let expected = TokenType::BOOL; - let value = Token::BOOL(false).to_token_type(); + let expected = TokenType::Bool; + let value = Token::Bool(false).to_token_type(); assert_eq!(value, expected); } @@ -357,36 +357,36 @@ mod test { #[test] fn test_token_type_comma() { - let expected = TokenType::COMMA; - let value = Token::COMMA.to_token_type(); + let expected = TokenType::Comma; + let value = Token::Comma.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_rbracket() { - let expected = TokenType::RBRACKET; - let value = Token::RBRACKET.to_token_type(); + let expected = TokenType::Rbracket; + let value = Token::Rbracket.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_lbracket() { - let expected = TokenType::LBRACKET; - let value = Token::LBRACKET.to_token_type(); + let expected = TokenType::Lbracket; + let value = Token::Lbracket.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_whitespace() { - let expected = TokenType::WHITESPACE; - let value = Token::WHITESPACE.to_token_type(); + let expected = TokenType::Whitespace; + let value = Token::Whitespace.to_token_type(); assert_eq!(value, expected); } #[test] fn test_token_type_quote() { - let expected = TokenType::QUOTE; - let value = Token::QUOTE.to_token_type(); + let expected = TokenType::Quote; + let value = Token::Quote.to_token_type(); assert_eq!(value, expected); } } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 2ca1a2f7..554db7e1 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -525,27 +525,27 @@ impl Parameters { pub fn token_to_parameter(token: &Token) -> Parameters { match token { - Token::INT(i) => Int(*i), - Token::FLOAT(f) => Float(*f), - Token::IDENTIFIER(s) => Identifier(s.clone()), - Token::OPE(Operator::PLUS) => PlusOperation, - Token::OPE(Operator::MINUS) => MinusOperation, - Token::OPE(Operator::MULTIPLICATION) => MultiplicationOperation, - Token::OPE(Operator::DIVIDE) => DivideOperation, - Token::OPE(Operator::EXPO) => ExpoOperation, - Token::OPE(Operator::EQUALITY) => Equal, - Token::OPE(Operator::GreaterOrEqual) => GreaterOrEqualOperation, - Token::OPE(Operator::GreaterThan) => GreaterOperation, - Token::OPE(Operator::LesserThan) => LesserOperation, - Token::OPE(Operator::LesserOrEqual) => LesserOrEqualOperation, - Token::OPE(Operator::NOT) => Not, - Token::OPE(Operator::Or) => OrOperation, - Token::OPE(Operator::And) => AndOperation, - Token::OPE(Operator::Selection) => SelectionOperation, - Token::OPE(Operator::ConcatOperation) => ConcatOperation, - Token::EQUAL => Assign, - Token::BOOL(b) => Bool(*b), - Token::RBRACKET => Vector(Box::from(Vec::new())), + Token::Int(i) => Int(*i), + Token::Float(f) => Float(*f), + Token::Identifier(s) => Identifier(s.clone()), + Token::Ope(Operator::Plus) => PlusOperation, + Token::Ope(Operator::Minus) => MinusOperation, + Token::Ope(Operator::Multiplication) => MultiplicationOperation, + Token::Ope(Operator::Divide) => DivideOperation, + Token::Ope(Operator::Expo) => ExpoOperation, + Token::Ope(Operator::Equality) => Equal, + Token::Ope(Operator::GreaterOrEqual) => GreaterOrEqualOperation, + Token::Ope(Operator::GreaterThan) => GreaterOperation, + Token::Ope(Operator::LesserThan) => LesserOperation, + Token::Ope(Operator::LesserOrEqual) => LesserOrEqualOperation, + Token::Ope(Operator::NOT) => Not, + Token::Ope(Operator::Or) => OrOperation, + Token::Ope(Operator::And) => AndOperation, + Token::Ope(Operator::Selection) => SelectionOperation, + Token::Ope(Operator::ConcatOperation) => ConcatOperation, + Token::Equal => Assign, + Token::Bool(b) => Bool(*b), + Token::Rbracket => Vector(Box::from(Vec::new())), _ => Null, } } diff --git a/src/parsing/parselets/infix_parselet.rs b/src/parsing/parselets/infix_parselet.rs index c834543c..c8a3321c 100644 --- a/src/parsing/parselets/infix_parselet.rs +++ b/src/parsing/parselets/infix_parselet.rs @@ -29,7 +29,7 @@ impl InfixParselet for IgnoreParselet { } } fn get_precedence(&self) -> i64 { - Precedence::IGNORE as i64 + Precedence::Ignore as i64 } } @@ -64,7 +64,7 @@ impl InfixParselet for AssignParselet { } fn get_precedence(&self) -> i64 { - Precedence::ASSIGNMENT as i64 + Precedence::Assignment as i64 } } @@ -81,14 +81,14 @@ impl InfixParselet for CallParselet { }; let mut lst: Vec = Vec::new(); - if !parser.match_token(TokenType::RPAR) { + if !parser.match_token(TokenType::Rpar) { lst.push(parser.parse_expression_empty()); - while parser.match_token(TokenType::COMMA) { + while parser.match_token(TokenType::Comma) { parser.consume(); let ast = parser.parse_expression_empty(); lst.push(ast); } - parser.consume_expected(TokenType::RPAR); + parser.consume_expected(TokenType::Rpar); } Call { name: name.to_string(), @@ -97,6 +97,6 @@ impl InfixParselet for CallParselet { } fn get_precedence(&self) -> i64 { - Precedence::CALL as i64 + Precedence::Call as i64 } } diff --git a/src/parsing/parselets/prefix_parselet.rs b/src/parsing/parselets/prefix_parselet.rs index c38c0e4f..d23083af 100644 --- a/src/parsing/parselets/prefix_parselet.rs +++ b/src/parsing/parselets/prefix_parselet.rs @@ -44,7 +44,7 @@ impl PrefixParselet for ValueParselet { impl PrefixParselet for OperatorPrefixParselet { fn parse(&self, parser: &mut CalcParser, token: &Token) -> Ast { - let operand = parser.parse_expression(Precedence::PREFIX as i64); + let operand = parser.parse_expression(Precedence::Prefix as i64); Ast::Node { value: token_to_parameter(token), left: Box::from(operand), @@ -56,7 +56,7 @@ impl PrefixParselet for OperatorPrefixParselet { impl PrefixParselet for GroupParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { let expression = parser.parse_expression_empty(); - parser.consume_expected(TokenType::RPAR); + parser.consume_expected(TokenType::Rpar); expression } } @@ -64,7 +64,7 @@ impl PrefixParselet for GroupParselet { impl PrefixParselet for ScopeParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { let expression = parser.parse_expression_empty(); - parser.consume_expected(TokenType::RSB); + parser.consume_expected(TokenType::Rsb); expression } } @@ -73,13 +73,13 @@ impl PrefixParselet for VecParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { let mut vec: Vec = Vec::new(); - if !parser.match_token(TokenType::RBRACKET) { + if !parser.match_token(TokenType::Rbracket) { vec.push(parser.parse_expression_empty()); - while parser.match_token(TokenType::COMMA) { + while parser.match_token(TokenType::Comma) { parser.consume(); vec.push(parser.parse_expression_empty()); } - parser.consume_expected(TokenType::RBRACKET); + parser.consume_expected(TokenType::Rbracket); } Ast::Node { @@ -94,15 +94,15 @@ impl PrefixParselet for QuoteParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { let mut str: String = String::new(); - if !parser.match_token(TokenType::QUOTE) { - while !parser.match_token(TokenType::QUOTE) { + if !parser.match_token(TokenType::Quote) { + while !parser.match_token(TokenType::Quote) { match parser.consume() { - Token::IDENTIFIER(s) => str = str + &s.to_string(), + Token::Identifier(s) => str = str + &s.to_string(), t => str = str + &t.to_string(), } } - parser.consume_expected(TokenType::QUOTE); + parser.consume_expected(TokenType::Quote); } Ast::Node { @@ -116,9 +116,9 @@ impl PrefixParselet for QuoteParselet { impl PrefixParselet for IfThenElseParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { let cond_expr = parser.parse_expression_empty(); - parser.consume_expected(TokenType::THEN); + parser.consume_expected(TokenType::Then); let lhs = parser.parse_expression(self.precedence); - parser.consume_expected(TokenType::ELSE); + parser.consume_expected(TokenType::Else); let rhs = parser.parse_expression(self.precedence); Ast::Conditional { @@ -131,9 +131,9 @@ impl PrefixParselet for IfThenElseParselet { impl PrefixParselet for WhileParselet { fn parse(&self, parser: &mut CalcParser, _token: &Token) -> Ast { - let cond_expr = parser.parse_expression(Precedence::WHILE as i64); - parser.consume_expected(TokenType::DO); - let body = parser.parse_expression(Precedence::WHILE as i64); + let cond_expr = parser.parse_expression(Precedence::While as i64); + parser.consume_expected(TokenType::Do); + let body = parser.parse_expression(Precedence::While as i64); Ast::While { condition: cond_expr.into(), diff --git a/src/parsing/parser.rs b/src/parsing/parser.rs index e0b7c38a..71a78a89 100644 --- a/src/parsing/parser.rs +++ b/src/parsing/parser.rs @@ -107,97 +107,97 @@ impl CalcParser<'_> { pub fn get_infix_parselet(&self, token_type: &TokenType) -> Option> { match token_type { - TokenType::PLUS => Some(Box::from(OperatorInfixParselet { + TokenType::Plus => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::SUM as i64), + precedence: (Precedence::Sum as i64), })), - TokenType::MINUS => Some(Box::from(OperatorInfixParselet { + TokenType::Minus => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::MINUS as i64), + precedence: (Precedence::Minus as i64), })), - TokenType::MULTIPLICATION => Some(Box::from(OperatorInfixParselet { + TokenType::Multiplication => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::PRODUCT as i64), + precedence: (Precedence::Product as i64), })), - TokenType::DIVIDE => Some(Box::from(OperatorInfixParselet { + TokenType::Divide => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::DIVIDE as i64), + precedence: (Precedence::Divide as i64), })), - TokenType::EQUAL => Some(Box::from(AssignParselet {})), - TokenType::EXPO => Some(Box::from(OperatorInfixParselet { + TokenType::Equal => Some(Box::from(AssignParselet {})), + TokenType::Expo => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::EXPONENT as i64), + precedence: (Precedence::Exponent as i64), })), - TokenType::SELECTION => Some(Box::from(OperatorInfixParselet { + TokenType::Selection => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::SELECTION as i64), + precedence: (Precedence::Selection as i64), })), - TokenType::CONCAT => Some(Box::from(OperatorInfixParselet { + TokenType::Concat => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONCAT as i64), + precedence: (Precedence::Concat as i64), })), - TokenType::LPAR => Some(Box::from(CallParselet {})), - TokenType::NOT => Some(Box::from(OperatorInfixParselet { + TokenType::Lpar => Some(Box::from(CallParselet {})), + TokenType::Not => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::EQUALITY => Some(Box::from(OperatorInfixParselet { + TokenType::Equality => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::LESSER => Some(Box::from(OperatorInfixParselet { + TokenType::Lesser => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::LESSEREQ => Some(Box::from(OperatorInfixParselet { + TokenType::LesserEq => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::GREATER => Some(Box::from(OperatorInfixParselet { + TokenType::Greater => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::GREATEREQ => Some(Box::from(OperatorInfixParselet { + TokenType::GreaterEq => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::OR => Some(Box::from(OperatorInfixParselet { + TokenType::Or => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::AND => Some(Box::from(OperatorInfixParselet { + TokenType::And => Some(Box::from(OperatorInfixParselet { is_right: false, - precedence: (Precedence::CONDITIONAL as i64), + precedence: (Precedence::Conditional as i64), })), - TokenType::IGNORE => Some(Box::from(IgnoreParselet {})), + TokenType::Ignore => Some(Box::from(IgnoreParselet {})), _ => None, } } pub fn get_prefix_parselet(&self, token_type: &TokenType) -> Option> { match token_type { - TokenType::PLUS => Some(Box::from(OperatorPrefixParselet {})), - TokenType::MINUS => Some(Box::from(OperatorPrefixParselet {})), - TokenType::MULTIPLICATION => Some(Box::from(OperatorPrefixParselet {})), - TokenType::DIVIDE => Some(Box::from(OperatorPrefixParselet {})), - TokenType::IDENTIFIER => Some(Box::from(ValueParselet {})), - TokenType::INT => Some(Box::from(ValueParselet {})), - TokenType::FLOAT => Some(Box::from(ValueParselet {})), - TokenType::BOOL => Some(Box::from(ValueParselet {})), - TokenType::LPAR => Some(Box::from(GroupParselet {})), - TokenType::NOT => Some(Box::from(OperatorPrefixParselet {})), - TokenType::EQUALITY => Some(Box::from(OperatorPrefixParselet {})), - TokenType::LESSER => Some(Box::from(OperatorPrefixParselet {})), - TokenType::LESSEREQ => Some(Box::from(OperatorPrefixParselet {})), - TokenType::GREATER => Some(Box::from(OperatorPrefixParselet {})), - TokenType::GREATEREQ => Some(Box::from(OperatorPrefixParselet {})), - TokenType::LBRACKET => Some(Box::from(VecParselet {})), - TokenType::QUOTE => Some(Box::from(QuoteParselet {})), - TokenType::IF => Some(Box::from(IfThenElseParselet { - precedence: Precedence::IFTHENELSE as i64, + TokenType::Plus => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Minus => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Multiplication => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Divide => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Identifier => Some(Box::from(ValueParselet {})), + TokenType::Int => Some(Box::from(ValueParselet {})), + TokenType::Float => Some(Box::from(ValueParselet {})), + TokenType::Bool => Some(Box::from(ValueParselet {})), + TokenType::Lpar => Some(Box::from(GroupParselet {})), + TokenType::Not => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Equality => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Lesser => Some(Box::from(OperatorPrefixParselet {})), + TokenType::LesserEq => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Greater => Some(Box::from(OperatorPrefixParselet {})), + TokenType::GreaterEq => Some(Box::from(OperatorPrefixParselet {})), + TokenType::Lbracket => Some(Box::from(VecParselet {})), + TokenType::Quote => Some(Box::from(QuoteParselet {})), + TokenType::If => Some(Box::from(IfThenElseParselet { + precedence: Precedence::IfThenElse as i64, })), - TokenType::WHILE => Some(Box::from(WhileParselet {})), - TokenType::LSB => Some(Box::from(ScopeParselet {})), + TokenType::While => Some(Box::from(WhileParselet {})), + TokenType::Lsb => Some(Box::from(ScopeParselet {})), _ => None, } }