### 2 releases

0.1.1 | Feb 13, 2022 |
---|---|

0.1.0 | Feb 13, 2022 |

#**1314** in Math

**AGPL-3.0-or-later**

21KB

498 lines

# RPN calculator

A basic command-line RPN calculator. It's similar to DC but with a few differences.

Numbers are stored as

rather than arbitrary-precision integers. This is simpler but sometimes less precise.`f64`

## Command List

: Prints the value at the top of the stack without popping it.`v`

: Prints the whole stack including indices. Index`V`

represents the top of the stack.`0`

: Same as`p`

but pops the value from the stack before printing it.`v`

,`+`

,`-`

,`*`

: Exactly what you expect. The first operand is popped first.`/`

: Pops`%`

, then pops`a`

, then pushes`b`

.`a``%`b

: A combination of`"`

and`/`

. Same argument order as`%`

, but pushes the quotient before pushing the remainder aka modulus.`%`

: Pops`^`

, then pops`a`

, then pushes`b`

.`a``^`b

: Pops`~`

, then pushes`a`

. In other words, negates the value at the top of the stack.`-`a

: Clears the stack.`c`

: Pops`d`

, then pushes`a`

twice.`a`

: Swaps the two values at the top of the stack.`r`

: Pops`R`

, then rotates the bottom`N`

values of the stack. The sign of`abs``(`N`)`

determines the rotation direction: negative is right-rotation, positive is left-rotation. The best way to understand this may be to try it yourself.`N`

: Pops`i`

, then sets the input radix to`a`

. The input radix determines how numbers are parsed. In radices less than or equal to 10, all numbers can be input as normal. In radices greater than 10, the number must start with a digit (0 to 9), but you can prefix the number with`a`

and it will result in the same value. For example, in base 16, trying to input`0`

as a number would run the`c2`

command, then input the number`c`

, so instead you should input`2`

.`0c2`

: Pushes the input radix.`I`

(space),

(tab),`\t`

(ASCII NUL),`\``0`

(newline): Does nothing, but splits numbers and other operations, allowing for multiple on the same line in the case of space, tab, and NUL (e.g.,`\n`

pushes`123``456`

and`123`

separately).`456`

: Pushes the current length of the stack, not including this value that is about to be pushed.`z`

: Gets help for a command.`?``<`command`>`

### Register Commands

Registers are indexed by their UTF-8 codepoint, which can be any single character. They are their own fully-fledged, freestanding stacks.

: Pops a value from the global stack and pushes it to the specified register.`s``<`reg`>`

: Pops a value from the specified register and pushes it to the global stack.`l``<`reg`>`

: Peeks a value from the stack without popping it and pushes it to the specified register.`S``<`reg`>`

: Peeks a value from the specified register and pushes it to the global stack.`L``<`reg`>`

: Performs`&``<`reg`>``<`operation`>`

inside the specified register. Nested register operations such as`operation`

are not permitted.`&`asb

## Usage Examples

`>` `2` `2``+`v
`4`
`>` c `2``~`V
`0``:` `-``2`
`>` 16i c2 V
`0``:` `2`
`>` c 0c2 V
`0``:` `194`
`>` 0a i c `123` sl V
`>` `&`lV
`0``:` `123`
`>` `456` sl `&`l`+` Ll v
`579`
`>` c `1` `2` `3` `4` V
`3``:` `1`
`2``:` `2`
`1``:` `3`
`0``:` `4`
`>` 3RV
`3``:` `1`
`2``:` `3`
`1``:` `4`
`0``:` `2`
`>` `3``~``RV`
`3``:` `1`
`2``:` `2`
`1``:` `3`
`0``:` `4`
`>` c `0` `0` `0` `0` zp
`4`
`>` c `1` `0` `/` p
inf

#### Dependencies

~210KB