-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn.go
359 lines (263 loc) · 11.6 KB
/
rpn.go
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
package main
var manpage = `
NAME
rpn - Programmable command-line calculator using reverse polish notation
SYNOPSIS
Usage: rpn [-bdvh] [<operator>]
Options:
-b, --batchmode enable batch mode
-d, --debug enable debug mode
-s, --stack show last 5 items of the stack (off by default)
-i --intermediate print intermediate results
-m, --manual show manual
-c, --config <file> load <file> containing LUA code
-p, --precision <int> floating point number precision (default 2)
-v, --version show version
-h, --help show help
When <operator> is given, batch mode ist automatically enabled. Use
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
DESCRIPTION
rpn is a command line calculator using reverse polish notation.
Working principle
Reverse Polish Notation (short: RPN) requires to have a stack where
numbers and results are being put. So, you put numbers onto the stack
and each math operation uses these for calculation, removes them and
puts the result back.
To visualize it, let's look at a calculation:
((80 + 20) / 2) * 4
This is how you enter the formula int an RPN calculator and how the
stack evolves during the operation:
| rpn commands | stack contents | calculation |
|--------------|----------------|---------------|
| 80 | 80 | |
| 20 | 80 20 | |
| + | 100 | 80 + 20 = 100 |
| 2 | 100 2 | |
| / | 50 | 100 / 2 = 50 |
| 4 | 50 4 | |
| x | 200 | 50 * 4 = 200 |
The last stack element 200 is the calculation result.
USAGE
The default mode of operation is the interactive mode. You'll get a
prompt which shows you the current size of the stack. At the prompt you
enter numbers followed by operators or mathematical functions. You can
use completion for the functions. You can either enter each number or
operator on its own line or separated by whitespace, that doesn't
matter. After a calculation the result will be immediately displayed
(and added to the stack). You can quit interactive mode using the
commands quit or exit or hit one of the "ctrl-d" or "ctrl-c" key
combinations.
If you feed data to standard input (STDIN), rpn just does the
calculation denoted in the contet fed in via stdin, prints the result
and exits. You can also specify a calculation on the commandline.
Here are the three variants ($ is the shell prompt):
$ rpn
rpn> 2
rpn> 2
rpn> +
= 4
$ rpn
rpn> 2 2 +
= 4
$ echo 2 2 + | rpn
4
$ rpn 2 2 +
4
The rpn calculator provides a batch mode which you can use to do math
operations on many numbers. Batch mode can be enabled using the
commandline option "-b" or toggled using the interactive command batch.
Not all math operations and functions work in batch mode though.
Example of batch mode usage:
$ rpn -b
rpn->batch > 2 2 2 2 +
= 8
$ rpn
rpn> batch
rpn->batch> 2 2 2 2 +
8
$ echo 2 2 2 2 + | rpn -b
8
$ echo 2 2 2 2 | rpn +
8
If the first parameter to rpn is a math operator or function, batch mode
is enabled automatically, see last example.
You can enter integers, floating point numbers (positive or negative) or
hex numbers (prefixed with 0x).
STACK MANIPULATION
There are lots of stack manipulation commands provided. The most
important one is undo which goes back to the stack before the last math
operation.
You can use dump to display the stack. If debugging is enabled ("-d"
switch or debug toggle command), then the backup stack is also being
displayed.
The stack can be reversed using the reverse command. However, sometimes
only the last two values are in the wrong order. Use the swap command to
exchange them.
You can use the shift command to remove the last number from the stack.
BUILTIN OPERATORS AND FUNCTIONS
Basic operators:
+ add
- subtract
/ divide
x multiply (alias: *)
^ power
Bitwise operators:
and bitwise and
or bitwise or
xor bitwise xor
< left shift
> right shift
Percent functions:
% percent
%- subtract percent
%+ add percent
Batch functions:
sum sum of all values (alias: +)
max max of all values
min min of all values
mean mean of all values (alias: avg)
median median of all values
Math functions:
mod sqrt abs acos acosh asin asinh atan atan2 atanh cbrt ceil cos cosh
erf erfc erfcinv erfinv exp exp2 expm1 floor gamma ilogb j0 j1 log
log10 log1p log2 logb pow round roundtoeven sin sinh tan tanh trunc y0
y1 copysign dim hypot
Conversion functions:
cm-to-inch
inch-to-cm
gallons-to-liters
liters-to-gallons
yards-to-meters
meters-to-yards
miles-to-kilometers
kilometers-to-miles
Configuration Commands:
[no]batch toggle batch mode (nobatch turns it off)
[no]debug toggle debug output (nodebug turns it off)
[no]showstack show the last 5 items of the stack (noshowtack turns it off)
Show commands:
dump display the stack contents
hex show last stack item in hex form (converted to int)
history display calculation history
vars show list of variables
Stack manipulation commands:
clear clear the whole stack
shift remove the last element of the stack
reverse reverse the stack elements
swap exchange the last two stack elements
dup duplicate last stack item
undo undo last operation
edit edit the stack interactively using vi or $EDITOR
Other commands:
help|? show this message
manual show manual
quit|exit|c-d|c-c exit program
Register variables:
>NAME Put last stack element into variable NAME
<NAME Retrieve variable NAME and put onto stack
Refer to https://pkg.go.dev/math for details about those functions.
There are also a number of shortcuts for some commands available:
d debug
b batch
s showstack
h history
p dump (aka print)
v vars
c clear
u undo
INTERACTIVE REPL
While you can use rpn in the command-line, the best experience you'll
have is the interactive repl (read eval print loop). Just execute "rpn"
and you'll be there.
In interactive mode you can use TAB completion to complete commands,
operators and functions. There's also a history, which allows you to
repeat complicated calculations (as long as you've entered them in one
line).
There are also a lot of key bindings, here are the most important ones:
ctrl-c + ctrl-d
Exit interactive rpn
ctrl-z
Send rpn to the backgound.
ctrl-a
Beginning of line.
ctrl-e
End of line.
ctrl-l
Clear the screen.
ctrl-r
Search through history.
COMMENTS
Lines starting with "#" are being ignored as comments. You can also
append comments to rpn input, e.g.:
# a comment
123 # another comment
In this case only 123 will be added to the stack.
VARIABLES
You can register the last item of the stack into a variable. Variable
names must be all caps. Use the ">NAME" command to put a value into
variable "NAME". Use "<NAME" to retrieve the value of variable "NAME"
and put it onto the stack.
The command vars can be used to get a list of all variables.
EXTENDING RPN USING LUA
You can use a lua script with lua functions to extend the calculator. By
default the tool looks for "~/.rpn.lua". You can also specify a script
using the <kbd>-c</kbd> flag.
Here's an example of such a script:
function add(a,b)
return a + b
end
function init()
register("add", 2, "addition")
end
Here we created a function "add()" which adds two parameters. All
parameters are "FLOAT64" numbers. You don't have to worry about stack
management, this is taken care of automatically.
The function "init()" MUST be defined, it will be called on startup. You
can do anything you like in there, but you need to call the "register()"
function to register your functions to the calculator. This function
takes these parameters:
* function name
* number of arguments expected (see below)
Number of expected arguments can be:
- 0: expect 1 argument but do NOT modify the stack
- 1-n: do a singular calculation
- -1: batch mode work with all numbers on the stack
* help text
Please refer to the lua language reference:
<https://www.lua.org/manual/5.4/> for more details about LUA.
Please note, that io, networking and system stuff is not allowed though.
So you can't open files, execute other programs or open a connection to
the outside!
CONFIGURATION
rpn can be configured via command line flags (see usage above). Most of
the flags are also available as interactive commands, such as "--batch"
has the same effect as the batch command.
The floating point number precision option "-p, --precision" however is
not available as interactive command, it MUST be configured on the
command line, if needed. The default precision is 2.
GETTING HELP
In interactive mode you can enter the help command (or ?) to get a short
help along with a list of all supported operators and functions.
To read the manual you can use the manual command in interactive mode.
The commandline option "-m" does the same thing.
If you have installed rpn as a package or using the distributed tarball,
there will also be a manual page you can read using "man rpn".
BUGS
In order to report a bug, unexpected behavior, feature requests or to
submit a patch, please open an issue on github:
<https://github.com/TLINDEN/rpnc/issues>.
LICENSE
This software is licensed under the GNU GENERAL PUBLIC LICENSE version
3.
Copyright (c) 2023-2024 by Thomas von Dein
This software uses the following GO modules:
readline (github.com/chzyer/readline)
Released under the MIT License, Copyright (c) 2016-2023 ChenYe
pflag (https://github.com/spf13/pflag)
Released under the BSD 3 license, Copyright 2013-2023 Steve Francia
gopher-lua (github.com/yuin/gopher-lua)
Released under the MIT License, Copyright (c) 2015-2023 Yusuke
Inuzuka
AUTHORS
Thomas von Dein tom AT vondein DOT org
`