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
#![cfg_attr(test, feature(test))]

#[macro_use]
extern crate decimal;
#[cfg(test)]
extern crate test;

#[cfg(test)]
mod bench;

mod error;
mod token;
pub mod parse;
pub mod value;

pub use value::Value;
pub use error::CalcError;

/// Evalulates a regular mathematical expression.
pub fn eval(input: &str) -> Result<Value, CalcError> {
    let mut env = parse::DefaultEnvironment;
    token::tokenize(input).and_then(|x| parse::parse(&x, &mut env))
}

/// Evalulates a regular mathematical expression, with extra environment
/// variables.
pub fn eval_with_env<E>(input: &str, env: &mut E) -> Result<Value, CalcError>
    where E: parse::Environment
{
    token::tokenize(input).and_then(|x| parse::parse(&x, env))
}

/// Evalulates mathematical expressions that are written in Polish Notation.
///
/// Polish Notation defines that a string of operators are given at the
/// beginning of the
/// expression, as prefixes, and are followed by a string of numbers to apply
/// each operation
/// to. Polish Notation enables writing mathematical expressions that don't
/// require grouping
/// via parenthesis. It's also referred to as Prefix Notation, or Normal Polish
/// Notation (NPN).
///
/// # Examples
///
/// - `+ * 3 4 5` is equivalent to `3 * 4 + 5`
/// - `+ / * 5 3 2 * + 1 3 5` is equivalent to `((5 * 3) / 2) + ((1 + 3) * 5)`
pub fn eval_polish(input: &str) -> Result<Value, CalcError> {
    let mut env = parse::DefaultEnvironment;
    token::tokenize_polish(input).and_then(|x| parse::parse(&x, &mut env))
}

/// Evalulates mathematical expressions that are written in Polish Notation,
/// with extra
/// environment variables.
///
/// Polish Notation defines that a string of operators are given at the
/// beginning of the
/// expression, as prefixes, and are followed by a string of numbers to apply
/// each operation
/// to. Polish Notation enables writing mathematical expressions that don't
/// require grouping
/// via parenthesis. It's also referred to as prefix notation, or Normal Polish
/// Notation (NPN).
///
/// # Examples
///
/// - `+ * 3 4 5` is equivalent to `3 * 4 + 5`
/// - `+ / * 5 3 2 * + 1 3 5` is equivalent to `((5 * 3) / 2) + ((1 + 3) * 5)`
pub fn eval_polish_with_env<E>(
    input: &str,
    env: &mut E,
) -> Result<Value, CalcError>
    where E: parse::Environment
{
    token::tokenize_polish(input).and_then(|x| parse::parse(&x, env))
}

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

    #[test]
    fn basics() {
        let cases =
            vec![
                ("  1 +   1", Value::Dec(2)),
                (" 4 * 7 - 14", Value::Dec(14)),
                (" 2 << 16 ", Value::Dec(131072)),
                (" ((4 * 18) % 17) / 3", Value::Float(d128!(4.0) / d128!(3.0))),
                ("2²³²", Value::Dec(4096)),
                ("4 ^ 3 ^ 2 ^ 3 ^ 4 ^ 2", Value::Dec(0)),
                ("3 << (4 >> 2)", Value::Dec(6)),
                ("~0", Value::Dec(-1)),
                // ("cos pi + sin (tau * (3 / 4))", Value::Float(d128!(-2.0))),
                ("~~5", Value::Dec(5)),
            ];
        for (input, expected) in cases {
            assert_eq!(eval(input), Ok(expected));
        }
    }

    #[test]
    fn polish() {
        let cases =
            vec![
                (" + 1 1", Value::Dec(2)),
                (" - * 4 7 14", Value::Dec(14)),
                (" << 2 16", Value::Dec(131072)),
                (" / % * 4 18 17 3", Value::Float(d128!(4.0) / d128!(3.0))),
                ("* + 1 3 5", Value::Dec(20)),
                ("+ / * 5 3 2 * + 1 3 5", Value::Float(d128!(27.5))),
                ("^ ^ ^ ^ ^ 4 3 2 3 4 2", Value::Dec(0)),
                ("<< 3 >> 4 2", Value::Dec(6)),
            ];
        for (input, expected) in cases {
            assert_eq!(eval_polish(input), Ok(expected));
        }
    }


    #[test]
    fn random() {
        let cases =
            vec![
                (
                    "((15 * 10) - 26 * 19 - 30 / ((57 * 79 + 93 / 87 / 47))) / 8",
                    Value::Float(d128!(-43.00083277394169075309321854399588))
                ),
                (
                    "(3 << 6) * 7 + (40 / 3)",
                    Value::Float(d128!(1357.333333333333333333333333333333))
                ),
                (
                    "(21 & (5) ^ (20 & 81)) / (25 << 3)",
                    Value::Float(d128!(0.105))
                ),
                (
                    "(79 & 14) * ((3) - 76 + 67 / (62) - (85 ^ (7 - (32) >> 52)))",
                    Value::Float(d128!(197.1290322580645161290322580645161))
                ),
            ];

        for (input, expected) in cases {
            assert_eq!(eval(input), Ok(expected));
        }
    }

}