summaryrefslogtreecommitdiff
path: root/src/types/operator.rs
blob: af607e32233597d80dc99142e1237c9042d76804 (plain) (blame)
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
#[derive(Clone, Copy)]
pub enum Operator {
    Equal,
    NotEqual,
    LessThan,
    GreaterThan,
    LessThanEqual,
    GreaterThanEqual,
    Add,
    Subtract,
    Multiply,
    Divide,
    Modulo,
    Exponent,
    LeftShift,
    RightShift,
    BitAnd,
    BitOr,
    BitXor,
    BitNot,
    Length,
}

impl Operator {
    pub fn from_str(string: &str) -> Option<Self> {
        match string {
            "="     => Some(Operator::Equal),
            "=="    => Some(Operator::Equal),
            "<eq>"  => Some(Operator::Equal),
            "!="    => Some(Operator::NotEqual),
            "<neq>" => Some(Operator::NotEqual),
            "<"     => Some(Operator::LessThan),
            "<lth>" => Some(Operator::LessThan),
            ">"     => Some(Operator::GreaterThan),
            "<gth>" => Some(Operator::GreaterThan),
            "<="    => Some(Operator::LessThanEqual),
            "<leq>" => Some(Operator::LessThanEqual),
            ">="    => Some(Operator::GreaterThanEqual),
            "<geq>" => Some(Operator::GreaterThanEqual),
            "+"     => Some(Operator::Add),
            "<add>" => Some(Operator::Add),
            "-"     => Some(Operator::Subtract),
            "<sub>" => Some(Operator::Subtract),
            "*"     => Some(Operator::Multiply),
            "<mul>" => Some(Operator::Multiply),
            "/"     => Some(Operator::Divide),
            "<div>" => Some(Operator::Divide),
            "<mod>" => Some(Operator::Modulo),
            "**"    => Some(Operator::Exponent),
            "<exp>" => Some(Operator::Exponent),
            "<<"    => Some(Operator::LeftShift),
            "<shl>" => Some(Operator::LeftShift),
            ">>"    => Some(Operator::RightShift),
            "<shr>" => Some(Operator::RightShift),
            "<and>" => Some(Operator::BitAnd),
            "<or>"  => Some(Operator::BitOr),
            "<xor>" => Some(Operator::BitXor),
            "<not>" => Some(Operator::BitNot),
            "<len>" => Some(Operator::Length),
            _      => None,
        }
    }
}

impl std::fmt::Display for Operator {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let string = match self {
            Operator::Equal            => "<eq>",
            Operator::NotEqual         => "<neq>",
            Operator::LessThan         => "<lth>",
            Operator::GreaterThan      => "<gth>",
            Operator::LessThanEqual    => "<leq>",
            Operator::GreaterThanEqual => "<geq>",
            Operator::Add              => "<add>",
            Operator::Subtract         => "<sub>",
            Operator::Multiply         => "<mul>",
            Operator::Divide           => "<div>",
            Operator::Modulo           => "<mod>",
            Operator::Exponent         => "<exp>",
            Operator::LeftShift        => "<shl>",
            Operator::RightShift       => "<shr>",
            Operator::BitAnd           => "<and>",
            Operator::BitOr            => "<or>",
            Operator::BitXor           => "<xor>",
            Operator::BitNot           => "<not>",
            Operator::Length           => "<len>",
        };
        write!(f, "{string}")
    }
}