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
use std::fmt;

/// These are all the functions/operators that can be used in an equation.
/// 
/// The operators are: Add (+), Subtract (-), Multiply (*), Divide(/), Modulo (%), Exponent(^).
/// 
/// All syntax is the same for the other functions but lowercase.

#[derive(Debug, Clone)]
pub enum Operator {
    Add,
    Subtract,
    Multiply,
    Divide,
    Modulo,
    Exponent,
    Max,
    Min,
    Pow,
    Log,
    Sin,
    Cos,
    Tan,
    Abs,
    Floor,
    Round,
    Ceil,
    Root,
    Exp,
    Ln,
    Sqrt,
    Error,
}

use Operator::*;

impl From<&str> for Operator {
    fn from(c: &str) -> Operator {
        match c {
            "+" => Add,
            "-" => Subtract,
            "*" => Multiply,
            "/" => Divide,
            "%" => Modulo,
            "^" => Exponent,
            "max" => Max,
            "min" => Min,
            "pow" => Pow,
            "log" => Log,
            "sin" => Sin,
            "cos" => Cos,
            "tan" => Tan,
            "abs" => Abs,
            "floor" => Floor,
            "round" => Round,
            "ceil" => Ceil,
            "root" => Root,
            "exp" => Exp,
            "ln" => Ln,
            "sqrt" => Sqrt,
            _ => Error,
        }
    }
}

impl From<char> for Operator {
    fn from(c: char) -> Operator {
        Operator::from(c.to_string().as_str())
    }
}

impl Operator {
    /// Compare the type of this operator to the given operator
    pub fn compare(&self, other: &Operator) -> bool {
        std::mem::discriminant(self) == std::mem::discriminant(other)
    }
}

impl fmt::Display for Operator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(
            match self {
                Add => "+",
                Subtract => "-",
                Multiply => "*",
                Divide => "/",
                Modulo => "%",
                Exponent => "^",
                Max => "max",
                Min => "min",
                Pow => "pow",
                Log => "logab",
                Sin => "sin",
                Cos => "cos",
                Tan => "tan",
                Abs => "abs",
                Floor => "floor",
                Round => "round",
                Ceil => "ceil",
                Root => "root",
                Exp => "exp",
                Ln => "ln",
                Sqrt => "sqrt",
                Error => "error",
            }
        )
    }
}

// Get precedence (importance) of an operator
pub fn get_precedence(c: Option<&char>) -> i8 {
    match c {
        Some(c) => match c {
            '+' => 1,
            '-' => 1,
            '*' => 3,
            '/' => 3,
            '%' => 3,
            '^' => 5,
            _ => -1,
        },
        None => -1,
    }
}