From 9af2dbea368da83ea06f8257fb1273ca00054ddf Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 28 Apr 2024 20:47:34 +0200 Subject: [PATCH 01/67] WIP started symbolic simplification --- src/main.rs | 2 -- src/parsing/ast.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 576ed97..6f3f5dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -293,7 +293,6 @@ fn main() { exit(0); } - if arg_final == "-u" || arg_final == "--update" { if cfg!(target_os = "windows") { Command::new("cmd") @@ -311,7 +310,6 @@ fn main() { exit(0); } - let lexed = lex(arg_final); let mut parser = init_calc_parser(&lexed); let parsed = parser.parse(); diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 0da6f95..810131e 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -32,6 +32,7 @@ pub enum Parameters { ExpoOperation, Vector(Box>), InterpreterVector(Box>), + Variable(Box,String), } #[derive(Debug, Clone, PartialEq)] @@ -48,6 +49,33 @@ pub enum Ast { }, } +impl Ast { + + fn simplify(&self) -> &Ast { + + let (value,left,right) = match &self { + + Nil => (Null,Box::from(Nil),Box::from(Nil)), + Ast::Call { name, lst } => (Null,Box::from(Nil),Box::from(Nil)), + Node { value, left, right } => { + (*value,*left,*right) + } + + }; + + match value { + Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => return self, + PlusOperation => { + match (left.simplify(),right.simplify()) { + + } + } + }; + + } + +} + impl Display for Parameters { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { @@ -74,6 +102,7 @@ impl Display for Parameters { InterpreterVector(a) => write!(f, "{:?}", a), Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), + Variable(d,s) => write!(f,"{d}{s}"), } } } @@ -271,7 +300,7 @@ impl Parameters { #[cfg(test)] mod test { use crate::parsing::ast::{Ast, Parameters}; - + impl Ast { pub fn new(p: Parameters) -> Self { Ast::Node { From ee7816dfaa91a6bdde4d39cff70aea0d1cc6d9f1 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 28 Apr 2024 20:59:51 +0200 Subject: [PATCH 02/67] move in exact math --- src/exact_math/mod.rs | 1 + src/exact_math/symbolic.rs | 23 +++++++++++++++++++++++ src/interpreting/interpreter.rs | 1 + src/parsing/ast.rs | 31 ++----------------------------- 4 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 src/exact_math/symbolic.rs diff --git a/src/exact_math/mod.rs b/src/exact_math/mod.rs index f149e73..aa6bec9 100644 --- a/src/exact_math/mod.rs +++ b/src/exact_math/mod.rs @@ -1 +1,2 @@ pub mod rationals; +pub mod symbolic; diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs new file mode 100644 index 0000000..adf0299 --- /dev/null +++ b/src/exact_math/symbolic.rs @@ -0,0 +1,23 @@ +use crate::parsing::ast::Ast::*; +use crate::parsing::ast::Parameters::*; +use crate::parsing::ast::{Ast, Parameters}; + +impl Ast { + fn simplify(&self) -> &Ast { + let null_vector = (&Null, &Box::from(Nil), &Box::from(Nil)); + let (value, left, right) = match &self { + Ast::Node { value, left, right } => (value, left, right), + _ => null_vector, + }; + + match value { + Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => { + return self + } + PlusOperation => {} + _ => (), + }; + + self + } +} diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 29a610f..53cc5fd 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -89,6 +89,7 @@ pub fn interpret( Parameters::InterpreterVector(Box::from(vec)) } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), + Parameters::Variable(ast, s) => Parameters::Null, }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 810131e..4d2734c 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -32,7 +32,7 @@ pub enum Parameters { ExpoOperation, Vector(Box>), InterpreterVector(Box>), - Variable(Box,String), + Variable(Box, String), } #[derive(Debug, Clone, PartialEq)] @@ -49,33 +49,6 @@ pub enum Ast { }, } -impl Ast { - - fn simplify(&self) -> &Ast { - - let (value,left,right) = match &self { - - Nil => (Null,Box::from(Nil),Box::from(Nil)), - Ast::Call { name, lst } => (Null,Box::from(Nil),Box::from(Nil)), - Node { value, left, right } => { - (*value,*left,*right) - } - - }; - - match value { - Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => return self, - PlusOperation => { - match (left.simplify(),right.simplify()) { - - } - } - }; - - } - -} - impl Display for Parameters { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { @@ -102,7 +75,7 @@ impl Display for Parameters { InterpreterVector(a) => write!(f, "{:?}", a), Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), - Variable(d,s) => write!(f,"{d}{s}"), + Variable(d, s) => write!(f, "{d}{s}"), } } } From 2690630dcf5ac8d478f9ac1d0e9e00e1decff501 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 28 Apr 2024 21:06:35 +0200 Subject: [PATCH 03/67] change to be more rust-y --- src/exact_math/symbolic.rs | 32 +++++++++++++++----------------- src/interpreting/interpreter.rs | 1 + 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index adf0299..1fa8391 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -1,23 +1,21 @@ +use crate::parsing::ast::Ast; use crate::parsing::ast::Ast::*; use crate::parsing::ast::Parameters::*; -use crate::parsing::ast::{Ast, Parameters}; -impl Ast { - fn simplify(&self) -> &Ast { - let null_vector = (&Null, &Box::from(Nil), &Box::from(Nil)); - let (value, left, right) = match &self { - Ast::Node { value, left, right } => (value, left, right), - _ => null_vector, - }; +fn simplify(ast: &Ast) -> Ast { + let null_vector = (&Null, &Box::from(Nil), &Box::from(Nil)); + let (value, left, right) = match ast { + Ast::Node { value, left, right } => (value, left, right), + _ => null_vector, + }; - match value { - Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => { - return self - } - PlusOperation => {} - _ => (), - }; + match value { + Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => { + return ast.clone() + } + PlusOperation => {} + _ => (), + }; - self - } + ast.clone() } diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 53cc5fd..39b1d88 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::*; use crate::interpreting::function::{ add, and, assign, divide, equal, expo, greater, greater_or_equal, lesser, lesser_or_equal, minus, mult, not, or, From eaa057b4c101ee4c0ada68cd75e0605f9fa853c7 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 28 Apr 2024 21:09:33 +0200 Subject: [PATCH 04/67] change rust test to be on every branch --- .github/workflows/rust-test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/rust-test.yml b/.github/workflows/rust-test.yml index b36fb21..96b204e 100644 --- a/.github/workflows/rust-test.yml +++ b/.github/workflows/rust-test.yml @@ -2,7 +2,6 @@ name: Rust on: push: - branches: [ "mistress" ] pull_request: branches: [ "mistress" ] From b025bf48b44ad0e4ad7b84f7cf301b58376efdbe Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 29 Apr 2024 12:56:48 +0200 Subject: [PATCH 05/67] WIP --- src/exact_math/symbolic.rs | 16 ++- src/interpreting/function.rs | 184 +++++++++++++++++++++----------- src/interpreting/interpreter.rs | 4 +- src/parsing/ast.rs | 8 +- 4 files changed, 146 insertions(+), 66 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index 1fa8391..b9cb405 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -13,9 +13,23 @@ fn simplify(ast: &Ast) -> Ast { Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => { return ast.clone() } - PlusOperation => {} + PlusOperation => { + let (simplified_left, simplified_right) = (simplify(left), simplify(right)); + } _ => (), }; ast.clone() } + +/* +* +* 2+x+y+10+x = ((((2+x)+y)+10)+x) +* +* +* +* +* +* +* +*/ diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 37f5fe1..6cb5357 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -12,18 +12,18 @@ pub fn apply_operator( f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, ) -> Parameters { let s = match value { - Parameters::Identifier(s) => s, - _ => "".to_string(), + Parameters::Identifier(ref s) => s, + _ => "", }; - if s == "".to_string() { + if s == "" { return Parameters::Null; } match ram { - None => value2, + None => f(value.clone(), value2.clone(), None), Some(i_ram) => { - let value = i_ram.get(&s); - match value { - None => value2, + let values = i_ram.get(s); + match values { + None => f(value.clone(), value2.clone(), None), Some(val) => f(val.clone(), value2.clone(), ram), } } @@ -97,59 +97,123 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Bool(b), (Parameters::Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - add, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add) - } - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - add, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - add, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add) - } - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, add) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, add) + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s != s2 { + Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Identifier(s2.clone())), + ) + } else { + Parameters::Mul( + Box::from(Parameters::Int(2)), + Box::from(Parameters::Identifier(s.clone())), + ) + } + } + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + add, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Int(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Parameters::Identifier(s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + }, + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Identifier(ss.clone())), + ), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + add, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(ss.clone())), + Box::from(Parameters::Rational(s.clone())), + ), + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + add, + ), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Identifier(s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Int(i.clone())), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Float(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Float(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + add, + ), + }, + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + add, + ), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, add), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), + }, + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let _second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + first } _ => Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 39b1d88..9687b19 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; -use crate::exact_math::symbolic::*; use crate::interpreting::function::{ add, and, assign, divide, equal, expo, greater, greater_or_equal, lesser, lesser_or_equal, minus, mult, not, or, @@ -90,7 +89,8 @@ pub fn interpret( Parameters::InterpreterVector(Box::from(vec)) } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), - Parameters::Variable(ast, s) => Parameters::Null, + Parameters::Plus(x, y) => Parameters::Plus(x.clone(), y.clone()), + Parameters::Mul(x, y) => Parameters::Mul(x.clone(), y.clone()), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 4d2734c..df7bc6d 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -32,7 +32,8 @@ pub enum Parameters { ExpoOperation, Vector(Box>), InterpreterVector(Box>), - Variable(Box, String), + Plus(Box, Box), + Mul(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -75,7 +76,8 @@ impl Display for Parameters { InterpreterVector(a) => write!(f, "{:?}", a), Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), - Variable(d, s) => write!(f, "{d}{s}"), + Plus(x, y) => write!(f, "({x}+{y})"), + Mul(x, y) => write!(f, "({x}*{y})"), } } } @@ -118,7 +120,7 @@ impl Parameters { return self.to_string(); } else { match ram.as_mut().unwrap().get(s) { - None => "This variable is not initialized yet".to_string(), + None => s.to_string(), Some(t) => t.clone().pretty_print( Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), From acc4c9e0ebab23ac51ba127c5ece3167e40bb336 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 29 Apr 2024 13:50:15 +0200 Subject: [PATCH 06/67] reimplementing vars --- src/interpreting/function.rs | 5 +---- src/interpreting/interpreter.rs | 1 + src/parsing/ast.rs | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 6cb5357..3075c5a 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -105,10 +105,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap apply_operator( diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 9687b19..3ed64b0 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -91,6 +91,7 @@ pub fn interpret( Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Plus(x, y) => Parameters::Plus(x.clone(), y.clone()), Parameters::Mul(x, y) => Parameters::Mul(x.clone(), y.clone()), + Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index df7bc6d..8d46e22 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::fmt::{Display, Formatter}; +use std::str::Chars; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; @@ -32,6 +33,7 @@ pub enum Parameters { ExpoOperation, Vector(Box>), InterpreterVector(Box>), + Var(Box, i64, String), Plus(Box, Box), Mul(Box, Box), } @@ -50,6 +52,38 @@ pub enum Ast { }, } +fn int_to_superscript_string(i: i64) -> String { + fn digit_to_superscript_char(i: &str) -> &str { + match i { + "0" => "⁰", + "1" => "¹", + "2" => "²", + "3" => "³", + "4" => "⁴", + "5" => "⁵", + "6" => "⁶", + "7" => "⁷", + "8" => "⁸", + "9" => "⁹", + _ => "", + } + } + + let mut vec = vec![]; + let string_int = i.to_string(); + string_int + .split("") + .map(|x| digit_to_superscript_char(x)) + .for_each(|f| vec.push(f)); + + let i = vec.join(""); + if i == "0".to_string() || i == "¹".to_string() { + "".to_string() + } else { + i + } +} + impl Display for Parameters { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { @@ -78,6 +112,7 @@ impl Display for Parameters { Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), Mul(x, y) => write!(f, "({x}*{y})"), + Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), } } } From 74886f94bb3f0c06a8ce63a68c771663b307d9de Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 29 Apr 2024 21:53:51 +0200 Subject: [PATCH 07/67] WIP TODO: var computation add --- src/exact_math/symbolic.rs | 122 ++++++++++++++++++++++++++--------- src/interpreting/function.rs | 64 +++++++++++++++++- 2 files changed, 155 insertions(+), 31 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index b9cb405..2a15b8d 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -1,35 +1,99 @@ -use crate::parsing::ast::Ast; -use crate::parsing::ast::Ast::*; +use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; -fn simplify(ast: &Ast) -> Ast { - let null_vector = (&Null, &Box::from(Nil), &Box::from(Nil)); - let (value, left, right) = match ast { - Ast::Node { value, left, right } => (value, left, right), - _ => null_vector, - }; +pub fn size(p: &Parameters) -> i32 { + match p { + Null + | Int(_) + | Str(_) + | Float(_) + | Identifier(_) + | PlusOperation + | MinusOperation + | MultiplicationOperation + | DivideOperation + | Assign + | ExpoOperation + | GreaterOperation + | GreaterOrEqualOperation + | LesserOperation + | LesserOrEqualOperation + | Equal + | Bool(_) + | Rational(_) + | OrOperation + | AndOperation + | Not + | Vector(_) + | InterpreterVector(_) => 0, + Plus(x, y) => 1 + size(x) + size(y), + Mul(x, y) => 1 + size(x) + size(y), + Var(x, _, _) => 1 + size(x), + } +} - match value { - Null | Int(_) | Str(_) | Float(_) | Bool(_) | Identifier(_) | Rational(_) => { - return ast.clone() - } - PlusOperation => { - let (simplified_left, simplified_right) = (simplify(left), simplify(right)); - } - _ => (), +#[cfg(test)] +mod test { + use crate::{ + exact_math::rationals::Rationals, + parsing::ast::{ + Ast, + Parameters::{self, *}, + }, }; - ast.clone() -} + use super::size; + #[test] + fn test_leaf() { + let v = vec![ + Null, + Int(1), + Str("".to_string()), + Float(1.0), + Identifier("".to_string()), + PlusOperation, + MinusOperation, + MultiplicationOperation, + DivideOperation, + Assign, + ExpoOperation, + GreaterOperation, + GreaterOrEqualOperation, + LesserOperation, + LesserOrEqualOperation, + Equal, + Bool(false), + Rational(Rationals::new(10, 10)), + OrOperation, + AndOperation, + Not, + Vector(Box::from(vec![Ast::Nil])), + InterpreterVector(Box::from(vec![Null])), + ]; -/* -* -* 2+x+y+10+x = ((((2+x)+y)+10)+x) -* -* -* -* -* -* -* -*/ + let should = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + ]; + + let _ = v + .into_iter() + .map(|f| size(f)) + .zip(should) + .for_each(|(x, y)| assert_eq!(x, y)); + } + + #[test] + fn test_size() { + let should = 2; + let tree = Plus( + Box::from(Plus( + Box::from(Parameters::Int(1)), + Box::from(Parameters::Int(2)), + )), + Box::from(Parameters::Int(3)), + ); + let result = size(tree); + assert_eq!(result, should); + } +} diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 3075c5a..291111a 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::{self, size}; use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::Bool; use crate::utils::matrix_utils::mult_matrix; @@ -201,16 +202,75 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Null, Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), }, + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { let first = Parameters::Plus( Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), s2.clone(), ); - let _second = Parameters::Plus( + let second = Parameters::Plus( s1.clone(), Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), ); - first + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } } _ => Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), From 069f5d02cfe7494e81e14258c73765988a209919 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 13:10:42 +0200 Subject: [PATCH 08/67] Plus addition done for rationals, floats and int --- src/interpreting/function.rs | 172 ++++++++++++++++++++++++++++++-- src/interpreting/interpreter.rs | 2 +- 2 files changed, 167 insertions(+), 7 deletions(-) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 291111a..9d92b55 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -38,18 +38,18 @@ pub fn apply_operator_reverse( f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, ) -> Parameters { let s = match value2 { - Parameters::Identifier(s) => s, - _ => "".to_string(), + Parameters::Identifier(ref s) => s, + _ => "", }; - if s == "".to_string() { + if s == "" { return Parameters::Null; } match ram { - None => value, + None => f(value.clone(), value2.clone(), None), Some(i_ram) => { - let val3 = i_ram.get(&s); + let val3 = i_ram.get(s); match val3 { - None => value, + None => f(value.clone(), value2.clone(), None), Some(val) => f(value.clone(), val.clone(), ram), } } @@ -202,7 +202,24 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Null, Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), }, + (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), *s3.clone(), ram)), + Box::from(add(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(add(*s1.clone(), *s4.clone(), ram)), + Box::from(add(*s2.clone(), *s3.clone(), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { let first = Parameters::Plus( Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), @@ -272,6 +289,149 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } _ => Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), ), diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 3ed64b0..94b2cb4 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -89,7 +89,7 @@ pub fn interpret( Parameters::InterpreterVector(Box::from(vec)) } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), - Parameters::Plus(x, y) => Parameters::Plus(x.clone(), y.clone()), + Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), Parameters::Mul(x, y) => Parameters::Mul(x.clone(), y.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), }; From efda9ed9df52a7c65fb589d1d7130cfb5b8b242e Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 13:29:55 +0200 Subject: [PATCH 09/67] Var 1/2 add vars computation with itself and identifiers --- src/interpreting/function.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 9d92b55..4364692 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; -use crate::exact_math::symbolic::{self, size}; +use crate::exact_math::symbolic::size; use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::Bool; use crate::utils::matrix_utils::mult_matrix; @@ -432,6 +432,39 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { + if z == z1 && y == y1 { + Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(x1.clone(), y1.clone(), z1.clone())), + ) + } + } + + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } _ => Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), ), From fcfac541941fc473dc892c8971965f4e5fbb01c8 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 13:32:11 +0200 Subject: [PATCH 10/67] fix test --- src/exact_math/symbolic.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index 2a15b8d..f5f2811 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -78,7 +78,7 @@ mod test { let _ = v .into_iter() - .map(|f| size(f)) + .map(|f| size(&f)) .zip(should) .for_each(|(x, y)| assert_eq!(x, y)); } @@ -93,7 +93,7 @@ mod test { )), Box::from(Parameters::Int(3)), ); - let result = size(tree); + let result = size(&tree); assert_eq!(result, should); } } From 7b9556d5eab205437ca7bc5e15ec37341f863ae2 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 16:09:47 +0200 Subject: [PATCH 11/67] Var 2/2 add variables with i --- src/interpreting/function.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 4364692..664926b 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -465,6 +465,36 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Int(i)), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Float(f)), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Rational(r.clone())), + ), _ => Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), ), From e815b28f09fc0e0be6065e95d6c9fd6032907a83 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 16:15:50 +0200 Subject: [PATCH 12/67] remove Mul --- src/exact_math/symbolic.rs | 1 - src/interpreting/interpreter.rs | 3 +-- src/parsing/ast.rs | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index f5f2811..030b8e9 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -27,7 +27,6 @@ pub fn size(p: &Parameters) -> i32 { | Vector(_) | InterpreterVector(_) => 0, Plus(x, y) => 1 + size(x) + size(y), - Mul(x, y) => 1 + size(x) + size(y), Var(x, _, _) => 1 + size(x), } } diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 94b2cb4..8c476bc 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -89,9 +89,8 @@ pub fn interpret( Parameters::InterpreterVector(Box::from(vec)) } Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), - Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), - Parameters::Mul(x, y) => Parameters::Mul(x.clone(), y.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), + Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 8d46e22..bcb1cf4 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -35,7 +35,6 @@ pub enum Parameters { InterpreterVector(Box>), Var(Box, i64, String), Plus(Box, Box), - Mul(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -111,7 +110,6 @@ impl Display for Parameters { Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), - Mul(x, y) => write!(f, "({x}*{y})"), Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), } } From cf4fca398f7445e074623a393987f263384d589f Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 21:03:24 +0200 Subject: [PATCH 13/67] minus --- src/exact_math/symbolic.rs | 1 + src/interpreting/function.rs | 181 +++++++++++++++++++++----------- src/interpreting/interpreter.rs | 1 + src/parsing/ast.rs | 4 +- 4 files changed, 125 insertions(+), 62 deletions(-) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index 030b8e9..ef9c967 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -27,6 +27,7 @@ pub fn size(p: &Parameters) -> i32 { | Vector(_) | InterpreterVector(_) => 0, Plus(x, y) => 1 + size(x) + size(y), + Minus(x, y) => 1 + size(x) + size(y), Var(x, _, _) => 1 + size(x), } } diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 664926b..37746ca 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -564,70 +564,129 @@ pub fn minus( (Bool(b), Parameters::Null) => Bool(b), (Parameters::Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - minus, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus) - } - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - minus, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - minus, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); - match v { - Parameters::Int(i) => Parameters::Int(-i), - _ => Parameters::Null, + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s != s2 { + Parameters::Minus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s2)), + ) + } else { + Parameters::Int(0) + } } - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); - match v { - Parameters::Float(i) => Parameters::Float(-i), - _ => Parameters::Null, + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + minus, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Int(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Null), + Box::from(Parameters::Identifier(s)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Null), + Box::from(Parameters::Identifier(s)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + }, + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Identifier(ss)), + ), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + minus, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Identifier(ss)), + Box::from(Parameters::Rational(s.clone())), + ), + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + minus, + ), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Identifier(s)), + ), + Some(_) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); + match v { + Parameters::Int(i) => Parameters::Int(-i), + _ => Parameters::Null, + } } - } + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Float(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Minus( + Box::from(Parameters::Float(i)), + Box::from(Parameters::Identifier(s)), + ), + Some(_) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); + match v { + Parameters::Float(i) => Parameters::Float(-i), + _ => Parameters::Null, + } + } + }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - minus, - ), - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - minus, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, minus) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, minus) - } + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator_reverse( + Parameters::InterpreterVector(vec.clone()), + Parameters::Identifier(s), + ram, + minus, + ), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + minus, + ), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, minus), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, minus), + }, _ => Parameters::Identifier( "Those two values are incompatible with the - operator".to_string(), ), diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 8c476bc..944c0ac 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -91,6 +91,7 @@ pub fn interpret( Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), + Parameters::Minus(x, y) => minus(*x.clone(), *y.clone(), Some(&ram)), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index bcb1cf4..0403162 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,6 +1,5 @@ use std::collections::HashMap; use std::fmt::{Display, Formatter}; -use std::str::Chars; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; @@ -35,6 +34,7 @@ pub enum Parameters { InterpreterVector(Box>), Var(Box, i64, String), Plus(Box, Box), + Minus(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -110,6 +110,7 @@ impl Display for Parameters { Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), + Minus(x, y) => write!(f, "({x}-{y})"), Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), } } @@ -162,6 +163,7 @@ impl Parameters { } } } + InterpreterVector(lst) => { let mut vec = Vec::new(); From 983b4fc8f753099ce63594012f874a3b2d2406be Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 21:45:10 +0200 Subject: [PATCH 14/67] replace all minus by plus erase minus clever printing --- src/exact_math/rationals.rs | 4 ++ src/exact_math/symbolic.rs | 1 - src/interpreting/function.rs | 111 ++++++-------------------------- src/interpreting/interpreter.rs | 1 - src/parsing/ast.rs | 24 ++++++- 5 files changed, 46 insertions(+), 95 deletions(-) diff --git a/src/exact_math/rationals.rs b/src/exact_math/rationals.rs index b833463..9eea7cc 100644 --- a/src/exact_math/rationals.rs +++ b/src/exact_math/rationals.rs @@ -32,6 +32,10 @@ impl Rationals { return self.over == 0; } + pub fn opposite(self) -> Self { + Rationals::new(self.under, -1 * self.over) + } + pub fn reduce(self) -> Self { let minus; let i1; diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index ef9c967..030b8e9 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -27,7 +27,6 @@ pub fn size(p: &Parameters) -> i32 { | Vector(_) | InterpreterVector(_) => 0, Plus(x, y) => 1 + size(x) + size(y), - Minus(x, y) => 1 + size(x) + size(y), Var(x, _, _) => 1 + size(x), } } diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 37746ca..f5915b4 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -308,77 +308,6 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { let first = Parameters::Plus( Box::from(add(*s1.clone(), Parameters::Float(f), ram)), @@ -567,9 +496,9 @@ pub fn minus( (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { None => { if s != s2 { - Parameters::Minus( + Parameters::Plus( Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s2)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s2)), ) } else { Parameters::Int(0) @@ -583,30 +512,30 @@ pub fn minus( ), }, (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Int(i)), + Box::from(Parameters::Int(-i)), ), Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus), }, (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Null), - Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), ), Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), }, (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Null), - Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), ), Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), }, (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Rational(s.clone())), - Box::from(Parameters::Identifier(ss)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, ss)), ), Some(_) => apply_operator_reverse( Parameters::Rational(s.clone()), @@ -616,9 +545,9 @@ pub fn minus( ), }, (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - None => Parameters::Minus( - Box::from(Parameters::Identifier(ss)), - Box::from(Parameters::Rational(s.clone())), + None => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, ss)), + Box::from(Parameters::Rational(s.opposite())), ), Some(_) => apply_operator( Parameters::Identifier(ss), @@ -628,9 +557,9 @@ pub fn minus( ), }, (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Int(i)), - Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), ), Some(_) => { let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); @@ -641,16 +570,16 @@ pub fn minus( } }, (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Minus( - Box::from(Parameters::Identifier(s)), - Box::from(Parameters::Float(i)), + None => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), + Box::from(Parameters::Float(-i)), ), Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus), }, (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Minus( + None => Parameters::Plus( Box::from(Parameters::Float(i)), - Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), ), Some(_) => { let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 944c0ac..8c476bc 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -91,7 +91,6 @@ pub fn interpret( Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), - Parameters::Minus(x, y) => minus(*x.clone(), *y.clone(), Some(&ram)), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 0403162..cafa9e2 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -34,7 +34,6 @@ pub enum Parameters { InterpreterVector(Box>), Var(Box, i64, String), Plus(Box, Box), - Minus(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -110,7 +109,6 @@ impl Display for Parameters { Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), - Minus(x, y) => write!(f, "({x}-{y})"), Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), } } @@ -164,6 +162,28 @@ impl Parameters { } } + Var(x, y, z) => match **x { + Int(1) => format!("{}{}", z, int_to_superscript_string(*y)), + Int(-1) => format!("-{}{}", z, int_to_superscript_string(*y)), + _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), + }, + + Plus(x, y) => { + let x_printed = x.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + let y_printed = y.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + if y_printed.chars().nth(0).unwrap() == '-' { + format!("{}{}", x_printed, y_printed) + } else { + format!("{}+{}", x_printed, y_printed) + } + } + InterpreterVector(lst) => { let mut vec = Vec::new(); From 7cf77752667b96c3dc59f429f4c2dcc82223a3d7 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 30 Apr 2024 22:26:37 +0200 Subject: [PATCH 15/67] finish add to minus --- src/interpreting/function.rs | 246 +++++++++++++++++++++++++++++++++++ src/parsing/ast.rs | 13 +- 2 files changed, 255 insertions(+), 4 deletions(-) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index f5915b4..77a6b03 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -616,6 +616,252 @@ pub fn minus( None => Parameters::Bool(b), Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, minus), }, + (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), *s3.clone(), ram)), + Box::from(minus(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(*s1.clone(), *s4.clone(), ram)), + Box::from(minus(*s2.clone(), *s3.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Identifier(s3.clone()), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Identifier(s3.clone()), *s2.clone(), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Int(i), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(i), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Float(f), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Float(f), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Rational(r.clone()), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Rational(r.clone()), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + if z == z1 && y == y1 { + Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x1.clone(), ram)), + y1.clone(), + z1.clone(), + )), + ) + } + } + + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(minus(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(-1)), + 1, + s.clone(), + )), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(minus(Parameters::Int(1), *x.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y.clone(), + z.clone(), + )), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Int(-i)), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Float(-f)), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Rational(r.opposite())), + ), _ => Parameters::Identifier( "Those two values are incompatible with the - operator".to_string(), ), diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index cafa9e2..a08ec0a 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -177,10 +177,15 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); - if y_printed.chars().nth(0).unwrap() == '-' { - format!("{}{}", x_printed, y_printed) - } else { - format!("{}+{}", x_printed, y_printed) + match y_printed.chars().nth(0) { + Some('-') => format!("{}{}", x_printed, y_printed), + _ => { + if y_printed == "0".to_string() { + format!("{}", x_printed) + } else { + format!("{}+{}", x_printed, y_printed) + } + } } } From f26dd6efdf2c0053baf3359968905bf217c5ae13 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 2 May 2024 13:42:54 +0200 Subject: [PATCH 16/67] add mul add test in symbolic --- src/exact_math/symbolic.rs | 22 ++++++++++++++++++++++ src/interpreting/interpreter.rs | 1 + src/parsing/ast.rs | 2 ++ 3 files changed, 25 insertions(+) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index 030b8e9..bf40fc6 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -28,11 +28,13 @@ pub fn size(p: &Parameters) -> i32 { | InterpreterVector(_) => 0, Plus(x, y) => 1 + size(x) + size(y), Var(x, _, _) => 1 + size(x), + Mul(x, y) => 1 + size(x) + size(y), } } #[cfg(test)] mod test { + use crate::{ exact_math::rationals::Rationals, parsing::ast::{ @@ -95,4 +97,24 @@ mod test { let result = size(&tree); assert_eq!(result, should); } + #[test] + fn test_size_mul() { + let should = 2; + let tree = Mul( + Box::from(Plus( + Box::from(Parameters::Int(1)), + Box::from(Parameters::Int(2)), + )), + Box::from(Parameters::Int(3)), + ); + let result = size(&tree); + assert_eq!(result, should); + } + #[test] + fn test_size_var() { + let should = 1; + let tree = Var(Box::from(Parameters::Int(1)), 1, "s".to_string()); + let result = size(&tree); + assert_eq!(result, should); + } } diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 8c476bc..27c9ec2 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -91,6 +91,7 @@ pub fn interpret( Parameters::InterpreterVector(a) => Parameters::InterpreterVector(a.clone()), Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), z.clone()), Parameters::Plus(x, y) => add(*x.clone(), *y.clone(), Some(&ram)), + Parameters::Mul(x, y) => mult(*x.clone(), *y.clone(), Some(&ram)), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index a08ec0a..dafbc53 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -34,6 +34,7 @@ pub enum Parameters { InterpreterVector(Box>), Var(Box, i64, String), Plus(Box, Box), + Mul(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -109,6 +110,7 @@ impl Display for Parameters { Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), + Mul(x, y) => write!(f, "({x}*{y})"), Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), } } From 182ae946397ab343c85d1a2adec1dbcff8c650a1 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 2 May 2024 14:02:29 +0200 Subject: [PATCH 17/67] implements multiplications 1/2 --- src/interpreting/function.rs | 144 ++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 54 deletions(-) diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs index 77a6b03..f812c59 100644 --- a/src/interpreting/function.rs +++ b/src/interpreting/function.rs @@ -986,61 +986,97 @@ pub fn mult( (Bool(b), Parameters::Null) => Bool(b), (Parameters::Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - mult, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult) - } + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s == s2 { + Parameters::Var(Box::from(Parameters::Int(1)), 2, s.clone()) + } else { + Parameters::Mul( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(1)), + 1, + s2.clone(), + )), + ) + } + } + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + mult, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), + }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - mult, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - mult, - ), - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult) - } - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - mult, - ), - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - mult, - ), - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, mult) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, mult) - } + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + mult, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + mult, + ), + None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + mult, + ), + }, + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator_reverse( + Parameters::InterpreterVector(vec.clone()), + Parameters::Identifier(s), + ram, + mult, + ), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, mult), + None => Parameters::Bool(b), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, mult), + None => Parameters::Bool(b), + }, _ => Parameters::Identifier( "@Those two values are incompatible with the * operator".to_string(), ), From 0e9dc86a93ae192c9295d2c5f829e5dc14fbd054 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Fri, 3 May 2024 20:07:17 +0200 Subject: [PATCH 18/67] split functions in different files split add,minus,mult,divide,expo in their respective files. --- src/functions/add.rs | 381 ++++++ src/functions/divide.rs | 109 ++ src/functions/expo.rs | 94 ++ src/functions/function.rs | 708 ++++++++++++ src/functions/minus.rs | 445 +++++++ src/functions/mod.rs | 6 + src/functions/mult.rs | 224 ++++ src/interpreting/function.rs | 1922 ------------------------------- src/interpreting/interpreter.rs | 10 +- src/interpreting/mod.rs | 1 - src/interpreting/stdlib.rs | 3 +- src/main.rs | 1 + src/utils/matrix_utils.rs | 4 +- 13 files changed, 1978 insertions(+), 1930 deletions(-) create mode 100644 src/functions/add.rs create mode 100644 src/functions/divide.rs create mode 100644 src/functions/expo.rs create mode 100644 src/functions/function.rs create mode 100644 src/functions/minus.rs create mode 100644 src/functions/mod.rs create mode 100644 src/functions/mult.rs delete mode 100644 src/interpreting/function.rs diff --git a/src/functions/add.rs b/src/functions/add.rs new file mode 100644 index 0000000..f26f5fb --- /dev/null +++ b/src/functions/add.rs @@ -0,0 +1,381 @@ +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::functions::function::apply_operator; +use crate::functions::function::apply_operator_reverse; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::*; +use std::collections::HashMap; + +pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), + (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), + (Parameters::Null, Parameters::InterpreterVector(vec)) => { + Parameters::InterpreterVector(vec.clone()) + } + (Parameters::InterpreterVector(vec), Parameters::Null) => { + Parameters::InterpreterVector(vec.clone()) + } + (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), + (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), + (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), + (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), + (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s + s2), + (Parameters::Rational(s), Parameters::Int(i)) => { + Parameters::Rational(s + Rationals::new(1, i)) + } + (Parameters::Int(i), Parameters::Rational(s)) => { + Parameters::Rational(s + Rationals::new(1, i)) + } + (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() + f), + (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f + s.approx()), + (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v + v2), + (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) + f), + (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v + f), + (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v + (i1 as f64)), + (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + let mut res = Vec::new(); + vec.into_iter() + .zip(vec2.into_iter()) + .map(|(x, y)| add(x.clone(), y.clone(), ram)) + .for_each(|s| res.push(s)); + Parameters::InterpreterVector(Box::from(res)) + } + (Bool(_), Parameters::Int(i)) => Parameters::Int(i), + (Bool(_), Parameters::Float(i)) => Parameters::Float(i), + (Parameters::Int(i), Bool(_)) => Parameters::Int(i), + (Parameters::Float(i), Bool(_)) => Parameters::Float(i), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s != s2 { + Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Identifier(s2.clone())), + ) + } else { + Parameters::Var(Box::from(Parameters::Int(2)), 1, s.clone()) + } + } + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + add, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Int(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Parameters::Identifier(s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + }, + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Identifier(ss.clone())), + ), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + add, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(ss.clone())), + Box::from(Parameters::Rational(s.clone())), + ), + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + add, + ), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Identifier(s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s)), + Box::from(Parameters::Int(i.clone())), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Float(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Float(i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + add, + ), + }, + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + add, + ), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, add), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Parameters::Null, + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), + }, + (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), *s3.clone(), ram)), + Box::from(add(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(add(*s1.clone(), *s4.clone(), ram)), + Box::from(add(*s2.clone(), *s3.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + if z == z1 && y == y1 { + Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(x1.clone(), y1.clone(), z1.clone())), + ) + } + } + + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Int(i)), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Float(f)), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Var(x, y, z)), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Rational(r.clone())), + ), + _ => Parameters::Identifier( + "@Those two values are incompatible with the + operator".to_string(), + ), + } +} diff --git a/src/functions/divide.rs b/src/functions/divide.rs new file mode 100644 index 0000000..d051203 --- /dev/null +++ b/src/functions/divide.rs @@ -0,0 +1,109 @@ +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::functions::function::apply_operator; +use crate::functions::function::apply_operator_reverse; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::*; +use std::collections::HashMap; + +pub fn divide( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), + (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), + (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Rational(Rationals::new(v2, v)), + (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) / f), + (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v / f), + (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v / (i1 as f64)), + (Parameters::Null, Parameters::InterpreterVector(vec)) => { + Parameters::InterpreterVector(vec.clone()) + } + (Parameters::InterpreterVector(vec), Parameters::Null) => { + Parameters::InterpreterVector(vec.clone()) + } + + (Parameters::Rational(s), Parameters::Null) => { + Parameters::Rational(Rationals::new(1, 1) / s) + } + (Parameters::Null, Parameters::Rational(s)) => { + Parameters::Rational(Rationals::new(1, 1) / s) + } + (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s / s2), + + (Parameters::Rational(s), Parameters::Int(i)) => { + Parameters::Rational(s / Rationals::new(1, i)) + } + (Parameters::Int(i), Parameters::Rational(s)) => { + Parameters::Rational(Rationals::new(1, i) / s) + } + (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() / f), + (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f / s.approx()), + (Bool(_), Parameters::Int(i)) => Parameters::Int(i), + (Bool(_), Parameters::Float(i)) => Parameters::Float(i), + (Parameters::Int(i), Bool(_)) => Parameters::Int(i), + (Parameters::Float(i), Bool(_)) => Parameters::Float(i), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + divide, + ), + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + divide, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + divide, + ), + + (Parameters::Identifier(s), Parameters::Int(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide) + } + (Parameters::Int(i), Parameters::Identifier(s)) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide); + match v { + Parameters::Float(i) => Parameters::Float(1.0 / i), + _ => Parameters::Null, + } + } + (Parameters::Null, Parameters::Identifier(s)) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) + } + (Parameters::Identifier(s), Parameters::Null) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) + } + (Parameters::Identifier(s), Parameters::Float(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide) + } + (Parameters::Float(i), Parameters::Identifier(s)) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide); + match v { + Parameters::Float(i) => Parameters::Float(1.0 / i), + _ => Parameters::Null, + } + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, divide) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, divide) + } + _ => Parameters::Identifier( + "@Those two values are incompatible with the / operator".to_string(), + ), + } +} diff --git a/src/functions/expo.rs b/src/functions/expo.rs new file mode 100644 index 0000000..7892760 --- /dev/null +++ b/src/functions/expo.rs @@ -0,0 +1,94 @@ +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::functions::add::add; +use crate::functions::function::apply_operator; +use crate::functions::function::apply_operator_reverse; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::*; +use crate::utils::matrix_utils::*; +use std::collections::HashMap; + +pub fn expo( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), + (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), + (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Float((v as f64).powf(v2 as f64)), + (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64).powf(f)), + (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v.powf(f)), + (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v.powf(i1 as f64)), + + (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), + (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), + (Parameters::Rational(s), Parameters::Rational(s2)) => { + Parameters::Float(s.approx().powf(s2.approx())) + } + (Parameters::Rational(s), Parameters::Int(i)) => { + Parameters::Float(s.approx().powf(i as f64)) + } + (Parameters::Int(i), Parameters::Rational(s)) => { + Parameters::Float((i as f64).powf(s.approx())) + } + (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx().powf(f)), + (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f.powf(s.approx())), + (Bool(_), Parameters::Int(i)) => Parameters::Int(i), + (Bool(_), Parameters::Float(i)) => Parameters::Float(i), + (Parameters::Int(i), Bool(_)) => Parameters::Int(i), + (Parameters::Float(i), Bool(_)) => Parameters::Float(i), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + expo, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, expo) + } + (Parameters::Int(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, expo) + } + (Parameters::Identifier(s), Parameters::Float(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo) + } + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + expo, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + expo, + ), + (Parameters::Identifier(s), Parameters::Null) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) + } + (Parameters::Null, Parameters::Identifier(s)) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) + } + (Parameters::Float(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo) + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, expo) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, expo) + } + + _ => Parameters::Identifier( + "@Those two values are incompatible with the ^ operator".to_string(), + ), + } +} diff --git a/src/functions/function.rs b/src/functions/function.rs new file mode 100644 index 0000000..15d2c2b --- /dev/null +++ b/src/functions/function.rs @@ -0,0 +1,708 @@ +use std::collections::HashMap; + +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::Bool; +use crate::utils::matrix_utils::mult_matrix; + +pub fn apply_operator( + value: Parameters, + value2: Parameters, + ram: Option<&HashMap>, + f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, +) -> Parameters { + let s = match value { + Parameters::Identifier(ref s) => s, + _ => "", + }; + if s == "" { + return Parameters::Null; + } + match ram { + None => f(value.clone(), value2.clone(), None), + Some(i_ram) => { + let values = i_ram.get(s); + match values { + None => f(value.clone(), value2.clone(), None), + Some(val) => f(val.clone(), value2.clone(), ram), + } + } + } +} + +pub fn apply_operator_reverse( + value: Parameters, + value2: Parameters, + ram: Option<&HashMap>, + f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, +) -> Parameters { + let s = match value2 { + Parameters::Identifier(ref s) => s, + _ => "", + }; + if s == "" { + return Parameters::Null; + } + match ram { + None => f(value.clone(), value2.clone(), None), + Some(i_ram) => { + let val3 = i_ram.get(s); + match val3 { + None => f(value.clone(), value2.clone(), None), + Some(val) => f(value.clone(), val.clone(), ram), + } + } + } +} + +pub fn assign(s: Parameters, s2: Parameters) -> (String, Parameters) { + match s { + Parameters::Identifier(s) => (s, s2), + _ => ("".to_string(), s2), + } +} + +pub fn greater( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(_)) => Bool(true), + (Parameters::Null, Parameters::Float(_)) => Bool(true), + (Parameters::Int(_), Parameters::Null) => Bool(true), + (Parameters::Float(_), Parameters::Null) => Bool(true), + (Parameters::Int(v), Parameters::Int(v2)) => Bool(v > v2), + (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) > f), + (Parameters::Float(v), Parameters::Float(f)) => Bool(v > f), + (Parameters::Float(v), Parameters::Int(i1)) => Bool(v > (i1 as f64)), + (Parameters::Rational(_), Parameters::Null) => Bool(true), + (Parameters::Null, Parameters::Rational(_)) => Bool(true), + (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s > s2), + (Parameters::Rational(s), Parameters::Int(i)) => Bool(s > Rationals::new(1, i)), + (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) > s), + (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() > f), + (Parameters::Float(f), Parameters::Rational(s)) => Bool(f > s.approx()), + (Bool(b), Parameters::Int(_)) => Bool(b), + (Bool(b), Parameters::Float(_)) => Bool(b), + (Parameters::Int(_), Bool(b)) => Bool(b), + (Parameters::Float(_), Bool(b)) => Bool(b), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + greater, + ), + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + greater, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + greater, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, greater) + } + (Parameters::Null, Parameters::Identifier(s)) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) + } + (Parameters::Identifier(s), Parameters::Null) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) + } + (Parameters::Int(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, greater) + } + (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( + Parameters::Identifier(s), + Parameters::Float(i), + ram, + greater, + ), + (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( + Parameters::Float(i), + Parameters::Identifier(s), + ram, + greater, + ), + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, greater) + } + + _ => Parameters::Identifier( + "@Those two values are incompatible with the > operator".to_string(), + ), + } +} + +pub fn lesser( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(_)) => Bool(false), + (Parameters::Null, Parameters::Float(_)) => Bool(false), + (Parameters::Int(_), Parameters::Null) => Bool(false), + (Parameters::Float(_), Parameters::Null) => Bool(false), + (Parameters::Int(v), Parameters::Int(v2)) => Bool(v < v2), + (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) < f), + (Parameters::Float(v), Parameters::Float(f)) => Bool(v < f), + (Parameters::Float(v), Parameters::Int(i1)) => Bool(v < (i1 as f64)), + (Parameters::Rational(_), Parameters::Null) => Bool(true), + (Parameters::Null, Parameters::Rational(_)) => Bool(true), + (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s < s2), + (Parameters::Rational(s), Parameters::Int(i)) => Bool(s < Rationals::new(1, i)), + (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) < s), + (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() < f), + (Parameters::Float(f), Parameters::Rational(s)) => Bool(f < s.approx()), + (Bool(b), Parameters::Int(_)) => Bool(b), + (Bool(b), Parameters::Float(_)) => Bool(b), + (Parameters::Int(_), Bool(b)) => Bool(b), + (Parameters::Float(_), Bool(b)) => Bool(b), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + lesser, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, lesser) + } + (Parameters::Null, Parameters::Identifier(s)) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) + } + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + lesser, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + lesser, + ), + (Parameters::Identifier(s), Parameters::Null) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) + } + (Parameters::Int(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, lesser) + } + (Parameters::Identifier(s), Parameters::Float(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, lesser) + } + (Parameters::Float(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, lesser) + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser) + } + + _ => Parameters::Identifier( + "@Those two values are incompatible with the < operator".to_string(), + ), + } +} + +pub fn greater_or_equal( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(_)) => Bool(true), + (Parameters::Null, Parameters::Float(_)) => Bool(true), + (Parameters::Int(_), Parameters::Null) => Bool(true), + (Parameters::Float(_), Parameters::Null) => Bool(true), + (Parameters::Int(v), Parameters::Int(v2)) => Bool(v >= v2), + (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) >= f), + (Parameters::Float(v), Parameters::Float(f)) => Bool(v >= f), + (Parameters::Float(v), Parameters::Int(i1)) => Bool(v >= (i1 as f64)), + (Bool(b), Parameters::Int(_)) => Bool(b), + (Bool(b), Parameters::Float(_)) => Bool(b), + (Parameters::Int(_), Bool(b)) => Bool(b), + (Parameters::Float(_), Bool(b)) => Bool(b), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b == b2), + + (Parameters::Rational(_), Parameters::Null) => Bool(true), + (Parameters::Null, Parameters::Rational(_)) => Bool(true), + (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s >= s2), + (Parameters::Rational(s), Parameters::Int(i)) => Bool(s >= Rationals::new(1, i)), + (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) >= s), + (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() >= f), + (Parameters::Float(f), Parameters::Rational(s)) => Bool(f >= s.approx()), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + greater_or_equal, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( + Parameters::Identifier(s), + Parameters::Int(i), + ram, + greater_or_equal, + ), + (Parameters::Null, Parameters::Identifier(s)) => apply_operator( + Parameters::Identifier(s), + Parameters::Null, + ram, + greater_or_equal, + ), + (Parameters::Identifier(s), Parameters::Null) => apply_operator( + Parameters::Identifier(s), + Parameters::Null, + ram, + greater_or_equal, + ), + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + greater_or_equal, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + greater_or_equal, + ), + (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( + Parameters::Int(i), + Parameters::Identifier(s), + ram, + greater_or_equal, + ), + (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( + Parameters::Identifier(s), + Parameters::Float(i), + ram, + greater_or_equal, + ), + (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( + Parameters::Float(i), + Parameters::Identifier(s), + ram, + greater_or_equal, + ), + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater_or_equal) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, greater_or_equal) + } + + _ => Parameters::Identifier( + "@Those two values are incompatible with the >= operator".to_string(), + ), + } +} + +pub fn lesser_or_equal( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(_)) => Bool(false), + (Parameters::Null, Parameters::Float(_)) => Bool(false), + (Parameters::Int(_), Parameters::Null) => Bool(false), + (Parameters::Float(_), Parameters::Null) => Bool(false), + (Parameters::Int(v), Parameters::Int(v2)) => Bool(v <= v2), + (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) <= f), + (Parameters::Float(v), Parameters::Float(f)) => Bool(v <= f), + (Parameters::Float(v), Parameters::Int(i1)) => Bool(v <= (i1 as f64)), + (Bool(b), Parameters::Int(_)) => Bool(b), + (Bool(b), Parameters::Float(_)) => Bool(b), + (Parameters::Int(_), Bool(b)) => Bool(b), + (Parameters::Float(_), Bool(b)) => Bool(b), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b == b2), + + (Parameters::Rational(_), Parameters::Null) => Bool(true), + (Parameters::Null, Parameters::Rational(_)) => Bool(true), + (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s <= s2), + (Parameters::Rational(s), Parameters::Int(i)) => Bool(s <= Rationals::new(1, i)), + (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) <= s), + (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() <= f), + (Parameters::Float(f), Parameters::Rational(s)) => Bool(f <= s.approx()), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + lesser_or_equal, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( + Parameters::Identifier(s), + Parameters::Int(i), + ram, + lesser_or_equal, + ), + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + lesser_or_equal, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + lesser_or_equal, + ), + (Parameters::Null, Parameters::Identifier(s)) => apply_operator( + Parameters::Identifier(s), + Parameters::Null, + ram, + lesser_or_equal, + ), + (Parameters::Identifier(s), Parameters::Null) => apply_operator( + Parameters::Identifier(s), + Parameters::Null, + ram, + lesser_or_equal, + ), + (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( + Parameters::Int(i), + Parameters::Identifier(s), + ram, + lesser_or_equal, + ), + (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( + Parameters::Identifier(s), + Parameters::Float(i), + ram, + lesser_or_equal, + ), + (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( + Parameters::Float(i), + Parameters::Identifier(s), + ram, + lesser_or_equal, + ), + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser_or_equal) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser_or_equal) + } + + _ => Parameters::Identifier( + "@Those two values are incompatible with the <= operator".to_string(), + ), + } +} + +pub fn equal( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(_)) => Bool(true), + (Parameters::Null, Parameters::Float(_)) => Bool(true), + (Parameters::Int(_), Parameters::Null) => Bool(true), + (Parameters::Float(_), Parameters::Null) => Bool(true), + (Parameters::Int(v), Parameters::Int(v2)) => Bool(v == v2), + (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) == f), + (Parameters::Float(v), Parameters::Float(f)) => Bool(v == f), + (Parameters::Float(v), Parameters::Int(i1)) => Bool(v == (i1 as f64)), + (Bool(_), Parameters::Int(_)) => Bool(false), + (Bool(_), Parameters::Float(_)) => Bool(false), + (Parameters::Int(_), Bool(_)) => Bool(false), + (Parameters::Float(_), Bool(_)) => Bool(false), + (Bool(_), Parameters::Null) => Bool(false), + (Parameters::Null, Bool(_)) => Bool(false), + (Bool(b), Bool(b2)) => Bool(b == b2), + + (Parameters::Rational(_), Parameters::Null) => Bool(true), + (Parameters::Null, Parameters::Rational(_)) => Bool(true), + (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s == s2), + (Parameters::Rational(s), Parameters::Int(i)) => Bool(s == Rationals::new(1, i)), + (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) == s), + (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() == f), + (Parameters::Float(f), Parameters::Rational(s)) => Bool(f == s.approx()), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + equal, + ), + (Parameters::Identifier(s), Parameters::Int(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, equal) + } + (Parameters::Null, Parameters::Identifier(s)) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) + } + (Parameters::Identifier(s), Parameters::Null) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) + } + (Parameters::Int(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, equal) + } + (Parameters::Identifier(s), Parameters::Float(i)) => { + apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, equal) + } + (Parameters::Float(i), Parameters::Identifier(s)) => { + apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, equal) + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, equal) + } + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, equal) + } + + (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + equal, + ), + (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + equal, + ), + + _ => Parameters::Identifier( + "@Those two values are incompatible with the == operator".to_string(), + ), + } +} + +pub fn not( + i: Parameters, + _i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match i { + Bool(b) => Bool(!b), + Parameters::Identifier(s) => { + apply_operator(Parameters::Identifier(s), Parameters::Null, ram, not) + } + _ => Bool(false), + } +} + +pub fn and(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { + match (i, i2) { + (Bool(b), Bool(b2)) => Bool(b && b2), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, and) + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, and) + } + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + and, + ), + _ => Bool(false), + } +} + +pub fn or(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { + match (i, i2) { + (Bool(b), Bool(b2)) => Bool(b || b2), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Parameters::Identifier(s), Bool(b)) => { + apply_operator(Parameters::Identifier(s), Bool(b), ram, or) + } + (Bool(b), Parameters::Identifier(s)) => { + apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, or) + } + (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + or, + ), + _ => Bool(false), + } +} + +#[cfg(test)] +mod test { + use crate::interpreting::function::{add, divide, minus, mult}; + use crate::parsing::ast::Parameters; + + #[test] + pub fn test_add_null() { + let expected = Parameters::Int(1); + let result = add(Parameters::Int(1), Parameters::Null, None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_add_simple() { + let expected = Parameters::Int(2); + let result = add(Parameters::Int(1), Parameters::Int(1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_add_float() { + let expected = Parameters::Float(2.1); + let result = add(Parameters::Float(0.1), Parameters::Float(2.0), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_add_int_float() { + let expected = Parameters::Float(2.1); + let result = add(Parameters::Int(2), Parameters::Float(0.1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_add_float_int() { + let expected = Parameters::Float(2.1); + let result = add(Parameters::Float(0.1), Parameters::Int(2), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_null() { + let expected = Parameters::Int(-1); + let result = minus(Parameters::Int(1), Parameters::Null, None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_null_rev() { + let expected = Parameters::Int(-1); + let result = minus(Parameters::Null, Parameters::Int(1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_simple() { + let expected = Parameters::Int(0); + let result = minus(Parameters::Int(1), Parameters::Int(1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_float() { + let expected = Parameters::Float(1.9); + let result = minus(Parameters::Float(2.0), Parameters::Float(0.1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_int_float() { + let expected = Parameters::Float(1.9); + let result = minus(Parameters::Int(2), Parameters::Float(0.1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_minus_float_int() { + let expected = Parameters::Float(-1.9); + let result = minus(Parameters::Float(0.1), Parameters::Int(2), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_mult_null() { + let expected = Parameters::Int(1); + let result = mult(Parameters::Int(1), Parameters::Null, None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_mult_simple() { + let expected = Parameters::Int(2); + let result = mult(Parameters::Int(1), Parameters::Int(2), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_mult_float() { + let expected = Parameters::Float(0.2); + let result = mult(Parameters::Float(0.1), Parameters::Float(2.0), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_mult_int_float() { + let expected = Parameters::Float(0.2); + let result = mult(Parameters::Int(2), Parameters::Float(0.1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_mult_float_int() { + let expected = Parameters::Float(0.2); + let result = mult(Parameters::Float(0.1), Parameters::Int(2), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_divide_null() { + let expected = Parameters::Int(1); + let result = divide(Parameters::Int(1), Parameters::Null, None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_divide_simple() { + let expected = + Parameters::Rational(crate::exact_math::rationals::Rationals { under: 1, over: 1 }); + let result = divide(Parameters::Int(1), Parameters::Int(1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_divide_float() { + let expected = Parameters::Float(0.05); + let result = divide(Parameters::Float(0.1), Parameters::Float(2.0), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_divide_int_float() { + let expected = Parameters::Float(20.0); + let result = divide(Parameters::Int(2), Parameters::Float(0.1), None); + assert_eq!(result, expected); + } + + #[test] + pub fn test_divide_float_int() { + let expected = Parameters::Float(0.05); + let result = divide(Parameters::Float(0.1), Parameters::Int(2), None); + assert_eq!(result, expected); + } +} diff --git a/src/functions/minus.rs b/src/functions/minus.rs new file mode 100644 index 0000000..208044c --- /dev/null +++ b/src/functions/minus.rs @@ -0,0 +1,445 @@ +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::functions::function::apply_operator; +use crate::functions::function::apply_operator_reverse; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::*; +use std::collections::HashMap; + +pub fn minus( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(v)) => Parameters::Int(-v), + (Parameters::Null, Parameters::Float(f)) => Parameters::Float(-f), + (Parameters::Int(v), Parameters::Null) => Parameters::Int(-v), + (Parameters::Float(f), Parameters::Null) => Parameters::Float(-f), + (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v - v2), + + (Parameters::Rational(s), Parameters::Null) => { + Parameters::Rational(Rationals::new(1, 0) - s) + } + + (Parameters::Null, Parameters::Rational(s)) => { + Parameters::Rational(Rationals::new(1, 0) - s) + } + (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s - s2), + (Parameters::Rational(s), Parameters::Int(i)) => { + Parameters::Rational(s - Rationals::new(1, i)) + } + (Parameters::Int(i), Parameters::Rational(s)) => { + Parameters::Rational(Rationals::new(1, i) - s) + } + (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() - f), + (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f - s.approx()), + (Parameters::InterpreterVector(vec), Parameters::Null) => { + let mut res = Vec::new(); + vec.into_iter() + .map(|x| minus(Parameters::Null, x.clone(), ram)) + .for_each(|z| res.push(z)); + Parameters::InterpreterVector(Box::from(res)) + } + + (Parameters::Null, Parameters::InterpreterVector(vec)) => { + let mut res = Vec::new(); + vec.into_iter() + .map(|x| minus(Parameters::Null, x.clone(), ram)) + .for_each(|z| res.push(z)); + Parameters::InterpreterVector(Box::from(res)) + } + + (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + let mut res = Vec::new(); + vec.into_iter() + .zip(vec2.into_iter()) + .map(|(x, y)| minus(x.clone(), y.clone(), ram)) + .for_each(|z| res.push(z)); + Parameters::InterpreterVector(Box::from(res)) + } + (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) - f), + (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v - f), + (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v - (i1 as f64)), + + (Bool(_), Parameters::Int(i)) => Parameters::Int(i), + (Bool(_), Parameters::Float(i)) => Parameters::Float(i), + (Parameters::Int(i), Bool(_)) => Parameters::Int(i), + (Parameters::Float(i), Bool(_)) => Parameters::Float(i), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s != s2 { + Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s2)), + ) + } else { + Parameters::Int(0) + } + } + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + minus, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Identifier(s.clone())), + Box::from(Parameters::Int(-i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Null), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Null), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + }, + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, ss)), + ), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + minus, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, ss)), + Box::from(Parameters::Rational(s.opposite())), + ), + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + minus, + ), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), + ), + Some(_) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); + match v { + Parameters::Int(i) => Parameters::Int(-i), + _ => Parameters::Null, + } + } + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), + Box::from(Parameters::Float(-i)), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Plus( + Box::from(Parameters::Float(i)), + Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), + ), + Some(_) => { + let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); + match v { + Parameters::Float(i) => Parameters::Float(-i), + _ => Parameters::Null, + } + } + }, + + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator_reverse( + Parameters::InterpreterVector(vec.clone()), + Parameters::Identifier(s), + ram, + minus, + ), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + minus, + ), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, minus), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, minus), + }, + (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), *s3.clone(), ram)), + Box::from(minus(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(*s1.clone(), *s4.clone(), ram)), + Box::from(minus(*s2.clone(), *s3.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Identifier(s3.clone()), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Identifier(s3.clone()), *s2.clone(), ram)), + ); + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Int(i), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(i), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Int(i), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Float(f), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Float(f), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Float(f), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = Parameters::Plus( + Box::from(minus(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus(Parameters::Rational(r.clone()), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Rational(r.clone()), *s2.clone(), ram)), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + if z == z1 && y == y1 { + Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x1.clone(), ram)), + y1.clone(), + z1.clone(), + )), + ) + } + } + + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(minus(*x.clone(), Parameters::Int(1), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(-1)), + 1, + s.clone(), + )), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s && y == 1 { + Parameters::Var(Box::from(minus(Parameters::Int(1), *x.clone(), ram)), y, z) + } else { + Parameters::Plus( + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y.clone(), + z.clone(), + )), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Int(-i)), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Float(-f)), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Var(x, y, z)), + Box::from(Parameters::Rational(r.opposite())), + ), + _ => Parameters::Identifier( + "Those two values are incompatible with the - operator".to_string(), + ), + } +} diff --git a/src/functions/mod.rs b/src/functions/mod.rs new file mode 100644 index 0000000..3742128 --- /dev/null +++ b/src/functions/mod.rs @@ -0,0 +1,6 @@ +pub mod add; +pub mod divide; +pub mod expo; +pub mod function; +pub mod minus; +pub mod mult; diff --git a/src/functions/mult.rs b/src/functions/mult.rs new file mode 100644 index 0000000..8d7d675 --- /dev/null +++ b/src/functions/mult.rs @@ -0,0 +1,224 @@ +use crate::exact_math::rationals::Rationals; +use crate::exact_math::symbolic::size; +use crate::functions::add::add; +use crate::functions::function::apply_operator; +use crate::functions::function::apply_operator_reverse; +use crate::parsing::ast::Parameters; +use crate::parsing::ast::Parameters::*; +use crate::utils::matrix_utils::*; +use std::collections::HashMap; + +pub fn mult( + i: Parameters, + i2: Parameters, + ram: Option<&HashMap>, +) -> Parameters { + match (i, i2) { + (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), + (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), + (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), + (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v * v2), + (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) * f), + (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v * f), + (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v * (i1 as f64)), + + (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), + (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), + (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s * s2), + (Parameters::Rational(s), Parameters::Int(i)) => { + Parameters::Rational(s * Rationals::new(1, i)) + } + (Parameters::Int(i), Parameters::Rational(s)) => { + Parameters::Rational(s * Rationals::new(1, i)) + } + (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() * f), + (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f * s.approx()), + (Parameters::Null, Parameters::InterpreterVector(vec)) => { + Parameters::InterpreterVector(vec.clone()) + } + (Parameters::InterpreterVector(vec), Parameters::Null) => { + Parameters::InterpreterVector(vec.clone()) + } + (Parameters::InterpreterVector(vec), Parameters::Int(v)) => { + let mut result = Vec::new(); + vec.into_iter() + .map(|x| mult(x.clone(), Parameters::Int(v), ram)) + .for_each(|x| result.push(x)); + Parameters::InterpreterVector(Box::from(result)) + } + (Parameters::Int(v), Parameters::InterpreterVector(vec)) => { + let mut result = Vec::new(); + vec.into_iter() + .map(|x| mult(x.clone(), Parameters::Int(v), ram)) + .for_each(|x| result.push(x)); + Parameters::InterpreterVector(Box::from(result)) + } + (Parameters::InterpreterVector(vec), Parameters::Float(v)) => { + let mut result = Vec::new(); + vec.into_iter() + .map(|x| mult(x.clone(), Parameters::Float(v), ram)) + .for_each(|x| result.push(x)); + Parameters::InterpreterVector(Box::from(result)) + } + (Parameters::Float(v), Parameters::InterpreterVector(vec)) => { + let mut result = Vec::new(); + vec.into_iter() + .map(|x| mult(x.clone(), Parameters::Float(v), ram)) + .for_each(|x| result.push(x)); + Parameters::InterpreterVector(Box::from(result)) + } + + (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + let mut res1 = Vec::new(); + let mut is_matrix = true; + let mut res = Vec::new(); + let mut res2 = Vec::new(); + + vec.clone().into_iter().for_each(|x| match x { + Parameters::InterpreterVector(l) => res.push(l.to_vec()), + p => { + is_matrix = false; + res1.push(p); + } + }); + vec2.clone().into_iter().for_each(|x| match x { + Parameters::InterpreterVector(l) => res2.push(l.to_vec()), + _ => { + is_matrix = false; + } + }); + + if !is_matrix { + let mut sum = Parameters::Null; + (*vec) + .into_iter() + .zip(vec2.into_iter()) + .map(|(a, b)| mult(a.clone(), b.clone(), ram)) + .for_each(|x| sum = add(sum.clone(), x, ram)); + + match sum { + Parameters::Int(i) => Parameters::Int(i), + Parameters::Float(f) => Parameters::Float(f), + _ => Parameters::Float(f64::NAN), + } + } else { + let matrix_result = mult_matrix(res, res2, ram); + + let mut res = Vec::new(); + + if matrix_result.len() == 0 { + return Parameters::Null; + } + + matrix_result + .into_iter() + .for_each(|x| res.push(Parameters::InterpreterVector(Box::from(x)))); + + Parameters::InterpreterVector(Box::from(res)) + } + } + + (Bool(_), Parameters::Int(i)) => Parameters::Int(i), + (Bool(_), Parameters::Float(i)) => Parameters::Float(i), + (Parameters::Int(i), Bool(_)) => Parameters::Int(i), + (Parameters::Float(i), Bool(_)) => Parameters::Float(i), + (Bool(b), Parameters::Null) => Bool(b), + (Parameters::Null, Bool(b)) => Bool(b), + (Bool(b), Bool(b2)) => Bool(b && b2), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => { + if s == s2 { + Parameters::Var(Box::from(Parameters::Int(1)), 2, s.clone()) + } else { + Parameters::Mul( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(1)), + 1, + s2.clone(), + )), + ) + } + } + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + mult, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), + }, + + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + mult, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + mult, + ), + None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), + }, + (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::InterpreterVector(vec.clone()), + ram, + mult, + ), + }, + (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { + None => Parameters::InterpreterVector(vec.clone()), + Some(_) => apply_operator_reverse( + Parameters::InterpreterVector(vec.clone()), + Parameters::Identifier(s), + ram, + mult, + ), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), + None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), + None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, mult), + None => Parameters::Bool(b), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, mult), + None => Parameters::Bool(b), + }, + _ => Parameters::Identifier( + "@Those two values are incompatible with the * operator".to_string(), + ), + } +} diff --git a/src/interpreting/function.rs b/src/interpreting/function.rs deleted file mode 100644 index f812c59..0000000 --- a/src/interpreting/function.rs +++ /dev/null @@ -1,1922 +0,0 @@ -use std::collections::HashMap; - -use crate::exact_math::rationals::Rationals; -use crate::exact_math::symbolic::size; -use crate::parsing::ast::Parameters; -use crate::parsing::ast::Parameters::Bool; -use crate::utils::matrix_utils::mult_matrix; - -pub fn apply_operator( - value: Parameters, - value2: Parameters, - ram: Option<&HashMap>, - f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, -) -> Parameters { - let s = match value { - Parameters::Identifier(ref s) => s, - _ => "", - }; - if s == "" { - return Parameters::Null; - } - match ram { - None => f(value.clone(), value2.clone(), None), - Some(i_ram) => { - let values = i_ram.get(s); - match values { - None => f(value.clone(), value2.clone(), None), - Some(val) => f(val.clone(), value2.clone(), ram), - } - } - } -} - -pub fn apply_operator_reverse( - value: Parameters, - value2: Parameters, - ram: Option<&HashMap>, - f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, -) -> Parameters { - let s = match value2 { - Parameters::Identifier(ref s) => s, - _ => "", - }; - if s == "" { - return Parameters::Null; - } - match ram { - None => f(value.clone(), value2.clone(), None), - Some(i_ram) => { - let val3 = i_ram.get(s); - match val3 { - None => f(value.clone(), value2.clone(), None), - Some(val) => f(value.clone(), val.clone(), ram), - } - } - } -} - -pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s + s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s + Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(s + Rationals::new(1, i)) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() + f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f + s.approx()), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v + v2), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) + f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v + f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v + (i1 as f64)), - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { - let mut res = Vec::new(); - vec.into_iter() - .zip(vec2.into_iter()) - .map(|(x, y)| add(x.clone(), y.clone(), ram)) - .for_each(|s| res.push(s)); - Parameters::InterpreterVector(Box::from(res)) - } - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { - None => { - if s != s2 { - Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Identifier(s2.clone())), - ) - } else { - Parameters::Var(Box::from(Parameters::Int(2)), 1, s.clone()) - } - } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - add, - ), - }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Int(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), - }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Identifier(s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), - }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Rational(s.clone())), - Box::from(Parameters::Identifier(ss.clone())), - ), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - add, - ), - }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(ss.clone())), - Box::from(Parameters::Rational(s.clone())), - ), - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - add, - ), - }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Identifier(s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), - }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s)), - Box::from(Parameters::Int(i.clone())), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), - }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Float(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), - }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Float(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), - }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), - }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), - }, - (Bool(b), Parameters::Identifier(s)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, add), - }, - (Parameters::Identifier(s), Bool(b)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), - }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), *s3.clone(), ram)), - Box::from(add(*s2.clone(), *s4.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(add(*s1.clone(), *s4.clone(), ram)), - Box::from(add(*s2.clone(), *s3.clone(), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - - if s1 > s2 { - second - } else { - first - } - } - (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), - ); - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), - ); - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Int(i), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Int(i), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Int(i)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Int(i), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { - if z == z1 && y == y1 { - Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(x1.clone(), y1.clone(), z1.clone())), - ) - } - } - - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { - if z == s && y == 1 { - Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - ) - } - } - - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { - if z == s && y == 1 { - Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - ) - } - } - - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var(x, y, z)), - ), - - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Int(i)), - ), - - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Var(x, y, z)), - ), - - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Float(f)), - ), - - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), - Box::from(Parameters::Var(x, y, z)), - ), - - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Rational(r.clone())), - ), - _ => Parameters::Identifier( - "@Those two values are incompatible with the + operator".to_string(), - ), - } -} - -pub fn minus( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(-v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(-f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(-v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(-f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v - v2), - - (Parameters::Rational(s), Parameters::Null) => { - Parameters::Rational(Rationals::new(1, 0) - s) - } - - (Parameters::Null, Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, 0) - s) - } - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s - s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s - Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, i) - s) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() - f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f - s.approx()), - (Parameters::InterpreterVector(vec), Parameters::Null) => { - let mut res = Vec::new(); - vec.into_iter() - .map(|x| minus(Parameters::Null, x.clone(), ram)) - .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) - } - - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - let mut res = Vec::new(); - vec.into_iter() - .map(|x| minus(Parameters::Null, x.clone(), ram)) - .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) - } - - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { - let mut res = Vec::new(); - vec.into_iter() - .zip(vec2.into_iter()) - .map(|(x, y)| minus(x.clone(), y.clone(), ram)) - .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) - } - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) - f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v - f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v - (i1 as f64)), - - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { - None => { - if s != s2 { - Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s2)), - ) - } else { - Parameters::Int(0) - } - } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - minus, - ), - }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Int(-i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus), - }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Null), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), - }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Null), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), - }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Rational(s.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, ss)), - ), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - minus, - ), - }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, ss)), - Box::from(Parameters::Rational(s.opposite())), - ), - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - minus, - ), - }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), - Some(_) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); - match v { - Parameters::Int(i) => Parameters::Int(-i), - _ => Parameters::Null, - } - } - }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), - Box::from(Parameters::Float(-i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus), - }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Float(i)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), - Some(_) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); - match v { - Parameters::Float(i) => Parameters::Float(-i), - _ => Parameters::Null, - } - } - }, - - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - minus, - ), - }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - minus, - ), - }, - (Bool(b), Parameters::Identifier(s)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, minus), - }, - (Parameters::Identifier(s), Bool(b)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, minus), - }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), *s3.clone(), ram)), - Box::from(minus(*s2.clone(), *s4.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(minus(*s1.clone(), *s4.clone(), ram)), - Box::from(minus(*s2.clone(), *s3.clone(), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - - if s1 > s2 { - second - } else { - first - } - } - (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), - ); - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Identifier(s3.clone()), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Identifier(s3.clone()), *s2.clone(), ram)), - ); - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Int(i), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Int(i), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(i), *s2.clone(), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Int(i)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Int(i), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Float(f), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Float(f), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Float(f), *s2.clone(), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Rational(r.clone()), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Rational(r.clone()), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Rational(r.clone()), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), - ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Rational(r.clone()), *s2.clone(), ram)), - ); - - let (s1, s2) = (size(&first), size(&second)); - if s1 > s2 { - second - } else { - first - } - } - - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { - if z == z1 && y == y1 { - Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x1.clone(), ram)), - y1.clone(), - z1.clone(), - )), - ) - } - } - - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { - if z == s && y == 1 { - Parameters::Var(Box::from(minus(*x.clone(), Parameters::Int(1), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(-1)), - 1, - s.clone(), - )), - ) - } - } - - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { - if z == s && y == 1 { - Parameters::Var(Box::from(minus(Parameters::Int(1), *x.clone(), ram)), y, z) - } else { - Parameters::Plus( - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y.clone(), - z.clone(), - )), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - ) - } - } - - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), - ), - - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Int(-i)), - ), - - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), - ), - - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Float(-f)), - ), - - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), - ), - - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Rational(r.opposite())), - ), - _ => Parameters::Identifier( - "Those two values are incompatible with the - operator".to_string(), - ), - } -} - -pub fn mult( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v * v2), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) * f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v * f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v * (i1 as f64)), - - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s * s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s * Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(s * Rationals::new(1, i)) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() * f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f * s.approx()), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Int(v)) => { - let mut result = Vec::new(); - vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Int(v), ram)) - .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) - } - (Parameters::Int(v), Parameters::InterpreterVector(vec)) => { - let mut result = Vec::new(); - vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Int(v), ram)) - .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) - } - (Parameters::InterpreterVector(vec), Parameters::Float(v)) => { - let mut result = Vec::new(); - vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Float(v), ram)) - .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) - } - (Parameters::Float(v), Parameters::InterpreterVector(vec)) => { - let mut result = Vec::new(); - vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Float(v), ram)) - .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) - } - - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { - let mut res1 = Vec::new(); - let mut is_matrix = true; - let mut res = Vec::new(); - let mut res2 = Vec::new(); - - vec.clone().into_iter().for_each(|x| match x { - Parameters::InterpreterVector(l) => res.push(l.to_vec()), - p => { - is_matrix = false; - res1.push(p); - } - }); - vec2.clone().into_iter().for_each(|x| match x { - Parameters::InterpreterVector(l) => res2.push(l.to_vec()), - _ => { - is_matrix = false; - } - }); - - if !is_matrix { - let mut sum = Parameters::Null; - (*vec) - .into_iter() - .zip(vec2.into_iter()) - .map(|(a, b)| mult(a.clone(), b.clone(), ram)) - .for_each(|x| sum = add(sum.clone(), x, ram)); - - match sum { - Parameters::Int(i) => Parameters::Int(i), - Parameters::Float(f) => Parameters::Float(f), - _ => Parameters::Float(f64::NAN), - } - } else { - let matrix_result = mult_matrix(res, res2, ram); - - let mut res = Vec::new(); - - if matrix_result.len() == 0 { - return Parameters::Null; - } - - matrix_result - .into_iter() - .for_each(|x| res.push(Parameters::InterpreterVector(Box::from(x)))); - - Parameters::InterpreterVector(Box::from(res)) - } - } - - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { - None => { - if s == s2 { - Parameters::Var(Box::from(Parameters::Int(1)), 2, s.clone()) - } else { - Parameters::Mul( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(1)), - 1, - s2.clone(), - )), - ) - } - } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - mult, - ), - }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), - }, - - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - mult, - ), - }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - mult, - ), - None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), - }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), - }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - mult, - ), - }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - mult, - ), - }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), - }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), - }, - (Bool(b), Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, mult), - None => Parameters::Bool(b), - }, - (Parameters::Identifier(s), Bool(b)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, mult), - None => Parameters::Bool(b), - }, - _ => Parameters::Identifier( - "@Those two values are incompatible with the * operator".to_string(), - ), - } -} - -pub fn divide( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Rational(Rationals::new(v2, v)), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) / f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v / f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v / (i1 as f64)), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - - (Parameters::Rational(s), Parameters::Null) => { - Parameters::Rational(Rationals::new(1, 1) / s) - } - (Parameters::Null, Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, 1) / s) - } - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s / s2), - - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s / Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, i) / s) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() / f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f / s.approx()), - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - divide, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - divide, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - divide, - ), - - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide); - match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => Parameters::Null, - } - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide); - match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => Parameters::Null, - } - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, divide) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, divide) - } - _ => Parameters::Identifier( - "@Those two values are incompatible with the / operator".to_string(), - ), - } -} - -pub fn expo( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Float((v as f64).powf(v2 as f64)), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64).powf(f)), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v.powf(f)), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v.powf(i1 as f64)), - - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => { - Parameters::Float(s.approx().powf(s2.approx())) - } - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Float(s.approx().powf(i as f64)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Float((i as f64).powf(s.approx())) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx().powf(f)), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f.powf(s.approx())), - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - expo, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, expo) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, expo) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo) - } - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - expo, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - expo, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, expo) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, expo) - } - - _ => Parameters::Identifier( - "@Those two values are incompatible with the ^ operator".to_string(), - ), - } -} - -pub fn assign(s: Parameters, s2: Parameters) -> (String, Parameters) { - match s { - Parameters::Identifier(s) => (s, s2), - _ => ("".to_string(), s2), - } -} - -pub fn greater( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v > v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) > f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v > f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v > (i1 as f64)), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s > s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s > Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) > s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() > f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f > s.approx()), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - greater, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - greater, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - greater, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, greater) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, greater) - } - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - greater, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), - ram, - greater, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, greater) - } - - _ => Parameters::Identifier( - "@Those two values are incompatible with the > operator".to_string(), - ), - } -} - -pub fn lesser( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(false), - (Parameters::Null, Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Parameters::Null) => Bool(false), - (Parameters::Float(_), Parameters::Null) => Bool(false), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v < v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) < f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v < f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v < (i1 as f64)), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s < s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s < Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) < s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() < f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f < s.approx()), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - lesser, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, lesser) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) - } - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - lesser, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - lesser, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, lesser) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, lesser) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, lesser) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser) - } - - _ => Parameters::Identifier( - "@Those two values are incompatible with the < operator".to_string(), - ), - } -} - -pub fn greater_or_equal( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v >= v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) >= f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v >= f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v >= (i1 as f64)), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b == b2), - - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s >= s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s >= Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) >= s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() >= f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f >= s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Int(i), - ram, - greater_or_equal, - ), - (Parameters::Null, Parameters::Identifier(s)) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Null) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - greater_or_equal, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - greater_or_equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - greater_or_equal, - ), - (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Int(i), - Parameters::Identifier(s), - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - greater_or_equal, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), - ram, - greater_or_equal, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater_or_equal) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, greater_or_equal) - } - - _ => Parameters::Identifier( - "@Those two values are incompatible with the >= operator".to_string(), - ), - } -} - -pub fn lesser_or_equal( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(false), - (Parameters::Null, Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Parameters::Null) => Bool(false), - (Parameters::Float(_), Parameters::Null) => Bool(false), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v <= v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) <= f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v <= f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v <= (i1 as f64)), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Bool(b), Bool(b2)) => Bool(b == b2), - - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s <= s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s <= Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) <= s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() <= f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f <= s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Int(i), - ram, - lesser_or_equal, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - lesser_or_equal, - ), - (Parameters::Null, Parameters::Identifier(s)) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Null) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - lesser_or_equal, - ), - (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Int(i), - Parameters::Identifier(s), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - lesser_or_equal, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), - ram, - lesser_or_equal, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser_or_equal) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser_or_equal) - } - - _ => Parameters::Identifier( - "@Those two values are incompatible with the <= operator".to_string(), - ), - } -} - -pub fn equal( - i: Parameters, - i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v == v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) == f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v == f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v == (i1 as f64)), - (Bool(_), Parameters::Int(_)) => Bool(false), - (Bool(_), Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Bool(_)) => Bool(false), - (Parameters::Float(_), Bool(_)) => Bool(false), - (Bool(_), Parameters::Null) => Bool(false), - (Parameters::Null, Bool(_)) => Bool(false), - (Bool(b), Bool(b2)) => Bool(b == b2), - - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s == s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s == Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) == s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() == f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f == s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, equal) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, equal) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, equal) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, equal) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, equal) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, equal) - } - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - equal, - ), - - _ => Parameters::Identifier( - "@Those two values are incompatible with the == operator".to_string(), - ), - } -} - -pub fn not( - i: Parameters, - _i2: Parameters, - ram: Option<&HashMap>, -) -> Parameters { - match i { - Bool(b) => Bool(!b), - Parameters::Identifier(s) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, not) - } - _ => Bool(false), - } -} - -pub fn and(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { - match (i, i2) { - (Bool(b), Bool(b2)) => Bool(b && b2), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, and) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, and) - } - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - and, - ), - _ => Bool(false), - } -} - -pub fn or(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { - match (i, i2) { - (Bool(b), Bool(b2)) => Bool(b || b2), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, or) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, or) - } - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - or, - ), - _ => Bool(false), - } -} - -#[cfg(test)] -mod test { - use crate::interpreting::function::{add, divide, minus, mult}; - use crate::parsing::ast::Parameters; - - #[test] - pub fn test_add_null() { - let expected = Parameters::Int(1); - let result = add(Parameters::Int(1), Parameters::Null, None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_add_simple() { - let expected = Parameters::Int(2); - let result = add(Parameters::Int(1), Parameters::Int(1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_add_float() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Float(0.1), Parameters::Float(2.0), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_add_int_float() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Int(2), Parameters::Float(0.1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_add_float_int() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Float(0.1), Parameters::Int(2), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_null() { - let expected = Parameters::Int(-1); - let result = minus(Parameters::Int(1), Parameters::Null, None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_null_rev() { - let expected = Parameters::Int(-1); - let result = minus(Parameters::Null, Parameters::Int(1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_simple() { - let expected = Parameters::Int(0); - let result = minus(Parameters::Int(1), Parameters::Int(1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_float() { - let expected = Parameters::Float(1.9); - let result = minus(Parameters::Float(2.0), Parameters::Float(0.1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_int_float() { - let expected = Parameters::Float(1.9); - let result = minus(Parameters::Int(2), Parameters::Float(0.1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_minus_float_int() { - let expected = Parameters::Float(-1.9); - let result = minus(Parameters::Float(0.1), Parameters::Int(2), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_mult_null() { - let expected = Parameters::Int(1); - let result = mult(Parameters::Int(1), Parameters::Null, None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_mult_simple() { - let expected = Parameters::Int(2); - let result = mult(Parameters::Int(1), Parameters::Int(2), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_mult_float() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Float(0.1), Parameters::Float(2.0), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_mult_int_float() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Int(2), Parameters::Float(0.1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_mult_float_int() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Float(0.1), Parameters::Int(2), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_divide_null() { - let expected = Parameters::Int(1); - let result = divide(Parameters::Int(1), Parameters::Null, None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_divide_simple() { - let expected = - Parameters::Rational(crate::exact_math::rationals::Rationals { under: 1, over: 1 }); - let result = divide(Parameters::Int(1), Parameters::Int(1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_divide_float() { - let expected = Parameters::Float(0.05); - let result = divide(Parameters::Float(0.1), Parameters::Float(2.0), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_divide_int_float() { - let expected = Parameters::Float(20.0); - let result = divide(Parameters::Int(2), Parameters::Float(0.1), None); - assert_eq!(result, expected); - } - - #[test] - pub fn test_divide_float_int() { - let expected = Parameters::Float(0.05); - let result = divide(Parameters::Float(0.1), Parameters::Int(2), None); - assert_eq!(result, expected); - } -} diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index 27c9ec2..c23c5ed 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -1,10 +1,12 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; -use crate::interpreting::function::{ - add, and, assign, divide, equal, expo, greater, greater_or_equal, lesser, lesser_or_equal, - minus, mult, not, or, -}; +use crate::functions::add::add; +use crate::functions::divide::divide; +use crate::functions::expo::expo; +use crate::functions::function::*; +use crate::functions::minus::minus; +use crate::functions::mult::mult; use crate::interpreting::stdlib::exec; use crate::parsing::ast::{Ast, Parameters}; diff --git a/src/interpreting/mod.rs b/src/interpreting/mod.rs index 7468fc8..4900225 100644 --- a/src/interpreting/mod.rs +++ b/src/interpreting/mod.rs @@ -1,3 +1,2 @@ -pub mod function; pub(crate) mod interpreter; pub mod stdlib; diff --git a/src/interpreting/stdlib.rs b/src/interpreting/stdlib.rs index d03e874..8c602c9 100644 --- a/src/interpreting/stdlib.rs +++ b/src/interpreting/stdlib.rs @@ -9,7 +9,8 @@ use crate::parsing::ast::{Ast, Parameters}; use crate::utils::matrix_utils::{lup_decompose, lup_determinant, lup_invert, transpose}; use crate::utils::plot_utils::computes_lines; -use super::function::{add as other_add, mult}; +use crate::functions::add::add as other_add; +use crate::functions::mult::mult; pub fn exec( s: String, diff --git a/src/main.rs b/src/main.rs index 6f3f5dc..10ec470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use std::io::BufRead; mod configuration; mod exact_math; +mod functions; mod interpreting; mod lexing; mod parsing; diff --git a/src/utils/matrix_utils.rs b/src/utils/matrix_utils.rs index b696937..8a1d237 100644 --- a/src/utils/matrix_utils.rs +++ b/src/utils/matrix_utils.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use crate::{ - interpreting::function::{add, divide, greater, minus, mult}, - parsing::ast::Parameters, + functions::add::add, functions::divide::divide, functions::function::*, + functions::minus::minus, functions::mult::mult, parsing::ast::Parameters, }; pub fn transpose(matrix: Vec>) -> Vec> { From ec473b92a32d4cb7f6d1d4a797e5922ba0fed9ae Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Fri, 3 May 2024 20:11:19 +0200 Subject: [PATCH 19/67] fix tests --- src/functions/function.rs | 5 ++++- src/utils/matrix_utils.rs | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 15d2c2b..0c40729 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -555,7 +555,10 @@ pub fn or(i: Parameters, i2: Parameters, ram: Option<&HashMap Date: Sat, 4 May 2024 12:42:59 +0200 Subject: [PATCH 20/67] bump to 2.14.0 --- CHANGELOG.md | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/configuration/loader.rs | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2b97b..fe5cff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Version 2.14.0 : Symbolic calculation! +You can use the calculator to compute and simplify symbolic computation problems +it's compatible with +,-,*,/,^. + # Version 2.13.1 : Bug fix Fix the issue of the function never used (moved implementation to the test module) diff --git a/Cargo.lock b/Cargo.lock index a466ca6..8a273fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,7 +221,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "mini-calc" -version = "2.13.1" +version = "2.14.0" dependencies = [ "ansi_term", "atty", diff --git a/Cargo.toml b/Cargo.toml index 6657409..f41a22b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mini-calc" -version = "2.13.1" +version = "2.14.0" license = "GPL-3.0-or-later" description = "A fully-featured minimalistic configurable rust calculator" homepage = "https://calc.nwa2coco.fr" diff --git a/src/configuration/loader.rs b/src/configuration/loader.rs index f508dc7..1b44b25 100644 --- a/src/configuration/loader.rs +++ b/src/configuration/loader.rs @@ -131,7 +131,7 @@ pub fn load_color(string: String) -> Color { pub fn replace_variable(str: String) -> String { str.replace("%author%", "Charlotte Thomas") - .replace("%version%", "v2.13.1") + .replace("%version%", "v2.14.0") .to_string() } From 6339cb3f671d10f36f4db1e4186021bb8abec8d2 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sat, 4 May 2024 12:44:40 +0200 Subject: [PATCH 21/67] fix main.rs --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 10ec470..5a0bc50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,7 +263,7 @@ fn handle_config(line: &str, config: Config) -> (String, Option) { fn main() { let mut args: Args = env::args(); - let version: String = "v2.13.1".to_string(); + let version: String = "v2.14.0".to_string(); if args.len() > 1 || !atty::is(Stream::Stdin) { let mut a = vec![]; From b53fd7fc3a447ca6811c7467d8b8b6cbdd23cb6e Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 5 May 2024 09:36:58 +0200 Subject: [PATCH 22/67] implements plus,var operations for mult pretty print mult 1/2 --- src/functions/mult.rs | 192 ++++++++++++++++++++++++++++++++++++++++++ src/parsing/ast.rs | 12 ++- 2 files changed, 203 insertions(+), 1 deletion(-) diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 8d7d675..e9e9164 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -8,6 +8,8 @@ use crate::parsing::ast::Parameters::*; use crate::utils::matrix_utils::*; use std::collections::HashMap; +use super::minus::minus; + pub fn mult( i: Parameters, i2: Parameters, @@ -217,6 +219,196 @@ pub fn mult( Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, mult), None => Parameters::Bool(b), }, + (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + let first = Parameters::Plus( + Box::from(add( + mult(*s1.clone(), *s3.clone(), ram), + mult(*s2.clone(), *s3.clone(), ram), + ram, + )), + Box::from(add( + mult(*s1.clone(), *s4.clone(), ram), + mult(*s2.clone(), *s4.clone(), ram), + ram, + )), + ); + let second = Parameters::Plus( + Box::from(add( + mult(*s1.clone(), *s3.clone(), ram), + mult(*s2.clone(), *s4.clone(), ram), + ram, + )), + Box::from(add( + mult(*s2.clone(), *s3.clone(), ram), + mult(*s1.clone(), *s4.clone(), ram), + ram, + )), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + //(s1,s2)*s3 = (s1s3+s2s3) + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = Parameters::Plus( + Box::from(mult( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + ram, + )), + ); + first + } + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(mult( + Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone()), + *s1.clone(), + ram, + )), + Box::from(mult( + Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone()), + *s2.clone(), + ram, + )), + ); + first + } + + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(mult(Parameters::Int(i), *s1.clone(), ram)), + Box::from(mult(Parameters::Int(i), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = Parameters::Plus( + Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), + Box::from(mult(*s2.clone(), Parameters::Int(i), ram)), + ); + first + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(mult(Parameters::Float(f), *s1.clone(), ram)), + Box::from(mult(Parameters::Float(f), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = Parameters::Plus( + Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), + Box::from(mult(*s2.clone(), Parameters::Float(f), ram)), + ); + first + } + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(mult(Parameters::Rational(r.clone()), *s1.clone(), ram)), + Box::from(mult(Parameters::Rational(r.clone()), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = Parameters::Plus( + Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ); + first + } + //x*y : x==y : x^2 else x*y + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + if z == z1 { + Parameters::Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(mult(Parameters::Int(1), *x1.clone(), ram)), + y1.clone(), + z1.clone(), + )), + ) + } + } + + //2x*x + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s { + Parameters::Var(Box::from(x.clone()), y + 1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s { + Parameters::Var(Box::from(x.clone()), y + 1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var( + Box::from(mult(Parameters::Int(1), *x.clone(), ram)), + y.clone(), + z.clone(), + )), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Int(i), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Int(i), ram)), + y, + z.clone(), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(Parameters::Float(f), *x.clone(), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Float(f), ram)), + y, + z.clone(), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(Parameters::Rational(r.clone()), *x.clone(), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Rational(r), ram)), + y, + z.clone(), + ), _ => Parameters::Identifier( "@Those two values are incompatible with the * operator".to_string(), ), diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index dafbc53..da373df 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -170,6 +170,17 @@ impl Parameters { _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), }, + Mul(x, y) => { + let x_printed = x.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + let y_printed = y.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + format!("{x_printed}*{y_printed}") + } Plus(x, y) => { let x_printed = x.pretty_print( Some(ram.as_mut().unwrap()), @@ -190,7 +201,6 @@ impl Parameters { } } } - InterpreterVector(lst) => { let mut vec = Vec::new(); From d751794cc14433fa83f54c26b837721226d5a966 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 5 May 2024 18:31:36 +0200 Subject: [PATCH 23/67] implements operations for mult --- src/functions/mult.rs | 155 +++++++++++++++++++++++++++++++++++++++++- src/parsing/ast.rs | 1 + 2 files changed, 155 insertions(+), 1 deletion(-) diff --git a/src/functions/mult.rs b/src/functions/mult.rs index e9e9164..b96e16a 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -253,7 +253,6 @@ pub fn mult( first } } - //(s1,s2)*s3 = (s1s3+s2s3) (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { let first = Parameters::Plus( Box::from(mult( @@ -331,6 +330,160 @@ pub fn mult( ); first } + + (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => Parameters::Plus( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ), + + (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ), + + (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { + let first = Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Mul( + s1.clone(), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { + let first = Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Mul( + s1.clone(), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { + let first = Parameters::Mul( + Box::from(mult(*s1.clone(), *s3.clone(), ram)), + Box::from(mult(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Mul( + Box::from(mult(*s1.clone(), *s4.clone(), ram)), + Box::from(mult(*s2.clone(), *s3.clone(), ram)), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + ), + + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + Box::from(mult( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), + Box::from(mult(*s2.clone(), Parameters::Int(i), ram)), + ), + + (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), + Box::from(mult(*s2.clone(), Parameters::Int(i), ram)), + ), + + (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), + Box::from(mult(*s2.clone(), Parameters::Float(f), ram)), + ), + + (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), + Box::from(mult(*s2.clone(), Parameters::Float(f), ram)), + ), + + (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ), + + (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + ), //x*y : x==y : x^2 else x*y (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 { diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index da373df..c3c7332 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -181,6 +181,7 @@ impl Parameters { ); format!("{x_printed}*{y_printed}") } + Plus(x, y) => { let x_printed = x.pretty_print( Some(ram.as_mut().unwrap()), From d2477365c0827f664fe3b8d5045d37ba41093ef1 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 5 May 2024 18:52:51 +0200 Subject: [PATCH 24/67] implements mul for add --- src/functions/add.rs | 110 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/functions/add.rs b/src/functions/add.rs index f26f5fb..d5773fd 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -312,6 +312,115 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { + let first = Parameters::Plus( + Box::from(add( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(add( + Parameters::Var(x.clone(), y, z.clone()), + *s1.clone(), + ram, + )), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(add( + Parameters::Var(x.clone(), y, z.clone()), + *s2.clone(), + ram, + )), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + + //(x*y)+(a*b) + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Mul(s3.clone(), s4.clone())), + ), + + (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ), + + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + + (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Int(i)), + ), + + (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + + (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Float(f)), + ), + + (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + + (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Rational(r.clone())), + ), + + (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + + (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y, z.clone())), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + + (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Var(x.clone(), y, z.clone())), + ), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) @@ -374,6 +483,7 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Identifier( "@Those two values are incompatible with the + operator".to_string(), ), From a60efae28cb22d0f9044fcc9df00d76f8819fdf1 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 5 May 2024 19:07:52 +0200 Subject: [PATCH 25/67] implements mult ops for minus --- src/functions/minus.rs | 135 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 208044c..f78a014 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -352,6 +352,141 @@ pub fn minus( } } + (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => { + let first = Parameters::Plus( + Box::from(minus( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Plus( + s1.clone(), + Box::from(minus( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (s1, s2) = (size(&first), size(&second)); + if s1 > s2 { + second + } else { + first + } + } + + (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => { + let first = Parameters::Plus( + Box::from(minus( + Parameters::Var(x.clone(), y, z.clone()), + *s1.clone(), + ram, + )), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ); + let second = Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus( + Parameters::Var(x.clone(), y, z.clone()), + *s2.clone(), + ram, + )), + ); + + let (s1, s2) = (size(&first), size(&second)); + + if s1 > s2 { + second + } else { + first + } + } + + //(x*y)+(a*b) + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s3.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s4.clone(), ram)), + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(-1)), + 1, + s.clone(), + )), + ), + + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Int(-i)), + ), + + (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Float(-f)), + ), + + (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Float(f)), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Rational(r.opposite())), + ), + + (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + )), + ), + + (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Var(x.clone(), y, z.clone())), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + )), + ), + + (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Var( + Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + y, + z.clone(), + )), + ), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) From cd7d2a9612bf4fbcd3bf12dd53ce2cbb2493a502 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 5 May 2024 19:12:47 +0200 Subject: [PATCH 26/67] delete unnecessary imports --- src/functions/function.rs | 3 +-- src/functions/mult.rs | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 0c40729..21348cf 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -1,10 +1,9 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; -use crate::exact_math::symbolic::size; + use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::Bool; -use crate::utils::matrix_utils::mult_matrix; pub fn apply_operator( value: Parameters, diff --git a/src/functions/mult.rs b/src/functions/mult.rs index b96e16a..cc7e760 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -8,8 +8,6 @@ use crate::parsing::ast::Parameters::*; use crate::utils::matrix_utils::*; use std::collections::HashMap; -use super::minus::minus; - pub fn mult( i: Parameters, i2: Parameters, From c1ec0cf7a9c9b40fc35a0e2680e268847e81bce3 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 6 May 2024 08:45:12 +0200 Subject: [PATCH 27/67] correct errors in add,minus and mult --- src/functions/add.rs | 10 ++++++++++ src/functions/minus.rs | 29 ++++++++++++++++++++++------- src/functions/mult.rs | 34 ++++++++++++++++++---------------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index d5773fd..972afc6 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -421,6 +421,16 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Plus(s3.clone(), s4.clone())), + ), + + (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Plus(s3.clone(), s4.clone())), + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + ), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) diff --git a/src/functions/minus.rs b/src/functions/minus.rs index f78a014..10e0c2f 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -405,12 +405,11 @@ pub fn minus( } } - //(x*y)+(a*b) (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( Box::from(Parameters::Mul(s1.clone(), s2.clone())), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s3.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s4.clone(), ram)), + s4.clone(), )), ), @@ -427,7 +426,7 @@ pub fn minus( Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + s2.clone(), )), ), @@ -440,7 +439,7 @@ pub fn minus( Box::from(Parameters::Int(i)), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + s2.clone(), )), ), @@ -453,7 +452,7 @@ pub fn minus( Box::from(Parameters::Float(f)), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + s2.clone(), )), ), @@ -466,7 +465,7 @@ pub fn minus( Box::from(Parameters::Rational(r.clone())), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + s2.clone(), )), ), @@ -474,7 +473,7 @@ pub fn minus( Box::from(Parameters::Var(x.clone(), y, z.clone())), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + s2.clone(), )), ), @@ -487,6 +486,22 @@ pub fn minus( )), ), + (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( + Box::from(Parameters::Mul(s1.clone(), s2.clone())), + Box::from(Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s3.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s4.clone(), ram)), + )), + ), + + (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(Parameters::Plus(s3.clone(), s4.clone())), + Box::from(Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + s2.clone(), + )), + ), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) diff --git a/src/functions/mult.rs b/src/functions/mult.rs index cc7e760..3f81d93 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -433,11 +433,7 @@ pub fn mult( Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), ram, )), - Box::from(mult( - *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - )), + s2.clone(), ), (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Mul( @@ -446,41 +442,47 @@ pub fn mult( Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), ram, )), - Box::from(mult( - *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - )), + s2.clone(), ), (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - Box::from(mult(*s2.clone(), Parameters::Int(i), ram)), + s2.clone(), ), (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - Box::from(mult(*s2.clone(), Parameters::Int(i), ram)), + s2.clone(), ), (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - Box::from(mult(*s2.clone(), Parameters::Float(f), ram)), + s2.clone(), ), (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - Box::from(mult(*s2.clone(), Parameters::Float(f), ram)), + s2.clone(), ), (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), - Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), ), (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), - Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ), + //(x*y)*(a+b) = x*y*a+x*y*b + (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( + Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), + Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), + ), + + (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), + Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), ), //x*y : x==y : x^2 else x*y (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { From 2b4ae6c2d9b06fc60a5fae411500262903ff2bae Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 6 May 2024 09:22:24 +0200 Subject: [PATCH 28/67] fix errors in mult format 0x => 0 format 0-x => -x --- src/functions/add.rs | 9 ++++++++- src/functions/minus.rs | 44 ++++++++++++++++++++++++++++-------------- src/functions/mult.rs | 10 +++++++++- src/parsing/ast.rs | 8 ++++++-- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index 972afc6..f805196 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -365,7 +365,10 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus(s1.clone(), s2.clone()), + + (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( Box::from(Parameters::Mul(s1.clone(), s2.clone())), Box::from(Parameters::Mul(s3.clone(), s4.clone())), @@ -431,6 +434,10 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Mul(s1.clone(), s2.clone()), + + (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 10e0c2f..0dc3fcf 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -22,9 +22,7 @@ pub fn minus( Parameters::Rational(Rationals::new(1, 0) - s) } - (Parameters::Null, Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, 0) - s) - } + (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.opposite()), (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s - s2), (Parameters::Rational(s), Parameters::Int(i)) => { Parameters::Rational(s - Rationals::new(1, i)) @@ -92,21 +90,19 @@ pub fn minus( Box::from(Parameters::Identifier(s.clone())), Box::from(Parameters::Int(-i)), ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus), + Some(_) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, minus) + } }, (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Null), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), + None => Parameters::Var(Box::from(Parameters::Int(-1)), 1, s), Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), }, (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Null), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + None => Parameters::Var(Box::from(Parameters::Int(-1)), 1, s), + Some(_) => { + apply_operator_reverse(Parameters::Identifier(s), Parameters::Null, ram, minus) + } }, (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { None => Parameters::Plus( @@ -141,7 +137,7 @@ pub fn minus( let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); match v { Parameters::Int(i) => Parameters::Int(-i), - _ => Parameters::Null, + p => minus(Parameters::Int(0), p, None), } } }, @@ -405,6 +401,16 @@ pub fn minus( } } + (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ), + + (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + ), + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( Box::from(Parameters::Mul(s1.clone(), s2.clone())), Box::from(Parameters::Mul( @@ -502,6 +508,16 @@ pub fn minus( )), ), + (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + s2.clone(), + ), + + (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul( + Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), + s2.clone(), + ), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 && y == y1 { Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 3f81d93..961267a 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -355,6 +355,10 @@ pub fn mult( )), ), + (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + + (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus(s1.clone(), s2.clone()), + (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { let first = Parameters::Mul( Box::from(mult( @@ -484,7 +488,11 @@ pub fn mult( Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), ), - //x*y : x==y : x^2 else x*y + + (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul(s1.clone(), s2.clone()), + + (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 { Parameters::Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index c3c7332..ff44c0a 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fmt::{Display, Formatter}; +use std::fmt::{format, Display, Formatter}; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; @@ -167,6 +167,7 @@ impl Parameters { Var(x, y, z) => match **x { Int(1) => format!("{}{}", z, int_to_superscript_string(*y)), Int(-1) => format!("-{}{}", z, int_to_superscript_string(*y)), + Int(0) => format!("0"), _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), }, @@ -183,7 +184,7 @@ impl Parameters { } Plus(x, y) => { - let x_printed = x.pretty_print( + let mut x_printed = x.pretty_print( Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); @@ -191,6 +192,9 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); + if x_printed == "0" { + x_printed = "".to_string() + } match y_printed.chars().nth(0) { Some('-') => format!("{}{}", x_printed, y_printed), _ => { From a26231c1ed4b4eb27d8a584c5bcc0e38fee6771b Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 6 May 2024 09:24:12 +0200 Subject: [PATCH 29/67] remove unnecessary import --- src/parsing/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index ff44c0a..b3f6e15 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fmt::{format, Display, Formatter}; +use std::fmt::{Display, Formatter}; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; From b74dec7807080ba28bf8253604dcba8b8c5297ce Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 7 May 2024 09:56:54 +0200 Subject: [PATCH 30/67] add div --- src/exact_math/symbolic.rs | 1 + src/interpreting/interpreter.rs | 1 + src/parsing/ast.rs | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/exact_math/symbolic.rs b/src/exact_math/symbolic.rs index bf40fc6..5296d68 100644 --- a/src/exact_math/symbolic.rs +++ b/src/exact_math/symbolic.rs @@ -29,6 +29,7 @@ pub fn size(p: &Parameters) -> i32 { Plus(x, y) => 1 + size(x) + size(y), Var(x, _, _) => 1 + size(x), Mul(x, y) => 1 + size(x) + size(y), + Div(x, y) => 1 + size(x) + size(y), } } diff --git a/src/interpreting/interpreter.rs b/src/interpreting/interpreter.rs index c23c5ed..af27e94 100644 --- a/src/interpreting/interpreter.rs +++ b/src/interpreting/interpreter.rs @@ -94,6 +94,7 @@ pub fn interpret( Parameters::Var(x, y, z) => Parameters::Var(x.clone(), y.clone(), 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)), }; last.clone() } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index b3f6e15..740b775 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -35,6 +35,7 @@ pub enum Parameters { Var(Box, i64, String), Plus(Box, Box), Mul(Box, Box), + Div(Box, Box), } #[derive(Debug, Clone, PartialEq)] @@ -112,6 +113,7 @@ impl Display for Parameters { Plus(x, y) => write!(f, "({x}+{y})"), Mul(x, y) => write!(f, "({x}*{y})"), Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), + Div(x, y) => write!(f, "({x}/{y})"), } } } From ca12ef01d0993478ebcd3f0a74ba952dba38153b Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 7 May 2024 10:12:19 +0200 Subject: [PATCH 31/67] add invert operation to rationals --- src/exact_math/rationals.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/exact_math/rationals.rs b/src/exact_math/rationals.rs index 9eea7cc..28767bc 100644 --- a/src/exact_math/rationals.rs +++ b/src/exact_math/rationals.rs @@ -36,6 +36,13 @@ impl Rationals { Rationals::new(self.under, -1 * self.over) } + pub fn invert(self) -> Result { + match self.over { + 0 => Err(Rationals::new(0, 1)), + _ => Ok(Rationals::new(self.over, self.under).reduce()), + } + } + pub fn reduce(self) -> Self { let minus; let i1; From 82415faea5240488567adf79fa204c80f8f6170d Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 7 May 2024 10:12:42 +0200 Subject: [PATCH 32/67] WIP add div operations --- src/functions/divide.rs | 62 +++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index d051203..bc1e53c 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -50,25 +50,51 @@ pub fn divide( (Bool(b), Parameters::Null) => Bool(b), (Parameters::Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - divide, - ), + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + divide, + ), + None => Parameters::Div( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(1)), + 1, + s2.clone(), + )), + ), + }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - divide, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - divide, - ), + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + divide, + ), + None => Parameters::Div( + Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Var( + Box::from(Parameters::Int(1)), + 1, + ss.clone(), + )), + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + divide, + ), + None => match s.invert() { + Ok(r) => Parameters::Var(Box::from(Parameters::Rational(r)), 1, ss.clone()), + Err(_) => Parameters::Null, + }, + }, (Parameters::Identifier(s), Parameters::Int(i)) => { apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide) From b761dbf078cd18ab454e021c6a1c45d1bf5e2599 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 12 May 2024 15:04:01 +0200 Subject: [PATCH 33/67] add basic operators for div add parenthesis EVERYWHERE! --- src/functions/divide.rs | 98 ++++++++++++++++++++++++++++------------- src/parsing/ast.rs | 24 +++++++--- 2 files changed, 87 insertions(+), 35 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index bc1e53c..6d0c494 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -96,38 +96,76 @@ pub fn divide( }, }, - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide); - match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => Parameters::Null, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Var( + Box::new(Parameters::Rational(Rationals::new(i, 1))), + 1, + s.clone(), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Div( + Box::from(Parameters::Int(i)), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ), + Some(_) => { + let v = apply_operator( + Parameters::Identifier(s.clone()), + Parameters::Int(i), + ram, + divide, + ); + match v { + Parameters::Float(i) => Parameters::Float(1.0 / i), + _ => divide(Parameters::Int(i), Parameters::Identifier(s.clone()), None), + } } - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide); - match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => Parameters::Null, + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Parameters::Div( + Box::new(Parameters::Int(1)), + Box::new(Parameters::Var(Box::new(Parameters::Int(1)), 1, s.clone())), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Div( + Box::new(Parameters::Int(1)), + Box::new(Parameters::Var(Box::new(Parameters::Int(1)), 1, s.clone())), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide), + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Var( + Box::from(Parameters::Rational(Rationals::rationalize(1.0 / i))), + 1, + s.clone(), + ), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Div( + Box::from(Parameters::Rational(Rationals::rationalize(i))), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ), + Some(_) => { + let v = + apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide); + match v { + Parameters::Float(i) => Parameters::Float(1.0 / i), + _ => Parameters::Null, + } } - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, divide) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, divide) - } + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, divide), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Parameters::Bool(b), + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, divide), + }, _ => Parameters::Identifier( "@Those two values are incompatible with the / operator".to_string(), ), diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 740b775..0b25db2 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fmt::{Display, Formatter}; +use std::fmt::{format, Display, Formatter}; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; @@ -112,7 +112,7 @@ impl Display for Parameters { Rational(s) => write!(f, "{s}"), Plus(x, y) => write!(f, "({x}+{y})"), Mul(x, y) => write!(f, "({x}*{y})"), - Var(x, y, s) => write!(f, "{x}{s}{}", int_to_superscript_string(*y)), + Var(x, y, s) => write!(f, "({x}){s}{}", int_to_superscript_string(*y)), Div(x, y) => write!(f, "({x}/{y})"), } } @@ -182,7 +182,7 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); - format!("{x_printed}*{y_printed}") + format!("({x_printed}*{y_printed})") } Plus(x, y) => { @@ -198,16 +198,30 @@ impl Parameters { x_printed = "".to_string() } match y_printed.chars().nth(0) { - Some('-') => format!("{}{}", x_printed, y_printed), + Some('-') => format!("({}{})", x_printed, y_printed), _ => { if y_printed == "0".to_string() { format!("{}", x_printed) } else { - format!("{}+{}", x_printed, y_printed) + format!("({}+{})", x_printed, y_printed) } } } } + + Div(x, y) => { + let x_printed = x.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + let y_printed = y.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + + format!("({x_printed}/{y_printed})") + } + InterpreterVector(lst) => { let mut vec = Vec::new(); From 3cee0d4a0f307b7f81c8603e6ed1a562e3e704ca Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 12 May 2024 15:10:22 +0200 Subject: [PATCH 34/67] add even more parenthesis (for more clarification, so we can be really really really sure about what we're calculating and what we're not, might add an option *not* to put so much parenthesis in the future (might) because this is a little less readable lol this commit line is long --- src/functions/divide.rs | 12 ++++++++---- src/parsing/ast.rs | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 6d0c494..7c369b9 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -75,9 +75,9 @@ pub fn divide( divide, ), None => Parameters::Div( - Box::from(Parameters::Rational(s.clone())), + Box::from(Parameters::Int(s.over)), Box::from(Parameters::Var( - Box::from(Parameters::Int(1)), + Box::from(Parameters::Int(s.under)), 1, ss.clone(), )), @@ -146,8 +146,12 @@ pub fn divide( }, (Parameters::Float(i), Parameters::Identifier(s)) => match ram { None => Parameters::Div( - Box::from(Parameters::Rational(Rationals::rationalize(i))), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Parameters::Int(Rationals::rationalize(i).over)), + Box::from(Parameters::Var( + Box::from(Parameters::Int(Rationals::rationalize(i).under)), + 1, + s.clone(), + )), ), Some(_) => { let v = diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 0b25db2..0c7e1ba 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -110,10 +110,10 @@ impl Display for Parameters { InterpreterVector(a) => write!(f, "{:?}", a), Str(s) => write!(f, "{s}"), Rational(s) => write!(f, "{s}"), - Plus(x, y) => write!(f, "({x}+{y})"), - Mul(x, y) => write!(f, "({x}*{y})"), + Plus(x, y) => write!(f, "(({x})+({y}))"), + Mul(x, y) => write!(f, "(({x})*({y}))"), Var(x, y, s) => write!(f, "({x}){s}{}", int_to_superscript_string(*y)), - Div(x, y) => write!(f, "({x}/{y})"), + Div(x, y) => write!(f, "(({x})/({y}))"), } } } @@ -182,7 +182,7 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); - format!("({x_printed}*{y_printed})") + format!("(({x_printed})*({y_printed}))") } Plus(x, y) => { @@ -203,7 +203,7 @@ impl Parameters { if y_printed == "0".to_string() { format!("{}", x_printed) } else { - format!("({}+{})", x_printed, y_printed) + format!("(({})+({}))", x_printed, y_printed) } } } @@ -219,7 +219,7 @@ impl Parameters { Some(function.as_mut().unwrap()), ); - format!("({x_printed}/{y_printed})") + format!("(({x_printed})/({y_printed}))") } InterpreterVector(lst) => { From e7bff4d2ad5dd9a96c0ca78029e12d168c46e0d0 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 12 May 2024 15:14:44 +0200 Subject: [PATCH 35/67] ok that was too much parenthesis, now it's a bit better, but we're less sure of what we're calculating, oh well they will know. *might* add more pretty printing in the future for variables --- src/parsing/ast.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 0c7e1ba..69e9479 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -182,7 +182,7 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); - format!("(({x_printed})*({y_printed}))") + format!("({x_printed})*({y_printed})") } Plus(x, y) => { @@ -203,7 +203,7 @@ impl Parameters { if y_printed == "0".to_string() { format!("{}", x_printed) } else { - format!("(({})+({}))", x_printed, y_printed) + format!("({})+({})", x_printed, y_printed) } } } @@ -219,7 +219,7 @@ impl Parameters { Some(function.as_mut().unwrap()), ); - format!("(({x_printed})/({y_printed}))") + format!("({x_printed})/({y_printed})") } InterpreterVector(lst) => { From 9ae95557ea0b94d2bf3fe6beb2ea069d83b00ce3 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Mon, 13 May 2024 21:46:11 +0200 Subject: [PATCH 36/67] WIP add mul/plus/var to div --- src/functions/divide.rs | 309 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 7c369b9..1dabbc8 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -6,6 +6,9 @@ use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; use std::collections::HashMap; +use super::add::add; +use super::mult::mult; + pub fn divide( i: Parameters, i2: Parameters, @@ -170,6 +173,312 @@ pub fn divide( None => Parameters::Bool(b), Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, divide), }, + (Parameters::Plus(s1, s2), Parameters::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 + } + (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + let first = add( + divide( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + ram, + ), + divide( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + ram, + ), + ram, + ); + first + } + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { + let first = Parameters::Div( + Box::from(Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone())), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ); + first + } + (Parameters::Int(i), Parameters::Plus(s1, s2)) => { + let first = Parameters::Div( + Box::from(Parameters::Int(i)), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + let first = add( + divide(*s1.clone(), Parameters::Int(i), ram), + divide(*s2.clone(), Parameters::Int(i), ram), + ram, + ); + first + } + + (Parameters::Float(f), Parameters::Plus(s1, s2)) => { + let first = Parameters::Div( + Box::from(Parameters::Float(f)), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + let first = add( + divide(*s1.clone(), Parameters::Float(f), ram), + divide(*s2.clone(), Parameters::Float(f), ram), + ram, + ); + first + } + (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { + let first = Parameters::Div( + Box::from(Parameters::Rational(r.clone())), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ); + first + } + + (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + let first = add( + divide(*s1.clone(), Parameters::Rational(r.clone()), ram), + divide(*s2.clone(), Parameters::Rational(r.clone()), ram), + ram, + ); + first + } + (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => Parameters::Div( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(add(*s1.clone(), *s2.clone(), ram)), + ), + + (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => add( + divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + ram, + ), + + (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + + (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus(s1.clone(), s2.clone()), + + (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { + let first = Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Mul( + s1.clone(), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { + let first = Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + s2.clone(), + ); + let second = Parameters::Mul( + s1.clone(), + Box::from(mult( + *s2.clone(), + Parameters::Var(x.clone(), y, z.clone()), + ram, + )), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { + let first = Parameters::Mul( + Box::from(mult(*s1.clone(), *s3.clone(), ram)), + Box::from(mult(*s2.clone(), *s4.clone(), ram)), + ); + let second = Parameters::Mul( + Box::from(mult(*s1.clone(), *s4.clone(), ram)), + Box::from(mult(*s2.clone(), *s3.clone(), ram)), + ); + + let (ss1, ss2) = (size(&first), size(&second)); + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + s2.clone(), + ), + + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + )), + s2.clone(), + ), + + (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ), + + (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), + s2.clone(), + ), + + (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ), + + (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), + s2.clone(), + ), + + (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ), + + (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( + Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + s2.clone(), + ), + //(x*y)*(a+b) = x*y*a+x*y*b + (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( + Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), + Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), + ), + + (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( + Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), + Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), + ), + + (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul(s1.clone(), s2.clone()), + + (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + + (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + if z == z1 { + Parameters::Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var( + Box::from(mult(Parameters::Int(1), *x1.clone(), ram)), + y1.clone(), + z1.clone(), + )), + ) + } + } + + //2x*x + (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + if z == s { + Parameters::Var(Box::from(x.clone()), y + 1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + if z == s { + Parameters::Var(Box::from(x.clone()), y + 1, z) + } else { + Parameters::Mul( + Box::from(Parameters::Var( + Box::from(mult(Parameters::Int(1), *x.clone(), ram)), + y.clone(), + z.clone(), + )), + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + ) + } + } + + (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Int(i), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Int(i), ram)), + y, + z.clone(), + ), + + (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(Parameters::Float(f), *x.clone(), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Float(f), ram)), + y, + z.clone(), + ), + + (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( + Box::from(mult(Parameters::Rational(r.clone()), *x.clone(), ram)), + y, + z.clone(), + ), + + (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( + Box::from(mult(*x.clone(), Parameters::Rational(r), ram)), + y, + z.clone(), + ), _ => Parameters::Identifier( "@Those two values are incompatible with the / operator".to_string(), ), From f627753180bb8bb30c720e7b847ee4286930ef9e Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 14 May 2024 20:16:33 +0200 Subject: [PATCH 37/67] WIP add mul/plus/var to div 2/n --- src/functions/divide.rs | 132 +++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 50 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 1dabbc8..75871d1 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -264,26 +264,21 @@ pub fn divide( ram, ), - (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + (Parameters::Null, Parameters::Plus(s1, s2)) => add(*s1.clone(), *s2.clone(), ram), - (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus(s1.clone(), s2.clone()), + (Parameters::Plus(s1, s2), Parameters::Null) => add(*s1.clone(), *s2.clone(), ram), + //x/yz = x/y * 1/z | 1/y * x/z (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { - let first = Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), - s2.clone(), + let first = mult( + divide(Parameters::Var(x.clone(), y, z.clone()), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, ); - let second = Parameters::Mul( - s1.clone(), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + ram, ); let (ss1, ss2) = (size(&first), size(&second)); @@ -295,22 +290,17 @@ pub fn divide( } } + //xy/z = x/z * y | y/z * x (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { - let first = Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), - s2.clone(), + let first = mult( + divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + *s2.clone(), + ram, ); - let second = Parameters::Mul( - s1.clone(), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + ram, ); let (ss1, ss2) = (size(&first), size(&second)); @@ -321,15 +311,17 @@ pub fn divide( first } } - + //xy/ab = x/ab * y/1 | y/ab * x (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { - let first = Parameters::Mul( - Box::from(mult(*s1.clone(), *s3.clone(), ram)), - Box::from(mult(*s2.clone(), *s4.clone(), ram)), + let first = mult( + divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + *s2.clone(), + ram, ); - let second = Parameters::Mul( - Box::from(mult(*s1.clone(), *s4.clone(), ram)), - Box::from(mult(*s2.clone(), *s3.clone(), ram)), + let second = mult( + *s1.clone(), + divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + ram, ); let (ss1, ss2) = (size(&first), size(&second)); @@ -340,23 +332,63 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Mul( - Box::from(mult( + //xy/a = x/a * y | y/a * x + (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => { + let first = mult( + divide( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + ), + *s2.clone(), + ram, + ); + let second = mult( *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + divide( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + ), ram, - )), - s2.clone(), - ), + ); - (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => { + let first = mult( + divide( + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + *s1.clone(), + ram, + ), + divide(Parameters::Int(1), *s2.clone(), ram), ram, - )), - s2.clone(), - ), + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide( + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + *s2.clone(), + ram, + ), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), From df4187e003e59f7a56950811b1b0b8ff827fbf7b Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 14 May 2024 20:52:26 +0200 Subject: [PATCH 38/67] WIP add mul/plus/var to div 3/4 --- src/functions/divide.rs | 148 +++++++++++++++++++++++++++++++++------- 1 file changed, 124 insertions(+), 24 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 75871d1..f83ad68 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -390,35 +390,135 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ), + //(s1s2/a => s1/a * s2 + (Parameters::Mul(s1, s2), Parameters::Int(i)) => { + let first = mult( + divide(*s1.clone(), Parameters::Int(i), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Int(i), ram), + ram, + ); - (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ), + let (ss1, ss2) = (size(&first), size(&second)); - (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ), + if ss1 > ss2 { + second + } else { + first + } + } - (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ), + //a/xy = a/x * 1/y | 1/x * a/y + (Parameters::Int(i), Parameters::Mul(s1, s2)) => { + let first = mult( + divide(Parameters::Int(i), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(i), *s1.clone(), ram), + divide(Parameters::Int(i), *s2.clone(), ram), + ram, + ); - (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), - s2.clone(), - ), + let (ss1, ss2) = (size(&first), size(&second)); - (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), - s2.clone(), - ), + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Float(f)) => { + let first = mult( + divide(*s1.clone(), Parameters::Float(f), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Float(f), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + //a/xy = a/x * 1/y | 1/x * a/y + (Parameters::Float(f), Parameters::Mul(s1, s2)) => { + let first = mult( + divide(Parameters::Float(f), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Float(f), *s1.clone(), ram), + divide(Parameters::Float(f), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Rational(r)) => { + let first = mult( + divide(*s1.clone(), Parameters::Rational(r.clone()), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Rational(r.clone()), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + //a/xy = a/x * 1/y | 1/x * a/y + (Parameters::Rational(r), Parameters::Mul(s1, s2)) => { + let first = mult( + divide(Parameters::Rational(r.clone()), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Rational(r.clone()), *s1.clone(), ram), + divide(Parameters::Rational(r.clone()), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } //(x*y)*(a+b) = x*y*a+x*y*b (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), From 216b3750bd7d9b0459fa3625f437a62e2a674f51 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Tue, 14 May 2024 21:52:09 +0200 Subject: [PATCH 39/67] implements add mul/plus/var to div 4/4 add copy to derived traits for Rationals add Float(1,-1) and Rationals(1/1,-1/1) to pretty printing --- src/exact_math/rationals.rs | 2 +- src/functions/divide.rs | 111 +++++++++++++++++++++--------------- src/parsing/ast.rs | 36 +++++++++--- 3 files changed, 95 insertions(+), 54 deletions(-) diff --git a/src/exact_math/rationals.rs b/src/exact_math/rationals.rs index 28767bc..5b1136d 100644 --- a/src/exact_math/rationals.rs +++ b/src/exact_math/rationals.rs @@ -2,7 +2,7 @@ use std::{fmt::Display, ops}; use crate::utils::integer_utils::gcd; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct Rationals { pub under: i64, pub over: i64, diff --git a/src/functions/divide.rs b/src/functions/divide.rs index f83ad68..a66403d 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -60,13 +60,10 @@ pub fn divide( ram, divide, ), - None => Parameters::Div( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(1)), - 1, - s2.clone(), - )), + None => divide( + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s2.clone()), + ram, ), }, @@ -181,6 +178,7 @@ pub fn divide( ); first } + //(x+y)/a = x/a + y/a (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { let first = add( divide( @@ -197,6 +195,7 @@ pub fn divide( ); first } + (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { let first = Parameters::Div( Box::from(Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone())), @@ -268,7 +267,6 @@ pub fn divide( (Parameters::Plus(s1, s2), Parameters::Null) => add(*s1.clone(), *s2.clone(), ram), - //x/yz = x/y * 1/z | 1/y * x/z (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { let first = mult( divide(Parameters::Var(x.clone(), y, z.clone()), *s1.clone(), ram), @@ -290,7 +288,6 @@ pub fn divide( } } - //xy/z = x/z * y | y/z * x (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { let first = mult( divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), @@ -311,7 +308,7 @@ pub fn divide( first } } - //xy/ab = x/ab * y/1 | y/ab * x + (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { let first = mult( divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), @@ -332,7 +329,6 @@ pub fn divide( } } - //xy/a = x/a * y | y/a * x (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => { let first = mult( divide( @@ -361,6 +357,7 @@ pub fn divide( first } } + (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => { let first = mult( divide( @@ -390,7 +387,6 @@ pub fn divide( } } - //(s1s2/a => s1/a * s2 (Parameters::Mul(s1, s2), Parameters::Int(i)) => { let first = mult( divide(*s1.clone(), Parameters::Int(i), ram), @@ -412,7 +408,6 @@ pub fn divide( } } - //a/xy = a/x * 1/y | 1/x * a/y (Parameters::Int(i), Parameters::Mul(s1, s2)) => { let first = mult( divide(Parameters::Int(i), *s1.clone(), ram), @@ -420,7 +415,7 @@ pub fn divide( ram, ); let second = mult( - divide(Parameters::Int(i), *s1.clone(), ram), + divide(Parameters::Int(1), *s1.clone(), ram), divide(Parameters::Int(i), *s2.clone(), ram), ram, ); @@ -455,7 +450,6 @@ pub fn divide( } } - //a/xy = a/x * 1/y | 1/x * a/y (Parameters::Float(f), Parameters::Mul(s1, s2)) => { let first = mult( divide(Parameters::Float(f), *s1.clone(), ram), @@ -463,7 +457,7 @@ pub fn divide( ram, ); let second = mult( - divide(Parameters::Float(f), *s1.clone(), ram), + divide(Parameters::Int(1), *s1.clone(), ram), divide(Parameters::Float(f), *s2.clone(), ram), ram, ); @@ -498,7 +492,6 @@ pub fn divide( } } - //a/xy = a/x * 1/y | 1/x * a/y (Parameters::Rational(r), Parameters::Mul(s1, s2)) => { let first = mult( divide(Parameters::Rational(r.clone()), *s1.clone(), ram), @@ -506,7 +499,7 @@ pub fn divide( ram, ); let second = mult( - divide(Parameters::Rational(r.clone()), *s1.clone(), ram), + divide(Parameters::Int(1), *s1.clone(), ram), divide(Parameters::Rational(r.clone()), *s2.clone(), ram), ram, ); @@ -519,29 +512,51 @@ pub fn divide( first } } - //(x*y)*(a+b) = x*y*a+x*y*b - (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( - Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), - Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), - ), - (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), - Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), - ), + //(xy)/(a+b) = x/(a+b) * y/1 + (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => { + let first = mult( + divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } - (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul(s1.clone(), s2.clone()), + //(x+y)/ab = x/ab + y/ab + (Parameters::Plus(s3, s4), Parameters::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 + } + + (Parameters::Null, Parameters::Mul(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), - (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + (Parameters::Mul(s1, s2), Parameters::Null) => mult(*s1.clone(), *s2.clone(), ram), (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { if z == z1 { - Parameters::Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) + Parameters::Var(Box::from(divide(*x.clone(), *x1.clone(), ram)), y - y1, z) } else { - Parameters::Mul( + Parameters::Div( Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), Box::from(Parameters::Var( - Box::from(mult(Parameters::Int(1), *x1.clone(), ram)), + Box::from(divide(Parameters::Int(1), *x1.clone(), ram)), y1.clone(), z1.clone(), )), @@ -552,9 +567,9 @@ pub fn divide( //2x*x (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { if z == s { - Parameters::Var(Box::from(x.clone()), y + 1, z) + Parameters::Var(Box::from(x.clone()), y - 1, z) } else { - Parameters::Mul( + Parameters::Div( Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), ) @@ -563,51 +578,55 @@ pub fn divide( (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { if z == s { - Parameters::Var(Box::from(x.clone()), y + 1, z) + Parameters::Var( + Box::from(divide(Parameters::Int(1), *x.clone(), ram)), + 1 - y, + z, + ) } else { - Parameters::Mul( + Parameters::Div( + Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), Box::from(Parameters::Var( Box::from(mult(Parameters::Int(1), *x.clone(), ram)), y.clone(), z.clone(), )), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), ) } } (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Int(i), ram)), - y, + Box::from(divide(Parameters::Int(i), *x.clone(), ram)), + -y, z.clone(), ), (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Int(i), ram)), + Box::from(divide(*x.clone(), Parameters::Int(i), ram)), y, z.clone(), ), (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(Parameters::Float(f), *x.clone(), ram)), - y, + Box::from(divide(Parameters::Float(f), *x.clone(), ram)), + -y, z.clone(), ), (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Float(f), ram)), + Box::from(divide(*x.clone(), Parameters::Float(f), ram)), y, z.clone(), ), (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(Parameters::Rational(r.clone()), *x.clone(), ram)), - y, + Box::from(divide(Parameters::Rational(r.clone()), *x.clone(), ram)), + -y, z.clone(), ), (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Rational(r), ram)), + Box::from(divide(*x.clone(), Parameters::Rational(r.clone()), ram)), y, z.clone(), ), diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 69e9479..1ac961d 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -55,6 +55,7 @@ pub enum Ast { fn int_to_superscript_string(i: i64) -> String { fn digit_to_superscript_char(i: &str) -> &str { match i { + "-" => "⁻", "0" => "⁰", "1" => "¹", "2" => "²", @@ -77,7 +78,9 @@ fn int_to_superscript_string(i: i64) -> String { .for_each(|f| vec.push(f)); let i = vec.join(""); - if i == "0".to_string() || i == "¹".to_string() { + if i == "⁰".to_string() { + "error".to_string() + } else if i == "¹" { "".to_string() } else { i @@ -166,12 +169,31 @@ impl Parameters { } } - Var(x, y, z) => match **x { - Int(1) => format!("{}{}", z, int_to_superscript_string(*y)), - Int(-1) => format!("-{}{}", z, int_to_superscript_string(*y)), - Int(0) => format!("0"), - _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), - }, + Var(x, y, z) => { + let l = int_to_superscript_string(*y); + if l == "error".to_string() { + format!("{}", x.clone()) + } else { + match **x { + Int(1) => format!("{}{}", z, int_to_superscript_string(*y)), + Float(f) if f >= 1.0 - 1e10 && f <= 1.0 + 1e10 => { + format!("{}{}", z, int_to_superscript_string(*y)) + } + Rational(r) if r.clone() == Rationals::new(1, 1) => { + format!("{}{}", z, int_to_superscript_string(*y)) + } + Int(-1) => format!("-{}{}", z, int_to_superscript_string(*y)), + Float(f) if f <= -1.0 - 1e10 && f >= -1.0 + 1e10 => { + format!("-{}{}", z, int_to_superscript_string(*y)) + } + Rational(r) if r.clone() == Rationals::new(-1, 1) => { + format!("-{}{}", z, int_to_superscript_string(*y)) + } + Int(0) => format!("0"), + _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), + } + } + } Mul(x, y) => { let x_printed = x.pretty_print( From d5b53156b43cc4556e06c63e489eeeb4198edd02 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 20:46:27 +0200 Subject: [PATCH 40/67] WIP add div operations to div --- src/functions/divide.rs | 322 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index a66403d..7e5f4e8 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -630,6 +630,328 @@ pub fn divide( y, z.clone(), ), + + (Parameters::Var(x, y, z), Parameters::Div(s1, s2)) => { + let first = mult( + divide(Parameters::Var(x.clone(), y, z.clone()), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Var(x, y, z)) => { + let first = mult( + divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Mul(s1, s2), Parameters::Div(s3, s4)) => { + let first = mult( + divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Mul(s3, s4)) => { + let first = mult( + divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Div(s3, s4)) => { + let first = mult( + divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Identifier(s)) => { + let first = mult( + divide( + *s1.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + ), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide( + *s2.clone(), + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + ), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Identifier(s), Parameters::Div(s1, s2)) => { + let first = mult( + divide( + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + *s1.clone(), + ram, + ), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide( + Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + *s2.clone(), + ram, + ), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Int(i)) => { + let first = mult( + divide(*s1.clone(), Parameters::Int(i), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Int(i), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Int(i), Parameters::Div(s1, s2)) => { + let first = mult( + divide(Parameters::Int(i), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(Parameters::Int(i), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Float(f)) => { + let first = mult( + divide(*s1.clone(), Parameters::Float(f), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Float(f), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Float(f), Parameters::Div(s1, s2)) => { + let first = mult( + divide(Parameters::Float(f), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(Parameters::Float(f), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Div(s1, s2), Parameters::Rational(r)) => { + let first = mult( + divide(*s1.clone(), Parameters::Rational(r.clone()), ram), + *s2.clone(), + ram, + ); + let second = mult( + *s1.clone(), + divide(*s2.clone(), Parameters::Rational(r.clone()), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + (Parameters::Rational(r), Parameters::Div(s1, s2)) => { + let first = mult( + divide(Parameters::Rational(r.clone()), *s1.clone(), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(Parameters::Rational(r.clone()), *s2.clone(), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + //(xy)/(a+b) = x/(a+b) * y/1 + (Parameters::Div(s1, s2), Parameters::Plus(s3, s4)) => { + let first = mult( + divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + divide(Parameters::Int(1), *s2.clone(), ram), + ram, + ); + let second = mult( + divide(Parameters::Int(1), *s1.clone(), ram), + divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + ); + + let (ss1, ss2) = (size(&first), size(&second)); + + if ss1 > ss2 { + second + } else { + first + } + } + + //(x+y)/ab = x/ab + y/ab + (Parameters::Plus(s3, s4), Parameters::Div(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 + } + + (Parameters::Null, Parameters::Div(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), + + (Parameters::Div(s1, s2), Parameters::Null) => mult(*s1.clone(), *s2.clone(), ram), _ => Parameters::Identifier( "@Those two values are incompatible with the / operator".to_string(), ), From cad85bc63c25e4aabc24fe7e5d1eaa8dcc1fb799 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 20:50:59 +0200 Subject: [PATCH 41/67] remove comments --- src/functions/divide.rs | 6 ------ src/functions/mult.rs | 3 +-- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 7e5f4e8..5bf898b 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -178,7 +178,6 @@ pub fn divide( ); first } - //(x+y)/a = x/a + y/a (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { let first = add( divide( @@ -513,7 +512,6 @@ pub fn divide( } } - //(xy)/(a+b) = x/(a+b) * y/1 (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => { let first = mult( divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), @@ -535,7 +533,6 @@ pub fn divide( } } - //(x+y)/ab = x/ab + y/ab (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => { let first = add( divide(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), @@ -564,7 +561,6 @@ pub fn divide( } } - //2x*x (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { if z == s { Parameters::Var(Box::from(x.clone()), y - 1, z) @@ -917,7 +913,6 @@ pub fn divide( } } - //(xy)/(a+b) = x/(a+b) * y/1 (Parameters::Div(s1, s2), Parameters::Plus(s3, s4)) => { let first = mult( divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), @@ -939,7 +934,6 @@ pub fn divide( } } - //(x+y)/ab = x/ab + y/ab (Parameters::Plus(s3, s4), Parameters::Div(s1, s2)) => { let first = add( divide(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 961267a..694b6d3 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -478,7 +478,7 @@ pub fn mult( Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), s2.clone(), ), - //(x*y)*(a+b) = x*y*a+x*y*b + (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), @@ -508,7 +508,6 @@ pub fn mult( } } - //2x*x (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { if z == s { Parameters::Var(Box::from(x.clone()), y + 1, z) From 89b8e0bacb101826e2f721b1d11e1fbf8afcbb0a Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:17:47 +0200 Subject: [PATCH 42/67] remove unnecessary clones remove unnecessary imports --- src/functions/add.rs | 16 +-- src/functions/divide.rs | 274 ++++++++-------------------------------- src/functions/minus.rs | 12 +- src/functions/mult.rs | 14 +- src/parsing/ast.rs | 2 +- 5 files changed, 74 insertions(+), 244 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index f805196..4ce092b 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -278,12 +278,12 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(add(*s1.clone(), Parameters::Rational(r), ram)), s2.clone(), ); let second = Parameters::Plus( s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(add(*s2.clone(), Parameters::Rational(r), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -296,12 +296,12 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(add(*s1.clone(), Parameters::Rational(r), ram)), s2.clone(), ); let second = Parameters::Plus( s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(add(*s2.clone(), Parameters::Rational(r), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -406,11 +406,11 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus( Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), ), (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), Box::from(Parameters::Mul(s1.clone(), s2.clone())), ), @@ -492,13 +492,13 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), Box::from(Parameters::Var(x, y, z)), ), (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), ), _ => Parameters::Identifier( diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 5bf898b..4344144 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -237,7 +237,7 @@ pub fn divide( } (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { let first = Parameters::Div( - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), Box::from(add(*s1.clone(), *s2.clone(), ram)), ); first @@ -245,8 +245,8 @@ pub fn divide( (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { let first = add( - divide(*s1.clone(), Parameters::Rational(r.clone()), ram), - divide(*s2.clone(), Parameters::Rational(r.clone()), ram), + divide(*s1.clone(), Parameters::Rational(r), ram), + divide(*s2.clone(), Parameters::Rational(r), ram), ram, ); first @@ -472,13 +472,13 @@ pub fn divide( (Parameters::Mul(s1, s2), Parameters::Rational(r)) => { let first = mult( - divide(*s1.clone(), Parameters::Rational(r.clone()), ram), + divide(*s1.clone(), Parameters::Rational(r), ram), *s2.clone(), ram, ); let second = mult( *s1.clone(), - divide(*s2.clone(), Parameters::Rational(r.clone()), ram), + divide(*s2.clone(), Parameters::Rational(r), ram), ram, ); @@ -493,13 +493,13 @@ pub fn divide( (Parameters::Rational(r), Parameters::Mul(s1, s2)) => { let first = mult( - divide(Parameters::Rational(r.clone()), *s1.clone(), ram), + divide(Parameters::Rational(r), *s1.clone(), ram), divide(Parameters::Int(1), *s2.clone(), ram), ram, ); let second = mult( divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Rational(r.clone()), *s2.clone(), ram), + divide(Parameters::Rational(r), *s2.clone(), ram), ram, ); @@ -616,303 +616,133 @@ pub fn divide( ), (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(divide(Parameters::Rational(r.clone()), *x.clone(), ram)), + Box::from(divide(Parameters::Rational(r), *x.clone(), ram)), -y, z.clone(), ), (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( - Box::from(divide(*x.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(divide(*x.clone(), Parameters::Rational(r), ram)), y, z.clone(), ), (Parameters::Var(x, y, z), Parameters::Div(s1, s2)) => { - let first = mult( - divide(Parameters::Var(x.clone(), y, z.clone()), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + let first = divide( + mult(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + *s1.clone(), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Var(x, y, z)) => { - let first = mult( - divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), - *s2.clone(), - ram, - ); - let second = mult( + let first = divide( *s1.clone(), - divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + mult(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Mul(s1, s2), Parameters::Div(s3, s4)) => { - let first = mult( - divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - *s2.clone(), + let first = divide( + mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), + *s3.clone(), ram, ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - ram, - ); - - let (ss1, ss2) = (size(&first), size(&second)); - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Mul(s3, s4)) => { - let first = mult( - divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - *s2.clone(), - ram, - ); - let second = mult( + let first = divide( *s1.clone(), - divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Div(s3, s4)) => { - let first = mult( - divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - *s2.clone(), - ram, - ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), + let first = divide( + mult(*s1.clone(), *s4.clone(), ram), + mult(*s2.clone(), *s3.clone(), ram), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Identifier(s)) => { - let first = mult( - divide( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), - *s2.clone(), - ram, - ); - let second = mult( + let first = divide( *s1.clone(), - divide( + mult( *s2.clone(), Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), ram, ), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Identifier(s), Parameters::Div(s1, s2)) => { - let first = mult( - divide( - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - *s1.clone(), - ram, - ), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide( + let first = divide( + mult( Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), *s2.clone(), ram, ), + *s1.clone(), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Int(i)) => { - let first = mult( - divide(*s1.clone(), Parameters::Int(i), ram), - *s2.clone(), - ram, - ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Int(i), ram), - ram, - ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + let first = divide(mult(*s1.clone(), Parameters::Int(i), ram), *s2.clone(), ram); + first } (Parameters::Int(i), Parameters::Div(s1, s2)) => { - let first = mult( - divide(Parameters::Int(i), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Int(i), *s2.clone(), ram), - ram, - ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + let first = divide(mult(Parameters::Int(i), *s2.clone(), ram), *s1.clone(), ram); + first } (Parameters::Div(s1, s2), Parameters::Float(f)) => { - let first = mult( - divide(*s1.clone(), Parameters::Float(f), ram), + let first = divide( + mult(*s1.clone(), Parameters::Float(f), ram), *s2.clone(), ram, ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Float(f), ram), - ram, - ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Float(f), Parameters::Div(s1, s2)) => { - let first = mult( - divide(Parameters::Float(f), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Float(f), *s2.clone(), ram), + let first = divide( + mult(Parameters::Float(f), *s2.clone(), ram), + *s1.clone(), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Div(s1, s2), Parameters::Rational(r)) => { - let first = mult( - divide(*s1.clone(), Parameters::Rational(r.clone()), ram), + let first = divide( + mult(*s1.clone(), Parameters::Rational(r), ram), *s2.clone(), ram, ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Rational(r.clone()), ram), - ram, - ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Rational(r), Parameters::Div(s1, s2)) => { - let first = mult( - divide(Parameters::Rational(r.clone()), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Rational(r.clone()), *s2.clone(), ram), + let first = divide( + mult(Parameters::Rational(r), *s2.clone(), ram), + *s1.clone(), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } - (Parameters::Div(s1, s2), Parameters::Plus(s3, s4)) => { let first = mult( divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 0dc3fcf..46ab81c 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -314,12 +314,12 @@ pub fn minus( (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(minus(*s1.clone(), Parameters::Rational(r), ram)), s2.clone(), ); let second = Parameters::Plus( s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(minus(*s2.clone(), Parameters::Rational(r), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -332,12 +332,12 @@ pub fn minus( (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { let first = Parameters::Plus( - Box::from(minus(Parameters::Rational(r.clone()), *s1.clone(), ram)), + Box::from(minus(Parameters::Rational(r), *s1.clone(), ram)), Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), ); let second = Parameters::Plus( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Rational(r.clone()), *s2.clone(), ram)), + Box::from(minus(Parameters::Rational(r), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -468,7 +468,7 @@ pub fn minus( ), (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), Box::from(Parameters::Mul( Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), s2.clone(), @@ -592,7 +592,7 @@ pub fn minus( ), (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Rational(r.clone())), + Box::from(Parameters::Rational(r)), Box::from(Parameters::Var( Box::from(minus(Parameters::Int(0), *x.clone(), ram)), y, diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 694b6d3..7be755d 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -315,16 +315,16 @@ pub fn mult( } (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { let first = Parameters::Plus( - Box::from(mult(Parameters::Rational(r.clone()), *s1.clone(), ram)), - Box::from(mult(Parameters::Rational(r.clone()), *s2.clone(), ram)), + Box::from(mult(Parameters::Rational(r), *s1.clone(), ram)), + Box::from(mult(Parameters::Rational(r), *s2.clone(), ram)), ); first } (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { let first = Parameters::Plus( - Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), - Box::from(mult(*s2.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), + Box::from(mult(*s2.clone(), Parameters::Rational(r), ram)), ); first } @@ -470,12 +470,12 @@ pub fn mult( ), (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), s2.clone(), ), (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r.clone()), ram)), + Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), s2.clone(), ), @@ -559,7 +559,7 @@ pub fn mult( ), (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(Parameters::Rational(r.clone()), *x.clone(), ram)), + Box::from(mult(Parameters::Rational(r), *x.clone(), ram)), y, z.clone(), ), diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 1ac961d..d44a4d0 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fmt::{format, Display, Formatter}; +use std::fmt::{Display, Formatter}; use crate::exact_math::rationals::Rationals; use crate::lexing::token::{Operator, Token}; From c5b038b29642278c57513f9ee2aea7472dce0de9 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:22:39 +0200 Subject: [PATCH 43/67] add div operations to div TODO add div operations to +,* and - test --- src/functions/divide.rs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 4344144..cfe1ecb 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -743,39 +743,28 @@ pub fn divide( ); first } + (Parameters::Div(s1, s2), Parameters::Plus(s3, s4)) => { - let first = mult( - divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - divide(Parameters::Int(1), *s2.clone(), ram), - ram, - ); - let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + let first = divide( + mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + *s2.clone(), ram, ); - - let (ss1, ss2) = (size(&first), size(&second)); - - if ss1 > ss2 { - second - } else { - first - } + first } (Parameters::Plus(s3, s4), Parameters::Div(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), + let first = divide( + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + *s1.clone(), ram, ); first } - (Parameters::Null, Parameters::Div(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), + (Parameters::Null, Parameters::Div(s1, s2)) => divide(*s1.clone(), *s2.clone(), ram), - (Parameters::Div(s1, s2), Parameters::Null) => mult(*s1.clone(), *s2.clone(), ram), + (Parameters::Div(s1, s2), Parameters::Null) => divide(*s1.clone(), *s2.clone(), ram), _ => Parameters::Identifier( "@Those two values are incompatible with the / operator".to_string(), ), From 769d07490be80427a1cafeaa9fdc42bcbaef8a77 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:23:40 +0200 Subject: [PATCH 44/67] fix mathematical error on div --- src/functions/divide.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index cfe1ecb..dcf2b8b 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -762,9 +762,9 @@ pub fn divide( first } - (Parameters::Null, Parameters::Div(s1, s2)) => divide(*s1.clone(), *s2.clone(), ram), + (Parameters::Null, Parameters::Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), - (Parameters::Div(s1, s2), Parameters::Null) => divide(*s1.clone(), *s2.clone(), ram), + (Parameters::Div(s1, s2), Parameters::Null) => divide(*s2.clone(), *s1.clone(), ram), _ => Parameters::Identifier( "@Those two values are incompatible with the / operator".to_string(), ), From bf635640209836a36fd0a24a248aa26b9e209bc3 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:27:52 +0200 Subject: [PATCH 45/67] simplify expressions move Parameters::X to X --- src/functions/add.rs | 475 ++++++++++++-------------------- src/functions/divide.rs | 557 +++++++++++++++----------------------- src/functions/minus.rs | 587 ++++++++++++++++------------------------ src/functions/mult.rs | 505 +++++++++++++--------------------- 4 files changed, 809 insertions(+), 1315 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index 4ce092b..03d1971 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -2,162 +2,119 @@ use crate::exact_math::rationals::Rationals; use crate::exact_math::symbolic::size; use crate::functions::function::apply_operator; use crate::functions::function::apply_operator_reverse; -use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; +use crate::parsing::ast::*; use std::collections::HashMap; pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s + s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s + Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(s + Rationals::new(1, i)) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() + f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f + s.approx()), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v + v2), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) + f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v + f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v + (i1 as f64)), - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + (Null, Int(v)) => Int(v), + (Null, Float(f)) => Float(f), + (Null, InterpreterVector(vec)) => InterpreterVector(vec.clone()), + (InterpreterVector(vec), Null) => 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 + 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), + (Int(v), Float(f)) => Float((v as f64) + f), + (Float(v), Float(f)) => Float(v + f), + (Float(v), Int(i1)) => Float(v + (i1 as f64)), + (InterpreterVector(vec), InterpreterVector(vec2)) => { let mut res = Vec::new(); vec.into_iter() .zip(vec2.into_iter()) .map(|(x, y)| add(x.clone(), y.clone(), ram)) .for_each(|s| res.push(s)); - Parameters::InterpreterVector(Box::from(res)) + InterpreterVector(Box::from(res)) } - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Bool(_), Int(i)) => Int(i), + (Bool(_), Float(i)) => Float(i), + (Int(i), Bool(_)) => Int(i), + (Float(i), Bool(_)) => Float(i), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + (Identifier(s), Identifier(s2)) => match ram { None => { if s != s2 { - Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Identifier(s2.clone())), + Plus( + Box::from(Identifier(s.clone())), + Box::from(Identifier(s2.clone())), ) } else { - Parameters::Var(Box::from(Parameters::Int(2)), 1, s.clone()) + Var(Box::from(Int(2)), 1, s.clone()) } } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - add, - ), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, add), }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Int(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + (Identifier(s), Int(i)) => match ram { + None => Plus(Box::from(Identifier(s.clone())), Box::from(Int(i))), + Some(_) => apply_operator(Identifier(s), Int(i), ram, add), }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Identifier(s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + (Null, Identifier(s)) => match ram { + None => Identifier(s.clone()), + Some(_) => apply_operator(Identifier(s), Null, ram, add), }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Rational(s.clone())), - Box::from(Parameters::Identifier(ss.clone())), - ), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - add, + (Rational(s), Identifier(ss)) => match ram { + None => Plus( + Box::from(Rational(s.clone())), + Box::from(Identifier(ss.clone())), ), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, add) + } }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(ss.clone())), - Box::from(Parameters::Rational(s.clone())), - ), - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - add, + (Identifier(ss), Rational(s)) => match ram { + None => Plus( + Box::from(Identifier(ss.clone())), + Box::from(Rational(s.clone())), ), + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, add), }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Identifier(s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, add), + (Identifier(s), Null) => match ram { + None => Identifier(s.clone()), + Some(_) => apply_operator(Identifier(s), Null, ram, add), }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s)), - Box::from(Parameters::Int(i.clone())), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, add), + (Int(i), Identifier(s)) => match ram { + None => Plus(Box::from(Identifier(s)), Box::from(Int(i.clone()))), + Some(_) => apply_operator(Identifier(s), Int(i), ram, add), }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Float(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + (Identifier(s), Float(i)) => match ram { + None => Plus(Box::from(Identifier(s.clone())), Box::from(Float(i))), + Some(_) => apply_operator(Identifier(s), Float(i), ram, add), }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Float(i)), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, add), + (Float(i), Identifier(s)) => match ram { + None => Plus(Box::from(Identifier(s.clone())), Box::from(Float(i))), + Some(_) => apply_operator(Identifier(s), Float(i), ram, add), }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), + (Identifier(s), InterpreterVector(vec)) => match ram { + None => Null, + Some(_) => apply_operator(Identifier(s), InterpreterVector(vec.clone()), ram, add), }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - add, - ), + (InterpreterVector(vec), Identifier(s)) => match ram { + None => Null, + Some(_) => apply_operator(Identifier(s), InterpreterVector(vec.clone()), ram, add), }, - (Bool(b), Parameters::Identifier(s)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, add), + (Bool(b), Identifier(s)) => match ram { + None => Null, + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, add), }, - (Parameters::Identifier(s), Bool(b)) => match ram { - None => Parameters::Null, - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, add), + (Identifier(s), Bool(b)) => match ram { + None => Null, + Some(_) => apply_operator(Identifier(s), Bool(b), ram, add), }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { - let first = Parameters::Plus( + (Plus(s1, s2), Plus(s3, s4)) => { + let first = Plus( Box::from(add(*s1.clone(), *s3.clone(), ram)), Box::from(add(*s2.clone(), *s4.clone(), ram)), ); - let second = Parameters::Plus( + let second = Plus( Box::from(add(*s1.clone(), *s4.clone(), ram)), Box::from(add(*s2.clone(), *s3.clone(), ram)), ); @@ -170,14 +127,14 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + (Plus(s1, s2), Identifier(s3)) => { + let first = Plus( + Box::from(add(*s1.clone(), Identifier(s3.clone()), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + Box::from(add(*s2.clone(), Identifier(s3.clone()), ram)), ); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -187,14 +144,14 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + (Identifier(s3), Plus(s1, s2)) => { + let first = Plus( + Box::from(add(*s1.clone(), Identifier(s3.clone()), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(add(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + Box::from(add(*s2.clone(), Identifier(s3.clone()), ram)), ); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -204,15 +161,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Int(i), ram)), - ); + (Int(i), Plus(s1, s2)) => { + let first = Plus(Box::from(add(*s1.clone(), Int(i), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Int(i), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -222,15 +173,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Int(i), ram)), - ); + (Plus(s1, s2), Int(i)) => { + let first = Plus(Box::from(add(*s1.clone(), Int(i), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Int(i), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -240,15 +185,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); + (Plus(s1, s2), Float(f)) => { + let first = Plus(Box::from(add(*s1.clone(), Float(f), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Float(f), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -258,15 +197,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Float(f), ram)), - ); + (Float(f), Plus(s1, s2)) => { + let first = Plus(Box::from(add(*s1.clone(), Float(f), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(add(*s2.clone(), Float(f), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -276,15 +209,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r), ram)), - ); + (Plus(s1, s2), Rational(r)) => { + 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 { @@ -294,15 +221,9 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add(*s1.clone(), Parameters::Rational(r), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(add(*s2.clone(), Parameters::Rational(r), ram)), - ); + (Rational(r), Plus(s1, s2)) => { + 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 { @@ -312,22 +233,14 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Plus(s1, s2), Var(x, y, z)) => { + let first = Plus( + Box::from(add(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(add( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + Box::from(add(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -338,22 +251,14 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Parameters::Plus( - Box::from(add( - Parameters::Var(x.clone(), y, z.clone()), - *s1.clone(), - ram, - )), + (Var(x, y, z), Plus(s1, s2)) => { + let first = Plus( + Box::from(add(Var(x.clone(), y, z.clone()), *s1.clone(), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(add( - Parameters::Var(x.clone(), y, z.clone()), - *s2.clone(), - ram, - )), + Box::from(add(Var(x.clone(), y, z.clone()), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -365,144 +270,116 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Parameters::Plus(s1.clone(), s2.clone()), + (Plus(s1, s2), Null) => Plus(s1.clone(), s2.clone()), - (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + (Null, Plus(s1, s2)) => Plus(s1.clone(), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Mul(s3.clone(), s4.clone())), + (Mul(s1, s2), Mul(s3, s4)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Mul(s3.clone(), s4.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + (Mul(s1, s2), Identifier(s)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ), - (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), + (Identifier(s), Mul(s1, s2)) => Plus( + Box::from(Var(Box::from(Int(1)), 1, s.clone())), + Box::from(Mul(s1.clone(), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Int(i)), - ), + (Mul(s1, s2), Int(i)) => Plus(Box::from(Mul(s1.clone(), s2.clone())), Box::from(Int(i))), - (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - ), + (Int(i), Mul(s1, s2)) => Plus(Box::from(Int(i)), Box::from(Mul(s1.clone(), s2.clone()))), - (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Float(f)), - ), + (Mul(s1, s2), Float(f)) => { + Plus(Box::from(Mul(s1.clone(), s2.clone())), Box::from(Float(f))) + } - (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - ), + (Float(f), Mul(s1, s2)) => { + Plus(Box::from(Float(f)), Box::from(Mul(s1.clone(), s2.clone()))) + } - (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Rational(r)), + (Mul(s1, s2), Rational(r)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Rational(r)), ), - (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Rational(r)), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), + (Rational(r), Mul(s1, s2)) => Plus( + Box::from(Rational(r)), + Box::from(Mul(s1.clone(), s2.clone())), ), - (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y, z.clone())), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), + (Var(x, y, z), Mul(s1, s2)) => Plus( + Box::from(Var(x.clone(), y, z.clone())), + Box::from(Mul(s1.clone(), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Var(x.clone(), y, z.clone())), + (Mul(s1, s2), Var(x, y, z)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Var(x.clone(), y, z.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Plus(s3.clone(), s4.clone())), + (Mul(s1, s2), Plus(s3, s4)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Plus(s3.clone(), s4.clone())), ), - (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Plus(s3.clone(), s4.clone())), - Box::from(Parameters::Mul(s1.clone(), s2.clone())), + (Plus(s3, s4), Mul(s1, s2)) => Plus( + Box::from(Plus(s3.clone(), s4.clone())), + Box::from(Mul(s1.clone(), s2.clone())), ), - (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul(s1.clone(), s2.clone()), + (Null, Mul(s1, s2)) => Mul(s1.clone(), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + (Mul(s1, s2), Null) => Mul(s1.clone(), s2.clone()), - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + (Var(x, y, z), Var(x1, y1, z1)) => { if z == z1 && y == y1 { - Parameters::Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) + Var(Box::from(add(*x.clone(), *x1.clone(), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(x1.clone(), y1.clone(), z1.clone())), + Plus( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(x1.clone(), y1.clone(), z1.clone())), ) } } - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + (Var(x, y, z), Identifier(s)) => { if z == s && y == 1 { - Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + Var(Box::from(add(*x.clone(), Int(1), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Plus( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + (Identifier(s), Var(x, y, z)) => { if z == s && y == 1 { - Parameters::Var(Box::from(add(*x.clone(), Parameters::Int(1), ram)), y, z) + Var(Box::from(add(*x.clone(), Int(1), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Plus( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var(x, y, z)), - ), + (Int(i), Var(x, y, z)) => Plus(Box::from(Int(i)), Box::from(Var(x, y, z))), - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Int(i)), - ), + (Var(x, y, z), Int(i)) => Plus(Box::from(Var(x, y, z)), Box::from(Int(i))), - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Var(x, y, z)), - ), + (Float(f), Var(x, y, z)) => Plus(Box::from(Float(f)), Box::from(Var(x, y, z))), - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Float(f)), - ), + (Var(x, y, z), Float(f)) => Plus(Box::from(Var(x, y, z)), Box::from(Float(f))), - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Rational(r)), - Box::from(Parameters::Var(x, y, z)), - ), + (Rational(r), Var(x, y, z)) => Plus(Box::from(Rational(r)), Box::from(Var(x, y, z))), - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Rational(r)), - ), + (Var(x, y, z), Rational(r)) => Plus(Box::from(Var(x, y, z)), Box::from(Rational(r))), - _ => Parameters::Identifier( - "@Those two values are incompatible with the + operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the + operator".to_string()), } } diff --git a/src/functions/divide.rs b/src/functions/divide.rs index dcf2b8b..7961cba 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -2,8 +2,8 @@ use crate::exact_math::rationals::Rationals; use crate::exact_math::symbolic::size; use crate::functions::function::apply_operator; use crate::functions::function::apply_operator_reverse; -use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; +use crate::parsing::ast::*; use std::collections::HashMap; use super::add::add; @@ -15,162 +15,123 @@ pub fn divide( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Rational(Rationals::new(v2, v)), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) / f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v / f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v / (i1 as f64)), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - - (Parameters::Rational(s), Parameters::Null) => { - Parameters::Rational(Rationals::new(1, 1) / s) - } - (Parameters::Null, Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, 1) / s) - } - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s / s2), - - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s / Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, i) / s) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() / f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f / s.approx()), - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Null, Int(v)) => Int(v), + (Null, Float(f)) => Float(f), + (Int(v), Null) => Int(v), + (Float(f), Null) => Float(f), + (Int(v), Int(v2)) => Rational(Rationals::new(v2, v)), + (Int(v), Float(f)) => Float((v as f64) / f), + (Float(v), Float(f)) => Float(v / f), + (Float(v), Int(i1)) => Float(v / (i1 as f64)), + (Null, InterpreterVector(vec)) => InterpreterVector(vec.clone()), + (InterpreterVector(vec), Null) => InterpreterVector(vec.clone()), + + (Rational(s), Null) => Rational(Rationals::new(1, 1) / s), + (Null, Rational(s)) => Rational(Rationals::new(1, 1) / s), + (Rational(s), Rational(s2)) => Rational(s / s2), + + (Rational(s), Int(i)) => Rational(s / Rationals::new(1, i)), + (Int(i), Rational(s)) => Rational(Rationals::new(1, i) / s), + (Rational(s), Float(f)) => Float(s.approx() / f), + (Float(f), Rational(s)) => Float(f / s.approx()), + (Bool(_), Int(i)) => Int(i), + (Bool(_), Float(i)) => Float(i), + (Int(i), Bool(_)) => Int(i), + (Float(i), Bool(_)) => Float(i), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - divide, - ), + (Identifier(s), Identifier(s2)) => match ram { + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, divide), None => divide( - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s2.clone()), + Var(Box::from(Int(1)), 1, s.clone()), + Var(Box::from(Int(1)), 1, s2.clone()), ram, ), }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - divide, - ), - None => Parameters::Div( - Box::from(Parameters::Int(s.over)), - Box::from(Parameters::Var( - Box::from(Parameters::Int(s.under)), - 1, - ss.clone(), - )), + (Rational(s), Identifier(ss)) => match ram { + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, divide) + } + None => Div( + Box::from(Int(s.over)), + Box::from(Var(Box::from(Int(s.under)), 1, ss.clone())), ), }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - divide, - ), + (Identifier(ss), Rational(s)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, divide), None => match s.invert() { - Ok(r) => Parameters::Var(Box::from(Parameters::Rational(r)), 1, ss.clone()), - Err(_) => Parameters::Null, + Ok(r) => Var(Box::from(Rational(r)), 1, ss.clone()), + Err(_) => Null, }, }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Var( - Box::new(Parameters::Rational(Rationals::new(i, 1))), - 1, - s.clone(), - ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, divide), + (Identifier(s), Int(i)) => match ram { + None => Var(Box::new(Rational(Rationals::new(i, 1))), 1, s.clone()), + Some(_) => apply_operator(Identifier(s), Int(i), ram, divide), }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Div( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + (Int(i), Identifier(s)) => match ram { + None => Div( + Box::from(Int(i)), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ), Some(_) => { - let v = apply_operator( - Parameters::Identifier(s.clone()), - Parameters::Int(i), - ram, - divide, - ); + let v = apply_operator(Identifier(s.clone()), Int(i), ram, divide); match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => divide(Parameters::Int(i), Parameters::Identifier(s.clone()), None), + Float(i) => Float(1.0 / i), + _ => divide(Int(i), Identifier(s.clone()), None), } } }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Div( - Box::new(Parameters::Int(1)), - Box::new(Parameters::Var(Box::new(Parameters::Int(1)), 1, s.clone())), + (Null, Identifier(s)) => match ram { + None => Div( + Box::new(Int(1)), + Box::new(Var(Box::new(Int(1)), 1, s.clone())), ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide), + Some(_) => apply_operator(Identifier(s), Null, ram, divide), }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Div( - Box::new(Parameters::Int(1)), - Box::new(Parameters::Var(Box::new(Parameters::Int(1)), 1, s.clone())), + (Identifier(s), Null) => match ram { + None => Div( + Box::new(Int(1)), + Box::new(Var(Box::new(Int(1)), 1, s.clone())), ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, divide), + Some(_) => apply_operator(Identifier(s), Null, ram, divide), }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Var( - Box::from(Parameters::Rational(Rationals::rationalize(1.0 / i))), + (Identifier(s), Float(i)) => match ram { + None => Var( + Box::from(Rational(Rationals::rationalize(1.0 / i))), 1, s.clone(), ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide), + Some(_) => apply_operator(Identifier(s), Float(i), ram, divide), }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Div( - Box::from(Parameters::Int(Rationals::rationalize(i).over)), - Box::from(Parameters::Var( - Box::from(Parameters::Int(Rationals::rationalize(i).under)), + (Float(i), Identifier(s)) => match ram { + None => Div( + Box::from(Int(Rationals::rationalize(i).over)), + Box::from(Var( + Box::from(Int(Rationals::rationalize(i).under)), 1, s.clone(), )), ), Some(_) => { - let v = - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, divide); + let v = apply_operator(Identifier(s), Float(i), ram, divide); match v { - Parameters::Float(i) => Parameters::Float(1.0 / i), - _ => Parameters::Null, + Float(i) => Float(1.0 / i), + _ => Null, } } }, - (Bool(b), Parameters::Identifier(s)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, divide), + (Bool(b), Identifier(s)) => match ram { + None => Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, divide), }, - (Parameters::Identifier(s), Bool(b)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, divide), + (Identifier(s), Bool(b)) => match ram { + None => Bool(b), + Some(_) => apply_operator(Identifier(s), Bool(b), ram, divide), }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { + (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), @@ -178,103 +139,95 @@ pub fn divide( ); first } - (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { + (Plus(s1, s2), Identifier(s3)) => { let first = add( - divide( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), - ram, - ), - divide( - *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), - ram, - ), + 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 } - (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { - let first = Parameters::Div( - Box::from(Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone())), + (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 } - (Parameters::Int(i), Parameters::Plus(s1, s2)) => { - let first = Parameters::Div( - Box::from(Parameters::Int(i)), + (Int(i), Plus(s1, s2)) => { + let first = Div( + Box::from(Int(i)), Box::from(add(*s1.clone(), *s2.clone(), ram)), ); first } - (Parameters::Plus(s1, s2), Parameters::Int(i)) => { + (Plus(s1, s2), Int(i)) => { let first = add( - divide(*s1.clone(), Parameters::Int(i), ram), - divide(*s2.clone(), Parameters::Int(i), ram), + divide(*s1.clone(), Int(i), ram), + divide(*s2.clone(), Int(i), ram), ram, ); first } - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Div( - Box::from(Parameters::Float(f)), + (Float(f), Plus(s1, s2)) => { + let first = Div( + Box::from(Float(f)), Box::from(add(*s1.clone(), *s2.clone(), ram)), ); first } - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { + (Plus(s1, s2), Float(f)) => { let first = add( - divide(*s1.clone(), Parameters::Float(f), ram), - divide(*s2.clone(), Parameters::Float(f), ram), + divide(*s1.clone(), Float(f), ram), + divide(*s2.clone(), Float(f), ram), ram, ); first } - (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { - let first = Parameters::Div( - Box::from(Parameters::Rational(r)), + (Rational(r), Plus(s1, s2)) => { + let first = Div( + Box::from(Rational(r)), Box::from(add(*s1.clone(), *s2.clone(), ram)), ); first } - (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { + (Plus(s1, s2), Rational(r)) => { let first = add( - divide(*s1.clone(), Parameters::Rational(r), ram), - divide(*s2.clone(), Parameters::Rational(r), ram), + divide(*s1.clone(), Rational(r), ram), + divide(*s2.clone(), Rational(r), ram), ram, ); first } - (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => Parameters::Div( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), + (Var(x, y, z), Plus(s1, s2)) => Div( + Box::from(Var(x.clone(), y.clone(), z.clone())), Box::from(add(*s1.clone(), *s2.clone(), ram)), ), - (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => add( - divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), - divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + (Plus(s1, s2), Var(x, y, z)) => add( + divide(*s1.clone(), Var(x.clone(), y, z.clone()), ram), + divide(*s2.clone(), Var(x.clone(), y, z.clone()), ram), ram, ), - (Parameters::Null, Parameters::Plus(s1, s2)) => add(*s1.clone(), *s2.clone(), ram), + (Null, Plus(s1, s2)) => add(*s1.clone(), *s2.clone(), ram), - (Parameters::Plus(s1, s2), Parameters::Null) => add(*s1.clone(), *s2.clone(), ram), + (Plus(s1, s2), Null) => add(*s1.clone(), *s2.clone(), ram), - (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { + (Var(x, y, z), Mul(s1, s2)) => { let first = mult( - divide(Parameters::Var(x.clone(), y, z.clone()), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Var(x.clone(), y, z.clone()), *s1.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + divide(Int(1), *s1.clone(), ram), + divide(Var(x.clone(), y, z.clone()), *s2.clone(), ram), ram, ); @@ -287,15 +240,15 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { + (Mul(s1, s2), Var(x, y, z)) => { let first = mult( - divide(*s1.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + divide(*s1.clone(), Var(x.clone(), y, z.clone()), ram), *s2.clone(), ram, ); let second = mult( *s1.clone(), - divide(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + divide(*s2.clone(), Var(x.clone(), y, z.clone()), ram), ram, ); @@ -308,7 +261,7 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { + (Mul(s1, s2), Mul(s3, s4)) => { let first = mult( divide(*s1.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), *s2.clone(), @@ -328,23 +281,15 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => { + (Mul(s1, s2), Identifier(s)) => { let first = mult( - divide( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), + divide(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), *s2.clone(), ram, ); let second = mult( *s1.clone(), - divide( - *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), + divide(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), ram, ); @@ -357,23 +302,15 @@ pub fn divide( } } - (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => { + (Identifier(s), Mul(s1, s2)) => { let first = mult( - divide( - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - *s1.clone(), - ram, - ), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Var(Box::from(Int(1)), 1, s.clone()), *s1.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide( - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - *s2.clone(), - ram, - ), + divide(Int(1), *s1.clone(), ram), + divide(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram), ram, ); @@ -386,17 +323,9 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Int(i)) => { - let first = mult( - divide(*s1.clone(), Parameters::Int(i), ram), - *s2.clone(), - ram, - ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Int(i), ram), - ram, - ); + (Mul(s1, s2), Int(i)) => { + let first = mult(divide(*s1.clone(), Int(i), ram), *s2.clone(), ram); + let second = mult(*s1.clone(), divide(*s2.clone(), Int(i), ram), ram); let (ss1, ss2) = (size(&first), size(&second)); @@ -407,15 +336,15 @@ pub fn divide( } } - (Parameters::Int(i), Parameters::Mul(s1, s2)) => { + (Int(i), Mul(s1, s2)) => { let first = mult( - divide(Parameters::Int(i), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Int(i), *s1.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Int(i), *s2.clone(), ram), + divide(Int(1), *s1.clone(), ram), + divide(Int(i), *s2.clone(), ram), ram, ); @@ -428,17 +357,9 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Float(f)) => { - let first = mult( - divide(*s1.clone(), Parameters::Float(f), ram), - *s2.clone(), - ram, - ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Float(f), ram), - ram, - ); + (Mul(s1, s2), Float(f)) => { + let first = mult(divide(*s1.clone(), Float(f), ram), *s2.clone(), ram); + let second = mult(*s1.clone(), divide(*s2.clone(), Float(f), ram), ram); let (ss1, ss2) = (size(&first), size(&second)); @@ -449,15 +370,15 @@ pub fn divide( } } - (Parameters::Float(f), Parameters::Mul(s1, s2)) => { + (Float(f), Mul(s1, s2)) => { let first = mult( - divide(Parameters::Float(f), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Float(f), *s1.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Float(f), *s2.clone(), ram), + divide(Int(1), *s1.clone(), ram), + divide(Float(f), *s2.clone(), ram), ram, ); @@ -470,17 +391,9 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Rational(r)) => { - let first = mult( - divide(*s1.clone(), Parameters::Rational(r), ram), - *s2.clone(), - ram, - ); - let second = mult( - *s1.clone(), - divide(*s2.clone(), Parameters::Rational(r), ram), - ram, - ); + (Mul(s1, s2), Rational(r)) => { + let first = mult(divide(*s1.clone(), Rational(r), ram), *s2.clone(), ram); + let second = mult(*s1.clone(), divide(*s2.clone(), Rational(r), ram), ram); let (ss1, ss2) = (size(&first), size(&second)); @@ -491,15 +404,15 @@ pub fn divide( } } - (Parameters::Rational(r), Parameters::Mul(s1, s2)) => { + (Rational(r), Mul(s1, s2)) => { let first = mult( - divide(Parameters::Rational(r), *s1.clone(), ram), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Rational(r), *s1.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), - divide(Parameters::Rational(r), *s2.clone(), ram), + divide(Int(1), *s1.clone(), ram), + divide(Rational(r), *s2.clone(), ram), ram, ); @@ -512,14 +425,14 @@ pub fn divide( } } - (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => { + (Mul(s1, s2), Plus(s3, s4)) => { let first = mult( divide(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - divide(Parameters::Int(1), *s2.clone(), ram), + divide(Int(1), *s2.clone(), ram), ram, ); let second = mult( - divide(Parameters::Int(1), *s1.clone(), ram), + divide(Int(1), *s1.clone(), ram), divide(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), ram, ); @@ -533,7 +446,7 @@ pub fn divide( } } - (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => { + (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), @@ -542,18 +455,18 @@ pub fn divide( first } - (Parameters::Null, Parameters::Mul(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), + (Null, Mul(s1, s2)) => mult(*s1.clone(), *s2.clone(), ram), - (Parameters::Mul(s1, s2), Parameters::Null) => mult(*s1.clone(), *s2.clone(), ram), + (Mul(s1, s2), Null) => mult(*s1.clone(), *s2.clone(), ram), - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + (Var(x, y, z), Var(x1, y1, z1)) => { if z == z1 { - Parameters::Var(Box::from(divide(*x.clone(), *x1.clone(), ram)), y - y1, z) + Var(Box::from(divide(*x.clone(), *x1.clone(), ram)), y - y1, z) } else { - Parameters::Div( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(divide(Parameters::Int(1), *x1.clone(), ram)), + Div( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var( + Box::from(divide(Int(1), *x1.clone(), ram)), y1.clone(), z1.clone(), )), @@ -561,29 +474,25 @@ pub fn divide( } } - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + (Var(x, y, z), Identifier(s)) => { if z == s { - Parameters::Var(Box::from(x.clone()), y - 1, z) + Var(Box::from(x.clone()), y - 1, z) } else { - Parameters::Div( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Div( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + (Identifier(s), Var(x, y, z)) => { if z == s { - Parameters::Var( - Box::from(divide(Parameters::Int(1), *x.clone(), ram)), - 1 - y, - z, - ) + Var(Box::from(divide(Int(1), *x.clone(), ram)), 1 - y, z) } else { - Parameters::Div( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Var( - Box::from(mult(Parameters::Int(1), *x.clone(), ram)), + 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(), )), @@ -591,61 +500,47 @@ pub fn divide( } } - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(divide(Parameters::Int(i), *x.clone(), ram)), - -y, - z.clone(), - ), + (Int(i), Var(x, y, z)) => Var(Box::from(divide(Int(i), *x.clone(), ram)), -y, z.clone()), - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Var( - Box::from(divide(*x.clone(), Parameters::Int(i), ram)), - y, - z.clone(), - ), + (Var(x, y, z), Int(i)) => Var(Box::from(divide(*x.clone(), Int(i), ram)), y, z.clone()), - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(divide(Parameters::Float(f), *x.clone(), ram)), - -y, - z.clone(), - ), + (Float(f), Var(x, y, z)) => { + Var(Box::from(divide(Float(f), *x.clone(), ram)), -y, z.clone()) + } - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Var( - Box::from(divide(*x.clone(), Parameters::Float(f), ram)), - y, - z.clone(), - ), + (Var(x, y, z), Float(f)) => Var(Box::from(divide(*x.clone(), Float(f), ram)), y, z.clone()), - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(divide(Parameters::Rational(r), *x.clone(), ram)), + (Rational(r), Var(x, y, z)) => Var( + Box::from(divide(Rational(r), *x.clone(), ram)), -y, z.clone(), ), - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( - Box::from(divide(*x.clone(), Parameters::Rational(r), ram)), + (Var(x, y, z), Rational(r)) => Var( + Box::from(divide(*x.clone(), Rational(r), ram)), y, z.clone(), ), - (Parameters::Var(x, y, z), Parameters::Div(s1, s2)) => { + (Var(x, y, z), Div(s1, s2)) => { let first = divide( - mult(Parameters::Var(x.clone(), y, z.clone()), *s2.clone(), ram), + mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram), *s1.clone(), ram, ); first } - (Parameters::Div(s1, s2), Parameters::Var(x, y, z)) => { + (Div(s1, s2), Var(x, y, z)) => { let first = divide( *s1.clone(), - mult(*s2.clone(), Parameters::Var(x.clone(), y, z.clone()), ram), + mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram), ram, ); first } - (Parameters::Mul(s1, s2), Parameters::Div(s3, s4)) => { + (Mul(s1, s2), Div(s3, s4)) => { let first = divide( mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), *s3.clone(), @@ -654,7 +549,7 @@ pub fn divide( first } - (Parameters::Div(s1, s2), Parameters::Mul(s3, s4)) => { + (Div(s1, s2), Mul(s3, s4)) => { let first = divide( *s1.clone(), mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), @@ -663,7 +558,7 @@ pub fn divide( first } - (Parameters::Div(s1, s2), Parameters::Div(s3, s4)) => { + (Div(s1, s2), Div(s3, s4)) => { let first = divide( mult(*s1.clone(), *s4.clone(), ram), mult(*s2.clone(), *s3.clone(), ram), @@ -672,79 +567,55 @@ pub fn divide( first } - (Parameters::Div(s1, s2), Parameters::Identifier(s)) => { + (Div(s1, s2), Identifier(s)) => { let first = divide( *s1.clone(), - mult( - *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - ), + mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), ram, ); first } - (Parameters::Identifier(s), Parameters::Div(s1, s2)) => { + (Identifier(s), Div(s1, s2)) => { let first = divide( - mult( - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - *s2.clone(), - ram, - ), + mult(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram), *s1.clone(), ram, ); first } - (Parameters::Div(s1, s2), Parameters::Int(i)) => { - let first = divide(mult(*s1.clone(), Parameters::Int(i), ram), *s2.clone(), ram); + (Div(s1, s2), Int(i)) => { + let first = divide(mult(*s1.clone(), Int(i), ram), *s2.clone(), ram); first } - (Parameters::Int(i), Parameters::Div(s1, s2)) => { - let first = divide(mult(Parameters::Int(i), *s2.clone(), ram), *s1.clone(), ram); + (Int(i), Div(s1, s2)) => { + let first = divide(mult(Int(i), *s2.clone(), ram), *s1.clone(), ram); first } - (Parameters::Div(s1, s2), Parameters::Float(f)) => { - let first = divide( - mult(*s1.clone(), Parameters::Float(f), ram), - *s2.clone(), - ram, - ); + (Div(s1, s2), Float(f)) => { + let first = divide(mult(*s1.clone(), Float(f), ram), *s2.clone(), ram); first } - (Parameters::Float(f), Parameters::Div(s1, s2)) => { - let first = divide( - mult(Parameters::Float(f), *s2.clone(), ram), - *s1.clone(), - ram, - ); + (Float(f), Div(s1, s2)) => { + let first = divide(mult(Float(f), *s2.clone(), ram), *s1.clone(), ram); first } - (Parameters::Div(s1, s2), Parameters::Rational(r)) => { - let first = divide( - mult(*s1.clone(), Parameters::Rational(r), ram), - *s2.clone(), - ram, - ); + (Div(s1, s2), Rational(r)) => { + let first = divide(mult(*s1.clone(), Rational(r), ram), *s2.clone(), ram); first } - (Parameters::Rational(r), Parameters::Div(s1, s2)) => { - let first = divide( - mult(Parameters::Rational(r), *s2.clone(), ram), - *s1.clone(), - ram, - ); + (Rational(r), Div(s1, s2)) => { + let first = divide(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram); first } - (Parameters::Div(s1, s2), Parameters::Plus(s3, s4)) => { + (Div(s1, s2), Plus(s3, s4)) => { let first = divide( mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), *s2.clone(), @@ -753,7 +624,7 @@ pub fn divide( first } - (Parameters::Plus(s3, s4), Parameters::Div(s1, s2)) => { + (Plus(s3, s4), Div(s1, s2)) => { let first = divide( mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), *s1.clone(), @@ -762,11 +633,9 @@ pub fn divide( first } - (Parameters::Null, Parameters::Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), + (Null, Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), - (Parameters::Div(s1, s2), Parameters::Null) => divide(*s2.clone(), *s1.clone(), ram), - _ => Parameters::Identifier( - "@Those two values are incompatible with the / operator".to_string(), - ), + (Div(s1, s2), Null) => divide(*s2.clone(), *s1.clone(), ram), + _ => Identifier("@Those two values are incompatible with the / operator".to_string()), } } diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 46ab81c..4e0bb72 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -12,188 +12,151 @@ pub fn minus( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(-v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(-f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(-v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(-f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v - v2), - - (Parameters::Rational(s), Parameters::Null) => { - Parameters::Rational(Rationals::new(1, 0) - s) - } - - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.opposite()), - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s - s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s - Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(Rationals::new(1, i) - s) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() - f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f - s.approx()), - (Parameters::InterpreterVector(vec), Parameters::Null) => { + (Null, Int(v)) => Int(-v), + (Null, Float(f)) => Float(-f), + (Int(v), Null) => Int(-v), + (Float(f), Null) => Float(-f), + (Int(v), Int(v2)) => Int(v - v2), + + (Rational(s), Null) => Rational(Rationals::new(1, 0) - s), + + (Null, Rational(s)) => Rational(s.opposite()), + (Rational(s), Rational(s2)) => Rational(s - s2), + (Rational(s), Int(i)) => Rational(s - Rationals::new(1, i)), + (Int(i), Rational(s)) => Rational(Rationals::new(1, i) - s), + (Rational(s), Float(f)) => Float(s.approx() - f), + (Float(f), Rational(s)) => Float(f - s.approx()), + (InterpreterVector(vec), Null) => { let mut res = Vec::new(); vec.into_iter() - .map(|x| minus(Parameters::Null, x.clone(), ram)) + .map(|x| minus(Null, x.clone(), ram)) .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) + InterpreterVector(Box::from(res)) } - (Parameters::Null, Parameters::InterpreterVector(vec)) => { + (Null, InterpreterVector(vec)) => { let mut res = Vec::new(); vec.into_iter() - .map(|x| minus(Parameters::Null, x.clone(), ram)) + .map(|x| minus(Null, x.clone(), ram)) .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) + InterpreterVector(Box::from(res)) } - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + (InterpreterVector(vec), InterpreterVector(vec2)) => { let mut res = Vec::new(); vec.into_iter() .zip(vec2.into_iter()) .map(|(x, y)| minus(x.clone(), y.clone(), ram)) .for_each(|z| res.push(z)); - Parameters::InterpreterVector(Box::from(res)) + InterpreterVector(Box::from(res)) } - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) - f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v - f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v - (i1 as f64)), - - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Int(v), Float(f)) => Float((v as f64) - f), + (Float(v), Float(f)) => Float(v - f), + (Float(v), Int(i1)) => Float(v - (i1 as f64)), + + (Bool(_), Int(i)) => Int(i), + (Bool(_), Float(i)) => Float(i), + (Int(i), Bool(_)) => Int(i), + (Float(i), Bool(_)) => Float(i), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + (Identifier(s), Identifier(s2)) => match ram { None => { if s != s2 { - Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s2)), + Plus( + Box::from(Var(Box::from(Int(1)), 1, s)), + Box::from(Var(Box::from(Int(-1)), 1, s2)), ) } else { - Parameters::Int(0) + Int(0) } } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - minus, - ), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, minus), }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Identifier(s.clone())), - Box::from(Parameters::Int(-i)), - ), - Some(_) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, minus) - } + (Identifier(s), Int(i)) => match ram { + None => Plus(Box::from(Identifier(s.clone())), Box::from(Int(-i))), + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, minus), }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - None => Parameters::Var(Box::from(Parameters::Int(-1)), 1, s), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, minus), + (Null, Identifier(s)) => match ram { + None => Var(Box::from(Int(-1)), 1, s), + Some(_) => apply_operator(Identifier(s), Null, ram, minus), }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Var(Box::from(Parameters::Int(-1)), 1, s), - Some(_) => { - apply_operator_reverse(Parameters::Identifier(s), Parameters::Null, ram, minus) - } + (Identifier(s), Null) => match ram { + None => Var(Box::from(Int(-1)), 1, s), + Some(_) => apply_operator_reverse(Identifier(s), Null, ram, minus), }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Rational(s.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, ss)), - ), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - minus, + (Rational(s), Identifier(ss)) => match ram { + None => Plus( + Box::from(Rational(s.clone())), + Box::from(Var(Box::from(Int(-1)), 1, ss)), ), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, minus) + } }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, ss)), - Box::from(Parameters::Rational(s.opposite())), - ), - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - minus, + (Identifier(ss), Rational(s)) => match ram { + None => Plus( + Box::from(Var(Box::from(Int(1)), 1, ss)), + Box::from(Rational(s.opposite())), ), + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, minus), }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), - ), + (Int(i), Identifier(s)) => match ram { + None => Plus(Box::from(Int(i)), Box::from(Var(Box::from(Int(-1)), 1, s))), Some(_) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, minus); + let v = apply_operator(Identifier(s), Int(i), ram, minus); match v { - Parameters::Int(i) => Parameters::Int(-i), - p => minus(Parameters::Int(0), p, None), + Int(i) => Int(-i), + p => minus(Int(0), p, None), } } }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s)), - Box::from(Parameters::Float(-i)), + (Identifier(s), Float(i)) => match ram { + None => Plus( + Box::from(Var(Box::from(Int(1)), 1, s)), + Box::from(Float(-i)), ), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus), + Some(_) => apply_operator(Identifier(s), Float(i), ram, minus), }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Plus( - Box::from(Parameters::Float(i)), - Box::from(Parameters::Var(Box::from(Parameters::Int(-1)), 1, s)), + (Float(i), Identifier(s)) => match ram { + None => Plus( + Box::from(Float(i)), + Box::from(Var(Box::from(Int(-1)), 1, s)), ), Some(_) => { - let v = apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, minus); + let v = apply_operator(Identifier(s), Float(i), ram, minus); match v { - Parameters::Float(i) => Parameters::Float(-i), - _ => Parameters::Null, + Float(i) => Float(-i), + _ => Null, } } }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - minus, - ), + (InterpreterVector(vec), Identifier(s)) => match ram { + None => InterpreterVector(vec.clone()), + Some(_) => { + apply_operator_reverse(InterpreterVector(vec.clone()), Identifier(s), ram, minus) + } }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - minus, - ), + (Identifier(s), InterpreterVector(vec)) => match ram { + None => InterpreterVector(vec.clone()), + Some(_) => apply_operator(Identifier(s), InterpreterVector(vec.clone()), ram, minus), }, - (Bool(b), Parameters::Identifier(s)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, minus), + (Bool(b), Identifier(s)) => match ram { + None => Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, minus), }, - (Parameters::Identifier(s), Bool(b)) => match ram { - None => Parameters::Bool(b), - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, minus), + (Identifier(s), Bool(b)) => match ram { + None => Bool(b), + Some(_) => apply_operator(Identifier(s), Bool(b), ram, minus), }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { - let first = Parameters::Plus( + (Plus(s1, s2), Plus(s3, s4)) => { + let first = Plus( Box::from(minus(*s1.clone(), *s3.clone(), ram)), Box::from(minus(*s2.clone(), *s4.clone(), ram)), ); - let second = Parameters::Plus( + let second = Plus( Box::from(minus(*s1.clone(), *s4.clone(), ram)), Box::from(minus(*s2.clone(), *s3.clone(), ram)), ); @@ -206,14 +169,14 @@ pub fn minus( first } } - (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Identifier(s3.clone()), ram)), + (Plus(s1, s2), Identifier(s3)) => { + let first = Plus( + Box::from(minus(*s1.clone(), Identifier(s3.clone()), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Identifier(s3.clone()), ram)), + Box::from(minus(*s2.clone(), Identifier(s3.clone()), ram)), ); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -223,14 +186,14 @@ pub fn minus( } } - (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Identifier(s3.clone()), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Identifier(s3), Plus(s1, s2)) => { + let first = Plus( + Box::from(minus(Identifier(s3.clone()), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Identifier(s3.clone()), *s2.clone(), ram)), + let second = Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Identifier(s3.clone()), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -240,14 +203,14 @@ pub fn minus( } } - (Parameters::Int(i), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Int(i), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Int(i), Plus(s1, s2)) => { + let first = Plus( + Box::from(minus(Int(i), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(i), *s2.clone(), ram)), + let second = Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Int(i), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -258,15 +221,9 @@ pub fn minus( } } - (Parameters::Plus(s1, s2), Parameters::Int(i)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Int(i), ram)), - ); + (Plus(s1, s2), Int(i)) => { + let first = Plus(Box::from(minus(*s1.clone(), Int(i), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(minus(*s2.clone(), Int(i), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -276,15 +233,9 @@ pub fn minus( } } - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Float(f), ram)), - ); + (Plus(s1, s2), Float(f)) => { + let first = Plus(Box::from(minus(*s1.clone(), Float(f), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(minus(*s2.clone(), Float(f), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -294,14 +245,14 @@ pub fn minus( } } - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Float(f), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Float(f), Plus(s1, s2)) => { + let first = Plus( + Box::from(minus(Float(f), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Float(f), *s2.clone(), ram)), + let second = Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Float(f), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -312,15 +263,9 @@ pub fn minus( } } - (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { - let first = Parameters::Plus( - Box::from(minus(*s1.clone(), Parameters::Rational(r), ram)), - s2.clone(), - ); - let second = Parameters::Plus( - s1.clone(), - Box::from(minus(*s2.clone(), Parameters::Rational(r), ram)), - ); + (Plus(s1, s2), Rational(r)) => { + let first = Plus(Box::from(minus(*s1.clone(), Rational(r), ram)), s2.clone()); + let second = Plus(s1.clone(), Box::from(minus(*s2.clone(), Rational(r), ram))); let (s1, s2) = (size(&first), size(&second)); if s1 > s2 { @@ -330,14 +275,14 @@ pub fn minus( } } - (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus(Parameters::Rational(r), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Rational(r), Plus(s1, s2)) => { + let first = Plus( + Box::from(minus(Rational(r), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Rational(r), *s2.clone(), ram)), + let second = Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Rational(r), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -348,22 +293,14 @@ pub fn minus( } } - (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => { - let first = Parameters::Plus( - Box::from(minus( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Plus(s1, s2), Var(x, y, z)) => { + let first = Plus( + Box::from(minus(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), s2.clone(), ); - let second = Parameters::Plus( + let second = Plus( s1.clone(), - Box::from(minus( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + Box::from(minus(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -374,22 +311,14 @@ pub fn minus( } } - (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(minus( - Parameters::Var(x.clone(), y, z.clone()), - *s1.clone(), - ram, - )), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Var(x, y, z), Plus(s1, s2)) => { + let first = Plus( + Box::from(minus(Var(x.clone(), y, z.clone()), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ); - let second = Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus( - Parameters::Var(x.clone(), y, z.clone()), - *s2.clone(), - ram, - )), + let second = Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Var(x.clone(), y, z.clone()), *s2.clone(), ram)), ); let (s1, s2) = (size(&first), size(&second)); @@ -401,131 +330,92 @@ pub fn minus( } } - (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Null, Plus(s1, s2)) => Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ), - (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s2.clone(), ram)), + (Plus(s1, s2), Null) => Plus( + Box::from(minus(Int(0), *s1.clone(), ram)), + Box::from(minus(Int(0), *s2.clone(), ram)), ), - (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s3.clone(), ram)), - s4.clone(), - )), + (Mul(s1, s2), Mul(s3, s4)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Mul(Box::from(minus(Int(0), *s3.clone(), ram)), s4.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(-1)), - 1, - s.clone(), - )), + (Mul(s1, s2), Identifier(s)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Var(Box::from(Int(-1)), 1, s.clone())), ), - (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Identifier(s), Mul(s1, s2)) => Plus( + Box::from(Var(Box::from(Int(1)), 1, s.clone())), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Int(-i)), - ), + (Mul(s1, s2), Int(i)) => Plus(Box::from(Mul(s1.clone(), s2.clone())), Box::from(Int(-i))), - (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Int(i), Mul(s1, s2)) => Plus( + Box::from(Int(i)), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Float(-f)), - ), + (Mul(s1, s2), Float(f)) => { + Plus(Box::from(Mul(s1.clone(), s2.clone())), Box::from(Float(-f))) + } - (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Float(f), Mul(s1, s2)) => Plus( + Box::from(Float(f)), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Rational(r.opposite())), + (Mul(s1, s2), Rational(r)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Rational(r.opposite())), ), - (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Rational(r)), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Rational(r), Mul(s1, s2)) => Plus( + Box::from(Rational(r)), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y, z.clone())), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Var(x, y, z), Mul(s1, s2)) => Plus( + Box::from(Var(x.clone(), y, z.clone())), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z.clone(), - )), + (Mul(s1, s2), Var(x, y, z)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Var(Box::from(minus(Int(0), *x.clone(), ram)), y, z.clone())), ), - (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( - Box::from(Parameters::Mul(s1.clone(), s2.clone())), - Box::from(Parameters::Plus( - Box::from(minus(Parameters::Int(0), *s3.clone(), ram)), - Box::from(minus(Parameters::Int(0), *s4.clone(), ram)), + (Mul(s1, s2), Plus(s3, s4)) => Plus( + Box::from(Mul(s1.clone(), s2.clone())), + Box::from(Plus( + Box::from(minus(Int(0), *s3.clone(), ram)), + Box::from(minus(Int(0), *s4.clone(), ram)), )), ), - (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( - Box::from(Parameters::Plus(s3.clone(), s4.clone())), - Box::from(Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - )), + (Plus(s3, s4), Mul(s1, s2)) => Plus( + Box::from(Plus(s3.clone(), s4.clone())), + Box::from(Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone())), ), - (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - ), + (Null, Mul(s1, s2)) => Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul( - Box::from(minus(Parameters::Int(0), *s1.clone(), ram)), - s2.clone(), - ), + (Mul(s1, s2), Null) => Mul(Box::from(minus(Int(0), *s1.clone(), ram)), s2.clone()), - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + (Var(x, y, z), Var(x1, y1, z1)) => { if z == z1 && y == y1 { - Parameters::Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) + Var(Box::from(minus(*x.clone(), *x1.clone(), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x1.clone(), ram)), + Plus( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var( + Box::from(minus(Int(0), *x1.clone(), ram)), y1.clone(), z1.clone(), )), @@ -533,79 +423,54 @@ pub fn minus( } } - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + (Var(x, y, z), Identifier(s)) => { if z == s && y == 1 { - Parameters::Var(Box::from(minus(*x.clone(), Parameters::Int(1), ram)), y, z) + Var(Box::from(minus(*x.clone(), Int(1), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(-1)), - 1, - s.clone(), - )), + Plus( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(Box::from(Int(-1)), 1, s.clone())), ) } } - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + (Identifier(s), Var(x, y, z)) => { if z == s && y == 1 { - Parameters::Var(Box::from(minus(Parameters::Int(1), *x.clone(), ram)), y, z) + Var(Box::from(minus(Int(1), *x.clone(), ram)), y, z) } else { - Parameters::Plus( - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), + Plus( + Box::from(Var( + Box::from(minus(Int(0), *x.clone(), ram)), y.clone(), z.clone(), )), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Int(i)), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), + (Int(i), Var(x, y, z)) => Plus( + Box::from(Int(i)), + Box::from(Var(Box::from(minus(Int(0), *x.clone(), ram)), y, z)), ), - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Int(-i)), - ), + (Var(x, y, z), Int(i)) => Plus(Box::from(Var(x, y, z)), Box::from(Int(-i))), - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Float(f)), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), + (Float(f), Var(x, y, z)) => Plus( + Box::from(Float(f)), + Box::from(Var(Box::from(minus(Int(0), *x.clone(), ram)), y, z)), ), - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Float(-f)), - ), + (Var(x, y, z), Float(f)) => Plus(Box::from(Var(x, y, z)), Box::from(Float(-f))), - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(Parameters::Rational(r)), - Box::from(Parameters::Var( - Box::from(minus(Parameters::Int(0), *x.clone(), ram)), - y, - z, - )), + (Rational(r), Var(x, y, z)) => Plus( + Box::from(Rational(r)), + Box::from(Var(Box::from(minus(Int(0), *x.clone(), ram)), y, z)), ), - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Plus( - Box::from(Parameters::Var(x, y, z)), - Box::from(Parameters::Rational(r.opposite())), - ), - _ => Parameters::Identifier( - "Those two values are incompatible with the - operator".to_string(), - ), + (Var(x, y, z), Rational(r)) => { + Plus(Box::from(Var(x, y, z)), Box::from(Rational(r.opposite()))) + } + _ => Identifier("Those two values are incompatible with the - operator".to_string()), } } diff --git a/src/functions/mult.rs b/src/functions/mult.rs index 7be755d..d5306b0 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -14,83 +14,75 @@ pub fn mult( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Int(v * v2), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64) * f), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v * f), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v * (i1 as f64)), - - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => Parameters::Rational(s * s2), - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Rational(s * Rationals::new(1, i)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Rational(s * Rationals::new(1, i)) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx() * f), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f * s.approx()), - (Parameters::Null, Parameters::InterpreterVector(vec)) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Null) => { - Parameters::InterpreterVector(vec.clone()) - } - (Parameters::InterpreterVector(vec), Parameters::Int(v)) => { + (Null, Int(v)) => Int(v), + (Null, Float(f)) => Float(f), + (Int(v), Null) => Int(v), + (Float(f), Null) => Float(f), + (Int(v), Int(v2)) => Int(v * v2), + (Int(v), Float(f)) => Float((v as f64) * f), + (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), 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()), + (Null, InterpreterVector(vec)) => InterpreterVector(vec.clone()), + (InterpreterVector(vec), Null) => InterpreterVector(vec.clone()), + (InterpreterVector(vec), Int(v)) => { let mut result = Vec::new(); vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Int(v), ram)) + .map(|x| mult(x.clone(), Int(v), ram)) .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) + InterpreterVector(Box::from(result)) } - (Parameters::Int(v), Parameters::InterpreterVector(vec)) => { + (Int(v), InterpreterVector(vec)) => { let mut result = Vec::new(); vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Int(v), ram)) + .map(|x| mult(x.clone(), Int(v), ram)) .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) + InterpreterVector(Box::from(result)) } - (Parameters::InterpreterVector(vec), Parameters::Float(v)) => { + (InterpreterVector(vec), Float(v)) => { let mut result = Vec::new(); vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Float(v), ram)) + .map(|x| mult(x.clone(), Float(v), ram)) .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) + InterpreterVector(Box::from(result)) } - (Parameters::Float(v), Parameters::InterpreterVector(vec)) => { + (Float(v), InterpreterVector(vec)) => { let mut result = Vec::new(); vec.into_iter() - .map(|x| mult(x.clone(), Parameters::Float(v), ram)) + .map(|x| mult(x.clone(), Float(v), ram)) .for_each(|x| result.push(x)); - Parameters::InterpreterVector(Box::from(result)) + InterpreterVector(Box::from(result)) } - (Parameters::InterpreterVector(vec), Parameters::InterpreterVector(vec2)) => { + (InterpreterVector(vec), InterpreterVector(vec2)) => { let mut res1 = Vec::new(); let mut is_matrix = true; let mut res = Vec::new(); let mut res2 = Vec::new(); vec.clone().into_iter().for_each(|x| match x { - Parameters::InterpreterVector(l) => res.push(l.to_vec()), + InterpreterVector(l) => res.push(l.to_vec()), p => { is_matrix = false; res1.push(p); } }); vec2.clone().into_iter().for_each(|x| match x { - Parameters::InterpreterVector(l) => res2.push(l.to_vec()), + InterpreterVector(l) => res2.push(l.to_vec()), _ => { is_matrix = false; } }); if !is_matrix { - let mut sum = Parameters::Null; + let mut sum = Null; (*vec) .into_iter() .zip(vec2.into_iter()) @@ -98,9 +90,9 @@ pub fn mult( .for_each(|x| sum = add(sum.clone(), x, ram)); match sum { - Parameters::Int(i) => Parameters::Int(i), - Parameters::Float(f) => Parameters::Float(f), - _ => Parameters::Float(f64::NAN), + Int(i) => Int(i), + Float(f) => Float(f), + _ => Float(f64::NAN), } } else { let matrix_result = mult_matrix(res, res2, ram); @@ -108,117 +100,92 @@ pub fn mult( let mut res = Vec::new(); if matrix_result.len() == 0 { - return Parameters::Null; + return Null; } matrix_result .into_iter() - .for_each(|x| res.push(Parameters::InterpreterVector(Box::from(x)))); + .for_each(|x| res.push(InterpreterVector(Box::from(x)))); - Parameters::InterpreterVector(Box::from(res)) + InterpreterVector(Box::from(res)) } } - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Bool(_), Int(i)) => Int(i), + (Bool(_), Float(i)) => Float(i), + (Int(i), Bool(_)) => Int(i), + (Float(i), Bool(_)) => Float(i), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + (Identifier(s), Identifier(s2)) => match ram { None => { if s == s2 { - Parameters::Var(Box::from(Parameters::Int(1)), 2, s.clone()) + Var(Box::from(Int(1)), 2, s.clone()) } else { - Parameters::Mul( - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), - Box::from(Parameters::Var( - Box::from(Parameters::Int(1)), - 1, - s2.clone(), - )), + Mul( + Box::from(Var(Box::from(Int(1)), 1, s.clone())), + Box::from(Var(Box::from(Int(1)), 1, s2.clone())), ) } } - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - mult, - ), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, mult), }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, mult), + None => Var(Box::from(Int(i)), 1, s.clone()), }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - mult, - ), + (Rational(s), Identifier(ss)) => match ram { + None => Var(Box::from(Rational(s.clone())), 1, ss.clone()), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, mult) + } }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - mult, - ), - None => Parameters::Var(Box::from(Parameters::Rational(s.clone())), 1, ss.clone()), + (Identifier(ss), Rational(s)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, mult), + None => Var(Box::from(Rational(s.clone())), 1, ss.clone()), }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Var(Box::from(Parameters::Int(i)), 1, s.clone()), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, mult), + (Int(i), Identifier(s)) => match ram { + None => Var(Box::from(Int(i)), 1, s.clone()), + Some(_) => apply_operator(Identifier(s), Int(i), ram, mult), }, - (Parameters::Identifier(s), Parameters::InterpreterVector(vec)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::InterpreterVector(vec.clone()), - ram, - mult, - ), + (Identifier(s), InterpreterVector(vec)) => match ram { + None => InterpreterVector(vec.clone()), + Some(_) => apply_operator(Identifier(s), InterpreterVector(vec.clone()), ram, mult), }, - (Parameters::InterpreterVector(vec), Parameters::Identifier(s)) => match ram { - None => Parameters::InterpreterVector(vec.clone()), - Some(_) => apply_operator_reverse( - Parameters::InterpreterVector(vec.clone()), - Parameters::Identifier(s), - ram, - mult, - ), + (InterpreterVector(vec), Identifier(s)) => match ram { + None => InterpreterVector(vec.clone()), + Some(_) => { + apply_operator_reverse(InterpreterVector(vec.clone()), Identifier(s), ram, mult) + } }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, mult), + None => Var(Box::from(Int(1)), 1, s.clone()), }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, mult), - None => Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, mult), + None => Var(Box::from(Int(1)), 1, s.clone()), }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + (Identifier(s), Float(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, mult), + None => Var(Box::from(Float(i)), 1, s.clone()), }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, mult), - None => Parameters::Var(Box::from(Parameters::Float(i)), 1, s.clone()), + (Float(i), Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, mult), + None => Var(Box::from(Float(i)), 1, s.clone()), }, - (Bool(b), Parameters::Identifier(s)) => match ram { - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, mult), - None => Parameters::Bool(b), + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, mult), + None => Bool(b), }, - (Parameters::Identifier(s), Bool(b)) => match ram { - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, mult), - None => Parameters::Bool(b), + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, mult), + None => Bool(b), }, - (Parameters::Plus(s1, s2), Parameters::Plus(s3, s4)) => { - let first = Parameters::Plus( + (Plus(s1, s2), Plus(s3, s4)) => { + let first = Plus( Box::from(add( mult(*s1.clone(), *s3.clone(), ram), mult(*s2.clone(), *s3.clone(), ram), @@ -230,7 +197,7 @@ pub fn mult( ram, )), ); - let second = Parameters::Plus( + let second = Plus( Box::from(add( mult(*s1.clone(), *s3.clone(), ram), mult(*s2.clone(), *s4.clone(), ram), @@ -251,130 +218,98 @@ pub fn mult( first } } - (Parameters::Plus(s1, s2), Parameters::Identifier(s3)) => { - let first = Parameters::Plus( + (Plus(s1, s2), Identifier(s3)) => { + let first = Plus( Box::from(mult( *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + Var(Box::from(Int(1)), 1, s3.clone()), ram, )), Box::from(mult( *s2.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s3.clone()), + Var(Box::from(Int(1)), 1, s3.clone()), ram, )), ); first } - (Parameters::Identifier(s3), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(mult( - Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone()), - *s1.clone(), - ram, - )), - Box::from(mult( - Parameters::Var(Box::new(Parameters::Int(1)), 1, s3.clone()), - *s2.clone(), - ram, - )), + (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 } - (Parameters::Int(i), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(mult(Parameters::Int(i), *s1.clone(), ram)), - Box::from(mult(Parameters::Int(i), *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 } - (Parameters::Plus(s1, s2), Parameters::Int(i)) => { - let first = Parameters::Plus( - Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - Box::from(mult(*s2.clone(), Parameters::Int(i), 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 } - (Parameters::Float(f), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(mult(Parameters::Float(f), *s1.clone(), ram)), - Box::from(mult(Parameters::Float(f), *s2.clone(), 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 } - (Parameters::Plus(s1, s2), Parameters::Float(f)) => { - let first = Parameters::Plus( - Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - Box::from(mult(*s2.clone(), Parameters::Float(f), 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 } - (Parameters::Rational(r), Parameters::Plus(s1, s2)) => { - let first = Parameters::Plus( - Box::from(mult(Parameters::Rational(r), *s1.clone(), ram)), - Box::from(mult(Parameters::Rational(r), *s2.clone(), ram)), + (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 } - (Parameters::Plus(s1, s2), Parameters::Rational(r)) => { - let first = Parameters::Plus( - Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), - Box::from(mult(*s2.clone(), Parameters::Rational(r), 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 } - (Parameters::Var(x, y, z), Parameters::Plus(s1, s2)) => Parameters::Plus( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Var(x, y, z), Plus(s1, s2)) => Plus( + Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ), - (Parameters::Plus(s1, s2), Parameters::Var(x, y, z)) => Parameters::Plus( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Plus(s1, s2), Var(x, y, z)) => Plus( + Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ), - (Parameters::Null, Parameters::Plus(s1, s2)) => Parameters::Plus(s1.clone(), s2.clone()), + (Null, Plus(s1, s2)) => Plus(s1.clone(), s2.clone()), - (Parameters::Plus(s1, s2), Parameters::Null) => Parameters::Plus(s1.clone(), s2.clone()), + (Plus(s1, s2), Null) => Plus(s1.clone(), s2.clone()), - (Parameters::Var(x, y, z), Parameters::Mul(s1, s2)) => { - let first = Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Var(x, y, z), Mul(s1, s2)) => { + let first = Mul( + Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), s2.clone(), ); - let second = Parameters::Mul( + let second = Mul( s1.clone(), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ); let (ss1, ss2) = (size(&first), size(&second)); @@ -386,22 +321,14 @@ pub fn mult( } } - (Parameters::Mul(s1, s2), Parameters::Var(x, y, z)) => { - let first = Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + (Mul(s1, s2), Var(x, y, z)) => { + let first = Mul( + Box::from(mult(*s1.clone(), Var(x.clone(), y, z.clone()), ram)), s2.clone(), ); - let second = Parameters::Mul( + let second = Mul( s1.clone(), - Box::from(mult( - *s2.clone(), - Parameters::Var(x.clone(), y, z.clone()), - ram, - )), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ); let (ss1, ss2) = (size(&first), size(&second)); @@ -413,12 +340,12 @@ pub fn mult( } } - (Parameters::Mul(s1, s2), Parameters::Mul(s3, s4)) => { - let first = Parameters::Mul( + (Mul(s1, s2), Mul(s3, s4)) => { + let first = Mul( Box::from(mult(*s1.clone(), *s3.clone(), ram)), Box::from(mult(*s2.clone(), *s4.clone(), ram)), ); - let second = Parameters::Mul( + let second = Mul( Box::from(mult(*s1.clone(), *s4.clone(), ram)), Box::from(mult(*s2.clone(), *s3.clone(), ram)), ); @@ -431,76 +358,54 @@ pub fn mult( } } - (Parameters::Mul(s1, s2), Parameters::Identifier(s)) => Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - )), + (Mul(s1, s2), Identifier(s)) => Mul( + Box::from(mult(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), s2.clone(), ), - (Parameters::Identifier(s), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult( - *s1.clone(), - Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone()), - ram, - )), + (Identifier(s), Mul(s1, s2)) => Mul( + Box::from(mult(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), s2.clone(), ), - (Parameters::Mul(s1, s2), Parameters::Int(i)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ), + (Mul(s1, s2), Int(i)) => Mul(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()), - (Parameters::Int(i), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Int(i), ram)), - s2.clone(), - ), + (Int(i), Mul(s1, s2)) => Mul(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Float(f)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ), + (Mul(s1, s2), Float(f)) => Mul(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()), - (Parameters::Float(f), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Float(f), ram)), - s2.clone(), - ), + (Float(f), Mul(s1, s2)) => Mul(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Rational(r)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), - s2.clone(), - ), + (Mul(s1, s2), Rational(r)) => { + Mul(Box::from(mult(*s1.clone(), Rational(r), ram)), s2.clone()) + } - (Parameters::Rational(r), Parameters::Mul(s1, s2)) => Parameters::Mul( - Box::from(mult(*s1.clone(), Parameters::Rational(r), ram)), - s2.clone(), - ), + (Rational(r), Mul(s1, s2)) => { + Mul(Box::from(mult(*s1.clone(), Rational(r), ram)), s2.clone()) + } - (Parameters::Mul(s1, s2), Parameters::Plus(s3, s4)) => Parameters::Plus( + (Mul(s1, s2), Plus(s3, s4)) => Plus( Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), ), - (Parameters::Plus(s3, s4), Parameters::Mul(s1, s2)) => Parameters::Plus( + (Plus(s3, s4), Mul(s1, s2)) => Plus( Box::from(mult(mult(*s1.clone(), *s3.clone(), ram), *s2.clone(), ram)), Box::from(mult(mult(*s1.clone(), *s4.clone(), ram), *s2.clone(), ram)), ), - (Parameters::Null, Parameters::Mul(s1, s2)) => Parameters::Mul(s1.clone(), s2.clone()), + (Null, Mul(s1, s2)) => Mul(s1.clone(), s2.clone()), - (Parameters::Mul(s1, s2), Parameters::Null) => Parameters::Mul(s1.clone(), s2.clone()), + (Mul(s1, s2), Null) => Mul(s1.clone(), s2.clone()), - (Parameters::Var(x, y, z), Parameters::Var(x1, y1, z1)) => { + (Var(x, y, z), Var(x1, y1, z1)) => { if z == z1 { - Parameters::Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) + Var(Box::from(mult(*x.clone(), *x1.clone(), ram)), y + y1, z) } else { - Parameters::Mul( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var( - Box::from(mult(Parameters::Int(1), *x1.clone(), ram)), + Mul( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var( + Box::from(mult(Int(1), *x1.clone(), ram)), y1.clone(), z1.clone(), )), @@ -508,69 +413,47 @@ pub fn mult( } } - (Parameters::Var(x, y, z), Parameters::Identifier(s)) => { + (Var(x, y, z), Identifier(s)) => { if z == s { - Parameters::Var(Box::from(x.clone()), y + 1, z) + Var(Box::from(x.clone()), y + 1, z) } else { - Parameters::Mul( - Box::from(Parameters::Var(x.clone(), y.clone(), z.clone())), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Mul( + Box::from(Var(x.clone(), y.clone(), z.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Identifier(s), Parameters::Var(x, y, z)) => { + (Identifier(s), Var(x, y, z)) => { if z == s { - Parameters::Var(Box::from(x.clone()), y + 1, z) + Var(Box::from(x.clone()), y + 1, z) } else { - Parameters::Mul( - Box::from(Parameters::Var( - Box::from(mult(Parameters::Int(1), *x.clone(), ram)), + Mul( + Box::from(Var( + Box::from(mult(Int(1), *x.clone(), ram)), y.clone(), z.clone(), )), - Box::from(Parameters::Var(Box::from(Parameters::Int(1)), 1, s.clone())), + Box::from(Var(Box::from(Int(1)), 1, s.clone())), ) } } - (Parameters::Int(i), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Int(i), ram)), - y, - z.clone(), - ), + (Int(i), Var(x, y, z)) => Var(Box::from(mult(*x.clone(), Int(i), ram)), y, z.clone()), - (Parameters::Var(x, y, z), Parameters::Int(i)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Int(i), ram)), - y, - z.clone(), - ), + (Var(x, y, z), Int(i)) => Var(Box::from(mult(*x.clone(), Int(i), ram)), y, z.clone()), - (Parameters::Float(f), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(Parameters::Float(f), *x.clone(), ram)), - y, - z.clone(), - ), + (Float(f), Var(x, y, z)) => Var(Box::from(mult(Float(f), *x.clone(), ram)), y, z.clone()), - (Parameters::Var(x, y, z), Parameters::Float(f)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Float(f), ram)), - y, - z.clone(), - ), + (Var(x, y, z), Float(f)) => Var(Box::from(mult(*x.clone(), Float(f), ram)), y, z.clone()), - (Parameters::Rational(r), Parameters::Var(x, y, z)) => Parameters::Var( - Box::from(mult(Parameters::Rational(r), *x.clone(), ram)), - y, - z.clone(), - ), + (Rational(r), Var(x, y, z)) => { + Var(Box::from(mult(Rational(r), *x.clone(), ram)), y, z.clone()) + } - (Parameters::Var(x, y, z), Parameters::Rational(r)) => Parameters::Var( - Box::from(mult(*x.clone(), Parameters::Rational(r), ram)), - y, - z.clone(), - ), - _ => Parameters::Identifier( - "@Those two values are incompatible with the * operator".to_string(), - ), + (Var(x, y, z), Rational(r)) => { + Var(Box::from(mult(*x.clone(), Rational(r), ram)), y, z.clone()) + } + _ => Identifier("@Those two values are incompatible with the * operator".to_string()), } } From c3b6fa7f1701c8443da819e49c1c41b6e47b74e0 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:36:51 +0200 Subject: [PATCH 46/67] simplify rationals --- src/functions/add.rs | 20 ++++++-------------- src/functions/divide.rs | 16 +++++++--------- src/functions/minus.rs | 14 ++++++-------- src/functions/mult.rs | 14 ++++++-------- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index 03d1971..0eaab6a 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -61,21 +61,13 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Identifier(s.clone()), Some(_) => apply_operator(Identifier(s), Null, ram, add), }, - (Rational(s), Identifier(ss)) => match ram { - None => Plus( - Box::from(Rational(s.clone())), - Box::from(Identifier(ss.clone())), - ), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, add) - } + (Rational(r), Identifier(ss)) => match ram { + 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(s)) => match ram { - None => Plus( - Box::from(Identifier(ss.clone())), - Box::from(Rational(s.clone())), - ), - Some(_) => apply_operator(Identifier(ss), Rational(s), ram, add), + (Identifier(ss), Rational(r)) => match ram { + None => Plus(Box::from(Identifier(ss.clone())), Box::from(Rational(r))), + Some(_) => apply_operator(Identifier(ss), Rational(r), ram, add), }, (Identifier(s), Null) => match ram { None => Identifier(s.clone()), diff --git a/src/functions/divide.rs b/src/functions/divide.rs index 7961cba..eae1885 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -50,18 +50,16 @@ pub fn divide( ), }, - (Rational(s), Identifier(ss)) => match ram { - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, divide) - } + (Rational(r), Identifier(ss)) => match ram { + Some(_) => apply_operator_reverse(Rational(r), Identifier(ss.clone()), ram, divide), None => Div( - Box::from(Int(s.over)), - Box::from(Var(Box::from(Int(s.under)), 1, ss.clone())), + Box::from(Int(r.over)), + Box::from(Var(Box::from(Int(r.under)), 1, ss.clone())), ), }, - (Identifier(ss), Rational(s)) => match ram { - Some(_) => apply_operator(Identifier(ss), Rational(s), ram, divide), - None => match s.invert() { + (Identifier(ss), Rational(r)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(r), ram, divide), + None => match r.invert() { Ok(r) => Var(Box::from(Rational(r)), 1, ss.clone()), Err(_) => Null, }, diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 4e0bb72..76049e1 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -86,21 +86,19 @@ pub fn minus( None => Var(Box::from(Int(-1)), 1, s), Some(_) => apply_operator_reverse(Identifier(s), Null, ram, minus), }, - (Rational(s), Identifier(ss)) => match ram { + (Rational(r), Identifier(ss)) => match ram { None => Plus( - Box::from(Rational(s.clone())), + Box::from(Rational(r)), Box::from(Var(Box::from(Int(-1)), 1, ss)), ), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, minus) - } + Some(_) => apply_operator_reverse(Rational(r), Identifier(ss.clone()), ram, minus), }, - (Identifier(ss), Rational(s)) => match ram { + (Identifier(ss), Rational(r)) => match ram { None => Plus( Box::from(Var(Box::from(Int(1)), 1, ss)), - Box::from(Rational(s.opposite())), + Box::from(Rational(r.opposite())), ), - Some(_) => apply_operator(Identifier(ss), Rational(s), ram, minus), + Some(_) => apply_operator(Identifier(ss), Rational(r), ram, minus), }, (Int(i), Identifier(s)) => match ram { None => Plus(Box::from(Int(i)), Box::from(Var(Box::from(Int(-1)), 1, s))), diff --git a/src/functions/mult.rs b/src/functions/mult.rs index d5306b0..fe609cd 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -136,15 +136,13 @@ pub fn mult( None => Var(Box::from(Int(i)), 1, s.clone()), }, - (Rational(s), Identifier(ss)) => match ram { - None => Var(Box::from(Rational(s.clone())), 1, ss.clone()), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, mult) - } + (Rational(r), Identifier(ss)) => match ram { + None => Var(Box::from(Rational(r)), 1, ss.clone()), + Some(_) => apply_operator_reverse(Rational(r), Identifier(ss.clone()), ram, mult), }, - (Identifier(ss), Rational(s)) => match ram { - Some(_) => apply_operator(Identifier(ss), Rational(s), ram, mult), - None => Var(Box::from(Rational(s.clone())), 1, ss.clone()), + (Identifier(ss), Rational(r)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(r), ram, mult), + None => Var(Box::from(Rational(r)), 1, ss.clone()), }, (Int(i), Identifier(s)) => match ram { None => Var(Box::from(Int(i)), 1, s.clone()), From 6226d177b6bbdafaedc01be6c11989270e3b0f4d Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 15 May 2024 21:38:22 +0200 Subject: [PATCH 47/67] remove unnecessary clone --- src/exact_math/rationals.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/exact_math/rationals.rs b/src/exact_math/rationals.rs index 5b1136d..61753dd 100644 --- a/src/exact_math/rationals.rs +++ b/src/exact_math/rationals.rs @@ -95,7 +95,7 @@ impl Rationals { impl Display for Rationals { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let fs = self.clone().reduce(); + let fs = self.reduce(); if fs.under == 1 { write!(f, "{}", fs.over) } else { From feee0a6ec54d444ce2091fb01c63621579540a23 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 16 May 2024 20:32:06 +0200 Subject: [PATCH 48/67] correct div to avoid stack overflow and correctness --- src/functions/divide.rs | 81 ++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/src/functions/divide.rs b/src/functions/divide.rs index eae1885..742e742 100644 --- a/src/functions/divide.rs +++ b/src/functions/divide.rs @@ -521,112 +521,109 @@ pub fn divide( ), (Var(x, y, z), Div(s1, s2)) => { - let first = divide( - mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram), - *s1.clone(), - ram, + let first = Div( + Box::from(mult(Var(x.clone(), y, z.clone()), *s2.clone(), ram)), + s1.clone(), ); first } (Div(s1, s2), Var(x, y, z)) => { - let first = divide( - *s1.clone(), - mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram), - ram, + let first = Div( + s1.clone(), + Box::from(mult(*s2.clone(), Var(x.clone(), y, z.clone()), ram)), ); first } (Mul(s1, s2), Div(s3, s4)) => { - let first = divide( - mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram), - *s3.clone(), - ram, + let first = Div( + Box::from(mult(*s4.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), + s3.clone(), ); first } (Div(s1, s2), Mul(s3, s4)) => { - let first = divide( - *s1.clone(), - mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram), - ram, + let first = Div( + s1.clone(), + Box::from(mult(*s2.clone(), mult(*s3.clone(), *s4.clone(), ram), ram)), ); first } (Div(s1, s2), Div(s3, s4)) => { - let first = divide( - mult(*s1.clone(), *s4.clone(), ram), - mult(*s2.clone(), *s3.clone(), ram), - ram, + let first = Div( + Box::from(mult(*s1.clone(), *s4.clone(), ram)), + Box::from(mult(*s2.clone(), *s3.clone(), ram)), ); first } (Div(s1, s2), Identifier(s)) => { - let first = divide( - *s1.clone(), - mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram), - ram, + let first = Div( + s1.clone(), + Box::from(mult(*s2.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), ); first } (Identifier(s), Div(s1, s2)) => { - let first = divide( - mult(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram), - *s1.clone(), - ram, + let first = Div( + Box::from(mult(Var(Box::from(Int(1)), 1, s.clone()), *s2.clone(), ram)), + s1.clone(), ); first } (Div(s1, s2), Int(i)) => { - let first = divide(mult(*s1.clone(), Int(i), ram), *s2.clone(), ram); + let first = Div(s1.clone(), Box::from(mult(*s2.clone(), Int(i), ram))); first } (Int(i), Div(s1, s2)) => { - let first = divide(mult(Int(i), *s2.clone(), ram), *s1.clone(), ram); + let first = Div(Box::from(mult(Int(i), *s2.clone(), ram)), s1.clone()); first } (Div(s1, s2), Float(f)) => { - let first = divide(mult(*s1.clone(), Float(f), ram), *s2.clone(), ram); + let first = Div(s1.clone(), Box::from(mult(*s2.clone(), Float(f), ram))); first } (Float(f), Div(s1, s2)) => { - let first = divide(mult(Float(f), *s2.clone(), ram), *s1.clone(), ram); + let first = Div(Box::from(mult(Float(f), *s2.clone(), ram)), s1.clone()); first } (Div(s1, s2), Rational(r)) => { - let first = divide(mult(*s1.clone(), Rational(r), ram), *s2.clone(), ram); + let first = Div( + Box::from(mult(*s1.clone(), Int(r.under), ram)), + Box::from(mult(*s2.clone(), Int(r.over), ram)), + ); first } (Rational(r), Div(s1, s2)) => { - let first = divide(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram); + let first = Div( + Box::from(mult(Int(r.under), *s2.clone(), ram)), + Box::from(mult(*s1.clone(), Int(r.over), ram)), + ); first } (Div(s1, s2), Plus(s3, s4)) => { - let first = divide( - mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - *s2.clone(), - ram, + let first = Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), ); first } (Plus(s3, s4), Div(s1, s2)) => { - let first = divide( - mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), - *s1.clone(), - ram, + let first = Div( + Box::from(mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s1.clone(), ); first } From e094922e6b9f2bd448a33a3a198c1fada3e2f805 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 16 May 2024 20:49:21 +0200 Subject: [PATCH 49/67] implements div for mult operations --- src/functions/mult.rs | 115 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/src/functions/mult.rs b/src/functions/mult.rs index fe609cd..51fc8e4 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -8,6 +8,8 @@ use crate::parsing::ast::Parameters::*; use crate::utils::matrix_utils::*; use std::collections::HashMap; +use super::divide::divide; + pub fn mult( i: Parameters, i2: Parameters, @@ -452,6 +454,119 @@ pub fn mult( (Var(x, y, z), Rational(r)) => { 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 + } + + (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 + } + + (Mul(s1, s2), Div(s3, s4)) => { + let first = Div( + Box::from(mult(*s3.clone(), mult(*s1.clone(), *s2.clone(), ram), ram)), + s4.clone(), + ); + first + } + + (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), 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), Identifier(s)) => { + let first = Div( + Box::from(mult(*s1.clone(), Var(Box::from(Int(1)), 1, s.clone()), ram)), + s2.clone(), + ); + first + } + + (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 + } + + (Div(s1, s2), Int(i)) => { + let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); + first + } + + (Int(i), Div(s1, s2)) => { + let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); + first + } + + (Div(s1, s2), Float(f)) => { + let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); + first + } + + (Float(f), Div(s1, s2)) => { + let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); + first + } + + (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 + } + + (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 + } + + (Div(s1, s2), Plus(s3, s4)) => { + let first = Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ); + first + } + + (Plus(s3, s4), Div(s1, s2)) => { + let first = Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ); + first + } + + (Null, Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), + + (Div(s1, s2), Null) => divide(*s2.clone(), *s1.clone(), ram), + _ => Identifier("@Those two values are incompatible with the * operator".to_string()), } } From 030d4d2f1a0048528a319df21233eb8f22157333 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 16 May 2024 21:07:58 +0200 Subject: [PATCH 50/67] pretty print superscript to be printed as a fraction --- src/parsing/ast.rs | 57 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index d44a4d0..fc6b1a2 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,7 +1,8 @@ use std::collections::HashMap; -use std::fmt::{Display, Formatter}; +use std::fmt::{format, Display, Formatter}; use crate::exact_math::rationals::Rationals; +use crate::functions::divide; use crate::lexing::token::{Operator, Token}; use crate::parsing::ast::Ast::{Nil, Node}; use crate::parsing::ast::Parameters::*; @@ -174,24 +175,58 @@ impl Parameters { if l == "error".to_string() { format!("{}", x.clone()) } else { - match **x { - Int(1) => format!("{}{}", z, int_to_superscript_string(*y)), + let division = l.starts_with("⁻"); + let separator = if division { "/" } else { "" }; + let v = &x.pretty_print( + Some(ram.as_mut().unwrap()), + Some(function.as_mut().unwrap()), + ); + let first_attach = match **x { + Int(1) => { + if division { + "1" + } else { + "" + } + } Float(f) if f >= 1.0 - 1e10 && f <= 1.0 + 1e10 => { - format!("{}{}", z, int_to_superscript_string(*y)) + if division { + "1" + } else { + "" + } } Rational(r) if r.clone() == Rationals::new(1, 1) => { - format!("{}{}", z, int_to_superscript_string(*y)) + if division { + "1" + } else { + "" + } + } + Int(-1) => { + if division { + "-1" + } else { + "-" + } } - Int(-1) => format!("-{}{}", z, int_to_superscript_string(*y)), Float(f) if f <= -1.0 - 1e10 && f >= -1.0 + 1e10 => { - format!("-{}{}", z, int_to_superscript_string(*y)) + if division { + "-1" + } else { + "" + } } Rational(r) if r.clone() == Rationals::new(-1, 1) => { - format!("-{}{}", z, int_to_superscript_string(*y)) + if division { + "-1" + } else { + "" + } } - Int(0) => format!("0"), - _ => format!("{}", Var(x.clone(), y.clone(), z.clone())), - } + _ => v, + }; + format!("{}{}{}{}", first_attach, separator, z, l.replace("⁻", "")) } } From e62c0ec4f583bd9ee66c7271d7df476300a381c8 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Thu, 16 May 2024 21:12:04 +0200 Subject: [PATCH 51/67] replace superscript 1 by nothing --- src/parsing/ast.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index fc6b1a2..0023b4f 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -226,7 +226,20 @@ impl Parameters { } _ => v, }; - format!("{}{}{}{}", first_attach, separator, z, l.replace("⁻", "")) + let e = l.replace("⁻", ""); + format!( + "{}{}{}{}", + first_attach, + separator, + z, + if l == "¹" { + "" + } else if l == "⁻¹" { + "" + } else { + e.as_str() + } + ) } } From 7f146d61974cb652775b3827b4e656a6e712ba69 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sat, 18 May 2024 10:15:58 +0200 Subject: [PATCH 52/67] WIP add div operations to add --- src/functions/add.rs | 141 ++++++++++++++++++++++++++++++++++++++++++ src/functions/mult.rs | 4 +- 2 files changed, 143 insertions(+), 2 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index 0eaab6a..e024153 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -6,6 +6,8 @@ use crate::parsing::ast::Parameters::*; use crate::parsing::ast::*; use std::collections::HashMap; +use super::mult::mult; + pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { match (i, i2) { (Null, Int(v)) => Int(v), @@ -372,6 +374,145 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap Plus(Box::from(Var(x, y, z)), 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 + } + + (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 + } + + (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 + } + + (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), 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), 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 + } + + (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 + } + + (Div(s1, s2), Int(i)) => { + let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); + first + } + + (Int(i), Div(s1, s2)) => { + let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); + first + } + + (Div(s1, s2), Float(f)) => { + let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); + first + } + + (Float(f), Div(s1, s2)) => { + let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); + first + } + + (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 + } + + (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 + } + + (Div(s1, s2), Plus(s3, s4)) => { + let first = Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ); + first + } + + (Plus(s3, s4), Div(s1, s2)) => { + let first = Div( + Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + s2.clone(), + ); + first + } + + (Null, Div(s1, s2)) => Div(s1.clone(), s2.clone()), + + (Div(s1, s2), Null) => Div(s1.clone(), s2.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 51fc8e4..ff1aa52 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -563,9 +563,9 @@ pub fn mult( first } - (Null, Div(s1, s2)) => divide(*s2.clone(), *s1.clone(), ram), + (Null, Div(s1, s2)) => Div(s1.clone(), s2.clone()), - (Div(s1, s2), Null) => divide(*s2.clone(), *s1.clone(), ram), + (Div(s1, s2), Null) => Div(s1.clone(), s2.clone()), _ => Identifier("@Those two values are incompatible with the * operator".to_string()), } From 18096c14deb547892367a22226d15dd4d006ecb9 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sat, 18 May 2024 10:26:30 +0200 Subject: [PATCH 53/67] bump to 3.0.0 --- CHANGELOG.md | 2 +- Cargo.lock | 2 +- Cargo.toml | 2 +- src/configuration/loader.rs | 2 +- src/main.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d6e996..3225da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# Version 2.14.0 : Symbolic calculation! +# Version 3.0.0 : Symbolic calculation! You can use the calculator to compute and simplify symbolic computation problems it's compatible with +,-,*,/,^. diff --git a/Cargo.lock b/Cargo.lock index 8a273fc..3f3b36e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,7 +221,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "mini-calc" -version = "2.14.0" +version = "3.0.0" dependencies = [ "ansi_term", "atty", diff --git a/Cargo.toml b/Cargo.toml index f41a22b..66bdd89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mini-calc" -version = "2.14.0" +version = "3.0.0" license = "GPL-3.0-or-later" description = "A fully-featured minimalistic configurable rust calculator" homepage = "https://calc.nwa2coco.fr" diff --git a/src/configuration/loader.rs b/src/configuration/loader.rs index 1b44b25..5417676 100644 --- a/src/configuration/loader.rs +++ b/src/configuration/loader.rs @@ -131,7 +131,7 @@ pub fn load_color(string: String) -> Color { pub fn replace_variable(str: String) -> String { str.replace("%author%", "Charlotte Thomas") - .replace("%version%", "v2.14.0") + .replace("%version%", "v3.0.0") .to_string() } diff --git a/src/main.rs b/src/main.rs index 5a0bc50..364154a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,7 +263,7 @@ fn handle_config(line: &str, config: Config) -> (String, Option) { fn main() { let mut args: Args = env::args(); - let version: String = "v2.14.0".to_string(); + let version: String = "v3.0.0".to_string(); if args.len() > 1 || !atty::is(Stream::Stdin) { let mut a = vec![]; From 5010215a1fcd16e43e57c2dd9c9eaa001719254d Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sat, 18 May 2024 10:30:38 +0200 Subject: [PATCH 54/67] change changelog for 3.0.0 --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3225da0..0b2572e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Version 3.0.0 : Symbolic calculation! -You can use the calculator to compute and simplify symbolic computation problems -it's compatible with +,-,*,/,^. +Version 3.0.0, at last! (if you've seen the PR for it it's been sitting for more +than a month!) + +Symbolic computation was added you can compute expressions with symbols (such as +x,y,whatever) and more ! # Version 2.13.2 : Bug fix Fix #43 From a31d58aa1de3a7c9e19e513f635db77f20e267b9 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 11:45:13 +0200 Subject: [PATCH 55/67] implements div operations for minus change pretty printing of var to put parenthesis --- src/functions/add.rs | 42 +++++++--- src/functions/minus.rs | 170 +++++++++++++++++++++++++++++++++++++++++ src/parsing/ast.rs | 7 +- 3 files changed, 205 insertions(+), 14 deletions(-) diff --git a/src/functions/add.rs b/src/functions/add.rs index e024153..fedaaa9 100644 --- a/src/functions/add.rs +++ b/src/functions/add.rs @@ -459,44 +459,60 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { - let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); + let first = Div( + Box::from(add(mult(*s2.clone(), Int(i), ram), *s1.clone(), ram)), + s2.clone(), + ); first } (Int(i), Div(s1, s2)) => { - let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); + let first = Div( + Box::from(add(mult(Int(i), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ); first } (Div(s1, s2), Float(f)) => { - let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); + let first = Div( + Box::from(add(mult(*s2.clone(), Float(f), ram), *s1.clone(), ram)), + s2.clone(), + ); first } (Float(f), Div(s1, s2)) => { - let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); + let first = Div( + Box::from(add(mult(Float(f), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ); first } (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)), + Box::from(add(mult(*s2.clone(), Rational(r), ram), *s1.clone(), ram)), + s2.clone(), ); first } (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)), + Box::from(add(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), ); first } (Div(s1, s2), Plus(s3, s4)) => { let first = Div( - Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), + Box::from(add( + *s1.clone(), + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + ram, + )), s2.clone(), ); first @@ -504,8 +520,12 @@ pub fn add(i: Parameters, i2: Parameters, ram: Option<&HashMap { let first = Div( - Box::from(mult(*s1.clone(), add(*s3.clone(), *s4.clone(), ram), ram)), - s2.clone(), + Box::from(add( + mult(*s2.clone(), add(*s3.clone(), *s4.clone(), ram), ram), + *s1.clone(), + ram, + )), + s1.clone(), ); first } diff --git a/src/functions/minus.rs b/src/functions/minus.rs index 76049e1..e8fd64a 100644 --- a/src/functions/minus.rs +++ b/src/functions/minus.rs @@ -6,6 +6,10 @@ use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; use std::collections::HashMap; +use super::add::add; +use super::divide::divide; +use super::mult::mult; + pub fn minus( i: Parameters, i2: Parameters, @@ -469,6 +473,172 @@ pub fn minus( (Var(x, y, z), Rational(r)) => { 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 + } + + (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 + } + + (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 + } + + (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), 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), Identifier(s)) => { + let first = Div( + Box::from(minus( + *s1.clone(), + mult( + *s2.clone(), + Var(Box::from(Parameters::Int(1)), 1, s.clone()), + ram, + ), + ram, + )), + s2.clone(), + ); + first + } + + (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(), + ram, + )), + s2.clone(), + ); + first + } + + (Div(s1, s2), Int(i)) => { + let first = Div(Box::from(mult(*s1.clone(), Int(i), ram)), s2.clone()); + first + } + + (Int(i), Div(s1, s2)) => { + let first = Div(Box::from(mult(Int(i), *s1.clone(), ram)), s2.clone()); + first + } + + (Div(s1, s2), Float(f)) => { + let first = Div(Box::from(mult(*s1.clone(), Float(f), ram)), s2.clone()); + first + } + + (Float(f), Div(s1, s2)) => { + let first = Div(Box::from(mult(Float(f), *s1.clone(), ram)), s2.clone()); + first + } + + (Div(s1, s2), Rational(r)) => { + let first = Div( + Box::from(minus(*s1.clone(), mult(*s2.clone(), Rational(r), ram), ram)), + s2.clone(), + ); + first + } + + (Rational(r), Div(s1, s2)) => { + let first = Div( + Box::from(minus(mult(Rational(r), *s2.clone(), ram), *s1.clone(), ram)), + s2.clone(), + ); + first + } + + //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 + } + + (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 + } + + (Null, Div(s1, s2)) => divide( + minus(Parameters::Int(0), *s1.clone(), ram), + *s2.clone(), + ram, + ), + + (Div(s1, s2), Null) => divide( + minus(Parameters::Int(0), *s1.clone(), ram), + *s2.clone(), + ram, + ), + _ => Identifier("Those two values are incompatible with the - operator".to_string()), } } diff --git a/src/parsing/ast.rs b/src/parsing/ast.rs index 0023b4f..21d7f37 100644 --- a/src/parsing/ast.rs +++ b/src/parsing/ast.rs @@ -1,8 +1,7 @@ use std::collections::HashMap; -use std::fmt::{format, Display, Formatter}; +use std::fmt::{Display, Formatter}; use crate::exact_math::rationals::Rationals; -use crate::functions::divide; use crate::lexing::token::{Operator, Token}; use crate::parsing::ast::Ast::{Nil, Node}; use crate::parsing::ast::Parameters::*; @@ -181,6 +180,8 @@ impl Parameters { Some(ram.as_mut().unwrap()), Some(function.as_mut().unwrap()), ); + let vs = format!("({v})"); + let first_attach = match **x { Int(1) => { if division { @@ -224,7 +225,7 @@ impl Parameters { "" } } - _ => v, + _ => vs.as_str(), }; let e = l.replace("⁻", ""); format!( From a5cb008fb4b063a778546ee424ca3e2d93d72734 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 11:53:26 +0200 Subject: [PATCH 56/67] remove stack overflow on exponentiation --- src/functions/expo.rs | 109 ++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/src/functions/expo.rs b/src/functions/expo.rs index 7892760..442023b 100644 --- a/src/functions/expo.rs +++ b/src/functions/expo.rs @@ -1,11 +1,7 @@ -use crate::exact_math::rationals::Rationals; -use crate::exact_math::symbolic::size; -use crate::functions::add::add; use crate::functions::function::apply_operator; use crate::functions::function::apply_operator_reverse; use crate::parsing::ast::Parameters; use crate::parsing::ast::Parameters::*; -use crate::utils::matrix_utils::*; use std::collections::HashMap; pub fn expo( @@ -43,49 +39,70 @@ pub fn expo( (Bool(b), Parameters::Null) => Bool(b), (Parameters::Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - expo, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, expo) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, expo) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo) - } + (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { + None => Parameters::Identifier(s), + Some(_) => apply_operator( + Parameters::Identifier(s), + Parameters::Identifier(s2), + ram, + expo, + ), + }, + (Parameters::Identifier(s), Parameters::Int(i)) => match ram { + None => Parameters::Int(i), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, expo), + }, + (Parameters::Int(i), Parameters::Identifier(s)) => match ram { + None => Parameters::Int(i), + Some(_) => { + apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, expo) + } + }, + (Parameters::Identifier(s), Parameters::Float(i)) => match ram { + None => Parameters::Float(i), + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo), + }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - expo, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - expo, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, expo) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, expo) - } + (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { + None => Parameters::Rational(s), + Some(_) => apply_operator_reverse( + Parameters::Rational(s.clone()), + Parameters::Identifier(ss.clone()), + ram, + expo, + ), + }, + (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + None => Rational(s), + Some(_) => apply_operator( + Parameters::Identifier(ss), + Parameters::Rational(s), + ram, + expo, + ), + }, + (Parameters::Identifier(s), Parameters::Null) => match ram { + None => Parameters::Null, + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo), + }, + (Parameters::Null, Parameters::Identifier(s)) => match ram { + None => Null, + Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo), + }, + (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + None => Float(i), + Some(_) => { + apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo) + } + }, + (Bool(b), Parameters::Identifier(s)) => match ram { + None => Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, expo), + }, + (Parameters::Identifier(s), Bool(b)) => match ram { + None => Bool(b), + Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, expo), + }, _ => Parameters::Identifier( "@Those two values are incompatible with the ^ operator".to_string(), From 03efbde962fd2a1f697cb197ac5631eafb24c999 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 11:54:50 +0200 Subject: [PATCH 57/67] simplify expo --- src/functions/expo.rs | 129 +++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 77 deletions(-) diff --git a/src/functions/expo.rs b/src/functions/expo.rs index 442023b..f2b3c4c 100644 --- a/src/functions/expo.rs +++ b/src/functions/expo.rs @@ -10,102 +10,77 @@ pub fn expo( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(v)) => Parameters::Int(v), - (Parameters::Null, Parameters::Float(f)) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Null) => Parameters::Int(v), - (Parameters::Float(f), Parameters::Null) => Parameters::Float(f), - (Parameters::Int(v), Parameters::Int(v2)) => Parameters::Float((v as f64).powf(v2 as f64)), - (Parameters::Int(v), Parameters::Float(f)) => Parameters::Float((v as f64).powf(f)), - (Parameters::Float(v), Parameters::Float(f)) => Parameters::Float(v.powf(f)), - (Parameters::Float(v), Parameters::Int(i1)) => Parameters::Float(v.powf(i1 as f64)), + (Null, Int(v)) => Int(v), + (Null, Float(f)) => Float(f), + (Int(v), Null) => Int(v), + (Float(f), Null) => Float(f), + (Int(v), Int(v2)) => Float((v as f64).powf(v2 as f64)), + (Int(v), Float(f)) => Float((v as f64).powf(f)), + (Float(v), Float(f)) => Float(v.powf(f)), + (Float(v), Int(i1)) => Float(v.powf(i1 as f64)), - (Parameters::Rational(s), Parameters::Null) => Parameters::Rational(s.clone()), - (Parameters::Null, Parameters::Rational(s)) => Parameters::Rational(s.clone()), - (Parameters::Rational(s), Parameters::Rational(s2)) => { - Parameters::Float(s.approx().powf(s2.approx())) - } - (Parameters::Rational(s), Parameters::Int(i)) => { - Parameters::Float(s.approx().powf(i as f64)) - } - (Parameters::Int(i), Parameters::Rational(s)) => { - Parameters::Float((i as f64).powf(s.approx())) - } - (Parameters::Rational(s), Parameters::Float(f)) => Parameters::Float(s.approx().powf(f)), - (Parameters::Float(f), Parameters::Rational(s)) => Parameters::Float(f.powf(s.approx())), - (Bool(_), Parameters::Int(i)) => Parameters::Int(i), - (Bool(_), Parameters::Float(i)) => Parameters::Float(i), - (Parameters::Int(i), Bool(_)) => Parameters::Int(i), - (Parameters::Float(i), Bool(_)) => Parameters::Float(i), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Rational(s), Null) => Rational(s.clone()), + (Null, Rational(s)) => Rational(s.clone()), + (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())), + (Rational(s), Float(f)) => Float(s.approx().powf(f)), + (Float(f), Rational(s)) => Float(f.powf(s.approx())), + (Bool(_), Int(i)) => Int(i), + (Bool(_), Float(i)) => Float(i), + (Int(i), Bool(_)) => Int(i), + (Float(i), Bool(_)) => Float(i), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => match ram { - None => Parameters::Identifier(s), - Some(_) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - expo, - ), + (Identifier(s), Identifier(s2)) => match ram { + None => Identifier(s), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, expo), }, - (Parameters::Identifier(s), Parameters::Int(i)) => match ram { - None => Parameters::Int(i), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, expo), + (Identifier(s), Int(i)) => match ram { + None => Int(i), + Some(_) => apply_operator(Identifier(s), Int(i), ram, expo), }, - (Parameters::Int(i), Parameters::Identifier(s)) => match ram { - None => Parameters::Int(i), - Some(_) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, expo) - } + (Int(i), Identifier(s)) => match ram { + None => Int(i), + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, expo), }, - (Parameters::Identifier(s), Parameters::Float(i)) => match ram { - None => Parameters::Float(i), - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, expo), + (Identifier(s), Float(i)) => match ram { + None => Float(i), + Some(_) => apply_operator(Identifier(s), Float(i), ram, expo), }, - (Parameters::Rational(s), Parameters::Identifier(ss)) => match ram { - None => Parameters::Rational(s), - Some(_) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - expo, - ), + (Rational(s), Identifier(ss)) => match ram { + None => Rational(s), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, expo) + } }, - (Parameters::Identifier(ss), Parameters::Rational(s)) => match ram { + (Identifier(ss), Rational(s)) => match ram { None => Rational(s), - Some(_) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - expo, - ), + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, expo), }, - (Parameters::Identifier(s), Parameters::Null) => match ram { - None => Parameters::Null, - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo), + (Identifier(s), Null) => match ram { + None => Null, + Some(_) => apply_operator(Identifier(s), Null, ram, expo), }, - (Parameters::Null, Parameters::Identifier(s)) => match ram { + (Null, Identifier(s)) => match ram { None => Null, - Some(_) => apply_operator(Parameters::Identifier(s), Parameters::Null, ram, expo), + Some(_) => apply_operator(Identifier(s), Null, ram, expo), }, - (Parameters::Float(i), Parameters::Identifier(s)) => match ram { + (Float(i), Identifier(s)) => match ram { None => Float(i), - Some(_) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, expo) - } + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, expo), }, - (Bool(b), Parameters::Identifier(s)) => match ram { + (Bool(b), Identifier(s)) => match ram { None => Bool(b), - Some(_) => apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, expo), + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, expo), }, - (Parameters::Identifier(s), Bool(b)) => match ram { + (Identifier(s), Bool(b)) => match ram { None => Bool(b), - Some(_) => apply_operator(Parameters::Identifier(s), Bool(b), ram, expo), + Some(_) => apply_operator(Identifier(s), Bool(b), ram, expo), }, - _ => Parameters::Identifier( - "@Those two values are incompatible with the ^ operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the ^ operator".to_string()), } } From 16bacb5fcbf665bd1b8b5ab1c55126467d26b108 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 11:55:55 +0200 Subject: [PATCH 58/67] simplify functions --- src/functions/function.rs | 694 ++++++++++++++------------------------ 1 file changed, 254 insertions(+), 440 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 21348cf..7f8e7f6 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use crate::exact_math::rationals::Rationals; use crate::parsing::ast::Parameters; -use crate::parsing::ast::Parameters::Bool; +use crate::parsing::ast::Parameters::*; pub fn apply_operator( value: Parameters, @@ -12,11 +12,11 @@ pub fn apply_operator( f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, ) -> Parameters { let s = match value { - Parameters::Identifier(ref s) => s, + Identifier(ref s) => s, _ => "", }; if s == "" { - return Parameters::Null; + return Null; } match ram { None => f(value.clone(), value2.clone(), None), @@ -37,11 +37,11 @@ pub fn apply_operator_reverse( f: fn(Parameters, Parameters, Option<&HashMap>) -> Parameters, ) -> Parameters { let s = match value2 { - Parameters::Identifier(ref s) => s, + Identifier(ref s) => s, _ => "", }; if s == "" { - return Parameters::Null; + return Null; } match ram { None => f(value.clone(), value2.clone(), None), @@ -57,7 +57,7 @@ pub fn apply_operator_reverse( pub fn assign(s: Parameters, s2: Parameters) -> (String, Parameters) { match s { - Parameters::Identifier(s) => (s, s2), + Identifier(s) => (s, s2), _ => ("".to_string(), s2), } } @@ -68,81 +68,46 @@ pub fn greater( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v > v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) > f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v > f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v > (i1 as f64)), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s > s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s > Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) > s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() > f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f > s.approx()), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Null, Int(_)) => Bool(true), + (Null, Float(_)) => Bool(true), + (Int(_), Null) => Bool(true), + (Float(_), Null) => Bool(true), + (Int(v), Int(v2)) => Bool(v > v2), + (Int(v), Float(f)) => Bool((v as f64) > f), + (Float(v), Float(f)) => Bool(v > f), + (Float(v), Int(i1)) => Bool(v > (i1 as f64)), + (Rational(_), Null) => Bool(true), + (Null, Rational(_)) => Bool(true), + (Rational(s), Rational(s2)) => Bool(s > s2), + (Rational(s), Int(i)) => Bool(s > Rationals::new(1, i)), + (Int(i), Rational(s)) => Bool(Rationals::new(1, i) > s), + (Rational(s), Float(f)) => Bool(s.approx() > f), + (Float(f), Rational(s)) => Bool(f > s.approx()), + (Bool(b), Int(_)) => Bool(b), + (Bool(b), Float(_)) => Bool(b), + (Int(_), Bool(b)) => Bool(b), + (Float(_), Bool(b)) => Bool(b), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - greater, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - greater, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - greater, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, greater) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, greater) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, greater) - } - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - greater, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), - ram, - greater, - ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater) + (Identifier(s), Identifier(s2)) => { + apply_operator(Identifier(s), Identifier(s2), ram, greater) } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, greater) + + (Rational(s), Identifier(ss)) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, greater) } + (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, greater), + (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, greater), + (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, greater), + (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, greater), + (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, greater), + (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, greater), + (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, greater), + (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, greater), + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, greater), - _ => Parameters::Identifier( - "@Those two values are incompatible with the > operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the > operator".to_string()), } } @@ -152,74 +117,45 @@ pub fn lesser( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(false), - (Parameters::Null, Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Parameters::Null) => Bool(false), - (Parameters::Float(_), Parameters::Null) => Bool(false), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v < v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) < f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v < f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v < (i1 as f64)), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s < s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s < Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) < s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() < f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f < s.approx()), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Null, Int(_)) => Bool(false), + (Null, Float(_)) => Bool(false), + (Int(_), Null) => Bool(false), + (Float(_), Null) => Bool(false), + (Int(v), Int(v2)) => Bool(v < v2), + (Int(v), Float(f)) => Bool((v as f64) < f), + (Float(v), Float(f)) => Bool(v < f), + (Float(v), Int(i1)) => Bool(v < (i1 as f64)), + (Rational(_), Null) => Bool(true), + (Null, Rational(_)) => Bool(true), + (Rational(s), Rational(s2)) => Bool(s < s2), + (Rational(s), Int(i)) => Bool(s < Rationals::new(1, i)), + (Int(i), Rational(s)) => Bool(Rationals::new(1, i) < s), + (Rational(s), Float(f)) => Bool(s.approx() < f), + (Float(f), Rational(s)) => Bool(f < s.approx()), + (Bool(b), Int(_)) => Bool(b), + (Bool(b), Float(_)) => Bool(b), + (Int(_), Bool(b)) => Bool(b), + (Float(_), Bool(b)) => Bool(b), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - lesser, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, lesser) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) - } - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - lesser, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - lesser, - ), - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, lesser) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, lesser) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, lesser) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, lesser) + (Identifier(s), Identifier(s2)) => { + apply_operator(Identifier(s), Identifier(s2), ram, lesser) } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser) + (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, lesser), + (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, lesser), + (Rational(s), Identifier(ss)) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, lesser) } + (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, lesser), + (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, lesser), + (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, lesser), + (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, lesser), + (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, lesser), + (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, lesser), + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, lesser), - _ => Parameters::Identifier( - "@Those two values are incompatible with the < operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the < operator".to_string()), } } @@ -229,94 +165,58 @@ pub fn greater_or_equal( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v >= v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) >= f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v >= f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v >= (i1 as f64)), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Null, Int(_)) => Bool(true), + (Null, Float(_)) => Bool(true), + (Int(_), Null) => Bool(true), + (Float(_), Null) => Bool(true), + (Int(v), Int(v2)) => Bool(v >= v2), + (Int(v), Float(f)) => Bool((v as f64) >= f), + (Float(v), Float(f)) => Bool(v >= f), + (Float(v), Int(i1)) => Bool(v >= (i1 as f64)), + (Bool(b), Int(_)) => Bool(b), + (Bool(b), Float(_)) => Bool(b), + (Int(_), Bool(b)) => Bool(b), + (Float(_), Bool(b)) => Bool(b), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b == b2), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s >= s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s >= Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) >= s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() >= f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f >= s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Int(i), - ram, - greater_or_equal, - ), - (Parameters::Null, Parameters::Identifier(s)) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Null) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - greater_or_equal, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - greater_or_equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - greater_or_equal, - ), - (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Int(i), - Parameters::Identifier(s), - ram, - greater_or_equal, - ), - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - greater_or_equal, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), + (Rational(_), Null) => Bool(true), + (Null, Rational(_)) => Bool(true), + (Rational(s), Rational(s2)) => Bool(s >= s2), + (Rational(s), Int(i)) => Bool(s >= Rationals::new(1, i)), + (Int(i), Rational(s)) => Bool(Rationals::new(1, i) >= s), + (Rational(s), Float(f)) => Bool(s.approx() >= f), + (Float(f), Rational(s)) => Bool(f >= s.approx()), + (Identifier(s), Identifier(s2)) => { + apply_operator(Identifier(s), Identifier(s2), ram, greater_or_equal) + } + (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, greater_or_equal), + (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, greater_or_equal), + (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, greater_or_equal), + + (Rational(s), Identifier(ss)) => apply_operator_reverse( + Rational(s.clone()), + Identifier(ss.clone()), ram, greater_or_equal, ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, greater_or_equal) + (Identifier(ss), Rational(s)) => { + apply_operator(Identifier(ss), Rational(s), ram, greater_or_equal) } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, greater_or_equal) + (Int(i), Identifier(s)) => { + apply_operator_reverse(Int(i), Identifier(s), ram, greater_or_equal) } + (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, greater_or_equal), + (Float(i), Identifier(s)) => { + apply_operator_reverse(Float(i), Identifier(s), ram, greater_or_equal) + } + (Bool(b), Identifier(s)) => { + apply_operator_reverse(Bool(b), Identifier(s), ram, greater_or_equal) + } + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, greater_or_equal), - _ => Parameters::Identifier( - "@Those two values are incompatible with the >= operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the >= operator".to_string()), } } @@ -326,94 +226,58 @@ pub fn lesser_or_equal( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(false), - (Parameters::Null, Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Parameters::Null) => Bool(false), - (Parameters::Float(_), Parameters::Null) => Bool(false), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v <= v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) <= f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v <= f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v <= (i1 as f64)), - (Bool(b), Parameters::Int(_)) => Bool(b), - (Bool(b), Parameters::Float(_)) => Bool(b), - (Parameters::Int(_), Bool(b)) => Bool(b), - (Parameters::Float(_), Bool(b)) => Bool(b), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), + (Null, Int(_)) => Bool(false), + (Null, Float(_)) => Bool(false), + (Int(_), Null) => Bool(false), + (Float(_), Null) => Bool(false), + (Int(v), Int(v2)) => Bool(v <= v2), + (Int(v), Float(f)) => Bool((v as f64) <= f), + (Float(v), Float(f)) => Bool(v <= f), + (Float(v), Int(i1)) => Bool(v <= (i1 as f64)), + (Bool(b), Int(_)) => Bool(b), + (Bool(b), Float(_)) => Bool(b), + (Int(_), Bool(b)) => Bool(b), + (Float(_), Bool(b)) => Bool(b), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b == b2), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s <= s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s <= Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) <= s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() <= f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f <= s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Int(i), - ram, - lesser_or_equal, - ), - - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - lesser_or_equal, - ), - (Parameters::Null, Parameters::Identifier(s)) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Null) => apply_operator( - Parameters::Identifier(s), - Parameters::Null, - ram, - lesser_or_equal, - ), - (Parameters::Int(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Int(i), - Parameters::Identifier(s), - ram, - lesser_or_equal, - ), - (Parameters::Identifier(s), Parameters::Float(i)) => apply_operator( - Parameters::Identifier(s), - Parameters::Float(i), - ram, - lesser_or_equal, - ), - (Parameters::Float(i), Parameters::Identifier(s)) => apply_operator_reverse( - Parameters::Float(i), - Parameters::Identifier(s), + (Rational(_), Null) => Bool(true), + (Null, Rational(_)) => Bool(true), + (Rational(s), Rational(s2)) => Bool(s <= s2), + (Rational(s), Int(i)) => Bool(s <= Rationals::new(1, i)), + (Int(i), Rational(s)) => Bool(Rationals::new(1, i) <= s), + (Rational(s), Float(f)) => Bool(s.approx() <= f), + (Float(f), Rational(s)) => Bool(f <= s.approx()), + (Identifier(s), Identifier(s2)) => { + apply_operator(Identifier(s), Identifier(s2), ram, lesser_or_equal) + } + (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, lesser_or_equal), + + (Rational(s), Identifier(ss)) => apply_operator_reverse( + Rational(s.clone()), + Identifier(ss.clone()), ram, lesser_or_equal, ), - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, lesser_or_equal) + (Identifier(ss), Rational(s)) => { + apply_operator(Identifier(ss), Rational(s), ram, lesser_or_equal) } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, lesser_or_equal) + (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), + (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), + (Int(i), Identifier(s)) => { + apply_operator_reverse(Int(i), Identifier(s), ram, lesser_or_equal) } + (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, lesser_or_equal), + (Float(i), Identifier(s)) => { + apply_operator_reverse(Float(i), Identifier(s), ram, lesser_or_equal) + } + (Bool(b), Identifier(s)) => { + apply_operator_reverse(Bool(b), Identifier(s), ram, lesser_or_equal) + } + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, lesser_or_equal), - _ => Parameters::Identifier( - "@Those two values are incompatible with the <= operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the <= operator".to_string()), } } @@ -423,76 +287,47 @@ pub fn equal( ram: Option<&HashMap>, ) -> Parameters { match (i, i2) { - (Parameters::Null, Parameters::Int(_)) => Bool(true), - (Parameters::Null, Parameters::Float(_)) => Bool(true), - (Parameters::Int(_), Parameters::Null) => Bool(true), - (Parameters::Float(_), Parameters::Null) => Bool(true), - (Parameters::Int(v), Parameters::Int(v2)) => Bool(v == v2), - (Parameters::Int(v), Parameters::Float(f)) => Bool((v as f64) == f), - (Parameters::Float(v), Parameters::Float(f)) => Bool(v == f), - (Parameters::Float(v), Parameters::Int(i1)) => Bool(v == (i1 as f64)), - (Bool(_), Parameters::Int(_)) => Bool(false), - (Bool(_), Parameters::Float(_)) => Bool(false), - (Parameters::Int(_), Bool(_)) => Bool(false), - (Parameters::Float(_), Bool(_)) => Bool(false), - (Bool(_), Parameters::Null) => Bool(false), - (Parameters::Null, Bool(_)) => Bool(false), + (Null, Int(_)) => Bool(true), + (Null, Float(_)) => Bool(true), + (Int(_), Null) => Bool(true), + (Float(_), Null) => Bool(true), + (Int(v), Int(v2)) => Bool(v == v2), + (Int(v), Float(f)) => Bool((v as f64) == f), + (Float(v), Float(f)) => Bool(v == f), + (Float(v), Int(i1)) => Bool(v == (i1 as f64)), + (Bool(_), Int(_)) => Bool(false), + (Bool(_), Float(_)) => Bool(false), + (Int(_), Bool(_)) => Bool(false), + (Float(_), Bool(_)) => Bool(false), + (Bool(_), Null) => Bool(false), + (Null, Bool(_)) => Bool(false), (Bool(b), Bool(b2)) => Bool(b == b2), - (Parameters::Rational(_), Parameters::Null) => Bool(true), - (Parameters::Null, Parameters::Rational(_)) => Bool(true), - (Parameters::Rational(s), Parameters::Rational(s2)) => Bool(s == s2), - (Parameters::Rational(s), Parameters::Int(i)) => Bool(s == Rationals::new(1, i)), - (Parameters::Int(i), Parameters::Rational(s)) => Bool(Rationals::new(1, i) == s), - (Parameters::Rational(s), Parameters::Float(f)) => Bool(s.approx() == f), - (Parameters::Float(f), Parameters::Rational(s)) => Bool(f == s.approx()), - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - equal, - ), - (Parameters::Identifier(s), Parameters::Int(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Int(i), ram, equal) - } - (Parameters::Null, Parameters::Identifier(s)) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) - } - (Parameters::Identifier(s), Parameters::Null) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, equal) - } - (Parameters::Int(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Int(i), Parameters::Identifier(s), ram, equal) - } - (Parameters::Identifier(s), Parameters::Float(i)) => { - apply_operator(Parameters::Identifier(s), Parameters::Float(i), ram, equal) - } - (Parameters::Float(i), Parameters::Identifier(s)) => { - apply_operator_reverse(Parameters::Float(i), Parameters::Identifier(s), ram, equal) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, equal) - } - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, equal) + (Rational(_), Null) => Bool(true), + (Null, Rational(_)) => Bool(true), + (Rational(s), Rational(s2)) => Bool(s == s2), + (Rational(s), Int(i)) => Bool(s == Rationals::new(1, i)), + (Int(i), Rational(s)) => Bool(Rationals::new(1, i) == s), + (Rational(s), Float(f)) => Bool(s.approx() == f), + (Float(f), Rational(s)) => Bool(f == s.approx()), + (Identifier(s), Identifier(s2)) => { + apply_operator(Identifier(s), Identifier(s2), ram, equal) } + (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, equal), + (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, equal), + (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, equal), + (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, equal), + (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, equal), + (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, equal), + (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, equal), + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, equal), - (Parameters::Rational(s), Parameters::Identifier(ss)) => apply_operator_reverse( - Parameters::Rational(s.clone()), - Parameters::Identifier(ss.clone()), - ram, - equal, - ), - (Parameters::Identifier(ss), Parameters::Rational(s)) => apply_operator( - Parameters::Identifier(ss), - Parameters::Rational(s), - ram, - equal, - ), + (Rational(s), Identifier(ss)) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, equal) + } + (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, equal), - _ => Parameters::Identifier( - "@Those two values are incompatible with the == operator".to_string(), - ), + _ => Identifier("@Those two values are incompatible with the == operator".to_string()), } } @@ -503,9 +338,7 @@ pub fn not( ) -> Parameters { match i { Bool(b) => Bool(!b), - Parameters::Identifier(s) => { - apply_operator(Parameters::Identifier(s), Parameters::Null, ram, not) - } + Identifier(s) => apply_operator(Identifier(s), Null, ram, not), _ => Bool(false), } } @@ -513,20 +346,11 @@ pub fn not( pub fn and(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { match (i, i2) { (Bool(b), Bool(b2)) => Bool(b && b2), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, and) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, and) - } - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - and, - ), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, and), + (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, and), + (Identifier(s), Identifier(s2)) => apply_operator(Identifier(s), Identifier(s2), ram, and), _ => Bool(false), } } @@ -534,20 +358,11 @@ pub fn and(i: Parameters, i2: Parameters, ram: Option<&HashMap>) -> Parameters { match (i, i2) { (Bool(b), Bool(b2)) => Bool(b || b2), - (Bool(b), Parameters::Null) => Bool(b), - (Parameters::Null, Bool(b)) => Bool(b), - (Parameters::Identifier(s), Bool(b)) => { - apply_operator(Parameters::Identifier(s), Bool(b), ram, or) - } - (Bool(b), Parameters::Identifier(s)) => { - apply_operator_reverse(Bool(b), Parameters::Identifier(s), ram, or) - } - (Parameters::Identifier(s), Parameters::Identifier(s2)) => apply_operator( - Parameters::Identifier(s), - Parameters::Identifier(s2), - ram, - or, - ), + (Bool(b), Null) => Bool(b), + (Null, Bool(b)) => Bool(b), + (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, or), + (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, or), + (Identifier(s), Identifier(s2)) => apply_operator(Identifier(s), Identifier(s2), ram, or), _ => Bool(false), } } @@ -562,149 +377,148 @@ mod test { #[test] pub fn test_add_null() { - let expected = Parameters::Int(1); - let result = add(Parameters::Int(1), Parameters::Null, None); + let expected = Int(1); + let result = add(Int(1), Null, None); assert_eq!(result, expected); } #[test] pub fn test_add_simple() { - let expected = Parameters::Int(2); - let result = add(Parameters::Int(1), Parameters::Int(1), None); + let expected = Int(2); + let result = add(Int(1), Int(1), None); assert_eq!(result, expected); } #[test] pub fn test_add_float() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Float(0.1), Parameters::Float(2.0), None); + let expected = Float(2.1); + let result = add(Float(0.1), Float(2.0), None); assert_eq!(result, expected); } #[test] pub fn test_add_int_float() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Int(2), Parameters::Float(0.1), None); + let expected = Float(2.1); + let result = add(Int(2), Float(0.1), None); assert_eq!(result, expected); } #[test] pub fn test_add_float_int() { - let expected = Parameters::Float(2.1); - let result = add(Parameters::Float(0.1), Parameters::Int(2), None); + let expected = Float(2.1); + let result = add(Float(0.1), Int(2), None); assert_eq!(result, expected); } #[test] pub fn test_minus_null() { - let expected = Parameters::Int(-1); - let result = minus(Parameters::Int(1), Parameters::Null, None); + let expected = Int(-1); + let result = minus(Int(1), Null, None); assert_eq!(result, expected); } #[test] pub fn test_minus_null_rev() { - let expected = Parameters::Int(-1); - let result = minus(Parameters::Null, Parameters::Int(1), None); + let expected = Int(-1); + let result = minus(Null, Int(1), None); assert_eq!(result, expected); } #[test] pub fn test_minus_simple() { - let expected = Parameters::Int(0); - let result = minus(Parameters::Int(1), Parameters::Int(1), None); + let expected = Int(0); + let result = minus(Int(1), Int(1), None); assert_eq!(result, expected); } #[test] pub fn test_minus_float() { - let expected = Parameters::Float(1.9); - let result = minus(Parameters::Float(2.0), Parameters::Float(0.1), None); + let expected = Float(1.9); + let result = minus(Float(2.0), Float(0.1), None); assert_eq!(result, expected); } #[test] pub fn test_minus_int_float() { - let expected = Parameters::Float(1.9); - let result = minus(Parameters::Int(2), Parameters::Float(0.1), None); + let expected = Float(1.9); + let result = minus(Int(2), Float(0.1), None); assert_eq!(result, expected); } #[test] pub fn test_minus_float_int() { - let expected = Parameters::Float(-1.9); - let result = minus(Parameters::Float(0.1), Parameters::Int(2), None); + let expected = Float(-1.9); + let result = minus(Float(0.1), Int(2), None); assert_eq!(result, expected); } #[test] pub fn test_mult_null() { - let expected = Parameters::Int(1); - let result = mult(Parameters::Int(1), Parameters::Null, None); + let expected = Int(1); + let result = mult(Int(1), Null, None); assert_eq!(result, expected); } #[test] pub fn test_mult_simple() { - let expected = Parameters::Int(2); - let result = mult(Parameters::Int(1), Parameters::Int(2), None); + let expected = Int(2); + let result = mult(Int(1), Int(2), None); assert_eq!(result, expected); } #[test] pub fn test_mult_float() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Float(0.1), Parameters::Float(2.0), None); + let expected = Float(0.2); + let result = mult(Float(0.1), Float(2.0), None); assert_eq!(result, expected); } #[test] pub fn test_mult_int_float() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Int(2), Parameters::Float(0.1), None); + let expected = Float(0.2); + let result = mult(Int(2), Float(0.1), None); assert_eq!(result, expected); } #[test] pub fn test_mult_float_int() { - let expected = Parameters::Float(0.2); - let result = mult(Parameters::Float(0.1), Parameters::Int(2), None); + let expected = Float(0.2); + let result = mult(Float(0.1), Int(2), None); assert_eq!(result, expected); } #[test] pub fn test_divide_null() { - let expected = Parameters::Int(1); - let result = divide(Parameters::Int(1), Parameters::Null, None); + let expected = Int(1); + let result = divide(Int(1), Null, None); assert_eq!(result, expected); } #[test] pub fn test_divide_simple() { - let expected = - Parameters::Rational(crate::exact_math::rationals::Rationals { under: 1, over: 1 }); - let result = divide(Parameters::Int(1), Parameters::Int(1), None); + let expected = Rational(crate::exact_math::rationals::Rationals { under: 1, over: 1 }); + let result = divide(Int(1), Int(1), None); assert_eq!(result, expected); } #[test] pub fn test_divide_float() { - let expected = Parameters::Float(0.05); - let result = divide(Parameters::Float(0.1), Parameters::Float(2.0), None); + let expected = Float(0.05); + let result = divide(Float(0.1), Float(2.0), None); assert_eq!(result, expected); } #[test] pub fn test_divide_int_float() { - let expected = Parameters::Float(20.0); - let result = divide(Parameters::Int(2), Parameters::Float(0.1), None); + let expected = Float(20.0); + let result = divide(Int(2), Float(0.1), None); assert_eq!(result, expected); } #[test] pub fn test_divide_float_int() { - let expected = Parameters::Float(0.05); - let result = divide(Parameters::Float(0.1), Parameters::Int(2), None); + let expected = Float(0.05); + let result = divide(Float(0.1), Int(2), None); assert_eq!(result, expected); } } From 48ab4ea7ee715af35cbe1f11d6b8ea2595b8b1d7 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:01:25 +0200 Subject: [PATCH 59/67] remove stack overflow on greater --- src/functions/function.rs | 64 ++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 7f8e7f6..3e1d8e1 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -90,23 +90,53 @@ pub fn greater( (Bool(b), Null) => Bool(b), (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Identifier(s), Identifier(s2)) => { - apply_operator(Identifier(s), Identifier(s2), ram, greater) - } - - (Rational(s), Identifier(ss)) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, greater) - } - (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, greater), - (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, greater), - (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, greater), - (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, greater), - (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, greater), - (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, greater), - (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, greater), - (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, greater), - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, greater), - + (Identifier(s), Identifier(s2)) => match ram { + None => Identifier(s), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, greater), + }, + + (Rational(s), Identifier(ss)) => match ram { + None => Rational(s), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, greater) + } + }, + (Identifier(ss), Rational(s)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, greater), + None => Rational(s), + }, + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, greater), + None => Int(i), + }, + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, greater), + None => Identifier(s), + }, + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, greater), + None => Identifier(s), + }, + (Int(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, greater), + None => Int(i), + }, + (Identifier(s), Float(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, greater), + None => Float(i), + }, + (Float(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, greater), + None => Float(i), + }, + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, greater), + None => Bool(b), + }, + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, greater), + None => Bool(b), + }, _ => Identifier("@Those two values are incompatible with the > operator".to_string()), } } From d6c46570e2b57e84a3c7c4fcc17cd62ccbac4ae9 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:06:42 +0200 Subject: [PATCH 60/67] remove stack overflow on lesser --- src/functions/function.rs | 62 +++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 3e1d8e1..bc0fc46 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -169,22 +169,52 @@ pub fn lesser( (Bool(b), Null) => Bool(b), (Null, Bool(b)) => Bool(b), (Bool(b), Bool(b2)) => Bool(b && b2), - (Identifier(s), Identifier(s2)) => { - apply_operator(Identifier(s), Identifier(s2), ram, lesser) - } - (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, lesser), - (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, lesser), - (Rational(s), Identifier(ss)) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, lesser) - } - (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, lesser), - (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, lesser), - (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, lesser), - (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, lesser), - (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, lesser), - (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, lesser), - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, lesser), - + (Identifier(s), Identifier(s2)) => match ram { + None => Identifier(s), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, lesser), + }, + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, lesser), + None => Int(i), + }, + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, lesser), + None => Identifier(s), + }, + (Rational(s), Identifier(ss)) => match ram { + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, lesser) + } + None => Rational(s), + }, + (Identifier(ss), Rational(s)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, lesser), + None => Rational(s), + }, + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, lesser), + None => Identifier(s), + }, + (Int(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, lesser), + None => Int(i), + }, + (Identifier(s), Float(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, lesser), + None => Float(i), + }, + (Float(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, lesser), + None => Float(i), + }, + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, lesser), + None => Bool(b), + }, + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, lesser), + None => Bool(b), + }, _ => Identifier("@Those two values are incompatible with the < operator".to_string()), } } From fee75dc0c6cd2e804ee01583b5708f6e92f9ff11 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:11:35 +0200 Subject: [PATCH 61/67] remove stack overflow on greater or equal --- src/functions/function.rs | 77 +++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index bc0fc46..fb3412d 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -248,34 +248,55 @@ pub fn greater_or_equal( (Int(i), Rational(s)) => Bool(Rationals::new(1, i) >= s), (Rational(s), Float(f)) => Bool(s.approx() >= f), (Float(f), Rational(s)) => Bool(f >= s.approx()), - (Identifier(s), Identifier(s2)) => { - apply_operator(Identifier(s), Identifier(s2), ram, greater_or_equal) - } - (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, greater_or_equal), - (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, greater_or_equal), - (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, greater_or_equal), - - (Rational(s), Identifier(ss)) => apply_operator_reverse( - Rational(s.clone()), - Identifier(ss.clone()), - ram, - greater_or_equal, - ), - (Identifier(ss), Rational(s)) => { - apply_operator(Identifier(ss), Rational(s), ram, greater_or_equal) - } - (Int(i), Identifier(s)) => { - apply_operator_reverse(Int(i), Identifier(s), ram, greater_or_equal) - } - (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, greater_or_equal), - (Float(i), Identifier(s)) => { - apply_operator_reverse(Float(i), Identifier(s), ram, greater_or_equal) - } - (Bool(b), Identifier(s)) => { - apply_operator_reverse(Bool(b), Identifier(s), ram, greater_or_equal) - } - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, greater_or_equal), - + (Identifier(s), Identifier(s2)) => match ram { + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, greater_or_equal), + None => Identifier(s), + }, + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, greater_or_equal), + None => Int(i), + }, + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, greater_or_equal), + None => Identifier(s), + }, + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, greater_or_equal), + None => Identifier(s), + }, + (Rational(s), Identifier(ss)) => match ram { + Some(_) => apply_operator_reverse( + Rational(s.clone()), + Identifier(ss.clone()), + ram, + greater_or_equal, + ), + None => Rational(s), + }, + (Identifier(ss), Rational(s)) => match ram { + None => Rational(s), + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, greater_or_equal), + }, + (Int(i), Identifier(s)) => match ram { + None => Int(i), + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, greater_or_equal), + }, + (Identifier(s), Float(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, greater_or_equal), + None => Float(i), + }, + (Float(i), Identifier(s)) => match ram { + None => Float(i), + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, greater_or_equal), + }, + (Bool(b), Identifier(s)) => match ram { + None => Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, greater_or_equal), + }, + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, greater_or_equal), + None => Bool(b), + }, _ => Identifier("@Those two values are incompatible with the >= operator".to_string()), } } From e1daa0c9f219ad1353bf298897c4c94cc9d2a655 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:16:27 +0200 Subject: [PATCH 62/67] remove stack overflow on lesser or equal remove unnessary import on mult --- src/functions/function.rs | 77 +++++++++++++++++++++++++-------------- src/functions/mult.rs | 2 - 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index fb3412d..cfecd27 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -330,34 +330,55 @@ pub fn lesser_or_equal( (Int(i), Rational(s)) => Bool(Rationals::new(1, i) <= s), (Rational(s), Float(f)) => Bool(s.approx() <= f), (Float(f), Rational(s)) => Bool(f <= s.approx()), - (Identifier(s), Identifier(s2)) => { - apply_operator(Identifier(s), Identifier(s2), ram, lesser_or_equal) - } - (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, lesser_or_equal), - - (Rational(s), Identifier(ss)) => apply_operator_reverse( - Rational(s.clone()), - Identifier(ss.clone()), - ram, - lesser_or_equal, - ), - (Identifier(ss), Rational(s)) => { - apply_operator(Identifier(ss), Rational(s), ram, lesser_or_equal) - } - (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), - (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), - (Int(i), Identifier(s)) => { - apply_operator_reverse(Int(i), Identifier(s), ram, lesser_or_equal) - } - (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, lesser_or_equal), - (Float(i), Identifier(s)) => { - apply_operator_reverse(Float(i), Identifier(s), ram, lesser_or_equal) - } - (Bool(b), Identifier(s)) => { - apply_operator_reverse(Bool(b), Identifier(s), ram, lesser_or_equal) - } - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, lesser_or_equal), - + (Identifier(s), Identifier(s2)) => match ram { + None => Identifier(s), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, lesser_or_equal), + }, + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, lesser_or_equal), + None => Int(i), + }, + (Rational(s), Identifier(ss)) => match ram { + Some(_) => apply_operator_reverse( + Rational(s.clone()), + Identifier(ss.clone()), + ram, + lesser_or_equal, + ), + None => Rational(s), + }, + (Identifier(ss), Rational(s)) => match ram { + None => Rational(s), + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, lesser_or_equal), + }, + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), + None => Identifier(s), + }, + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, lesser_or_equal), + None => Identifier(s), + }, + (Int(i), Identifier(s)) => match ram { + None => Int(i), + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, lesser_or_equal), + }, + (Identifier(s), Float(i)) => match ram { + None => Float(i), + Some(_) => apply_operator(Identifier(s), Float(i), ram, lesser_or_equal), + }, + (Float(i), Identifier(s)) => match ram { + None => Float(i), + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, lesser_or_equal), + }, + (Bool(b), Identifier(s)) => match ram { + None => Bool(b), + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, lesser_or_equal), + }, + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, lesser_or_equal), + None => Bool(b), + }, _ => Identifier("@Those two values are incompatible with the <= operator".to_string()), } } diff --git a/src/functions/mult.rs b/src/functions/mult.rs index ff1aa52..9d61189 100644 --- a/src/functions/mult.rs +++ b/src/functions/mult.rs @@ -8,8 +8,6 @@ use crate::parsing::ast::Parameters::*; use crate::utils::matrix_utils::*; use std::collections::HashMap; -use super::divide::divide; - pub fn mult( i: Parameters, i2: Parameters, From 05120490f323b00d8849ff098fde3cf8ae15677b Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:18:15 +0200 Subject: [PATCH 63/67] fix test --- src/functions/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index cfecd27..d0e612a 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -475,7 +475,7 @@ mod test { use crate::functions::divide::divide; use crate::functions::minus::minus; use crate::functions::mult::mult; - use crate::parsing::ast::Parameters; + use crate::parsing::ast::Parameters::*; #[test] pub fn test_add_null() { From 8eef0c503e79369d589d4773e5ba51dcb53bca6f Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:23:35 +0200 Subject: [PATCH 64/67] remove stack overflow for equal --- src/functions/function.rs | 63 +++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index d0e612a..c6e07f6 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -412,22 +412,53 @@ pub fn equal( (Int(i), Rational(s)) => Bool(Rationals::new(1, i) == s), (Rational(s), Float(f)) => Bool(s.approx() == f), (Float(f), Rational(s)) => Bool(f == s.approx()), - (Identifier(s), Identifier(s2)) => { - apply_operator(Identifier(s), Identifier(s2), ram, equal) - } - (Identifier(s), Int(i)) => apply_operator(Identifier(s), Int(i), ram, equal), - (Null, Identifier(s)) => apply_operator(Identifier(s), Null, ram, equal), - (Identifier(s), Null) => apply_operator(Identifier(s), Null, ram, equal), - (Int(i), Identifier(s)) => apply_operator_reverse(Int(i), Identifier(s), ram, equal), - (Identifier(s), Float(i)) => apply_operator(Identifier(s), Float(i), ram, equal), - (Float(i), Identifier(s)) => apply_operator_reverse(Float(i), Identifier(s), ram, equal), - (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, equal), - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, equal), - - (Rational(s), Identifier(ss)) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, equal) - } - (Identifier(ss), Rational(s)) => apply_operator(Identifier(ss), Rational(s), ram, equal), + (Identifier(s), Identifier(s2)) => match ram { + None => Identifier(s), + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, equal), + }, + (Identifier(s), Int(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Int(i), ram, equal), + None => Int(i), + }, + (Null, Identifier(s)) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, equal), + None => Identifier(s), + }, + (Identifier(s), Null) => match ram { + Some(_) => apply_operator(Identifier(s), Null, ram, equal), + None => Identifier(s), + }, + (Int(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Int(i), Identifier(s), ram, equal), + None => Int(i), + }, + (Identifier(s), Float(i)) => match ram { + Some(_) => apply_operator(Identifier(s), Float(i), ram, equal), + None => Float(i), + }, + (Float(i), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Float(i), Identifier(s), ram, equal), + None => Float(i), + }, + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, equal), + None => Bool(b), + }, + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, equal), + None => Bool(b), + }, + + (Rational(s), Identifier(ss)) => match ram { + None => Rational(s), + Some(_) => { + apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, equal) + } + }, + (Identifier(ss), Rational(s)) => match ram { + Some(_) => apply_operator(Identifier(ss), Rational(s), ram, equal), + None => Rational(s), + }, _ => Identifier("@Those two values are incompatible with the == operator".to_string()), } From 523f92373703c6ae1b37a13b4807e703ebe9b7b4 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:25:00 +0200 Subject: [PATCH 65/67] remove unnessary clones --- src/functions/function.rs | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index c6e07f6..2f573d9 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -97,9 +97,7 @@ pub fn greater( (Rational(s), Identifier(ss)) => match ram { None => Rational(s), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, greater) - } + Some(_) => apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, greater), }, (Identifier(ss), Rational(s)) => match ram { Some(_) => apply_operator(Identifier(ss), Rational(s), ram, greater), @@ -182,9 +180,7 @@ pub fn lesser( None => Identifier(s), }, (Rational(s), Identifier(ss)) => match ram { - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, lesser) - } + Some(_) => apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, lesser), None => Rational(s), }, (Identifier(ss), Rational(s)) => match ram { @@ -265,12 +261,9 @@ pub fn greater_or_equal( None => Identifier(s), }, (Rational(s), Identifier(ss)) => match ram { - Some(_) => apply_operator_reverse( - Rational(s.clone()), - Identifier(ss.clone()), - ram, - greater_or_equal, - ), + Some(_) => { + apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, greater_or_equal) + } None => Rational(s), }, (Identifier(ss), Rational(s)) => match ram { @@ -339,12 +332,9 @@ pub fn lesser_or_equal( None => Int(i), }, (Rational(s), Identifier(ss)) => match ram { - Some(_) => apply_operator_reverse( - Rational(s.clone()), - Identifier(ss.clone()), - ram, - lesser_or_equal, - ), + Some(_) => { + apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, lesser_or_equal) + } None => Rational(s), }, (Identifier(ss), Rational(s)) => match ram { @@ -451,9 +441,7 @@ pub fn equal( (Rational(s), Identifier(ss)) => match ram { None => Rational(s), - Some(_) => { - apply_operator_reverse(Rational(s.clone()), Identifier(ss.clone()), ram, equal) - } + Some(_) => apply_operator_reverse(Rational(s), Identifier(ss.clone()), ram, equal), }, (Identifier(ss), Rational(s)) => match ram { Some(_) => apply_operator(Identifier(ss), Rational(s), ram, equal), From cb2610a8d79d4b2a83e1e93ef5e7418b473877c0 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:28:19 +0200 Subject: [PATCH 66/67] remove stack overflow on other functions --- src/functions/function.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/functions/function.rs b/src/functions/function.rs index 2f573d9..c51daf0 100644 --- a/src/functions/function.rs +++ b/src/functions/function.rs @@ -459,7 +459,10 @@ pub fn not( ) -> Parameters { match i { Bool(b) => Bool(!b), - Identifier(s) => apply_operator(Identifier(s), Null, ram, not), + Identifier(s) => match ram { + None => Bool(false), + Some(_) => apply_operator(Identifier(s), Null, ram, not), + }, _ => Bool(false), } } @@ -469,9 +472,18 @@ pub fn and(i: Parameters, i2: Parameters, ram: Option<&HashMap Bool(b && b2), (Bool(b), Null) => Bool(b), (Null, Bool(b)) => Bool(b), - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, and), - (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, and), - (Identifier(s), Identifier(s2)) => apply_operator(Identifier(s), Identifier(s2), ram, and), + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, and), + None => Bool(b), + }, + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, and), + None => Bool(b), + }, + (Identifier(s), Identifier(s2)) => match ram { + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, and), + None => Bool(false), + }, _ => Bool(false), } } @@ -481,9 +493,18 @@ pub fn or(i: Parameters, i2: Parameters, ram: Option<&HashMap Bool(b || b2), (Bool(b), Null) => Bool(b), (Null, Bool(b)) => Bool(b), - (Identifier(s), Bool(b)) => apply_operator(Identifier(s), Bool(b), ram, or), - (Bool(b), Identifier(s)) => apply_operator_reverse(Bool(b), Identifier(s), ram, or), - (Identifier(s), Identifier(s2)) => apply_operator(Identifier(s), Identifier(s2), ram, or), + (Identifier(s), Bool(b)) => match ram { + Some(_) => apply_operator(Identifier(s), Bool(b), ram, or), + None => Bool(b), + }, + (Bool(b), Identifier(s)) => match ram { + Some(_) => apply_operator_reverse(Bool(b), Identifier(s), ram, or), + None => Bool(b), + }, + (Identifier(s), Identifier(s2)) => match ram { + Some(_) => apply_operator(Identifier(s), Identifier(s2), ram, or), + None => Bool(false), + }, _ => Bool(false), } } From 307875b95aab31a6bd92e1d0199604d123b246aa Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Sun, 19 May 2024 12:34:08 +0200 Subject: [PATCH 67/67] change makefile update CHANGELOG --- CHANGELOG.md | 2 +- Makefile | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b2572e..bf69d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Version 3.0.0 : Symbolic calculation! Version 3.0.0, at last! (if you've seen the PR for it it's been sitting for more -than a month!) +than three weeks!) Symbolic computation was added you can compute expressions with symbols (such as x,y,whatever) and more ! diff --git a/Makefile b/Makefile index 2833d59..f9d7d19 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,5 @@ release: cargo build --release test: cargo test +publish: + cargo publish