1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::fmt;
use std::ops;
use std::collections::HashMap;

use crate::core;
use crate::core::Actions;

/// # `Expression` struct
///
/// Probably the most important structure of this crate. It provides a tree-like structure
/// for expressions. The basic idea is that the `Expression` contains two arguments and the
/// action to be done with them. But so far, there are situations when there cannot be exactly two
/// arguments (for example, an ordinary number or a variable), so `Actions` have
/// taken responsibility for such cases.
///
/// # Example
/// ```edition2018
/// use rusymbols::Expression;
/// let x = Expression::new_var("x"); // new 'x' variable creation
/// let y = Expression::new_var("y"); // new 'y' variable creation
/// let expr = x + y; // expression
///
/// assert_eq!(expr.to_string(), "x + y")
/// ```
///
/// ## Beware of automatic parentheses
/// ```edition2018
/// use rusymbols::Expression;
/// let x = Expression::new_var("x"); // new 'x' variable creation
/// let y = Expression::new_var("y"); // new 'y' variable creation
/// let z = Expression::new_var("z"); // new 'z' variable creation
/// // according to the rules of arithmetic, multiplication takes precedence over addition
/// let expr = x.clone() + y.clone() * z.clone(); // equivalent to x + (y * z)
///
/// let mut expr_2 = x.clone() + y.clone(); // equivalent to x + y
/// expr_2 = expr_2 * z.clone(); // equivalent to (x + y) * z
///
/// let expr_3 = (x + y) * z;
///
/// assert_ne!(expr.to_string(), expr_2.to_string());
/// assert_eq!(expr_2.to_string(), expr_3.to_string());
///
/// assert_eq!(expr.to_string(), "x + y * z");
/// assert_eq!(expr_2.to_string(), "(x + y) * z");
/// ```
#[derive(Clone, Debug, Default)]
pub struct Expression {
    args: Vec<Expression>,
    kind: Actions,
}

/// Associated functions
impl Expression {
    /// New `Expression` constructor
    /// #### Example
    /// ```
    /// use rusymbols::{core, Expression};
    /// let x = Expression::new_var("x"); // new 'x' variable creation
    /// let two = Expression::new_val(2.0); // new 2.0 value creation
    /// let expr = Expression::new(x, two, core::Actions::Mul); // new expression creation
    ///
    /// assert_eq!(expr.to_string(), "x * 2")
    /// ```
    pub fn new(left: Expression, right: Expression, kind: Actions) -> Expression {
        Expression{ args: vec![left, right], kind}
    }

    /// New `Expression` constructor with variable literal
    /// #### Example
    /// ```
    /// use rusymbols::{core, Expression};
    /// let x = Expression::new_var("x"); // new 'x' variable creation
    /// let y = Expression::new_var("y"); // new 'y' value creation
    /// let expr = x / y;
    ///
    /// assert_eq!(expr.to_string(), "x / y")
    /// ```
    pub fn new_var(literal: &str) -> Expression {
        Expression { args: vec![], kind: Actions::Var(String::from(literal)) }
    }


    /// New `Expression` constructor with numeric value
    /// #### Example
    /// ```
    /// use rusymbols::{core, Expression};
    /// use std::collections::HashMap;
    ///
    /// let ten = Expression::new_val(10.0); // new 'x' variable creation
    /// let two = Expression::new_val(2.0); // new 2.0 value creation
    /// let expr = ten * two; // new expression creation
    /// let args: HashMap<&str, f64> = HashMap::new();
    ///
    /// assert_eq!(expr.to_string(), "10 * 2");
    /// assert_eq!(expr.eval_args(&args).unwrap(), 20.0);
    ///
    /// ```
    pub fn new_val(value: f64) -> Expression {
        Expression { args: vec![], kind: Actions::Val(value) }
    }

    ///New `Expression` in brackets
    pub fn new_brackets(expression: Expression, brackets: core::Brackets) -> Expression {
        Expression { args: vec![], kind: Actions::Brackets(Box::new(expression), brackets)}
    }
}


///Taking ownership of data
impl Expression {

    /// Wraps the given `Expression` with the specified parentheses
    /// #### Example
    /// ```
    /// use rusymbols::Expression;
    /// use rusymbols::core::Brackets;
    /// let x = Expression::new_var("x");
    /// let expr = x.brackets(Brackets::Square);
    ///
    /// assert_eq!(expr.to_string(), "[x]")
    /// ```
    #[inline]
    pub fn brackets(self, brackets: core::Brackets) -> Expression {
        Expression::new_brackets(self, brackets)
    }

    /// Wraps the given `Expression` with the round brackets
    /// #### Example
    /// ```
    /// use rusymbols::Expression;
    /// let x = Expression::new_var("x");
    /// let expr = x.brackets_round();
    ///
    /// assert_eq!(expr.to_string(), "(x)")
    /// ```
    #[inline]
    pub fn brackets_round(self) -> Expression {
        self.brackets(core::Brackets::Round)
    }

