rpn(1)
- reverse polish notation
#21
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
e.g.
A reverse polish notation calculator.
-i
reads arguments from standard input.Some questions regarding design:
+
,-
,*
%
?&
,|
,^
,~
?/
be division or floor division? (4 / 3 = 1.333... versus 4 / 3 = 1)\
?**
for exponentiation)?This is an option for a reverse polish notation library in Rust we could use/distribute:
https://github.com/lillycat332/dc-rs
My idea for interaction is that by default, rpn(1) prints the closest value on the stack at the end of argument acceptance. If run in interactive mode, rpn(1) prints the closest value on the stack after every token:
This may be more clear presented like so:
Whereas in arguments mode:
I'm not a big fan of dc(1p) or bc(1p) and needing to do stuff like
Or whatever to get them to work; specifically the
p
command which should really be the default (why wouldn't you want to see the output of computation?).rpn(1) - reverse polish notationto `rpn(1)` - reverse polish notationI'm deciding, tentatively, with comments still desired:
+
,-
- addition and subtraction*
,**
- multiplication and exponentiation (see Python)^
,|
,&
,~
- bitwise operators (see C)&&
,||
,!
- boolean operators (see C)//
,%
- floor division and modulus (see Python)?
- flow control (see blang and below)>
,<
,==
,>=
,<=
- comparisons (see C):
- duplicate (see below)?
pops the stack and, if the value is 0, does nothing, otherwise discards the next token in the program.:
pops the stack and pushes the value twice.There's no ability to return to any previous part of the program, by design; this is a calculator, not a language, and complex programs shouldn't be written in it.
Numbers are entered formatted
0xN
for hexadecimal,0oN
for octal,0bN
for binary, and traditionally for decimal, and displayed as decimal only.It's tempting to facilitate use of environment variables but I don't see an elegant way to do so and the functionality already exists in the shell. Program-local variables too, because they can't be implemented elegantly.
Emma is working on this in #36.
When writing
npc 1 2 *
the shell automatically takes the asterisk as a wildard and it must be quoted. Should we do anything about this (i.e. havex
be the multiplication operator when parsing arguments instead of stdin)?What purpose would these serve here?
intcmp(1)
?Let's just make it need to be quoted. I know you made Unicode multiplication runes work too so that's an option.
Bitwise operators are very useful for programming and I've used bitwise operations in calculators in the past for making intcmp(1) and some UTF-8 implementation stuff.
Boolean operators might be useful for scripting. Not scripting in-calculator but for the scenario where someone wants the calc to print [zero] or [a valid answer] or something. Same with comparisons. These are all niceties though and not important.
After a conversation with Trinity, we’ve decided rpn is feature-complete as it is. I’ll get it merged tomorrow.
Actually, I wanted to implement comparisons and then I’ll merge it.
Comparisons might be tricky because it's hard to compare floating point numbers. 0.3 doesn't always equal 0.3 bit-by-bit. Maybe it would be better to have a
~=
for equality ± some value and then===
for strict exact equality.We discussed bitwise operators not being useful for this calculator as it's floating point. I'm working on another RPN implementation (srpn(1) - simpler rpn) that might be nice for an Extras repository that will be fixed-point to support bitwise operations and non-decimal bases.
I’ll leave out comparisons. I think it’s basically done, then.