hpn 0.6.3

Text-based RPN-style calculator in the HP Voyager tradition.
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
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
// Copyright 2022 Tim Hammerquist
//
// Licensed under the [MIT license](https://opensource.org/licenses/MIT).
// This file may not be copied, modified, or distributed except according to those terms.

use lazy_static::lazy_static;
use std::fmt::{self, Write};

use crate::atom::Atom;
use crate::prelude::{FromPrimitive, Number, Rng, ToPrimitive, Zero};
use crate::util::{c_to_f, f_to_c, factorial, help, y_pow_x};

const PRECISION: usize = 3;
const WIDTH: usize = 8;

/// Underlying implementation of the 4-register stack.
pub type Stack = [Number; 4];

#[derive(Clone, Debug, Default)]
pub(crate) struct Memory {
    last_x: Number,
}

/// Enum used to map registers (X, Y, Z, T) to stack index.
#[derive(Clone, Debug)]
enum Register {
    X = 0,
    Y,
    Z,
    T,
}

/// Primary struct backing the HPN engine.
#[derive(Debug)]
pub struct HPN {
    history: Vec<String>,
    stack: Stack,
    memory: Memory,
}

impl HPN {
    /// Constructs new HPN instance, with emtpy tape and 0 in each register.
    /// ```
    /// use hpn::prelude::*;
    ///
    /// let mut hp = HPN::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        HPN {
            history: vec![],
            memory: Memory::default(),
            stack: Stack::default(),
        }
    }

    /// Parses and evaluates the given string, applying each change in turn.
    /// ```
    /// # use hpn::prelude::*;
    /// # let mut hp = HPN::new();
    /// hp.evaluate("2 6 *");
    /// assert_eq!(Number::from(12), *hp.x());
    /// ```
    pub fn evaluate(&mut self, line: &str) {
        Atom::tokenize(line)
            .iter()
            .for_each(|atom| self.apply(atom));
    }

    /// Clears the history for this calculator object. Does not alter the stack or memory.
    pub fn clear_tape(&mut self) {
        self.history.clear();
    }

    /// Returns the value of the `x` register.
    /// ```
    /// # use hpn::prelude::*;
    /// # let mut hp = HPN::new();
    /// hp.evaluate("1");
    /// assert_eq!(*hp.x(), Number::one());
    /// ```
    #[must_use]
    pub fn x(&self) -> &Number {
        &self.stack[Register::X as usize]
    }

    /// Returns the value of the `y` register.
    /// ```
    /// # use hpn::prelude::*;
    /// # let mut hp = HPN::new();
    /// hp.evaluate("0 1");
    /// assert_eq!(*hp.x(), Number::one());
    /// assert_eq!(*hp.y(), Number::zero());
    /// ```
    #[must_use]
    pub fn y(&self) -> &Number {
        &self.stack[Register::Y as usize]
    }

    /// Returns the value of the `z` register.
    /// ```
    /// # use hpn::prelude::*;
    /// # let mut hp = HPN::new();
    /// hp.evaluate("0 1 2");
    /// assert_eq!(*hp.z(), Number::zero());
    /// ```
    #[must_use]
    pub fn z(&self) -> &Number {
        &self.stack[Register::Z as usize]
    }

    /// Returns the value of the `t` register.
    /// ```
    /// # use hpn::prelude::*;
    /// # let mut hp = HPN::new();
    /// hp.evaluate("8 4 2 1");
    /// assert_eq!(*hp.t(), Number::from(8));
    /// ```
    #[must_use]
    pub fn t(&self) -> &Number {
        &self.stack[Register::T as usize]
    }

    /// Returns the accumulated history of operations
    /// performed in this calculator as a lazy iterator of `String`s.
    /// ```
    /// # use hpn::prelude::*;
    /// let hp = HPN::from("3 4 7 - +");
    /// hp.tape().for_each(|line| println!("{}", line));
    /// ```
    pub fn tape(&self) -> impl Iterator<Item = String> {
        self.history
            .clone()
            .into_iter()
            .chain([self.to_string()])
            .enumerate()
            .map(|(i, line)| format!("{:2}: {}", i, line))
    }

    /// Applies an atom to the current stack.
    #[allow(clippy::too_many_lines)]
    fn apply(&mut self, atom: &Atom) {
        self.log_operation(Some(atom));

        if matches!(atom, Atom::Help) {
            self.log_message(&help());
            return;
        }

        lazy_static! {
            static ref BIG_E: Number =
                Number::from_f64(std::f64::consts::E).expect("should not fail");
            static ref BIG_PI: Number =
                Number::from_f64(std::f64::consts::PI).expect("should not fail");
        }

        if atom.saves_last_x() {
            self.memory.last_x = self.x().clone();
        }

        match atom {
            Atom::Abs => self.replace(Register::X, self.x().abs()),
            Atom::Add => {
                let sum = self.y() + self.x();
                self.pop();
                self.replace(Register::X, sum);
            }
            Atom::CToF => self.replace(Register::X, c_to_f(self.x())),
            Atom::ChangeSign => self.replace(Register::X, -self.x()),
            Atom::ClearX => self.replace(Register::X, Number::zero()),
            Atom::Cube => self.replace(Register::X, self.x().cube()),
            Atom::CubeRoot => self.replace(Register::X, self.x().cbrt()),
            Atom::Div => {
                let (x, y) = (self.x(), self.y());
                if x.is_zero() {
                    self.log_message("Error 0: Cannot divide by zero");
                } else {
                    let dividend = y / x;
                    self.pop();
                    self.replace(Register::X, dividend);
                }
            }
            Atom::Euler => self.push(BIG_E.clone()),
            Atom::Exchange => self.stack.swap(0, 1),
            Atom::FToC => self.replace(Register::X, f_to_c(self.x())),
            Atom::Factorial => match factorial(self.x()) {
                Some(n) => {
                    self.replace(Register::X, n);
                }
                None => self.log_message("Error: failed to narrow X"),
            },
            Atom::Help => self.log_message(&help()),
            Atom::IDiv => {
                let x = self.x();
                if x.is_zero() {
                    self.log_message("Error 0: Cannot divide by zero");
                } else {
                    let dividend = self.y() / x;
                    self.pop();
                    self.replace(Register::X, dividend.round(0));
                }
            }
            Atom::LastX => self.push(self.memory.last_x.clone()),
            Atom::Mul => {
                let product = self.y() * self.x();
                self.pop();
                self.replace(Register::X, product);
            }
            Atom::PI => self.push(BIG_PI.clone()),
            Atom::Push => self.push(self.x().clone()),
            Atom::Random => {
                let rnd_f64: f64 = rand::thread_rng().gen();
                match Number::from_f64(rnd_f64) {
                    Some(rnd) => self.push(rnd),
                    None => {
                        self.log_message(&format!("Error: Failed to convert value {:?}", rnd_f64));
                    }
                }
            }
            Atom::Reciprocal => match self.x().clone() {
                x if x.is_zero() => {
                    self.log_message("Error 0: Division by zero");
                }
                x => self.replace(Register::X, 1 / x),
            },
            Atom::Remainder => {
                let x = self.x();
                if x.is_zero() {
                    self.log_message("Error 0: Cannot divide by zero");
                } else {
                    let remainder = self.y() % x;
                    self.pop();
                    self.replace(Register::X, remainder);
                }
            }
            Atom::Roll => self.stack.rotate_left(1),
            Atom::Square => self.replace(Register::X, self.x().square()),
            Atom::SquareRoot => match self.x().sqrt() {
                Some(result) => self.replace(Register::X, result),
                None => self.log_message("Error 0"),
            },
            Atom::Sub => {
                let difference = self.y() - self.x();
                self.pop();
                self.replace(Register::X, difference);
            }
            Atom::YToX => match y_pow_x(self.y(), self.x()) {
                Some(result) => self.replace(Register::X, result),
                None => self.log_message("Error 0"),
            },
            Atom::Value(n) => self.push(n.clone()),
            Atom::BadToken(_) => {
                self.log_message(&format!("Error: {:?}", atom));
            }
        }
    }

    fn log_message(&mut self, message: &str) {
        self.history.push(message.to_owned());
    }

    fn log_operation(&mut self, opt_atom: Option<&Atom>) {
        let mut s = self.to_string();
        if let Some(atom) = opt_atom {
            write!(s, "  <- {}", atom).unwrap();
        }
        self.log_message(&s);
    }

    fn pop(&mut self) -> Number {
        let x = self.x().clone();
        self.stack[Register::X as usize] = self.t().clone();
        self.stack.rotate_left(1);
        x
    }

    fn push(&mut self, n: Number) {
        self.stack.rotate_right(1);
        self.replace(Register::X, n);
    }

    fn replace(&mut self, reg: Register, value: Number) {
        self.stack[reg as usize] = value;
    }
}

