corewars_parser/phase/evaluation/
expression.rs

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Helper functions for evaluating an expression syntax tree.
//!
//! Most functions here panic instead of returning Result because at this point
//! any errors should have been caught earlier during initial parsing.

use crate::grammar::{Pair, Rule};

/// Evaluate an Expression. Panics if the expression tree is invalid, which
/// should only happen due to programmer error (either the grammar or this code
/// is incorrect).
pub fn evaluate(pair: Pair) -> i32 {
    let mut result = None;
    let mut boolean_op: fn(i32, i32) -> i32 =
        |_, _| unreachable!("BooleanOp called before first operand");

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::Value => {
                let operand = evaluate_value(inner_pair);
                result = result.map(|x| boolean_op(x, operand)).or(Some(operand));
            }
            Rule::BooleanOp => {
                boolean_op = match inner_pair.as_str() {
                    "&&" => |a, b| i32::from(a != 0 && b != 0),
                    "||" => |a, b| i32::from(a != 0 || b != 0),
                    op => unreachable!("Invalid BooleanOp {:?}", op),
                }
            }
            other => unreachable!("unexpected rule in Expression: {:?}", other),
        }
    }

    result.unwrap_or_else(|| panic!("Invalid Expression"))
}

fn evaluate_value(pair: Pair) -> i32 {
    let mut result = None;
    let mut compare_op: fn(i32, i32) -> i32 =
        |_, _| unreachable!("CompareOp called before first operand");

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::Sum => {
                let operand = evaluate_sum(inner_pair);
                result = result.map(|x| compare_op(x, operand)).or(Some(operand));
            }
            Rule::CompareOp => {
                // Casting bool to integer is always 0 or 1
                compare_op = match inner_pair.as_str() {
                    ">" => |a, b| i32::from(a > b),
                    ">=" => |a, b| i32::from(a >= b),
                    "<" => |a, b| i32::from(a < b),
                    "<=" => |a, b| i32::from(a <= b),
                    "==" => |a, b| i32::from(a == b),
                    "!=" => |a, b| i32::from(a != b),
                    op => unreachable!("Invalid CompareOp {:?}", op),
                };
            }
            other => unreachable!("unexpected rule in Value: {:?}", other),
        }
    }

    result.unwrap_or_else(|| panic!("Invalid Value"))
}

fn evaluate_sum(pair: Pair) -> i32 {
    let mut result = None;
    let mut add_op: fn(i32, i32) -> i32 = |_, _| unreachable!("AddOp called before first operand");

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::Product => {
                let operand = evaluate_product(inner_pair);
                result = result.map(|x| add_op(x, operand)).or(Some(operand));
            }
            Rule::AddOp => {
                add_op = match inner_pair.as_str() {
                    "+" => |a, b| a + b,
                    "-" => |a, b| a - b,
                    op => unreachable!("Invalid AddOp {:?}", op),
                };
            }
            rule => unreachable!("unexpected {:?} in Sum: {:?}", rule, inner_pair.as_str()),
        }
    }

    result.unwrap_or_else(|| panic!("Invalid Sum"))
}

fn evaluate_product(pair: Pair) -> i32 {
    let mut result = None;
    let mut mul_op: fn(i32, i32) -> i32 =
        |_, _| unreachable!("MultiplyOp called before first operand");

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::UnaryExpr => {
                let operand = evaluate_unary(inner_pair);
                result = result.map(|x| mul_op(x, operand)).or(Some(operand));
            }
            Rule::MultiplyOp => {
                mul_op = match inner_pair.as_str() {
                    "*" => |a, b| a * b,
                    "/" => |a, b| a / b,
                    "%" => |a, b| a % b,
                    op => unreachable!("Invalid MultiplyOp {:?}", op),
                };
            }
            rule => unreachable!(
                "unexpected {:?} in Product: {:?}",
                rule,
                inner_pair.as_str()
            ),
        }
    }

    result.unwrap_or_else(|| panic!("Invalid Product"))
}

fn evaluate_unary(pair: Pair) -> i32 {
    let mut result = None;
    let mut unary_ops: Vec<fn(i32) -> i32> = Vec::new();

    for inner_pair in pair.into_inner() {
        match inner_pair.as_rule() {
            Rule::Number => result = Some(evaluate_number(&inner_pair)),
            Rule::Expression => result = Some(evaluate(inner_pair)),
            Rule::UnaryOp => match inner_pair.as_str() {
                "-" => unary_ops.push(|x| -x),
                "+" => (), // Identity function
                "!" => unary_ops.push(|x| i32::from(x == 0)),
                other => unreachable!("Invalid unary operator {:?}", other),
            },
            other => unreachable!(
                "unexpected {:?} in UnaryExpr: {:?}",
                other,
                inner_pair.as_str()
            ),
        }
    }

    for op in unary_ops {
        result = result.map(op);
    }

    result.unwrap_or_else(|| panic!("UnaryExpr did not contain a value"))
}

fn evaluate_number(pair: &Pair) -> i32 {
    assert!(pair.as_rule() == Rule::Number);
    pair.as_str()
        .parse::<i32>()
        .unwrap_or_else(|_| panic!("Invalid Number: {:?}", pair.as_str()))
}

#[cfg(test)]
mod test {
    use super::*;

    use crate::grammar::parse_expression;

    use test_case::test_case;

    // Unary/atoms
    #[test_case("10" => 10; "number")]
    #[test_case("-47" => -47; "negative number")]
    #[test_case("! 10" => 0; "boolean not true")]
    #[test_case("! 0" => 1; "boolean not false")]
    // Product
    #[test_case("2 * 3" => 6; "product")]
    #[test_case("24 / 4 / 2" => 3; "multiple quotient")]
    #[test_case("24 % 15 % 6" => 3; "multiple modulo")]
    // Sum
    #[test_case("1 + 1" => 2; "sum")]
    #[test_case("1 + 2 * 3" => 7; "sum and product")]
    // Parentheses
    #[test_case("(2)" => 2; "parenthesized number")]
    #[test_case("(2 * 3)" => 6; "parenthesized product")]
    #[test_case("-(2 * 3)" => -6; "unary parenthesized product")]
    #[test_case("(2 + 3)" => 5; "parenthesized sum")]
    // Comparison
    #[test_case("1 < 2" => 1; "less than")]
    #[test_case("1 <= 2" => 1; "less than or equal")]
    #[test_case("1 > 2" => 0; "greater than")]
    #[test_case("1 >= 2" => 0; "greater than or equal")]
    #[test_case("1 == 2" => 0; "equals")]
    #[test_case("1 != 2" => 1; "not equals")]
    // Boolean
    #[test_case("0 && 1" => 0; "boolean and")]
    #[test_case("0 || 1" => 1; "boolean or")]
    fn evaluates_expressions(input: &str) -> i32 {
        let pair = parse_expression(input).expect("Failed to parse as Expression");

        evaluate(pair)
    }
}