bexeval 0.1.2

bexeval is a Rust crate for evaluating string formulas restricted to values as integers only.
Documentation
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use std::collections::HashMap;
use crate::lexer::{Lexer, LexToken};
use crate::value_type::Integer;

#[derive(Clone, Debug, PartialEq)]
pub struct ParserError {
    msg: String,
}

impl ParserError {
    pub fn new(msg: &str) -> ParserError {
        ParserError {
            msg: msg.to_string(),
        }
    }
    pub fn msg(&self) -> &str {
        self.msg.as_str()
    }
}
impl std::fmt::Display for ParserError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.msg())
    }
}
impl std::error::Error for ParserError {}

pub struct Parser<T: Integer = i32> {
    _phantom: std::marker::PhantomData<T>
}

/// By default, `i32` is used as the value type
impl Default for Parser {
    fn default() -> Self {
        Parser::<i32>::new()
    }
}

impl<T: Integer> Parser<T> {

    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    /// Available when expression string does not contain variables
    pub fn eval(&self, src: &str) -> Result<T, ParserError>{
        self.eval_inner(src, &HashMap::new())
    }

    /// Used when the expression string contains variables
    /// The ctx argument stores information about the variable in array of tuples(&str, T).
    pub fn eval_context(&self, src: &str, ctx: &[(&str, T)]) -> Result<T, ParserError> {
        self.eval_inner(src, &HashMap::from_iter(ctx.iter().map(|(s, i)| (s.to_string(), *i))))
    }

    /// Internal method to convert the expression string to integer values
    pub(crate) fn eval_inner(&self, src: &str, ctx: &HashMap<String, T>) -> Result<T, ParserError> {
        let mut stack = Vec::new();
        for token in self.to_rpn(src)?.into_iter() {
            match token {
                Token::Value(val) => {stack.push(val);},
                Token::Variable(var) => {
                    if let Some(&val) = ctx.get(&var) {
                        stack.push(val);
                    }
                    else {
                        return Err(ParserError::new(format!("Undefined Variable: {}", var).as_str()));
                    }
                },
                Token::Unary(op) => {
                    if let Some(val) = stack.pop() {
                        stack.push(op.eval(val));
                    }
                    else {
                        return Err(ParserError::new("The expression is incorrect"));
                    }
                },
                Token::Binary(op) => {
                    if let (Some(val2), Some(val1)) = (stack.pop(), stack.pop()) {
                        stack.push(op.eval(val1, val2));
                    }
                    else {
                        return Err(ParserError::new("The expression is incorrect"));
                    }
                }
                Token::Function(func) => {
                    let ret = match func.n_arg() {
                        1 => {
                            if let Some(val) = stack.pop() {
                                func.eval1(val)
                            }
                            else {
                                return Err(ParserError::new("The expression is incorrect"));
                            }
                        },
                        2 => {
                            if let (Some(val2), Some(val1)) = (stack.pop(), stack.pop()) {
                                func.eval2(val1, val2)
                            }
                            else {
                                return Err(ParserError::new("The expression is incorrect"));
                            }
                        }
                        _ => unreachable!()
                    };
                    stack.push(ret);
                },
                _ => {
                    return Err(ParserError::new("The expression is incorrect"));
                }
            }
        }
        if stack.len() == 1 {
            Ok(stack.pop().unwrap())
        }
        else {
            Err(ParserError::new("Failed to evaluate"))
        }
    }
    
    /// From a string statement of the form “var = expr”, evaluate `expr` and assign it to `var`.
    /// Example of `src` argument : 
    /// "x = 2 + 6"
    /// "y = x * 2"
    /// ParserError if `expr` contains a variable with no information
    pub(crate) fn parse_statement(&self, stmt: &str, ctx: &HashMap<String, T>) -> Result<(String, T), ParserError> {
        let mut split = stmt.splitn(2, '=');
        if let (Some(var_str), Some(expr)) = (split.next(), split.next()) {
            let mut var_iter = Lexer::new(var_str)?.into_iter();
            
            let var = if let (Some(LexToken::Variable(s)), None) = (var_iter.next(), var_iter.next()) {
                s
            }
            else {
                return Err(ParserError::new("The syntax on the left side is incorrect"));
            };
            
            let val = self.eval_inner(expr, ctx)?;

            Ok((var, val))
        }
        else {
            Err(ParserError::new("The format of the statement must be \"var = expression\""))
        }
    }