    /// Raises an `Expression` to a specified power
    /// #### Example
    /// ```
    /// use rusymbols::Expression;
    /// let x = Expression::new_var("x");
    /// let y = Expression::new_var("y");
    /// let expr = x.pow(y);
    ///
    /// assert_eq!(expr.to_string(), "x**y")
    /// ```
    pub fn pow(mut self, mut rhs: Self) -> Self {
        if self.kind < Actions::Pow { self = self.brackets_round() };
        if rhs.kind < Actions::Pow { rhs = rhs.brackets_round() };

        Expression::new(self, rhs, core::Actions::Pow)
    }

    /// Wraps the given `Expression` with parentheses if the given `Actions` has a higher priority
    #[inline(always)]
    fn brace_if(self, target: Actions) -> Self {
        if self.kind < target { self.brackets_round() }
        else { self }
    }
}

///Borrowing methods
impl Expression {
    /// Tries to get the numeric value of an `Expression` by replacing
    /// variables with values from a `HashMap`
    ///
    /// Returns None if the expression cannot be evaluated (e.g. insufficient variable values were passed)
    ///
    /// # Usage
    /// ```edition2018
    /// const LITERAL_X: &str = "x";
    /// const LITERAL_Y: &str = "y";
    ///
    /// use std::collections::HashMap;
    /// use rusymbols::Expression;
    ///
    /// let mut args: HashMap<&str, f64> = HashMap::new();
    /// args.insert(LITERAL_X, 2.0);
    /// args.insert(LITERAL_Y, 2.0);
    ///
    /// let x = Expression::new_var(LITERAL_X);
    /// let y = Expression::new_var(LITERAL_Y);
    /// let expr = x * y;
    ///
    /// assert_eq!(expr.eval_args(&args).unwrap(), 4.0);
    /// ```
    pub fn eval_args(&self, args: &HashMap<&str, f64>) -> Option<f64> {
        match &self.kind {
            Actions::Val(value) => Some(value.clone()),
            Actions::Var(var) => Some(args.get(var.as_str())?.clone()),
            Actions::Add => Some(self.args[0].eval_args(args)? + self.args[1].eval_args(args)?),
            Actions::Mul => Some(self.args[0].eval_args(args)? * self.args[1].eval_args(args)?),
            Actions::Pow => Some(self.args[0].eval_args(args)?.powf(self.args[1].eval_args(args)?)),
            Actions::Brackets(expr, _) => expr.eval_args(args),
            Actions::Div => {
                let denominator = Expression::new(self.args[1].clone(),
                                                  Expression::new_val(-1.0),
                                                  Actions::Pow);
                Expression::new(self.args[0].clone(),
                                denominator,
                                Actions::Mul).eval_args(args)
            },
            Actions::Sub => {
                Expression::new(self.args[0].clone(),
                                -self.args[1].clone(),
                                Actions::Add).eval_args(args)
            }
        }
    }
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self.kind.clone() {
            Actions::Val(value) => f.write_str(&value.to_string()),
            Actions::Var(literal) => f.write_str(&literal),
            Actions::Brackets(expr, brackets) => {
                let (left, right) = brackets.get_symbols();
                f.write_str(left)?;
                f.write_str(&expr.to_string())?;
                f.write_str(right)
            },
            _ => {
                f.write_str(&self.args[0].to_string())?;
                f.write_str(&self.kind.to_string())?;
                f.write_str(&self.args[1].to_string())
            }
        }
    }
}

impl ops::Add for Expression {
    type Output = Expression;

    fn add(mut self, mut rhs: Self) -> Self::Output {
        self = self.brace_if(Actions::Add);
        rhs = rhs.brace_if(Actions::Add);
        Expression::new(self, rhs, Actions::Add)
    }
}

impl ops::Mul for Expression {
    type Output = Expression;

    fn mul(mut self, mut rhs: Self) -> Self::Output {
        self = self.brace_if(Actions::Mul);
        rhs = rhs.brace_if(Actions::Mul);
        Expression::new(self, rhs, core::Actions::Mul)
    }
}

impl ops::Div for Expression {
    type Output = Self;

    fn div(mut self, mut rhs: Self) -> Self::Output {
        self = self.brace_if(Actions::Div);
        rhs = rhs.brace_if(Actions::Div);
        Expression::new(self, rhs, Actions::Div)
    }
}

impl ops::Neg for Expression {
    type Output = Expression;

    fn neg(self) -> Self::Output {
        Expression::new(self, Expression::new_val(-1.0), Actions::Mul)
    }
}

impl ops::Sub for Expression {
    type Output = Expression;

    fn sub(mut self, mut rhs: Self) -> Self::Output {
        self = self.brace_if(Actions::Add);
        rhs = rhs.brace_if(Actions::Add);
        Expression::new(self, rhs, Actions::Sub)
    }
}