-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperations.go
434 lines (411 loc) · 15.4 KB
/
operations.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// This file is part of rpn, a simple and useful CLI RPN calculator.
// For further information, check https://github.com/marcopaganini/rpn
//
// (C) Sep/2024 by Marco Paganini <paganini AT paganini DOT net>
package main
import (
"errors"
"fmt"
"strings"
"github.com/ericlagergren/decimal"
"github.com/fatih/color"
)
type (
// ophandler contains the handler for a single operation. numArgs
// indicates how many arguments the function needs in the stack.
ophandler struct {
op string // operator or command
desc string // operation description (used by help)
numArgs int // Number of arguments to function
// Function receives the entire inverted stack (x=0, y=1, etc) and
// returns the number of elements to be popped from the stack, and a
// list of elements to be added pushes to the stack.
fn func([]*decimal.Big) ([]*decimal.Big, int, error)
}
// opsType contains the base information for a list of operations and
// their descriptions. The operations go in a list of interfaces so
// we can also use strings and print them in the help() function.
opsType struct {
base int // Base for printing (default = 10)
debug bool // Debug state
decimals int // How many decimals to use when printing
degmode bool // Degrees mode (default = Radians)
stack *stackType // stack object to use
ops []interface{} // list of ophandlers & descriptions
}
// opmapType is a handler to operation map, used to find the right
// operation function to call.
opmapType map[string]ophandler
)
// radOrDeg converts the value passed to radians if degmode (degrees
// mode) is set. Otherwise, it just returns the same value (radians).
func radOrDeg(ctx decimal.Context, n *decimal.Big, degmode bool) *decimal.Big {
if degmode {
pi := ctx.Pi(big())
npi := big().Mul(n, pi)
return ctx.Quo(big(), npi, bigUint(180))
}
return n
}
func bigToUint64(x *decimal.Big) uint64 {
// Calculate floor(x)
floor, ok := big().Set(x).Uint64()
if !ok {
fmt.Printf(warnMsg("Note: %f truncated to %d (uint64)\n"), x, floor)
}
return floor
}
func newOpsType(ctx decimal.Context, stack *stackType) *opsType {
ret := &opsType{
base: 10,
decimals: 6,
stack: stack,
}
var build string
if Build == "" {
build = "no version info"
} else {
build = "v" + Build
}
ret.ops = []interface{}{
// Header
"BOLD:Online help for " + programTitle + " (" + build + ").",
"BOLD:See http://github.com/marcopaganini/rpn for full details.",
"",
"BOLD:Data entry:",
" number <ENTER> - push a number on top of the stack.",
" operation <ENTER> - perform an operation on the stack (see below).",
"",
" It's also possible to separate multiple operations with space:",
" 10 2 3 * - (result = 4)",
"",
" Prefix numbers with 0x to indicate hexadecimal, 0 for octal.",
"",
"BOLD:Operations:",
"",
"BOLD:Basic Operations",
ophandler{"+", "Add x to y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{big().Add(a[0], a[1])}, 2, nil
}},
ophandler{"-", "Subtract x from y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{big().Sub(a[1], a[0])}, 2, nil
}},
ophandler{"*", "Multiply x and y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{big().Mul(a[0], a[1])}, 2, nil
}},
ophandler{"/", "Divide y by x", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Quo(big(), a[1], a[0])}, 2, nil
}},
ophandler{"chs", "Change signal of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{a[0].Neg(a[0])}, 1, nil
}},
ophandler{"inv", "Invert x (1/x)", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Quo(big(), bigUint(1), a[0])}, 1, nil
}},
ophandler{"^", "Raise y to the power of x", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), a[1], a[0])}, 2, nil
}},
ophandler{"mod", "Calculates y modulo x", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Rem(big(), a[1], a[0])}, 2, nil
}},
ophandler{"sqr", "Calculate square root of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Sqrt(big(), a[0])}, 1, nil
}},
ophandler{"cbr", "Calculate cubic root of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
e := big().Quo(bigFloat("1"), bigFloat("3"))
return []*decimal.Big{ctx.Pow(big(), a[0], e)}, 1, nil
}},
ophandler{"%", "Calculate x% of y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := big().Mul(a[0], a[1])
ctx.Quo(z, z, bigUint(100))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"sum", "Sum all elements in stack", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
sum := big()
for _, v := range a {
sum.Add(sum, v)
}
return []*decimal.Big{sum}, len(a), nil
}},
ophandler{"fac", "Calculate factorial of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Floor(big(), a[0])
if z.Sign() < 0 {
return nil, 1, errors.New("factorial requires a positive number")
}
fact := bigUint(1)
for ix := bigUint(1); ix.Cmp(z) <= 0; ix.Add(ix, bigUint(1)) {
fact.Mul(fact, ix)
}
return []*decimal.Big{fact}, 1, nil
}},
"",
"BOLD:Bitwise Operations",
ophandler{"and", "Logical AND between x and y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x := bigToUint64(a[0])
y := bigToUint64(a[1])
z := x & y
return []*decimal.Big{bigUint(z)}, 2, nil
}},
ophandler{"or", "Logical OR between x and y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x := bigToUint64(a[0])
y := bigToUint64(a[1])
z := x | y
return []*decimal.Big{bigUint(z)}, 2, nil
}},
ophandler{"xor", "Logical XOR between x and y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x := bigToUint64(a[0])
y := bigToUint64(a[1])
z := y ^ x
return []*decimal.Big{bigUint(z)}, 2, nil
}},
ophandler{"lshift", "Shift y left x times", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x := bigToUint64(a[0])
y := bigToUint64(a[1])
z := y << x
return []*decimal.Big{bigUint(z)}, 2, nil
}},
ophandler{"rshift", "Shift y right x times", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x := bigToUint64(a[0])
y := bigToUint64(a[1])
z := y >> x
return []*decimal.Big{bigUint(z)}, 2, nil
}},
"",
"BOLD:Trigonometric and Log Operations",
ophandler{"sin", "Sine of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Sin(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"cos", "Cosine of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Cos(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"tan", "Tangent of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Tan(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"asin", "Arcsine of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Asin(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"acos", "Arccosine of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Acos(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"atan", "Arctangent of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Atan(big(), radOrDeg(ctx, a[0], ret.degmode))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"exp", "Calculate e ^ x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Exp(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},
ophandler{"ln", "Natural logarithm of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Log(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},
ophandler{"log", "Common logarithm of x", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := ctx.Log10(big(), a[0])
return []*decimal.Big{z}, 1, nil
}},
"",
"BOLD:Miscellaneous Operations",
ophandler{"f2c", "Convert x in Fahrenheit to Celsius", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := big()
z.Sub(a[0], bigUint(32))
z.Mul(z, bigUint(5))
z.Quo(z, bigUint(9))
return []*decimal.Big{z}, 1, nil
}},
ophandler{"c2f", "Convert x in Celsius to Fahrenheit", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
z := big()
z.Mul(a[0], bigUint(9))
z.Quo(z, bigUint(5))
z.Add(z, bigUint(32))
return []*decimal.Big{z}, 1, nil
}},
"",
"BOLD:Stack Operations",
ophandler{"p", "Display stack", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
stack.print(ctx, ret.base, ret.decimals)
return nil, 0, nil
}},
ophandler{"c", "Clear stack", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
stack.clear()
return nil, 0, nil
}},
ophandler{"=", "Print top of stack (x)", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
stack.printTop(ctx, ret.base, ret.decimals)
return nil, 0, nil
}},
ophandler{"d", "Drop top of stack (x)", 1, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return nil, 1, nil
}},
ophandler{"dup", "Duplicate top of stack", 1, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
stack.push(a[0])
return nil, 0, nil
}},
ophandler{"x", "Exchange x and y", 2, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{a[0], a[1]}, 2, nil
}},
"",
"BOLD:Math and Physical constants",
ophandler{"PI", "The famous transcedental number", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pi(big())}, 0, nil
}},
ophandler{"E", "Another famous transcedental number", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.E(big())}, 0, nil
}},
ophandler{"C", "Speed of light in vacuum, in m/s", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{bigFloat("299792458")}, 0, nil
}},
ophandler{"MOL", "Avogadro's number", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{bigFloat("6.02214154e23")}, 0, nil
}},
"",
"BOLD:Computer constants",
ophandler{"KB", "Kilobyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(10), bigUint(3))}, 0, nil
}},
ophandler{"MB", "Megabyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(10), bigUint(6))}, 0, nil
}},
ophandler{"GB", "Gigabyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(10), bigUint(9))}, 0, nil
}},
ophandler{"MB", "Terabyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(10), bigUint(12))}, 0, nil
}},
ophandler{"KIB", "Kibibyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(2), bigUint(10))}, 0, nil
}},
ophandler{"MIB", "Mebibyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(2), bigUint(20))}, 0, nil
}},
ophandler{"GIB", "Gibibyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(2), bigUint(30))}, 0, nil
}},
ophandler{"TIB", "Tebibyte", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
return []*decimal.Big{ctx.Pow(big(), bigUint(2), bigUint(40))}, 0, nil
}},
"",
"BOLD:Program Control",
ophandler{"dec", "Output in decimal", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 10
ret.degmode = false
return nil, 0, nil
}},
ophandler{"bin", "Output in binary", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 2
ret.degmode = false
return nil, 0, nil
}},
ophandler{"oct", "Output in octal", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 8
ret.degmode = false
return nil, 0, nil
}},
ophandler{"hex", "Output in hexadecimal", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 16
ret.degmode = false
return nil, 0, nil
}},
ophandler{"deg", "All angles in degrees", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 10
ret.degmode = true
return nil, 0, nil
}},
ophandler{"rad", "All angles in radians", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.base = 10
ret.degmode = false
return nil, 0, nil
}},
ophandler{"fmt", "Change output to X decimals", 0, func(a []*decimal.Big) ([]*decimal.Big, int, error) {
x, ok := a[0].Uint64()
if !ok || !a[0].IsInt() {
return nil, 1, errors.New("precision must be a positive integer")
}
ret.decimals = int(x)
return nil, 1, nil
}},
ophandler{"debug", "Toggle debugging", 0, func(_ []*decimal.Big) ([]*decimal.Big, int, error) {
ret.debug = !ret.debug
fmt.Printf(warnMsg("Debugging state: %v\n"), ret.debug)
return nil, 0, nil
}},
"",
"BOLD:Please Note:",
" - x means the number at the top of the stack",
" - y means the second number from the top of the stack",
}
return ret
}
// operation performs an operation on the stack and returns a slice of elements
// added to the stack and the number of elements removed from the stack.
func operation(handler ophandler, stack *stackType) ([]*decimal.Big, int, error) {
// Make sure we have enough arguments in the list.
length := len(stack.list)
if length < handler.numArgs {
return nil, 0, fmt.Errorf("this operation requires at least %d items in the stack", handler.numArgs)
}
// args contains a copy of all elements in the stack reversed. This makes
// it easier for functions to use x as a[0], y as a[1], etc.
args := []*decimal.Big{}
for ix := length - 1; ix >= 0; ix-- {
args = append(args, stack.list[ix])
}
ret, remove, err := handler.fn(args)
if err != nil {
return nil, 0, err
}
// Remove the number of arguments this operation consumes if needed.
if remove > 0 && len(stack.list) < remove {
return nil, 0, fmt.Errorf("(internal) operation %q wants to pop %d items, but we only have %d", handler.op, remove, len(stack.list))
}
stack.list = stack.list[0 : len(stack.list)-remove]
// Add the return values from the function to the stack if we have any.
if len(ret) > 0 {
stack.push(ret...)
}
return ret, remove, nil
}
// opmap returns a map of op (command) -> ophandler that can be easily used
// later to find the function to be executed. It takes a slice of interfaces
// and returns a map[string][ophandler].
func (x opsType) opmap() opmapType {
ret := map[string]ophandler{}
for _, v := range x.ops {
if h, ok := v.(ophandler); ok {
ret[h.op] = h
}
}
return ret
}
// help displays the help message to the screen based on the contents of opmap.
func (x opsType) help() error {
pager, err := newPager()
if err != nil {
return err
}
if !pager.colorSupport {
color.NoColor = true
}
for _, v := range x.ops {
// ophandler lines.
if handler, ok := v.(ophandler); ok {
fmt.Fprintf(pager.w, " - %s: %s\n", bold(handler.op), handler.desc)
continue
}
// Regular strings.
// Anything starting with "BOLD:" is printed in bold.
if s, ok := v.(string); ok {
if strings.HasPrefix(s, "BOLD:") {
s = bold(s[5:])
}
fmt.Fprintln(pager.w, s)
}
}
// Turn color support back on.
color.NoColor = false
return pager.wait()
}