    /// Convert LexToken to Token and then to Reverse Polish Notation
    /// Overflow due to the type of the number is treated as a ParserError
    /// If the number type is u8, strings parsed as greater than 256 will result in a ParserError
    /// (Based on the rules of the `from_str` function)
    fn to_rpn(&self, src: &str) -> Result<Vec<Token<T>>, ParserError> {
        // from lextokens to tokens
        let mut tokens = Vec::new();
        let mut paren_count = 0;
        let mut lt_iter = Lexer::new(src)?.into_iter().peekable();
        while let Some(lt) = lt_iter.next() {
            match lt {
                LexToken::LeftParen => {
                    tokens.push(Token::Symbol(Symbol::LeftParen));
                    paren_count += 1;
                },
                LexToken::RightParen => {
                    tokens.push(Token::Symbol(Symbol::RightParen));
                    paren_count -= 1;
                },
                LexToken::Comma => {
                    tokens.push(Token::Symbol(Symbol::Comma));
                },
                LexToken::Plus => {
                    tokens.push(Token::Binary(BinOp::Add));
                },
                LexToken::Minus => {
                    match tokens.last() {
                        Some(Token::Symbol(Symbol::RightParen)) | Some(Token::Value(_)) | Some(Token::Variable(_)) => {
                            tokens.push(Token::Binary(BinOp::Sub));
                        },
                        _ => {tokens.push(Token::Unary(UnaryOp::Neg));}   
                    }
                },
                LexToken::Star => {
                    tokens.push(Token::Binary(BinOp::Mul));
                },
                LexToken::Slash => {
                    tokens.push(Token::Binary(BinOp::Div));
                },
                LexToken::Percent => {
                    tokens.push(Token::Binary(BinOp::Mod));
                },
                LexToken::And => {
                    tokens.push(Token::Binary(BinOp::And));
                },
                LexToken::Or => {
                    tokens.push(Token::Binary(BinOp::Or));
                },
                LexToken::Xor => {
                    tokens.push(Token::Binary(BinOp::Xor));
                },
                LexToken::Not => {
                    tokens.push(Token::Unary(UnaryOp::Not));
                },
                LexToken::Less => {
                    tokens.push(Token::Binary(BinOp::Less));
                },
                LexToken::LessEqual => {
                    tokens.push(Token::Binary(BinOp::LessEqual));
                },
                LexToken::Greater => {
                    tokens.push(Token::Binary(BinOp::Greater));
                },
                LexToken::GreaterEqual => {
                    tokens.push(Token::Binary(BinOp::GreaterEqual));
                },
                LexToken::LeftShift => {
                    tokens.push(Token::Binary(BinOp::Shl));
                },
                LexToken::RightShift => {
                    tokens.push(Token::Binary(BinOp::Shr));
                },
                LexToken::Equal => {
                    tokens.push(Token::Equal);
                },
                LexToken::EqualEqual => {
                    tokens.push(Token::Binary(BinOp::EqualEqual));
                },
                LexToken::NotEqual => {
                    tokens.push(Token::Binary(BinOp::NotEqual));
                },
                LexToken::Variable(s) => {
                    if let Some(LexToken::LeftParen) = lt_iter.peek() {
                        tokens.push(Token::Function(s.parse::<Function>()?));
                    }
                    else {
                        tokens.push(Token::Variable(s));
                    }
                }
                LexToken::Number(s) => {
                    let n = match s.replace("_", "").parse::<T>() {
                        Ok(n) => {n},
                        Err(_) => {
                            return Err(ParserError::new(format!("Cannot parse string \"{}\" to {}", s, std::any::type_name::<T>()).as_str()));
                        }
                    };
                    tokens.push(Token::Value(n));
                }
            }
            if paren_count < 0 {
                return Err(ParserError::new("The number of parentheses does not match"));
            }
        }
        
        if paren_count != 0 {
            return Err(ParserError::new("The number of parentheses does not match"));
        }
        

        // From Infix notation to Reverse Polish Notation using Shunting yard Algorithm
        let mut rpn_stack: Vec<Token<T>> = Vec::new();
        let mut op_stack: Vec<Token<T>> = Vec::new();

        for token in tokens.into_iter() {
            match token {
                Token::Variable(_) | Token::Value(_) => {
                    rpn_stack.push(token);
                }
                _ => {
                    let (lp, _) = token.precedence();
                    while let Some(top_token) = op_stack.last() {
                        let (_, rp) = top_token.precedence();
                        if lp > rp {
                            break;
                        }
                        rpn_stack.push(op_stack.pop().unwrap());
                    }

                    if matches!(token, Token::Symbol(Symbol::RightParen)) {
                        match op_stack.last() {
                            Some(Token::Symbol(Symbol::LeftParen)) => {
                                op_stack.pop();
                                if let Some(Token::Function(_)) = op_stack.last() {
                                    rpn_stack.push(op_stack.pop().unwrap());
                                }
                                continue;
                            },
                            _ => {
                                return Err(ParserError::new("Unexpected parenthesis"));
                            }
                        }
                    }

                    if !matches!(token, Token::Symbol(Symbol::Comma)) {
                        op_stack.push(token);
                    }
                }
            }
        }

        while let Some(top_token) = op_stack.pop() {
            if matches!(top_token, Token::Symbol(Symbol::RightParen)) || matches!(top_token, Token::Symbol(Symbol::LeftParen)) {
                return Err(ParserError::new("Unexpected parenthesis"));
            }
            rpn_stack.push(top_token);
        }

        Ok(rpn_stack)
    }
    
}

