-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
303 lines (269 loc) · 5.14 KB
/
main.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"unicode"
"github.com/askeladdk/prattle"
)
const (
number int = 1 + iota
plus
minus
star
slash
caret
modulo
leftPar
rightPar
pi
bang
squareRoot
answer
)
var rune2name = map[rune]int{
'+': plus,
'-': minus,
'*': star,
'/': slash,
'^': caret,
'ˆ': caret,
'%': modulo,
'(': leftPar,
')': rightPar,
'π': pi,
'!': bang,
'√': squareRoot,
}
func scan(s *prattle.Scanner) int {
s.ExpectAny(unicode.IsSpace)
s.Skip()
r := s.Peek()
switch {
case s.Done():
return 0
case s.Expect('a'):
if s.Expect('n') {
if s.Expect('s') {
return answer
}
}
case s.ExpectOne(unicode.IsDigit):
s.ExpectAny(unicode.IsDigit)
if s.Expect('.') {
s.ExpectAny(unicode.IsDigit)
}
return number
case s.Expect('.'):
if s.ExpectOne(unicode.IsDigit) {
s.ExpectAny(unicode.IsDigit)
return number
}
case s.ExpectOne(prattle.OneOf("+-*/^ˆ%()π!√")):
return rune2name[r]
}
s.Advance()
return -1
}
type calculator struct {
stack []float64
answer float64
}
func (c *calculator) pop() (v float64) {
n := len(c.stack)
if n == 0 {
return math.NaN()
}
v, c.stack = c.stack[n-1], c.stack[:n-1]
return
}
func (c *calculator) push(v float64) {
c.stack = append(c.stack, v)
}
func (c *calculator) number(p *prattle.Parser, t prattle.Token) error {
v, err := strconv.ParseFloat(t.Text, 64)
if err != nil {
return err
}
c.push(v)
return nil
}
func (c *calculator) pi(p *prattle.Parser, t prattle.Token) error {
c.push(math.Pi)
return nil
}
func (c *calculator) ans(p *prattle.Parser, t prattle.Token) error {
c.push(c.answer)
return nil
}
func (c *calculator) binop(kind int) {
right := c.pop()
left := c.pop()
var result float64
switch kind {
case plus:
result = left + right
case minus:
result = left - right
case star:
result = left * right
case slash:
result = left / right
case caret:
result = math.Pow(left, right)
case modulo:
result = math.Mod(left, right)
case squareRoot:
result = math.Pow(right, 1/left)
default:
result = math.NaN()
}
c.push(result)
}
func (c *calculator) binopLeftAssoc(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(c.Precedence(t.Kind)); err != nil {
return err
}
c.binop(t.Kind)
return nil
}
func (c *calculator) binopRightAssoc(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(c.Precedence(t.Kind) - 1); err != nil {
return err
}
c.binop(t.Kind)
return nil
}
func (c *calculator) binopNonAssoc(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(c.Precedence(t.Kind)); err != nil {
return err
}
c.binop(t.Kind)
return prattle.NonAssoc
}
func (c *calculator) unary(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(c.Precedence(t.Kind)); err != nil {
return err
}
v := c.pop()
switch t.Kind {
case minus:
c.push(-v)
case squareRoot:
c.push(math.Sqrt(v))
default:
return c.ParseError(t)
}
return nil
}
func (c *calculator) factorial(p *prattle.Parser, t prattle.Token) error {
v := c.pop()
i, f := math.Modf(v)
if f != 0 {
return fmt.Errorf("cannot compute factorial of fractional number '%f'", v)
}
a, b := float64(1), int(i)
for ; b > 0; b-- {
a *= float64(b)
}
c.push(a)
return nil
}
func (c *calculator) paren(p *prattle.Parser, t prattle.Token) error {
if err := p.Parse(1); err != nil {
return err
} else if !p.Expect(rightPar) {
return c.ParseError(t)
}
return nil
}
func (c *calculator) Prefix(kind int) prattle.ParseFunc {
switch kind {
case number:
return c.number
case pi:
return c.pi
case answer:
return c.ans
case minus, squareRoot:
return c.unary
case leftPar:
return c.paren
default:
return nil
}
}
func (c *calculator) Infix(kind int) prattle.ParseFunc {
switch kind {
case plus, minus, star, slash, modulo:
return c.binopLeftAssoc
case caret:
return c.binopRightAssoc
case bang:
return c.factorial
case squareRoot:
return c.binopNonAssoc
default:
return nil
}
}
func (c *calculator) Precedence(kind int) int {
switch kind {
default:
return 0
case leftPar, rightPar:
return 1
case squareRoot:
return 2
case plus, minus:
return 3
case star, slash, modulo:
return 4
case caret:
return 5
case number, pi:
return 6
case bang:
return 7
}
}
func (c *calculator) ParseError(t prattle.Token) error {
if t.Kind == 0 {
return fmt.Errorf("incomplete equation")
}
return fmt.Errorf("i do not understand '%s'", t.Text)
}
func (c *calculator) calculate(expr string) (v float64, err error) {
s := prattle.Scanner{Scan: scan}
p := prattle.Parser{Driver: c}
err = p.Init(s.InitWithString(expr)).Parse(0)
if err == nil && p.Peek().Kind != 0 {
err = p.ParseError(p.Peek())
}
v = c.pop()
return
}
func main() {
fmt.Println("welcome to calculator")
fmt.Println("enter an equation or q to quit")
fmt.Println("enter π for pi, √ for square root")
var calc calculator
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("> ")
scanner.Scan()
text := scanner.Text()
if text == "" {
continue
} else if text == "q" {
break
} else if v, err := calc.calculate(text); err != nil {
fmt.Println(err)
} else {
fmt.Println(v)
calc.answer = v
}
}
}