hpn/
lib.rs

1// Copyright 2023 Tim Hammerquist
2//
3// Licensed under the [MIT license](https://opensource.org/licenses/MIT).
4// This file may not be copied, modified, or distributed except according to those terms.
5
6//! [HP Voyager][hp_voyager]-inspired text-based RPN calculator
7//!
8//! Originating from a deep desire to have an RPN calculator from inside an IRC session, the `HPN`
9//! struct evaluates stack-based calculator instructions in a HP Voyager-inspired "virtual
10//! calculator".
11//!
12//! HPN uses a 4-register stack-based RPN implementation based on and inspired by the [HP
13//! Voyager][hp_voyager] series of calculators.
14//!
15//! Calculator state and history are preserved inside the `HPN` object.
16//!
17//! The current state of registers are available as instance methods; eg, `hpn.x()`, `hpn.y()`,
18//! etc.
19//!
20//! ```
21//! use hpn::prelude::*;
22//!
23//! // convert 21ºC to ºF
24//! let mut hp = HPN::from("21 9 * 5 / 32 +");
25//! assert_eq!(Some(69.8), hp.x().to_f64());
26//! // or just
27//! hp.evaluate("21 ctof");
28//! assert_eq!(Some(69.8), hp.x().to_f64());
29//! ```
30//!
31//! Snapshots of each operation performed are available via the `HPN::tape()` instance method.
32//!
33//! ```
34//! # use hpn::prelude::*;
35//! # let mut hp = HPN::from("21 9 * 5 / 32 +");
36//! # hp.evaluate("21 ctof");
37//! let tape = hp.tape().collect::<Vec<_>>().join("\n");
38//! println!("{tape}");
39//! // Output:
40//! // 0: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:    0.000 ]  <- 21
41//! // 1: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:   21.000 ]  <- 9
42//! // 2: [ T:    0.000 | Z:    0.000 | Y:   21.000 | X:    9.000 ]  <- *
43//! // 3: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:  189.000 ]  <- 5
44//! // 4: [ T:    0.000 | Z:    0.000 | Y:  189.000 | X:    5.000 ]  <- /
45//! // 5: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:   37.800 ]  <- 32
46//! // 6: [ T:    0.000 | Z:    0.000 | Y:   37.800 | X:   32.000 ]  <- +
47//! // 7: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:   69.800 ]  <- 21
48//! // 8: [ T:    0.000 | Z:    0.000 | Y:   69.800 | X:   21.000 ]  <- ctof
49//! // 9: [ T:    0.000 | Z:    0.000 | Y:   69.800 | X:   69.800 ]
50//! ```
51//!
52//! ## Command-line Client
53//!
54//! The easiest way to use this calculator is via the CLI client `hpnc`:
55//!
56//! ```text
57//! $ cargo install hpn
58//! $ hpnc
59//! hpnc x.x.x
60//! > 77 ftoc
61//!  0: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:    0.000 ]  <- 77
62//!  1: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:   77.000 ]  <- ftoc
63//!  2: [ T:    0.000 | Z:    0.000 | Y:    0.000 | X:   25.000 ]
64//! => 25
65//! >
66//! ```
67//!
68//! [hp_voyager]: https://en.wikipedia.org/wiki/Hewlett-Packard_Voyager_series
69
70#![warn(clippy::pedantic)]
71#![deny(clippy::all)]
72#![deny(missing_docs)]
73
74pub mod prelude;
75
76mod atom;
77mod hpn;
78mod util;
79
80pub use crate::hpn::HPN;