#[derive(Debug)]
pub enum Token<T: Integer> {
    Equal,
    Symbol(Symbol),
    Unary(UnaryOp),
    Binary(BinOp),
    Value(T),
    Variable(String),
    Function(Function),
}

#[derive(Debug)]
pub enum Symbol {
    LeftParen,
    RightParen,
    Comma,
}

#[derive(Debug)]
pub enum UnaryOp {
    Neg,
    Not,
}

impl UnaryOp {
    fn eval<T: Integer>(&self, a: T) -> T {
        match self {
            Self::Neg => a.wrapping_neg(),
            Self::Not => !a,
        }
    }
}

#[derive(Debug)]
pub enum BinOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
    And,
    Or,
    Xor,
    Shl,
    Shr,
    EqualEqual,
    Less,
    LessEqual,
    Greater,
    GreaterEqual,
    NotEqual
}

impl BinOp {
    fn eval<T: Integer>(&self, a: T, b: T) -> T {
        match self {
            Self::Add => a.wrapping_add(&b),
            Self::Sub => a.wrapping_sub(&b),
            Self::Mul => a.wrapping_mul(&b),
            Self::Div => a / b,
            Self::Mod => a % b,
            Self::And => a & b,
            Self::Or => a | b,
            Self::Xor => a ^ b,
            Self::Shl => a.wrapping_shl(b.as_()),
            Self::Shr => a.wrapping_shr(b.as_()),
            Self::EqualEqual => T::from(a == b),
            Self::Less => T::from(a < b),
            Self::LessEqual => T::from(a <= b),
            Self::Greater => T::from(a > b),
            Self::GreaterEqual => T::from(a >= b),
            Self::NotEqual => T::from(a != b),
        }
    }
}

#[derive(Debug)]
pub enum Function {
    Pow,
    Max,
    Min,
    Abs,
    AbsDiff,
    CountOnes,
    CountZeros,
    LeadingZeros,
    TrailingZeros,
    RotateLeft,
    RotateRight
}

impl Function {
    fn n_arg(&self) -> u8 {
        match self {
            Self::Pow | Self::Max | Self::Min
            | Self::AbsDiff 
            | Self::RotateLeft | Self::RotateRight => {2},
            _ => {1}
        }
    }

    fn eval1<T: Integer>(&self, x1: T) -> T {
        match self {
            Self::CountOnes => x1.count_ones(),
            Self::CountZeros => x1.count_zeros(),
            Self::LeadingZeros => x1.leading_zeros(),
            Self::TrailingZeros => x1.trailing_zeros(),
            Self::Abs => x1.abs(),
            _ => unreachable!("The number of arguments should be 1")
        }
    }

    fn eval2<T: Integer>(&self, x1: T, x2: T) -> T {
        match self {
            Self::Pow => x1.wrapping_pow(x2.as_()),
            Self::Max => x1.max(x2),
            Self::Min => x1.min(x2),
            Self::AbsDiff => x1.abs_diff(x2),
            Self::RotateLeft => x1.rotate_left(x2.as_()),
            Self::RotateRight => x1.rotate_right(x2.as_()),
            _ => unreachable!("The number of arguments should be 2")
        }
    }
}

impl std::str::FromStr for Function {
    type Err = ParserError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "pow" => Ok(Self::Pow),
            "max" => Ok(Self::Max),
            "min" => Ok(Self::Min),
            "abs" => Ok(Self::Abs),
            "abs_diff" => Ok(Self::AbsDiff),
            "count_ones" => Ok(Self::CountOnes),
            "count_zeros" => Ok(Self::CountZeros),
            "leading_zeros" => Ok(Self::LeadingZeros),
            "trailing_zeros" => Ok(Self::TrailingZeros),
            "rotate_left" => Ok(Self::RotateLeft),
            "rotate_right" => Ok(Self::RotateRight),
            _ => Err(ParserError::new(format!("Cannot use function: {}", s).as_str()))
        }
    }
}

