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

#[derive(Debug, PartialEq, Eq)]
pub enum Operation {
    Division,
    Addition,
    Multiply,
    Subtraction,
}
#[derive(Debug, PartialEq)]

pub enum Expression {
    Literal(f64),
    Binary(Box<Expression>, Operation, Box<Expression>),
    Grouping(Box<Expression>),
    Error(ExpressionErrors),
}

#[derive(Debug, PartialEq, Eq)]
pub enum ExpressionErrors {
    UnexpectedElement(String),
}

impl fmt::Display for ExpressionErrors {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExpressionErrors::UnexpectedElement(message) => {
                write!(f, "{}", message)
            }
        }
    }
}

impl Expression {
    pub fn execute(&self) -> f64 {
        match self {
            Expression::Literal(num) => *num,
            Expression::Binary(left, operation, right) => {
                let left = Expression::execute(left);
                let right = Expression::execute(right);
                match &operation {
                    Operation::Addition => left + right,
                    Operation::Division => left / right,
                    Operation::Multiply => left * right,
                    Operation::Subtraction => left - right,
                }
            }
            Expression::Grouping(expr) => Expression::execute(expr),
            Expression::Error(_) => {
                unreachable!()
            }
        }
    }
}