aboutsummaryrefslogtreecommitdiff
path: root/examples/arithmetic/parser_ast.rs
blob: 20feb267ac0a2b072399c9d1f2bd573e331d92d9 (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
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
use std::fmt;
use std::fmt::{Debug, Display, Formatter};

use std::str::FromStr;

use winnow::prelude::*;
use winnow::{
    ascii::{digit1 as digits, multispace0 as multispaces},
    combinator::alt,
    combinator::delimited,
    combinator::repeat,
    token::one_of,
};

#[derive(Debug, Clone)]
pub enum Expr {
    Value(i64),
    Add(Box<Expr>, Box<Expr>),
    Sub(Box<Expr>, Box<Expr>),
    Mul(Box<Expr>, Box<Expr>),
    Div(Box<Expr>, Box<Expr>),
    Paren(Box<Expr>),
}

impl Expr {
    pub fn eval(&self) -> i64 {
        match self {
            Self::Value(v) => *v,
            Self::Add(lhs, rhs) => lhs.eval() + rhs.eval(),
            Self::Sub(lhs, rhs) => lhs.eval() - rhs.eval(),
            Self::Mul(lhs, rhs) => lhs.eval() * rhs.eval(),
            Self::Div(lhs, rhs) => lhs.eval() / rhs.eval(),
            Self::Paren(expr) => expr.eval(),
        }
    }
}

impl Display for Expr {
    fn fmt(&self, format: &mut Formatter<'_>) -> fmt::Result {
        use Expr::{Add, Div, Mul, Paren, Sub, Value};
        match *self {
            Value(val) => write!(format, "{}", val),
            Add(ref left, ref right) => write!(format, "{} + {}", left, right),
            Sub(ref left, ref right) => write!(format, "{} - {}", left, right),
            Mul(ref left, ref right) => write!(format, "{} * {}", left, right),
            Div(ref left, ref right) => write!(format, "{} / {}", left, right),
            Paren(ref expr) => write!(format, "({})", expr),
        }
    }
}

pub fn expr(i: &mut &str) -> PResult<Expr> {
    let init = term.parse_next(i)?;

    repeat(0.., (one_of(['+', '-']), term))
        .fold(
            move || init.clone(),
            |acc, (op, val): (char, Expr)| {
                if op == '+' {
                    Expr::Add(Box::new(acc), Box::new(val))
                } else {
                    Expr::Sub(Box::new(acc), Box::new(val))
                }
            },
        )
        .parse_next(i)
}

fn term(i: &mut &str) -> PResult<Expr> {
    let init = factor.parse_next(i)?;

    repeat(0.., (one_of(['*', '/']), factor))
        .fold(
            move || init.clone(),
            |acc, (op, val): (char, Expr)| {
                if op == '*' {
                    Expr::Mul(Box::new(acc), Box::new(val))
                } else {
                    Expr::Div(Box::new(acc), Box::new(val))
                }
            },
        )
        .parse_next(i)
}

fn factor(i: &mut &str) -> PResult<Expr> {
    delimited(
        multispaces,
        alt((digits.try_map(FromStr::from_str).map(Expr::Value), parens)),
        multispaces,
    )
    .parse_next(i)
}

fn parens(i: &mut &str) -> PResult<Expr> {
    delimited("(", expr, ")")
        .map(|e| Expr::Paren(Box::new(e)))
        .parse_next(i)
}

#[test]
fn factor_test() {
    let input = "3";
    let expected = Ok(("", String::from("Value(3)")));
    assert_eq!(factor.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 12";
    let expected = Ok(("", String::from("Value(12)")));
    assert_eq!(factor.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = "537 ";
    let expected = Ok(("", String::from("Value(537)")));
    assert_eq!(factor.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = "  24     ";
    let expected = Ok(("", String::from("Value(24)")));
    assert_eq!(factor.map(|e| format!("{e:?}")).parse_peek(input), expected);
}

#[test]
fn term_test() {
    let input = " 12 *2 /  3";
    let expected = Ok(("", String::from("Div(Mul(Value(12), Value(2)), Value(3))")));
    assert_eq!(term.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 12 *2 /  3";
    let expected = Ok(("", String::from("Div(Mul(Value(12), Value(2)), Value(3))")));
    assert_eq!(term.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 2* 3  *2 *2 /  3";
    let expected = Ok((
        "",
        String::from("Div(Mul(Mul(Mul(Value(2), Value(3)), Value(2)), Value(2)), Value(3))"),
    ));
    assert_eq!(term.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 48 /  3/2";
    let expected = Ok(("", String::from("Div(Div(Value(48), Value(3)), Value(2))")));
    assert_eq!(term.map(|e| format!("{e:?}")).parse_peek(input), expected);
}

#[test]
fn expr_test() {
    let input = " 1 +  2 ";
    let expected = Ok(("", String::from("Add(Value(1), Value(2))")));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 12 + 6 - 4+  3";
    let expected = Ok((
        "",
        String::from("Add(Sub(Add(Value(12), Value(6)), Value(4)), Value(3))"),
    ));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 1 + 2*3 + 4";
    let expected = Ok((
        "",
        String::from("Add(Add(Value(1), Mul(Value(2), Value(3))), Value(4))"),
    ));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);
}

#[test]
fn parens_test() {
    let input = " (  2 )";
    let expected = Ok(("", String::from("Paren(Value(2))")));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = " 2* (  3 + 4 ) ";
    let expected = Ok((
        "",
        String::from("Mul(Value(2), Paren(Add(Value(3), Value(4))))"),
    ));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);

    let input = "  2*2 / ( 5 - 1) + 3";
    let expected = Ok((
        "",
        String::from("Add(Div(Mul(Value(2), Value(2)), Paren(Sub(Value(5), Value(1)))), Value(3))"),
    ));
    assert_eq!(expr.map(|e| format!("{e:?}")).parse_peek(input), expected);
}