impl<T: Integer> Token<T> {
    fn precedence(&self) -> (u8, u8) {
        match self {
            Self::Binary(BinOp::Add) | Self::Binary(BinOp::Sub) => (50, 51),
            Self::Binary(BinOp::Mul) | Self::Binary(BinOp::Div) | Self::Binary(BinOp::Mod) => (55, 56),
            Self::Binary(BinOp::Shl) | Self::Binary(BinOp::Shr) => (48, 49),
            Self::Binary(BinOp::Less) | Self::Binary(BinOp::LessEqual)
            | Self::Binary(BinOp::Greater) | Self::Binary(BinOp::GreaterEqual) => (44, 45),
            Self::Binary(BinOp::EqualEqual) | Self::Binary(BinOp::NotEqual) => (42, 43),
            Self::Binary(BinOp::And) => (39, 40),
            Self::Binary(BinOp::Xor) => (37, 38),
            Self::Binary(BinOp::Or) => (35, 36),
            Self::Unary(UnaryOp::Neg) | Self::Unary(UnaryOp::Not) => (99, 80),
            Self::Function(_) => (97, 3),
            Self::Symbol(Symbol::LeftParen) => (99, 3),
            Self::Symbol(Symbol::RightParen) => (4, 100),
            Self::Equal => (2, 1),
            Self::Symbol(Symbol::Comma) => (5, 6),
            _ => (100, 100)
        }
    }
}

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

    #[test]
    fn parse_expr() {
        assert_eq!(
            Parser::default().eval("1 + 4").unwrap(),
            1 + 4
        );
        assert_eq!(
            Parser::<i32>::new().eval("5 * 7 * 36 % 13 / 3").unwrap(),
            5 * 7 * 36 % 13 / 3
        );
        assert_eq!(
            Parser::<i32>::new().eval("1 + 2 * 3 - 8 ^ 5 & 29 | 15 / 2 % 9 & 25").unwrap(),
            1 + 2 * 3 - 8 ^ 5 & 29 | 15 / 2 % 9 & 25
        );
        assert_eq!(
            Parser::<i32>::new().eval("1+2*3-8^5&29|15/2%9&25").unwrap(),
            1 + 2 * 3 - 8 ^ 5 & 29 | 15 / 2 % 9 & 25
        );
        assert_eq!(
            Parser::<i32>::new().eval("(1 == 1) + 5 * (3 <= 2)").unwrap(),
            1
        );
        assert_eq!(
            Parser::<i32>::new().eval("max(1, 2) + min(3, 5) + pow(2, 3)").unwrap(),
            1.max(2) + 3.min(5) + 2i32.pow(3)
        );

        assert_eq!(
            Parser::<u8>::new().eval("123 * 146 | 126").unwrap(),
            123u8.wrapping_mul(146) | 126
        );
        assert_eq!(
            Parser::<u64>::new().eval("-1245 - 214_456").unwrap(),
            1245u64.wrapping_neg() - 214456
        );
        assert_eq!(
            Parser::<u16>::new().eval("max(1, 2) + min(3, 5) + pow(2, 3)").unwrap(),
            1.max(2) + 3.min(5) + 2u16.pow(3)
        );

        assert!(
            matches!(
                Parser::<i8>::new().eval("256"),
                Err(ParserError{..})
            )
        );
        assert!(
            matches!(
                Parser::<i8>::new().eval("1+4 5*4"),
                Err(ParserError{..})
            )
        );
        assert!(
            matches!(
                Parser::<i8>::new().eval("(1+4 ) ) * 3"),
                Err(ParserError{..})
            )
        );     
        assert!(
            matches!(
                Parser::<i8>::new().eval("1++41"),
                Err(ParserError{..})
            )
        );                                   
    }

    #[test]
    fn parse_expr_with_context() {
        let ctx = [("x", 5), ("y", 2)];
        assert_eq!(
            Parser::<i32>::new().eval_context("1 + y * 3 - (8 ^ x) | 15 / 2 % 9 & 25", &ctx).unwrap(), 
            1 + 2 * 3 - (8 ^ 5) | 15 / 2 % 9 & 25
        );
        assert_eq!(
            Parser::<i32>::new().eval_context("(1 == 0) + 5 * (3 >= y)", &ctx).unwrap(),
            5
        );
        assert_eq!(
            Parser::<i32>::new().eval_context("max(1, y) + min(9, x) + pow(2, x)", &ctx).unwrap(),
            1.max(2) + 9.min(5) + 2i32.pow(5)
        );

        assert!(
            matches!(
                Parser::<i32>::new().eval_context("1+x y*4", &ctx),
                Err(ParserError{..})
            )
        );
    }
}