summaryrefslogtreecommitdiff
path: root/exprparser/src/types.rs
blob: 8551661a69ea44a81bfc95090c83d5018752b68a (plain)
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
#[derive(Clone, Debug)]
pub enum Expression {
    Add(Box<Term>, Box<Expression>),
    Sub(Box<Term>, Box<Expression>),
    Term(Box<Term>),
}

#[derive(Clone, Debug)]
pub enum Term {
    Mul(Box<Factor>, Box<Term>),
    Div(Box<Factor>, Box<Term>),
    Factor(Box<Factor>),
}

#[derive(Clone, Debug)]
pub enum Factor {
    Expression(Box<Expression>),
    Value(f64),
    Symbol(String),
}

impl Expression {
    pub fn new_add(t: Term, e: Expression) -> Expression {
        Expression::Add(Box::new(t), Box::new(e))
    }

    pub fn new_sub(t: Term, e: Expression) -> Expression {
        Expression::Sub(Box::new(t), Box::new(e))
    }

    pub fn new_term(t: Term) -> Expression {
        Expression::Term(Box::new(t))
    }
}

impl Term {
    pub fn new_mul(f: Factor, t: Term) -> Term {
        Term::Mul(Box::new(f), Box::new(t))
    }

    pub fn new_div(f: Factor, t: Term) -> Term {
        Term::Div(Box::new(f), Box::new(t))
    }

    pub fn new_factor(f: Factor) -> Term {
        Term::Factor(Box::new(f))
    }
}

impl Factor {
    pub fn new_expression(e: Expression) -> Factor {
        Factor::Expression(Box::new(e))
    }

    pub fn new_value(v: f64) -> Factor {
        Factor::Value(v)
    }

    pub fn new_symbol(s: String) -> Factor {
        Factor::Symbol(s)
    }
}