-
Notifications
You must be signed in to change notification settings - Fork 30
/
clac.1
345 lines (344 loc) · 8.24 KB
/
clac.1
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
.Dd May 3, 2017
.Dt CLAC 1
.
.Sh NAME
.
.Nm clac
.Nd A command line, stack-based calculator with postfix notation
.Sh SYNOPSIS
.
.Nm
.Op Ar expression
.
.Sh DESCRIPTION
.
.Nm
is a command line, stack-based calculator with postfix notation
that displays the stack contents at all times. As you type, the
stack changes are reflected immediately.
.Pp
In a stack-based postfix calculator, entering a number pushes it
on a stack, and arithmetic operations pop their arguments from the
stack and push the result. As all the operations take a fixed number
of arguments, there's no room for ambiguity: parentheses and operator
precedence are not needed. Postfix notation is also known as reverse
Polish notation, or RPN.
.
.Ss Non-interactive mode
.
If an
.Em expression
is provided, clac will process it and print each element in the
stack starting from the top. It will then exit immediately.
.
.Ss Commands
.
When a command requires an argument, it pops a value from the stack.
If the stack is empty, a zero is provided instead. In the descriptions
below, the top of the stack (and thus the first value popped) is
represented by the letter `a`, while the second value popped is
represented by the letter `b`. For example, if the stack is composed
of the number `1`, `2` and `3` (with `3` at the top of the stack),
when we describe the sum then `a` will be `3` and `b` will be `2`.
It is important to note that subtraction and division invert the
order of the arguments before performing the operation: with `1`,
`2` and `3` in the stack, when you type `-` it will pop the values
`3` and `2` and push the result of `2 - 3`. This is in the tradition
of other postfix calculators and programming languages.
.
A description of the available commands follows.
.
.Ss Arithmetic operations
.
.Bl -tag -width Fl
.It Ic +
Pop two values `a` and `b` and push the result of `a + b`.
.
.It Ic -
Pop two values `a` and `b` and push the result of `b - a`.
.
.It Ic *
Pop two values `a` and `b` and push the result of `a * b`.
.
.It Ic /
Pop two values `a` and `b` and push the result of `b / a`.
.El
.
.Ss Modulo operation
.
.Bl -tag -width Fl
.It Ic %
Pop two values `a` and `b` and push the remainder of the Euclidean
division of `b` by `a`.
.El
.
.Ss Exponentiation
.
.Bl -tag -width Fl
.It Ic ^
Pop two values `a` and `b` and push the result of `b ^ a`.
.El
.
.Ss Logarithm
.
.Bl -tag -width Fl
.It Ic ln
Pop the value `a` and push its natural logarithm.
.It Ic log
Pop the value `a` and push its logarithm to base 10.
.El
.
.Ss Factorial
.
.Bl -tag -width Fl
.It Ic !
Pop the value `a` and push its factorial.
.El
.
.Ss Trigonometry
.
.Bl -tag -width Fl
.It Ic sin
Pop the value `a` and push its sine.
.It Ic cos
Pop the value `a` and push its cosine.
.It Ic tan
Pop the value `a` and push its tangent.
.It Ic asin
Pop the value `a` and push its arc sine.
.It Ic acos
Pop the value `a` and push its arc cosine.
.It Ic atan
Pop the value `a` and push its arc tangent.
.It Ic atan2
Pop two values `a` and `b` and push the arc tangent of `b / a`,
using the signs of `a` and `b` to determine the quadrant.
.El
.
.Ss Error function
.
.Bl -tag -width Fl
.It Ic erf
Pop the value `a` and push its error function.
.El
.
.Ss Summation
.
.Bl -tag -width Fl
.It Ic sum
Pop all the values in the stack and push their sum.
.It Ic add
Pop the value `a` and remove that many items from the stack. Push
their sum.
.El
.
.Ss Product
.
.Bl -tag -width Fl
.It Ic prod
Pop all the values in the stack and push their product.
.It Ic mul
Pop the value `a` and remove that many items from the stack. Push
their product.
.El
.
.Ss Rounding
.
.Bl -tag -width Fl
.It Ic ceil
Pop the value `a` and push the smallest integer value greater than or
equal to `a`.
.It Ic floor
Pop the value `a` and push the largest integer value less than or
equal to `a`.
.It Ic round
Pop the value `a` and push the integer value closest to `a`.
.El
.
.Ss Absolute value
.
.Bl -tag -width Fl
.It Ic abs
Pop the value `a` and push the non-negative value of `a`.
.El
.
.Ss Binary operations
.
.Bl -tag -width Fl
.It Ic and
Pop two values `a` and `b` and push the binary AND of `a` and `b`.
.It Ic or
Pop two values `a` and `b` and push the binary OR of `a` and `b`.
.It Ic xor
Pop two values `a` and `b` and push the binary XOR of `a` and `b`.
.El
.
.Ss Stack manipulation
.
.Bl -tag -width Fl
.It Ic swap
Pop two values `a` and `b` and push the values `a`, `b`.
.It Ic dup
Pop the value `a` and push the values `a`, `a`.
.It Ic roll
Pop two values `a` and `b` and rotate `b` elements in the stack `a`
times.
.It Ic drop
Remove the top of the stack.
.It Ic clear
Remove all the elements in the stack.
.It Ic count
Push the number of items in the stack.
.It Ic _
Push on the stack the result of the last operation.
.El
.
.Ss Stashing
.
.Bl -tag -width Fl
.It Ic stash
Pop the value `a` and move that many items to the stash.
.It Ic fetch
Pop the value `a` and move that many items from the stash.
.It Ic .
Stash the top of the stack.
.It Ic ,
Fetch one stashed item.
.It Ic :
Stash all the items in the stack.
.It Ic ;
Fetch all stashed items.
.El
.
.Ss History
.
Use
.Aq Ic C-p
/
.Aq Ic C-n
or
.Aq Ic Up
/
.Aq Ic Down
to navigate the history.
.
.Ss User defined operations
.
It is possible to define operations (or
.Em words ,
as they are usually called in stack based programming languages)
by editing the
.Pa words
configuration file. It is not created by default, but clac will use
some environment variables in order to search for word definitions.
.
.Bl -tag -width X
.It Ic $CLAC_WORDS
If set, it should point to a file containing word definitions.
.It Ic $XDG_CONFIG_HOME
If set, clac will search for
.Pa $XDG_CONFIG_HOME/clac/words
.It Ic $HOME
If set, clac will search for
.Pa $HOME/.config/clac/words
.El
.
.Ss How to define words
.
Words are defined as aliases, with one alias on each line. Empty
lines are ignored. Here are some examples:
.Pp
.Dl Sy pi No 3.14159265358979323846
.Dl Sy tau Qq "pi 2 *"
.Dl Sy sqrt Qq "0.5 ^"
.Pp
Note that an alias has two parts: a word to be defined and its
meaning. That's why the
.Sy tau
and
.Sy sqrt
definitions are enclosed in double quotes. If the double quotes are
removed, clac will complain that it can't parse the command. For
example, if we remove the double quotes from
.Sy sqrt
and start clac, we will get this error message:
.Pp
.Dl Incorrect definition: sqrt 0.5 ^
.Dl (~/.config/clac/words:3)
.Pp
User defined words can be used as if they were built-in commands:
.Pp
.Dl $ clac Qq "42 dup * pi *"
.Dl Sy 5541.76944093239
.
.Ss Comments
.
Any lines that begin with
.Sy #
are considered comments and are ignored. There are no inline comments,
and any
.Sy #
characters that are not the first character of a line are interpreted
literally. To define
.Sy #
as a word, wrap it in double quotes in the definition:
.Pp
.Dl # This is a comment.
.Dl \(dq#\(dq count
.Pp
The first line is ignored, and the second line assigns
.Sy #
to the operation
.Sy count.
.
.Ss How to list defined words
.
If you type `words` and hit enter, clac will list the defined words.
.
.Ss How to reload defined words
.
If you type `reload` and hit enter, clac will reload the words file.
.
.Sh EXAMPLES
.
While the most interesting aspect of clac is the ability to visualize
the stack as it is updated with each key press, at some point you
may want use clac just to get a quick result or call it from a
script. For that reason, clac can be used in non-interactive mode
by invoking it with an argument.
.Pp
Here are some examples of non-interactive invocations:
.Pp
.Dl $ clac Qq "3 4 +"
.Dl Sy 7
.Pp
.Dl $ clac Qq "2 3 4 +"
.Dl Sy 7
.Dl Sy 2
.Pp
When clac finishes evaluating the expression "2 3 4 +", there are
two elements in the stack: the number 7 at the top of the stack and
the number 2 at the bottom of the stack. The elements are printed
in order, one per line, starting from the top of the stack.
.Pp
This other example uses the stashing features. Let's say we want
to push two numbers and get the result of their multiplication plus
the square of the second number.
.Pp
.Dl $ clac Qq "4 3 dup dup * . * , +"
.Dl Sy 21
.Pp
Another example that uses the stash would be to get the average of
all the elements in the stack:
.Pp
.Dl $ clac Qq "1 2 3 4 count . sum , /"
.Dl Sy 2.5
.Pp
In fact, if you find yourself calculating averages very often, you can
define the word
.Sy avg
as
.Qq Sy "count . sum , /" .
.
.Sh AUTHOR
.An Michel Martens Aq [email protected]