/// Same as `HPN::new()`
impl Default for HPN {
    fn default() -> Self {
        HPN::new()
    }
}

/// Displays the current state of the stack.
impl fmt::Display for HPN {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "[ T: {t:w$.p$} | Z: {z:w$.p$} | Y: {y:w$.p$} | X: {x:w$.p$} ]",
            w = WIDTH,
            p = PRECISION,
            x = self.x(),
            y = self.y(),
            z = self.z(),
            t = self.t(),
        )
    }
}

/// Constructs an HPN instance and evaluates the expression passed.
impl From<&str> for HPN {
    fn from(expr: &str) -> Self {
        let mut hp = HPN::new();
        hp.evaluate(expr);
        hp
    }
}

/// Constructs an HPN instance with the given initial stack.
impl From<[f64; 4]> for HPN {
    fn from(values: [f64; 4]) -> HPN {
        let stack: Stack = values.map(|n| Number::from_f64(n).unwrap_or_else(Number::zero));
        HPN::from(stack)
    }
}

/// Constructs an HPN instance with the given initial stack.
impl From<[i32; 4]> for HPN {
    fn from(values: [i32; 4]) -> HPN {
        let stack: Stack = values.map(|n| Number::from_i32(n).unwrap_or_else(Number::zero));
        HPN::from(stack)
    }
}

