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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
//! This crate contains logic for an application to parse and evaluate
//! basic rational number arithmetic using standard operator precedence and
//! associativity.
#![no_std]
#![deny(
    unsafe_code,
    unused,
    warnings,
    clippy::all,
    clippy::cargo,
    clippy::nursery,
    clippy::pedantic
)]
#![allow(
    clippy::arithmetic_side_effects,
    clippy::exhaustive_enums,
    clippy::implicit_return,
    clippy::integer_arithmetic,
    clippy::into_iter_on_ref,
    clippy::question_mark,
    clippy::single_char_lifetime_names,
    clippy::unseparated_literal_suffix
)]
extern crate alloc;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use cache::Cache;
use core::convert;
use core::fmt::{self, Display, Formatter};
use num_bigint::{BigInt, BigUint, Sign};
use num_rational::Ratio;
use num_integer::Integer;
use num_traits::Pow;
use E::{
    DivByZero, ExpDivByZero, ExpIsNotInt, InvalidAbs, InvalidDec, InvalidPar, InvalidQuit,
    InvalidRecall, MissingLF, MissingTerm, NotEnoughPrevResults, NotNonNegIntFact, TrailingSyms,
};
use O::{Empty, Exit, Prev, Val};
/// Fixed-sized cache that automatically overwrites the oldest data
/// when a new item is added and the cache is full. One can think of
/// [`Cache`] as a very limited but more performant [`VecDeque`][alloc::collections::VecDeque] that only
/// adds new data or reads old data.
pub mod cache;
/// A value from a previous expression that is to be
/// displayed potentially in decimal form.
#[derive(Debug)]
pub struct PrevVal<'a> {
    /// A cached value from a previous expression.
    val: &'a Ratio<BigInt>,
    /// None iff the previous value is to be displayed
    /// in fractional form; otherwise contains the number
    /// of fractional digits from 0 to 9 `val` will be rounded
    /// to.
    round: Option<u8>,
}
impl<'a> Display for PrevVal<'a> {
    #[inline]
    #[allow(unsafe_code, clippy::as_conversions, clippy::indexing_slicing)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(rem) = self.round {
            let r = rem as usize;
            let mult = BigInt::from_biguint(Sign::Plus, BigUint::new(vec![10])).pow(r);
            // int < 0 iff val <= -1. frac < 0 iff val is a negative non-integer.
            let (int, frac) = (self.val * &mult).round().numer().div_rem(&mult);
            let int_str = int.to_string().into_bytes();
            let frac_str = frac.to_string().into_bytes();
            // The max length is the length of int_str plus the number of fractional digits
            // plus a possible negative sign plus a possible decimal point.
            // At most we will over-allocate by 2 precisely when int is negative
            // and r is 0.
            let mut v = Vec::with_capacity(int_str.len() + 2 + r);
            // In the case val is a negative value greater than -1,
            // we have to add the negative sign since 0 is unsigned.
            // Technically this can panic! if one of the Strings returned from
            // to_string is empty; however that is a bug, or at least undesired
            // behavior, in the to_string implementation from BigInt.
            let frac_val = match (int_str[0] == b'-', frac_str[0] == b'-') {
                (false, true) => {
                    v.push(b'-');
                    &frac_str[1..]
                }
                (true, true) => &frac_str[1..],
                _ => frac_str.as_slice(),
            };
            v.extend_from_slice(int_str.as_slice());
            if r > 0 {
                v.push(b'.');
                let len = v.len();
                // frac_val.len() is <= r.
                while v.len() < len + r - frac_val.len() {
                    v.push(b'0');
                }
                v.extend_from_slice(frac_val);
            }
            // SAFETY:
            // v contains precisely the UTF-8 code units returned from Strings
            // returned from the to_string function on the integer and fraction part of
            // val plus optionally the single byte encodings of ".", "-", and "0".
             writeln!(f, "> {}", unsafe { String::from_utf8_unchecked(v) })
        } else {
            writeln!(f, "> {}", self.val)
        }
    }
}
/// The error that is returned when attempting
/// to evaluate the input.
#[derive(Clone, Copy, Debug)]
pub enum E {
    /// The input was not terminated with a line feed.
    MissingLF,
    /// The input began with a `q` but had other non-spaces
    /// that followed it.
    InvalidQuit,
    /// The input began with an `=` but
    /// was otherwise syntactically invalid.
    InvalidRecall,
    /// A sub-expression in the input would have led
    /// to a division by zero.
    DivByZero(usize),
    /// A sub-expression in the input would have led
    /// to a rational number that was not 0 or 1 to be
    /// raised to a non-integer power.
    ExpIsNotInt(usize),
    /// A sub-expression in the input would have led
    /// to 0 being raised to a negative power which itself
    /// would have led to a division by zero.
    ExpDivByZero(usize),
    /// A sub-expression in the input would have led
    /// to a non-integer factorial or a negative integer factorial.
    NotNonNegIntFact(usize),
    /// The input contained a non-empty sequence of digits followed
    /// by `.` which was not followed by a non-empty sequence of digits.
    InvalidDec(usize),
    /// A recall expression was used to recall the *i*-th previous result,
    /// but there are fewer than *i* previous results where
    /// *i ∈ {1, 2, 3, 4, 5, 6, 7, 8}*.
    NotEnoughPrevResults(u8),
    /// The input did not contain a closing `|`.
    InvalidAbs(usize),
    /// The input did not contain a closing `)`.
    InvalidPar(usize),
    /// A sub-expression in the input had a missing terminal expression
    /// where a terminal expression is a decimal literal expression,
    /// recall expression, absolute value expression, or a parenthetical
    /// expression.
    MissingTerm(usize),
    /// The input started with a valid expression but was immediately followed
    /// by symbols that could not be chained with the preceding expression.
    TrailingSyms(usize),
}
impl Display for E {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            MissingLF => f.write_str("The last character is not a line feed.\n"),
            InvalidRecall => f.write_str("Invalid recall statement. A recall statement must be of the extended regex form: ^ *= *[0-9]?@[1-8]? *$.\n"),
            InvalidQuit => f.write_str("Invalid quit statement. A quit statement must be of the extended regex form: ^ *q *$.\n"),
            DivByZero(u) => writeln!(f, "Division by zero ending at position {u}."),
            ExpIsNotInt(u) => writeln!(f, "Non-integer exponent with a base that was not 0 or 1 ending at position {u}."),
            ExpDivByZero(u) => writeln!(f, "Non-negative exponent with a base of 0 ending at position {u}."),
            NotNonNegIntFact(u) => writeln!(f, "Factorial of a rational number that was not a non-negative integer ending at position {u}."),
            InvalidDec(u) => writeln!(f, "Invalid decimal literal expression ending at position {u}. A decimal literal expression must be of the extended regex form:  *[0-9]+(\\.[0-9]+)?."),
            NotEnoughPrevResults(len) => writeln!(f, "There are only {len} previous results."),
            InvalidAbs(u) => writeln!(f, "Invalid absolute value expression ending at position {u}. An absolute value expression is an addition expression enclosed in '||'."),
            InvalidPar(u) => writeln!(f, "Invalid parenthetical expression ending at position {u}. A parenthetical expression is an addition expression enclosed in '()'."),
            MissingTerm(u) => writeln!(f, "Missing terminal expression at position {u}. A terminal expression is a decimal literal expression, recall expression, or addition expression enclosed in vertical bars or parentheses."),
            TrailingSyms(u) => writeln!(f, "Trailing symbols starting at position {u}."),
        }
    }
}
/// A successful evaluation of an input.
#[derive(Debug)]
pub enum O<'a> {
    /// The result of a passed expression.
    Val(&'a Ratio<BigInt>),
    /// The previous result of the requested
    /// recall statement.
    Prev(PrevVal<'a>),
    /// The input contained all spaces which is part of our language
    /// but does not do anything.
    Empty,
    /// A quit statement was issued to terminate the program.
    Exit,
}
impl<'a> Display for O<'a> {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            Val(r) => writeln!(f, "> {r}"),
            Prev(ref p) => p.fmt(f),
            Empty | Exit => Ok(()),
        }
    }
}
/// Evaluates the supplied input.
pub struct Evaluator<'input, 'cache, 'exp> {
    /// The input to be evaluated.
    utf8: &'input [u8],
    /// The index within `utf8` that evaluation needs to continue.
    /// We use this instead of slicing from `utf8` since we want
    /// to be able to report the position within the input
    /// that an error occurs.
    i: usize,
    /// The cache of previously evaluated expressions.
    prev: &'cache mut Cache<Ratio<BigInt>, 8>,
    /// Buffer used to evaluate exponentiation sub-expressions.
    /// We require buffering exponentiation sub-expressions due
    /// to their being right-associative. Instead of repeatedly
    /// creating temporary internal buffers, we rely on a supplied
    /// buffer that can be re-used across multiple evaluations.
    exp_cache: &'exp mut Vec<Ratio<BigInt>>,
}
impl<'input, 'cache, 'exp> Evaluator<'input, 'cache, 'exp> {
    /// Creates an `Evaluator<'input, 'cache, 'exp>` based on the supplied arguments.
    #[inline]
    pub fn new(
        utf8: &'input [u8],
        prev: &'cache mut Cache<Ratio<BigInt>, 8>,
        exp_cache: &'exp mut Vec<Ratio<BigInt>>,
    ) -> Evaluator<'input, 'cache, 'exp> {
        Self {
            utf8,
            i: 0,
            prev,
            exp_cache,
        }
    }
    /// Evaluates the input consuming the `Evaluator<'input, 'cache, 'exp>`.

    /// # Errors
    ///
    /// Returns an [`E`] iff the input violates the calc language.
    #[inline]
    #[allow(clippy::indexing_slicing)]
    pub fn evaluate(mut self) -> Result<O<'cache>, E> {
        self.utf8 = if self.utf8.last().map_or(true, |b| *b != b'\n') {
            return Err(MissingLF)
        } else {
                &self.utf8[..(self.utf8.len() - 1)]
        };
        self.consume_ws();
        let Some(b) = self.utf8.get(self.i) else {
            return Ok(Empty)
        };
        if *b == b'=' {
            self.i += 1;
            self.consume_ws();
            let Some(b2) = self.utf8.get(self.i) else {
                return Err(InvalidRecall)
            };
            let b3 = *b2;
            let round = b3.is_ascii_digit().then(|| {
                self.i += 1;
                b3 - b'0'
            });
            self.get_recall().map(|val| Prev(PrevVal { val, round }))
        } else if *b == b'q' {
            self.i += 1;
            self.consume_ws();
            if self.i == self.utf8.len() {
                Ok(Exit)
            } else {
                Err(InvalidQuit)
            }
        } else {
            self.get_adds().and_then(move |r| {
                self.consume_ws();
                if self.i == self.utf8.len() {
                    Ok(Val(self.prev.push(r)))
                } else {
                    Err(TrailingSyms(self.i))
                }
            })
        }
    }
    /// Reads from the input until the next non-space byte value.
    #[inline]
    #[allow(clippy::indexing_slicing)]
    fn consume_ws(&mut self) {
        // ControlFlow makes more sense to use in try_fold; however due to a lack
        // of a map_or_else function, it is easier to simply return a Result with
        // Err taking the role of ControlFlow::Break.
        self.i += self.utf8[self.i..]
            .into_iter()
            .try_fold(0, |val, b| {
                if *b == b' ' {
                    Ok(val + 1)
                } else {
                    // Only an "error" in the sense
                    // we want try_fold to terminate.
                    Err(val)
                }
            })
            .map_or_else(convert::identity, convert::identity);
    }
    /// Evaluates addition expressions as defined in the calc language.
    /// This function is used for both addition and subtraction operations which
    /// themselves are based on multiplication expressions.
    #[inline]
    fn get_adds(&mut self) -> Result<Ratio<BigInt>, E> {
        let mut left = self.get_mults()?;
        let mut j;
        self.consume_ws();
        while let Some(i) = self.utf8.get(self.i) {
            j = *i;
            self.consume_ws();
            if j == b'+' {
                self.i += 1;
                self.consume_ws();
                left += self.get_mults()?;
            } else if j == b'-' {
                self.i += 1;
                self.consume_ws();
                left -= self.get_mults()?;
            } else {
                break;
            }
        }
        Ok(left)
    }
    /// Evaluates multiplication expressions as defined in the calc language.
    /// This function is used for both multiplication and division operations which
    /// themselves are based on negation expressions.
    #[inline]
    fn get_mults(&mut self) -> Result<Ratio<BigInt>, E> {
        let mut left = self.get_neg()?;
        let mut right;
        let mut j;
        self.consume_ws();
        while let Some(i) = self.utf8.get(self.i) {
            j = *i;
            self.consume_ws();
            if j == b'*' {
                self.i += 1;
                self.consume_ws();
                left *= self.get_neg()?;
            } else if j == b'/' {
                self.i += 1;
                self.consume_ws();
                right = self.get_neg()?;
                if right.numer().sign() == Sign::NoSign {
                    return Err(DivByZero(self.i));
                }
                left /= right;
            } else {
                break;
            }
        }
        Ok(left)
    }
    /// Evaluates negation expressions as defined in the calc language.
    /// This function is based on exponentiation expressions.
    #[inline]
    fn get_neg(&mut self) -> Result<Ratio<BigInt>, E> {
        let mut count = 0usize;
        while let Some(b) = self.utf8.get(self.i) {
            if *b == b'-' {
                self.i += 1;
                self.consume_ws();
                count += 1;
            } else {
                break;
            }
        }
        self.get_exps()
            .map(|val| if count & 1 == 0 { val } else { -val })
    }
    /// Evaluates exponentiation expressions as defined in the calc language.
    /// This function is based on negation expressions.
    #[inline]
    #[allow(clippy::unwrap_used, clippy::unwrap_in_result)]
    fn get_exps(&mut self) -> Result<Ratio<BigInt>, E> {
        let mut t = self.get_fact()?;
        let ix = self.exp_cache.len();
        let mut prev;
        let mut numer;
        self.exp_cache.push(t);
        self.consume_ws();
        let mut j;
        while let Some(i) = self.utf8.get(self.i) {
            j = *i;
            self.consume_ws();
            if j == b'^' {
                self.i += 1;
                self.consume_ws();
                t = self.get_neg()?;
                // This is safe since we always push a value into it
                // before entering the loop.
                prev = self.exp_cache.last().unwrap();
                numer = prev.numer();
                // Equiv to checking if prev is 0.
                if numer.sign() == Sign::NoSign {
                    if t.numer().sign() == Sign::Minus {
                        return Err(ExpDivByZero(self.i));
                    }
                    self.exp_cache.push(t);
                } else if prev.is_integer() {
                    if numer == &BigInt::new(Sign::Plus, vec![1]) {
                        // 1 raised to anything is 1, so we don't bother
                        // storing the exponent.
                    } else if t.is_integer() {
                        self.exp_cache.push(t);
                    } else {
                        return Err(ExpIsNotInt(self.i));
                    }
                } else if t.is_integer() {
                    self.exp_cache.push(t);
                } else {
                    return Err(ExpIsNotInt(self.i));
                }
            } else {
                break;
            }
        }
        self.exp_cache.drain(ix..).try_rfold(
            Ratio::from_integer(BigInt::new(Sign::Plus, vec![1])),
            |exp, base| {
                if exp.is_integer() {
                    Ok(base.pow(exp.numer()))
                } else if base.numer().sign() == Sign::NoSign {
                    Ok(base)
                } else {
                    Err(ExpIsNotInt(self.i))
                }
            },
        )
    }
    /// Evaluates factorial expressions as defined in the calc language.
    /// This function is based on terminal expressions.
    #[inline]
    fn get_fact(&mut self) -> Result<Ratio<BigInt>, E> {
        /// Calculates the factorial of `val`.
        #[inline]
        fn fact(mut val: BigUint) -> BigUint {
            let zero = BigUint::new(Vec::new());
            let one = BigUint::new(vec![1]);
            let mut calc = BigUint::new(vec![1]);
            while val > zero {
                calc *= &val;
                val -= &one;
            }
            calc
        }
        let t = self.get_term()?;
        let Some(b) = self.utf8.get(self.i) else {
            return Ok(t)
        };
        if *b == b'!' {
            self.i += 1;
            if t.is_integer() {
                // We can make a copy of self.i here, or call map_or instead
                // of map_or_else.
                let i = self.i;
                t.numer()
                    .to_biguint()
                    .map_or_else(|| Err(NotNonNegIntFact(i)), |val| {
                        let mut factorial = fact(val);
                        while let Some(b2) = self.utf8.get(self.i) {
                            if *b2 == b'!' {
                                self.i += 1;
                                factorial = fact(factorial);
                            } else {
                                break;
                            }
                        }
                        Ok(Ratio::from_integer(BigInt::from_biguint(
                            Sign::Plus,
                            factorial,
                        )))
                    })
            } else {
                Err(NotNonNegIntFact(self.i))
            }
        } else {
            Ok(t)
        }
    }
    /// Evaluates terminal expressions as defined in the calc language.
    /// This function is based on parenthetical expressions, absolute value
    /// expressions, recall expressions, and numer literal expressions.
    #[inline]
    #[allow(clippy::option_if_let_else)]
    fn get_term(&mut self) -> Result<Ratio<BigInt>, E> {
        match self.get_rational() {
            Ok(o) => match o {
                Some(r) => Ok(r),
                None => match self.get_prev() {
                    Ok(o2) => match o2 {
                        Some(r2) => Ok(r2.clone()),
                        None => self
                            .get_abs()
                            .and_then(|o3| o3.map_or_else(|| self.get_par(), Ok)),
                    },
                    Err(e2) => Err(e2),
                },
            },
            Err(e) => Err(e),
        }
    }
    /// Evaluates parenthetical expressions as defined in the calc language.
    /// This function is based on addition expressions.
    /// This is the last terminal expression attempted when needing
    /// a terminal expression; as a result, it is the only terminal expression
    /// that does not return an `Option`.
    #[inline]
    fn get_par(&mut self) -> Result<Ratio<BigInt>, E> {
        // This is the last kind of terminal expression that is attempted.
        // If there is no more data, then we have a missing terminal expression.
        let Some(b) = self.utf8.get(self.i) else {
            return Err(MissingTerm(self.i));
        };
        // This is always the last terminal expression we call; so if there is a missing open parenthesis,
        // then the sentence is grammatically incorrect.
        if *b == b'(' {
            self.i += 1;
            self.consume_ws();
            let r = self.get_adds()?;
            self.consume_ws();
            let Some(b2) = self.utf8.get(self.i) else {
                return Err(InvalidPar(self.i))
            };
            let b3 = *b2;
            if b3 == b')' {
                self.i += 1;
                Ok(r)
            } else {
                Err(InvalidPar(self.i))
            }
        } else {
            Err(MissingTerm(self.i))
        }
    }
    /// Evaluates absolute value expressions as defined in the calc language.
    /// This function is based on addition expressions.
    #[inline]
    fn get_abs(&mut self) -> Result<Option<Ratio<BigInt>>, E> {
        let Some(b) = self.utf8.get(self.i) else {
            return Ok(None)
        };
        if *b == b'|' {
            self.i += 1;
            self.consume_ws();
            let r = self.get_adds()?;
            self.consume_ws();
            let Some(b2) = self.utf8.get(self.i) else {
                return Err(InvalidAbs(self.i))
            };
            let b3 = *b2;
            if b3 == b'|' {
                self.i += 1;
                Ok(Some(if r.numer().sign() == Sign::Minus {
                    -r
                } else {
                    r
                }))
            } else {
                Err(InvalidAbs(self.i))
            }
        } else {
            Ok(None)
        }
    }
    /// Obtains the index within `self.prev` that a recall
    /// expression is to return. This is a separate function
    /// from [`get_prev`] due to [`get_recall`] also using this
    /// logic.
    #[inline]
    #[allow(clippy::as_conversions)]
    fn get_prev_idx(&mut self) -> Option<usize> {
        let Some(b) = self.utf8.get(self.i) else {
          return None  
        };
        (*b == b'@').then(|| {
            self.i += 1;
            self.utf8.get(self.i).map_or(0, |b2| {
                if (b'1'..b'9').contains(b2) {
                    self.i += 1;
                    (*b2 - b'1') as usize
                } else {
                    0
                }
            })
        })
    }
    /// Evaluates recall expressions as defined in the calc language.
    #[inline]
    #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
    fn get_prev(&mut self) -> Result<Option<&Ratio<BigInt>>, E> {
        self.get_prev_idx().map_or_else(|| Ok(None), |i| {
            self.prev
                .get(i)
                .map_or_else(|| Err(NotEnoughPrevResults(self.prev.len() as u8)), |r| Ok(Some(r)))
        })
    }
    /// Evaluates recall statements as defined in the calc language.
    /// This function has to be separate from [`get_prev`] since it returns
    /// a shared reference with 'cache lifetime which requires the function to move self
    /// since it has an exclusive reference to the cache.
    #[inline]
    #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
    fn get_recall(mut self) -> Result<&'cache Ratio<BigInt>, E> {
        self.get_prev_idx().map_or_else(|| Err(InvalidRecall), move |i| {
            self.consume_ws();
            self.prev.get(i).map_or_else(|| Err(NotEnoughPrevResults(self.prev.len() as u8)), |val| {
                if self.i == self.utf8.len() {
                    Ok(val)
                } else {
                    Err(InvalidRecall)
                }
            })
        })
    }
    /// Evaluates number literal expressions as defined in the calc language.
    #[inline]
    #[allow(
        clippy::as_conversions,
        clippy::cast_lossless,
        clippy::indexing_slicing
    )]
    fn get_rational(&mut self) -> Result<Option<Ratio<BigInt>>, E> {
        #[inline]
        // ControlFlow makes more sense to use in try_fold; however due to a lack
        // of a map_or_else function, it is easier to simply return a Result with
        // Err taking the role of ControlFlow::Break.
        /// Used to parse a sequence of digits into an unsigned integer.
        fn to_biguint(v: &[u8]) -> (BigUint, usize) {
            v.into_iter()
                .try_fold((BigUint::new(Vec::new()), 0), |mut prev, d| {
                    if d.is_ascii_digit() {
                        prev.1 += 1;
                        prev.0 = prev.0 * 10u8 + (*d - b'0');
                        Ok(prev)
                    } else {
                        Err(prev)
                    }
                })
                .map_or_else(convert::identity, convert::identity)
        }
        let (int, len) = to_biguint(&self.utf8[self.i..]);
        if len == 0 {
            return Ok(None);
        }
        self.i += len;
        if let Some(b) = self.utf8.get(self.i) {
            if *b == b'.' {
                self.i += 1;
                let (numer, len2) = to_biguint(&self.utf8[self.i..]);
                if len2 == 0 {
                    Err(InvalidDec(self.i))
                } else {
                    self.i += len2;
                    Ok(Some(
                        Ratio::from_integer(BigInt::from_biguint(Sign::Plus, int))
                            + Ratio::new(
                                BigInt::from_biguint(Sign::Plus, numer),
                                BigInt::from_biguint(Sign::Plus, BigUint::new(vec![10]).pow(len2)),
                            ),
                    ))
                }
            } else {
                Ok(Some(Ratio::from_integer(BigInt::from_biguint(
                    Sign::Plus,
                    int,
                ))))
            }
        } else {
            Ok(Some(Ratio::from_integer(BigInt::from_biguint(
                Sign::Plus,
                int,
            ))))
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn prev_string() {
        assert!(PrevVal { val: &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![12]))), round: None }.to_string() == "> 12\n");
        assert!(PrevVal { val: &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![12]))), round: Some(0) }.to_string() == "> 12\n");
        assert!(PrevVal { val: &Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![12]))), round: Some(1) }.to_string() == "> -12.0\n");
        assert!(PrevVal { val: &Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))), round: None }.to_string() == "> 2/3\n");
        assert!(PrevVal { val: &Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))), round: Some(0)}.to_string() == "> -1\n");
        assert!(PrevVal { val: &Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))), round: Some(1)}.to_string() == "> -0.7\n");
        assert!(PrevVal { val: &Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![4])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![6]))), round: Some(2)}.to_string() == "> -0.67\n");
    }
    #[test]
    fn number_literal() {
        // Normal 0 is fine.
        assert!(Evaluator::new(b"0", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        // Leading 0s and trailing 0s are fine.
        assert!(Evaluator::new(b"0000.00000", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        // Parsing stops at first non-(digit/period).
        assert!(Evaluator::new(b"1 0", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        let int = b"3397450981271938475135134759823759835414";
        let frac = b"913759810573549872354897210539127530981570";
        let left = Ratio::from_integer(BigInt::parse_bytes(int, 10).unwrap());
        let right = Ratio::new(BigInt::parse_bytes(frac, 10).unwrap(), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![10]).pow(frac.len() + 1)));
        let mut vec = Vec::new();
        vec.extend_from_slice(int);
        vec.push(b'.');
        vec.push(b'0');
        vec.extend_from_slice(frac);
        // Test a number whose integer and fraction portions are larger than u128::MAX.
        assert!(Evaluator::new(vec.as_slice(), &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == left + right);
        // Leading 0s and trailing 0s for a non-zero value are fine.
        assert!(Evaluator::new(b"000000014.0000000000000", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![14]))));
        // A sequence of digits followed immediately by a decimal point but no digits after is invalid.
        assert!(match Evaluator::new(b"1.", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap_err() {
            E::InvalidDec(i) => i == 2,
            _ => false,
        });
        // A sequence of digits followed immediately by a decimal point and space but no digits after is invalid.
        // This just shows that spaces are not ignored in number literals.
        assert!(match Evaluator::new(b"1. 2", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap_err() {
            E::InvalidDec(i) => i == 2,
            _ => false,
        });
        // A non-space starting the input is valid but produces no value.
        // This also shows that an invalid byte sequence does not produce an error here.
        assert!(Evaluator::new(b"a1", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().is_none());
        // A space starting the input is valid but produces no value.
        assert!(Evaluator::new(b" 1", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().is_none());
        // Negative literals don't exist, so this should succeed but produce nothing.
        assert!(Evaluator::new(b"-1", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().is_none());
        // '/' is division and not part of a number literal so only the "numerator" is parsed.
        assert!(Evaluator::new(b"1/2", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // A sequence of digits followed by invalid bytes is valid and produces a number equal to the digits before the invalid bytes.
        assert!(Evaluator::new(b"130alj", &mut Cache::new(), &mut Vec::new()).get_rational().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![130]))));
    }
    #[test]
    fn recall_expression() {
        // If the input does not start with '@', then it's valid but produces nothing.
        assert!(Evaluator::new(b"1", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap().is_none());
        assert!(Evaluator::new(b"a", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap().is_none());
        assert!(Evaluator::new(b" @", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap().is_none());
        // Invalid recall expression since there are no previous results.
        assert!(match Evaluator::new(b"@", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap_err() {
            E::NotEnoughPrevResults(count) => count == 0,
            _ => false,
        });
        // Invalid recall expression since there are no previous results.
        assert!(match Evaluator::new(b"@4", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap_err() {
            E::NotEnoughPrevResults(count) => count == 0,
            _ => false,
        });
        // Invalid recall expression since there are no previous results.
        // The input violates our grammar, but this error happens before that.
        assert!(match Evaluator::new(b"@0", &mut Cache::new(), &mut Vec::new()).get_prev().unwrap_err() {
            E::NotEnoughPrevResults(count) => count == 0,
            _ => false,
        });
        // Successfully extract previous expression.
        let mut cache = Cache::new();
        Evaluator::new(b"1\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        assert!(Evaluator::new(b"@", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // Invalid characters are ignored at this stage.
        assert!(Evaluator::new(b"@&", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // 0 is not a valid previous value only 1-8 are.
        assert!(Evaluator::new(b"@0", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // 9 is not a valid previous value only 1-8 are.
        assert!(Evaluator::new(b"@9", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // Spaces are not cleaned; otherwise this would error since we only have 1 previous value.
        assert!(Evaluator::new(b"@ 2", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // One digits are looked at so this is not @<ten>.
        assert!(Evaluator::new(b"@10", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // Invalid recall expression since there are no previous results.
        assert!(match Evaluator::new(b"@2", &mut cache, &mut Vec::new()).get_prev().unwrap_err() {
            E::NotEnoughPrevResults(count) => count == 1,
            _ => false,
        });
        Evaluator::new(b"2\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        // Previous value is correct.
        assert!(Evaluator::new(b"@", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))));
        // Second previous value is correct.
        assert!(Evaluator::new(b"@2", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // Invalid recall expression since there are no previous results.
        assert!(match Evaluator::new(b"@3", &mut cache, &mut Vec::new()).get_prev().unwrap_err() {
            E::NotEnoughPrevResults(count) => count == 2,
            _ => false,
        });
        let mut v = vec![0, b'\n'];
        for i in b'3'..=b'8' {
            v[0] = i;
            Evaluator::new(v.as_slice(), &mut cache, &mut Vec::new()).evaluate().unwrap();
        }
        v[0] = b'@';
        for i in b'1'..=b'8' {
            v[1] = i;
            // Cache is filled up correctly storing all previous values.
            assert!(Evaluator::new(v.as_slice(), &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![(b'9' - i) as u32]))));
        }
        // Only parses the first @ since the second @ is not a digit between 1 and 8.
        assert!(Evaluator::new(b"@@", &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![8]))));
        Evaluator::new(b"9\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        // Oldest value is overwritten; all others remain.
        for i in b'1'..=b'8' {
            v[1] = i;
            assert!(Evaluator::new(v.as_slice(), &mut cache, &mut Vec::new()).get_prev().unwrap().unwrap() == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![((b'9' + 1) - i) as u32]))));
        }
    }
    #[test]
    fn abs() {
        // Missing closing '|'
        assert!(match Evaluator::new(b"|1", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap_err() {
            E::InvalidAbs(i) => i == 2,
            _ => false,
        });
        assert!(match Evaluator::new(b"||1 + 2|", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap_err() {
            E::InvalidAbs(i) => i == 8,
            _ => false,
        });
        assert!(Evaluator::new(b"|  0  |", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        assert!(Evaluator::new(b"| -  5 |", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![5]))));
        assert!(Evaluator::new(b"| | 2 -  5| * 9 |", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![27]))));
        // If the input does not start with '|', then it's valid but produces nothing.
        assert!(Evaluator::new(b" |9|", &mut Cache::new(), &mut Vec::new()).get_abs().unwrap().is_none());
    }
    #[test]
    fn par() {
        // Missing closing ')'
        assert!(match Evaluator::new(b"(1", &mut Cache::new(), &mut Vec::new()).get_par().unwrap_err() {
            E::InvalidPar(i) => i == 2,
            _ => false,
        });
        assert!(match Evaluator::new(b"((1 + 2)", &mut Cache::new(), &mut Vec::new()).get_par().unwrap_err() {
            E::InvalidPar(i) => i == 8,
            _ => false,
        });
        assert!(Evaluator::new(b"(  0  )", &mut Cache::new(), &mut Vec::new()).get_par().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        assert!(Evaluator::new(b"( -  5 )", &mut Cache::new(), &mut Vec::new()).get_par().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![5]))));
        assert!(Evaluator::new(b"( ( 2 -  5) * 9 )", &mut Cache::new(), &mut Vec::new()).get_par().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![27]))));
        // If the input does not start with '(', then it's invalid since get_par must only be called as the last terminal expression which means whitespace must be consumed already.
        assert!(match Evaluator::new(b" (2)", &mut Cache::new(), &mut Vec::new()).get_par().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
    }
    #[test]
    fn term() {
        assert!(Evaluator::new(b"0000.00000", &mut Cache::new(), &mut Vec::new()).get_term().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        assert!(Evaluator::new(b"|4|", &mut Cache::new(), &mut Vec::new()).get_term().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        assert!(Evaluator::new(b"(4)", &mut Cache::new(), &mut Vec::new()).get_term().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        // Terminal expressions do no clean up before or after.
        assert!(match Evaluator::new(b" 2", &mut Cache::new(), &mut Vec::new()).get_term().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        let mut cache = Cache::new();
        Evaluator::new(b"1\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        assert!(Evaluator::new(b"@", &mut cache, &mut Vec::new()).get_term().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
    }
    #[test]
    fn factorial() {
        // Negative integer is not allowed.
        assert!(match Evaluator::new(b"(-1)!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap_err() {
            E::NotNonNegIntFact(i) => i == 5,
            _ => false,
        });
        // Non-integer is not allowed.
        assert!(match Evaluator::new(b"2.5!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap_err() {
            E::NotNonNegIntFact(i) => i == 4,
            _ => false,
        });
        // factorials always become terminal expressions eventually.
        assert!(Evaluator::new(b"7", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![7]))));
        assert!(Evaluator::new(b"(7)", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![7]))));
        assert!(Evaluator::new(b"|7|", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![7]))));
        let mut cache = Cache::new();
        Evaluator::new(b"3\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        assert!(Evaluator::new(b"@!", &mut cache, &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"@", &mut cache, &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))));
        // 0! = 1.
        assert!(Evaluator::new(b"0.0!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // 1! = 1.
        assert!(Evaluator::new(b"1!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // 4! = 24, and spaces are not consumed.
        assert!(Evaluator::new(b"4! ", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![24]))));
        // Factorials can be chained.
        assert!(Evaluator::new(b"3!! ", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![720]))));
        // only factorial is consumed.
        assert!(Evaluator::new(b"2!+3", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))));
        // Error since leading/trailing whitespace is not consumed by factorial or higher precedence expressions.
        assert!(match Evaluator::new(b" 2!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        // Error since leading/trailing whitespace is not consumed by factorial or higher precedence expressions.
        assert!(match Evaluator::new(b"-2!", &mut Cache::new(), &mut Vec::new()).get_fact().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
    }
    #[test]
    fn exp() {
        // 1 can be raised to anything and return 1.
        // Also white space is ignored between operator.
        assert!(Evaluator::new(b"1  ^  0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"1^0.5", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"1^(-1/2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"1.0^(-2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        // 0 can be raised to any non-negative value and will always return 0 unless raised to 0 which will return 1.
        assert!(Evaluator::new(b"0^0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"0^1", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(vec![0]))));
        assert!(Evaluator::new(b"0^0.5", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(vec![0]))));
        // Anything else can only be raised to integers.
        assert!(Evaluator::new(b"4^0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"4^1", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        assert!(Evaluator::new(b"4^(-2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![16]))));
        assert!(Evaluator::new(b"(-4)^0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"(-4)^1", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![4]))));
        assert!(Evaluator::new(b"(-4)^2", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![16]))));
        assert!(Evaluator::new(b"(-4)^(-2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![16]))));
        assert!(Evaluator::new(b"(-4)^(-3)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![1])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![64]))));
        assert!(Evaluator::new(b"(2/3)^0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"(2/3)^(2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![9]))));
        assert!(Evaluator::new(b"(2/3)^(-3)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![27])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![8]))));
        assert!(Evaluator::new(b"(-2/3)^0", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"(-2/3)^(2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![9]))));
        assert!(Evaluator::new(b"(-2/3)^(3)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![8])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![27]))));
        assert!(Evaluator::new(b"(-2/3)^(-2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![9])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        assert!(Evaluator::new(b"(-2/3)^(-3)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![27])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![8]))));
        // Error since 0 cannot be raised to a negative power.
        assert!(match Evaluator::new(b"0^(-1)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap_err() {
            E::ExpDivByZero(i) => i == 6,
            _ => false,
        });
        // Error anything other than 0 or 1 cannot be raised to a non-integer power.
        assert!(match Evaluator::new(b"2^(1/2)", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap_err() {
            E::ExpIsNotInt(i) => i == 7,
            _ => false,
        });
        // exps always become factorials eventually.
        assert!(Evaluator::new(b"3!", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![6]))));
        // exponentiation has lower precedence than factorials.
        assert!(Evaluator::new(b"2^3!", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![64]))));
        assert!(Evaluator::new(b"3!^2", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![36]))));
        // Error since leading/trailing whitespace is not consumed by exponentiation or higher precedence expressions.
        assert!(match Evaluator::new(b" 2^2", &mut Cache::new(), &mut Vec::new()).get_exps().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
    }
    #[test]
    fn neg() {
        assert!(Evaluator::new(b"-1", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"-   -  1", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"-0", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::NoSign, BigUint::new(Vec::new()))));
        // negation has lower precedence than exponentiation.
        assert!(Evaluator::new(b"-2^2", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![4]))));
        // negation always becomes exponentiation eventually.
        assert!(Evaluator::new(b"2.0^2.0", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        // Error since leading/trailing whitespace is not consumed by exponentiation or higher precedence expressions.
        assert!(match Evaluator::new(b" -2", &mut Cache::new(), &mut Vec::new()).get_neg().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
    }
    #[test]
    fn mult() {
        assert!(Evaluator::new(b"2  *   3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"-2  *   3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"2  *   -3.0", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"-2.5*-3.5", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![35])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        assert!(Evaluator::new(b"4.0 /  6", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))));
        assert!(Evaluator::new(b"6/3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))));
        assert!(Evaluator::new(b"-6/3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2]))));
        assert!(Evaluator::new(b"6/-3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2]))));
        assert!(Evaluator::new(b"-  6 / -  3", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))));
        // Number literals are not strictly equivalent to "ratios" as "ratios" don't exist (i.e., 2/3 is not the ratio of 2 to 3 but is the rational number two divided by the rational number 3).
        assert!(Evaluator::new(b"1/1.5", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() != Evaluator::new(b"1/3/2", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap());
        assert!(Evaluator::new(b"1/1.5", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Evaluator::new(b"1/(3/2)", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap());
        // multiplication always becomes negation eventually.
        assert!(Evaluator::new(b"-2.0", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2]))));
        // Error since leading/trailing whitespace is not consumed by multiplication or higher precedence expressions.
        assert!(match Evaluator::new(b" 2*2", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b" 2/2", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        // Cannot divide by 0.
        assert!(match Evaluator::new(b"2/0", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap_err() {
            E::DivByZero(i) => i == 3,
            _ => false,
        });
        // multiplication has lower precedence than exponentiation.
        assert!(Evaluator::new(b"2*2^2", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![8]))));
        assert!(Evaluator::new(b"8/2^2", &mut Cache::new(), &mut Vec::new()).get_mults().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))));
    }
    #[test]
    fn add() {
        assert!(Evaluator::new(b"2  +   3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![5]))));
        assert!(Evaluator::new(b"-2  +   3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"2  +   -3.0", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![1]))));
        assert!(Evaluator::new(b"-2.5+-3.5", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"4.0 -  6", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2]))));
        assert!(Evaluator::new(b"6-3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))));
        assert!(Evaluator::new(b"-6-3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![9]))));
        assert!(Evaluator::new(b"6--3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![9]))));
        assert!(Evaluator::new(b"-  6 - -  3", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![3]))));
        // addition always becomes multiplication eventually.
        assert!(Evaluator::new(b"2 * 8", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![16]))));
        assert!(Evaluator::new(b"8 / 2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![4]))));
        // Error since leading/trailing whitespace is not consumed by addition or higher precedence expressions.
        assert!(match Evaluator::new(b" 2+2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b" 2-2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        // addition has lower precedence than multiplication.
        assert!(Evaluator::new(b"2+2*2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![6]))));
        assert!(Evaluator::new(b"2+2/2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))));
        assert!(Evaluator::new(b"2-2*2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Minus, BigUint::new(vec![2]))));
        assert!(Evaluator::new(b"2-2/2", &mut Cache::new(), &mut Vec::new()).get_adds().unwrap() == Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))));
    }
    #[test]
    fn empty() {
        assert!(match Evaluator::new(b"  \n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap() {
            O::Empty => true,
            _ => false,
        });
        assert!(match Evaluator::new(b"\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap() {
            O::Empty => true,
            _ => false,
        });
        assert!(match Evaluator::new(b"\r\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"\t\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"\n\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"\n ", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingLF => true,
            _ => false,
        });
    }
    #[test]
    fn exit() {
        assert!(match Evaluator::new(b"  q    \n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap() {
            O::Exit=> true,
            _ => false,
        });
        assert!(match Evaluator::new(b"q\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap() {
            O::Exit=> true,
            _ => false,
        });
        assert!(match Evaluator::new(b"\rq\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"\tq\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"q\n\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::InvalidQuit => true,
            _ => false,
        });
        assert!(match Evaluator::new(b"\nq\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"q", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingLF => true,
            _ => false,
        });
    }
    #[test]
    fn recall_statement() {
        let mut cache = Cache::new();
        Evaluator::new(b"1\n", &mut cache, &mut Vec::new()).evaluate().unwrap();
        assert!(match Evaluator::new(b"  =   @    \n", &mut cache, &mut Vec::new()).evaluate().unwrap() {
            O::Prev(v)=> v.val == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))) && v.round.is_none(),
            _ => false,
        });
        assert!(match Evaluator::new(b"  = 2@    \n", &mut cache, &mut Vec::new()).evaluate().unwrap() {
            O::Prev(v)=> v.val == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))) && v.round.unwrap() == 2,
            _ => false,
        });
        assert!(match Evaluator::new(b"=9@1\n", &mut cache, &mut Vec::new()).evaluate().unwrap() {
            O::Prev(v)=> v.val == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))) && v.round.unwrap() == 9,
            _ => false,
        });
        assert!(match Evaluator::new(b"=0@1\n", &mut cache, &mut Vec::new()).evaluate().unwrap() {
            O::Prev(v)=> v.val == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1]))) && v.round.unwrap() == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"=@2\n", &mut Cache::new(), &mut Vec::new()).evaluate().unwrap_err() {
            E::NotEnoughPrevResults(count)=> count == 0,
            _ => false,
        });
        assert!(match Evaluator::new(b"=@ 1\n", &mut cache, &mut Vec::new()).evaluate().unwrap_err() {
            E::InvalidRecall => true,
            _ => false,
        });
        assert!(match Evaluator::new(b"=0 @1\n", &mut cache, &mut Vec::new()).evaluate().unwrap_err() {
            E::InvalidRecall => true,
            _ => false,
        });
        assert!(match Evaluator::new(b"=@", &mut cache, &mut Vec::new()).evaluate().unwrap_err() {
            E::MissingLF=> true,
            _ => false,
        });
    }
    #[test]
    fn eval() {
        use core::str::FromStr;
        let mut cache = Cache::new();
        let mut exp = Vec::new();
        assert!(match Evaluator::new(b"1+2\n", &mut cache, &mut exp).evaluate().unwrap() {
            O::Val(i) => i == &Ratio::from_integer(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![3]))),
            _ => false,
        });
        assert!(match Evaluator::new(b"-1/2+2*@\n", &mut cache, &mut exp).evaluate().unwrap() {
            O::Val(i) => i == &Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![11])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![2]))),
            _ => false,
        });
        assert!(match Evaluator::new(b"@^@2!\n", &mut cache, &mut exp).evaluate().unwrap() {
            O::Val(i) => i == &Ratio::new(BigInt::from_biguint(Sign::Plus, BigUint::new(vec![1771561])), BigInt::from_biguint(Sign::Plus, BigUint::new(vec![64]))),
            _ => false,
        });
        // Verified with Wolfram Alpha.
        assert!(match Evaluator::new(b"  1 + (2 * |(7.98 - 12/7)|) / 4!^@3!^|1-3|  \n", &mut cache, &mut exp).evaluate().unwrap() {
            O::Val(i) => i == &Ratio::from_str("2841328814244153299237884950647090899374680152474331/2841328814244153299237884950647090899374680152473600").unwrap(),
            _ => false,
        });
        // Invalid UTF-8 does not cause a panic!.
        assert!(match Evaluator::new(&[255, 255, 255, b'\n'], &mut cache, &mut exp).evaluate().unwrap_err() {
            E::MissingTerm(i) => i == 0,
            _ => false,
        });
        assert!(match Evaluator::new(&[b'2', 255, b'\n'], &mut cache, &mut exp).evaluate().unwrap_err() {
            E::TrailingSyms(i) => i == 1,
            _ => false,
        });
        // Exactly one line feed is required.
        assert!(match Evaluator::new(&[b'2', b'\n', b'\n'], &mut cache, &mut exp).evaluate().unwrap_err() {
            E::TrailingSyms(i) => i == 1,
            _ => false,
        });
        assert!(match Evaluator::new(&[b'\n'], &mut cache, &mut exp).evaluate().unwrap() {
            O::Empty => true,
            _ => false,
        });
        // Empty input does not cause a panic!.
        assert!(match Evaluator::new(&[], &mut cache, &mut exp).evaluate().unwrap_err() {
            E::MissingLF => true,
            _ => false,
        });
    }
}