-
Notifications
You must be signed in to change notification settings - Fork 5
/
calculator.go
149 lines (144 loc) · 3.29 KB
/
calculator.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
package calculator
import (
"fmt"
"math"
)
var functions = map[string]interface{}{
"abs": math.Abs,
"acos": math.Acos,
"acosh": math.Acosh,
"asin": math.Asin,
"asinh": math.Asinh,
"atan": math.Atan,
"atan2": math.Atan2,
"atanh": math.Atanh,
"cbrt": math.Cbrt,
"ceil": math.Ceil,
"copysign": math.Copysign,
"cos": math.Cos,
"cosh": math.Cosh,
"dim": math.Dim,
"erf": math.Erf,
"erfc": math.Erfc,
"erfcinv": math.Erfcinv, // Go 1.10+
"erfinv": math.Erfinv, // Go 1.10+
"exp": math.Exp,
"exp2": math.Exp2,
"expm1": math.Expm1,
"fma": math.FMA, // Go 1.14+
"floor": math.Floor,
"gamma": math.Gamma,
"hypot": math.Hypot,
"j0": math.J0,
"j1": math.J1,
"log": math.Log,
"log10": math.Log10,
"log1p": math.Log1p,
"log2": math.Log2,
"logb": math.Logb,
"max": math.Max,
"min": math.Min,
"mod": math.Mod,
"nan": math.NaN,
"nextafter": math.Nextafter,
"pow": math.Pow,
"remainder": math.Remainder,
"round": math.Round, // Go 1.10+
"roundtoeven": math.RoundToEven, // Go 1.10+
"sin": math.Sin,
"sinh": math.Sinh,
"sqrt": math.Sqrt,
"tan": math.Tan,
"tanh": math.Tanh,
"trunc": math.Trunc,
"y0": math.Y0,
"y1": math.Y1,
}
func call(funcName string, args []float64) (float64, error) {
f, ok := functions[funcName]
if !ok {
return 0, fmt.Errorf("unknown function %s", funcName)
}
switch f := f.(type) {
case func() float64:
return f(), nil
case func(float64) float64:
return f(args[0]), nil
case func(float64, float64) float64:
return f(args[0], args[1]), nil
case func(float64, float64, float64) float64:
return f(args[0], args[1], args[2]), nil
default:
return 0, fmt.Errorf("invalid function %s", funcName)
}
}
func calculate(n *node) (float64, error) {
switch n.kind {
case addNode:
left, err := calculate(n.left)
if err != nil {
return 0, err
}
right, err := calculate(n.right)
if err != nil {
return 0, err
}
return left + right, nil
case subNode:
left, err := calculate(n.left)
if err != nil {
return 0, err
}
right, err := calculate(n.right)
if err != nil {
return 0, err
}
return left - right, nil
case mulNode:
left, err := calculate(n.left)
if err != nil {
return 0, err
}
right, err := calculate(n.right)
if err != nil {
return 0, err
}
return left * right, nil
case divNode:
left, err := calculate(n.left)
if err != nil {
return 0, err
}
right, err := calculate(n.right)
if err != nil {
return 0, err
}
return left / right, nil
case numNode:
return n.val, nil
case funcNode:
args := []float64{}
for _, arg := range n.args {
val, err := calculate(arg)
if err != nil {
return 0, err
}
args = append(args, val)
}
return call(n.funcName, args)
}
return 0, fmt.Errorf("unknown node type: %s", n.kind)
}
// Calculate calculates expr
func Calculate(expr string) (float64, error) {
tokens, err := tokenize(expr)
if err != nil {
return 0, err
}
p := newParser(tokens)
n, err := p.parse()
if err != nil {
return 0, err
}
return calculate(n)
}