/// Constructs an HPN instance with the given initial stack.
impl From<[Number; 4]> for HPN {
    fn from(stack: Stack) -> HPN {
        let mut hp = HPN {
            stack,
            ..HPN::new()
        };
        hp.log_operation(None);
        hp
    }
}

/// Constructs an HPN instance with the given initial stack.
impl TryFrom<&HPN> for [f64; 4] {
    type Error = &'static str;

    fn try_from(hp: &HPN) -> Result<Self, Self::Error> {
        hp.stack
            .iter()
            .enumerate()
            .try_fold([0.0, 0.0, 0.0, 0.0], |mut acc, (i, d)| {
                Some({
                    acc[i] = d.to_f64()?;
                    acc
                })
            })
            .ok_or("conversion failed")
    }
}

/// Constructs an HPN instance with the given initial stack.
impl TryFrom<&HPN> for [i32; 4] {
    type Error = &'static str;

    fn try_from(hp: &HPN) -> Result<Self, Self::Error> {
        hp.stack
            .iter()
            .enumerate()
            .try_fold([0, 0, 0, 0], |mut acc, (i, d)| {
                Some({
                    acc[i] = d.to_i32()?;
                    acc
                })
            })
            .ok_or("conversion failed")
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::{FromStr, One, ToPrimitive, Zero};

    use super::*;

    #[test]
    fn test_returns_stack_set() {
        let hp = HPN::from([2, 3, 5, 8]);
        assert_eq!(Some(2), hp.x().to_i32());
        assert_eq!(Some(3), hp.y().to_i32());
        assert_eq!(Some(5), hp.z().to_i32());
        assert_eq!(Some(8), hp.t().to_i32());
    }

    #[test]
    fn test_returns_stack_unset() {
        let hp = HPN::new();
        let zero = Number::zero();
        assert_eq!(&zero, hp.x());
        assert_eq!(&zero, hp.y());
        assert_eq!(&zero, hp.z());
        assert_eq!(&zero, hp.t());
    }

    #[test]
    fn test_invalid_token() {
        let hp = HPN::from("IAmBad");
        dbg!(&hp);
        assert!(hp.history.last().unwrap().contains("Error"));
    }

    #[test]
    fn test_abs() {
        let hp = HPN::from("3 5 - abs");
        assert_eq!(&Number::from(2), hp.x());
    }

    #[test]
    fn test_add() {
        let hp = HPN::from("2 3 +");
        assert_eq!(&Number::from(5), hp.x());
    }

    #[test]
    fn test_cube() {
        let hp = HPN::from("2 x^3 1.1 x^3");
        assert_eq!(&Number::from(8), hp.y());
        assert_eq!(&Number::from_str("1.331").unwrap(), hp.x());
    }

    #[test]
    fn test_cube_root() {
        let hp = HPN::from("8 cbrt 1.331 cbrt");
        assert_eq!(&Number::from_i32(2).unwrap(), hp.y());
        assert_eq!(&Number::from_f64(1.1).unwrap(), hp.x());
    }

    #[test]
    fn test_change_sign() {
        let hp = HPN::from("3 chs");
        assert_eq!(&Number::from(-3), hp.x());

        let hp = HPN::from("-7 chs");
        assert_eq!(&Number::from(7), hp.x());
    }

    #[test]
    fn test_clear_x() {
        let hp = HPN::from("2 3 5 + clx");
        assert_eq!(Ok([0, 2, 0, 0]), <[i32; 4]>::try_from(&hp));
    }

    #[test]
    fn test_div() {
        let hp = HPN::from("1.2 0.5 /");
        assert_eq!(&Number::from_str("2.4").unwrap(), hp.x());
    }

    #[test]
    fn test_div_by_zero() {
        let hp = HPN::from("3 0 /");
        dbg!(&hp);
        assert!(hp.history.last().unwrap().starts_with("Error 0"));
        assert_eq!(hp.y().to_i32(), Some(3));
        assert_eq!(hp.x().to_i32(), Some(0));
    }

    #[test]
    fn test_euler() {
        let hp = HPN::from("e");
        assert_eq!(Some(std::f64::consts::E), hp.x().to_f64());
    }

    #[test]
    fn test_exchange() {
        let hp = HPN::from("2 3 x<>y");
        assert_eq!(&Number::from(2), hp.x());
        assert_eq!(&Number::from(3), hp.y());
    }

    #[test]
    fn test_factorial_excl() {
        assert_eq!(&Number::from(39_916_800), HPN::from("11 n!").x());
    }

    #[test]
    fn test_idiv() {
        let hp = HPN::from("1.2 0.5 //");
        assert_eq!(&Number::from(2), hp.x());
    }

    #[test]
    fn test_idiv_by_zero() {
        let hp = HPN::from("3 0 //");
        dbg!(&hp);
        assert!(hp.history.last().unwrap().starts_with("Error 0"));
        assert_eq!(hp.y().to_i32(), Some(3));
        assert_eq!(hp.x().to_i32(), Some(0));
    }

    #[test]
    fn test_inv_x() {
        let hp = HPN::from("2 1/x");
        assert_eq!(Some(0.5), hp.x().to_f64());
    }

    #[test]
    fn test_last_x() {
        let hp = HPN::from("2 3 * lstx");
        assert_eq!(Ok([3, 6, 0, 0]), <[i32; 4]>::try_from(&hp));
    }

    #[test]
    fn test_mul() {
        let hp = HPN::from("2 3 *");
        assert_eq!(&Number::from(6), hp.x());
    }

    #[test]
    fn test_pi() {
        let hp = HPN::from("pi");
        assert_eq!(Some(std::f64::consts::PI), hp.x().to_f64());
    }

    #[test]
    fn test_push() {
        let hp = HPN::from("2 push");
        assert_eq!(Some(2), hp.x().to_i32());
        assert_eq!(Some(2), hp.y().to_i32());
    }

    #[test]
    fn test_random() {
        let mut hp = HPN::new();
        for _ in 0..4 {
            hp.apply(&Atom::Random);
            let rnd = hp.x();
            assert!(rnd >= &Number::zero() && rnd < &Number::one());
        }
    }

    #[test]
    fn test_remainder() {
        let hp = HPN::from("12 7 rmd");
        assert_eq!(Some(5.0), hp.x().to_f64());
    }

    #[test]
    fn test_roll() {
        let hp = HPN::from("2 3 5 8 roll");
        let expected = [5, 3, 2, 8].map(Number::from);
        assert_eq!(expected, hp.stack);
    }

    #[test]
    fn test_sub() {
        let hp = HPN::from("2 3 -");
        assert_eq!(&Number::from(-1), hp.x());
    }

    #[test]
    fn test_y_pow_x() {
        // floating point
        let hp = HPN::from("1.1 3 y^x");
        assert_eq!(&Number::from_str("1.331").unwrap(), hp.x());
        // (rough) integral
        let hp = HPN::from("2 3 y^x");
        assert_eq!(&Number::from(8), hp.x());
    }

    #[test]
    fn test_celsius_to_fahrenheit() {
        let result = HPN::from("100 9 * 5 / 32 +").x().to_i32();
        assert_eq!(Some(212), result);
    }

    #[test]
    fn test_fahrenheit_to_celsius() {
        let result = HPN::from("212 32 - 5 * 9 /").x().to_i32();
        assert_eq!(Some(100), result);
    }

    #[test]
    fn test_sample_stack_buster() {
        let mut hp = HPN::from("2 3 5 8 13");
        assert_eq!([13, 8, 5, 3], <[i32; 4]>::try_from(&hp).unwrap());
        hp.evaluate("- + 1 / /");
        assert_eq!([0, 3, 3, 3], <[i32; 4]>::try_from(&hp).unwrap());
    }

    #[test]
    fn test_stack_backfills_with_t() {
        let mut hp = HPN::from("2 3 5 8");
        assert_eq!(Some(2), hp.t().to_i32());
        hp.apply(&Atom::Add);
        assert_eq!(Some(2), hp.t().to_i32());
        hp.apply(&Atom::Add);
        assert_eq!(Some(2), hp.t().to_i32());
        hp.apply(&Atom::Add);
        assert_eq!(Some(2), hp.t().to_i32());
        hp.apply(&Atom::Mul);
    }

    #[test]
    fn test_hpn_to_array_f64() {
        let hp = HPN::from([1.0, 2.0, 4.0, 8.0]);
        let expected = [1.0, 2.0, 4.0, 8.0];

        let result = <[f64; 4]>::try_from(&hp).unwrap();
        for (i, n) in expected.iter().enumerate() {
            assert!((*n - result[i]).abs() < std::f64::EPSILON);
        }
    }

    #[test]
    fn test_hpn_to_array_i32() {
        let hp = HPN::from("2 3 5 8 13 push");
        let expected = [13, 13, 8, 5];
        let result = <[i32; 4]>::try_from(&hp).unwrap();
        assert_eq!(expected, result);
    }

    /// Used to verify output when run explicitly. Ignored otherwise.
    #[ignore]
    #[test]
    fn test_output() {
        let hp = HPN::from("21 9 * 5 / 32 +");
        dbg!(&hp);
        dbg!(hp.tape().collect::<Vec<_>>());
        dbg!(hp.x());
        hp.tape().for_each(|line| println!("{}", line));
        panic!();
    }
}