-
Notifications
You must be signed in to change notification settings - Fork 17
/
evaler.go
385 lines (341 loc) · 9.67 KB
/
evaler.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
// Package evaler implements a simple fp arithmetic expression evaluator.
//
// See README.md for documentation.
package evaler
import (
"fmt"
"math"
"math/big"
"regexp"
"strconv"
"strings"
"github.com/soniah/evaler/stack"
)
// all regex's here
var fp_rx = regexp.MustCompile(`(\d+(?:\.\d+)?)`) // simple fp number
var functions_rx = regexp.MustCompile(`(sin|cos|tan|ln|arcsin|arccos|arctan|sqrt)`)
var symbols_rx *regexp.Regexp // TODO used as a mutable global variable!!
var unary_minus_rx = regexp.MustCompile(`((?:^|[-+^%*/<>!=(])\s*)-`)
var whitespace_rx = regexp.MustCompile(`\s+`)
var symbolTable map[string]string // TODO used as a mutable global variable!!
// Operator '@' means unary minus
var operators = []string{"-", "+", "*", "/", "<", ">", "@", "^", "**", "%", "!=", "==", ">=", "<="}
// prec returns the operator's precedence
func prec(op string) (result int) {
if op == "-" || op == "+" {
result = 1
} else if op == "*" || op == "/" {
result = 2
} else if op == "^" || op == "%" || op == "**" {
result = 3
} else if op == "@" {
result = 4
} else if functions_rx.MatchString(op) {
result = 5
} else {
result = 0
}
return
}
// opGTE returns true if op1's precedence is >= op2
func opGTE(op1, op2 string) bool {
return prec(op1) >= prec(op2)
}
func isFunction(token string) bool {
return functions_rx.MatchString(token)
}
// isOperator returns true if token is an operator
func isOperator(token string) bool {
for _, v := range operators {
if v == token {
return true
}
}
return false
}
// isOperand returns true if token is an operand
func isOperand(token string) bool {
return fp_rx.MatchString(token)
}
func isSymbol(token string) bool {
for k := range symbolTable {
if k == token {
return true
}
}
return false
}
// convert2postfix converts an infix expression to postfix
func convert2postfix(tokens []string) []string {
var stack stack.Stack
var result []string
for _, token := range tokens {
stackString := fmt.Sprint(stack) // HACK - debugging
stackString += "" // HACK - debugging
if isOperator(token) {
OPERATOR:
for {
top, err := stack.Top()
if err == nil && top != "(" {
if opGTE(top.(string), token) {
pop, _ := stack.Pop()
result = append(result, pop.(string))
continue
} else {
break OPERATOR
}
}
break OPERATOR
}
stack.Push(token)
} else if isFunction(token) {
FUNCTION:
for {
top, err := stack.Top()
if err == nil && top != "(" {
if opGTE(top.(string), token) {
pop, _ := stack.Pop()
result = append(result, pop.(string))
}
} else {
break FUNCTION
}
break FUNCTION
}
stack.Push(token)
} else if token == "(" {
stack.Push(token)
} else if token == ")" {
PAREN:
for {
top, err := stack.Top()
if err == nil && top != "(" {
pop, _ := stack.Pop()
result = append(result, pop.(string))
} else {
stack.Pop() // pop off "("
break PAREN
}
}
} else if isOperand(token) {
result = append(result, token)
} else if isSymbol(token) {
result = append(result, symbolTable[token])
} else {
result = append(result, token)
}
}
for !stack.IsEmpty() {
pop, _ := stack.Pop()
result = append(result, pop.(string))
}
return result
}
// evaluatePostfix takes a postfix expression and evaluates it
func evaluatePostfix(postfix []string) (*big.Rat, error) {
var stack stack.Stack
result := new(big.Rat) // note: a new(big.Rat) has value "0/1" ie zero
for _, token := range postfix {
stackString := fmt.Sprint(stack) // HACK - debugging
stackString += "" // HACK - debugging
if isOperand(token) {
bigrat := new(big.Rat)
if _, err := fmt.Sscan(token, bigrat); err != nil {
return nil, fmt.Errorf("unable to scan %s", token)
}
stack.Push(bigrat)
} else if isOperator(token) {
op2, err2 := stack.Pop()
if err2 != nil {
return nil, err2
}
var op1 interface{}
if token != "@" {
var err1 error
if op1, err1 = stack.Pop(); err1 != nil {
return nil, err1
}
}
dummy := new(big.Rat)
switch token {
case "**", "^":
float1 := BigratToFloat(op1.(*big.Rat))
float2 := BigratToFloat(op2.(*big.Rat))
float_result := math.Pow(float1, float2)
stack.Push(FloatToBigrat(float_result))
case "%":
float1 := BigratToFloat(op1.(*big.Rat))
float2 := BigratToFloat(op2.(*big.Rat))
float_result := math.Mod(float1, float2)
stack.Push(FloatToBigrat(float_result))
case "*":
result := dummy.Mul(op1.(*big.Rat), op2.(*big.Rat))
stack.Push(result)
case "/":
result := dummy.Quo(op1.(*big.Rat), op2.(*big.Rat))
stack.Push(result)
case "+":
result = dummy.Add(op1.(*big.Rat), op2.(*big.Rat))
stack.Push(result)
case "-":
result = dummy.Sub(op1.(*big.Rat), op2.(*big.Rat))
stack.Push(result)
case "<":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) <= -1 {
stack.Push(big.NewRat(1, 1))
} else {
stack.Push(new(big.Rat))
}
case "<=":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) <= 0 {
stack.Push(big.NewRat(1, 1))
} else {
stack.Push(new(big.Rat))
}
case ">":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) >= 1 {
stack.Push(big.NewRat(1, 1))
} else {
stack.Push(new(big.Rat))
}
case ">=":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) >= 0 {
stack.Push(big.NewRat(1, 1))
} else {
stack.Push(new(big.Rat))
}
case "==":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) == 0 {
stack.Push(big.NewRat(1, 1))
} else {
stack.Push(new(big.Rat))
}
case "!=":
if op1.(*big.Rat).Cmp(op2.(*big.Rat)) == 0 {
stack.Push(new(big.Rat))
} else {
stack.Push(big.NewRat(1, 1))
}
case "@":
result := dummy.Mul(big.NewRat(-1, 1), op2.(*big.Rat))
stack.Push(result)
}
} else if isFunction(token) {
op2, err := stack.Pop()
if err != nil {
return nil, err
}
switch token {
case "sin":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Sin(float_result)))
case "cos":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Cos(float_result)))
case "tan":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Tan(float_result)))
case "arcsin":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Asin(float_result)))
case "arccos":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Acos(float_result)))
case "arctan":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Atan(float_result)))
case "ln":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Log(float_result)))
case "sqrt":
float_result := BigratToFloat(op2.(*big.Rat))
stack.Push(FloatToBigrat(math.Sqrt(float_result)))
}
} else {
return nil, fmt.Errorf("unknown token %v", token)
}
}
retval, err := stack.Pop()
if err != nil {
return nil, err
}
return retval.(*big.Rat), nil
}
// Tokenise takes an expr string and converts it to a slice of tokens
//
// Tokenise puts spaces around all non-numbers, removes leading and
// trailing spaces, then splits on spaces
//
func Tokenise(expr string) []string {
spaced := unary_minus_rx.ReplaceAllString(expr, "$1 @")
spaced = fp_rx.ReplaceAllString(spaced, " ${1} ")
spaced = functions_rx.ReplaceAllString(spaced, " ${1} ")
if symbols_rx != nil {
spaced = symbols_rx.ReplaceAllString(spaced, " ${1} ")
}
symbols := []string{"(", ")"}
for _, symbol := range symbols {
spaced = strings.Replace(spaced, symbol, fmt.Sprintf(" %s ", symbol), -1)
}
stripped := whitespace_rx.ReplaceAllString(strings.TrimSpace(spaced), "|")
result := strings.Split(stripped, "|")
return result
}
// Eval takes an infix string arithmetic expression, and evaluates it
//
// Usage:
// result, err := evaler.Eval("1+2")
// Returns: the result of the evaluation, and any errors
//
func Eval(expr string) (result *big.Rat, err error) {
defer func() {
if e := recover(); e != nil {
result = nil
err = fmt.Errorf("Invalid Expression: %s", expr)
}
}()
tokens := Tokenise(expr)
postfix := convert2postfix(tokens)
return evaluatePostfix(postfix)
}
// EvalWithVariables allows variables to be passed into expressions, for
// example evaluate "x + 1" where x=5
func EvalWithVariables(expr string, variables map[string]string) (result *big.Rat, err error) {
symbolTable = variables
s := ""
for k := range symbolTable {
s += k
}
symbols_rx = regexp.MustCompile(fmt.Sprintf("(%s)", s))
return Eval(expr)
}
// BigratToInt converts a *big.Rat to an int64 (with truncation); it
// returns an error for integer overflows.
func BigratToInt(bigrat *big.Rat) (int64, error) {
float_string := bigrat.FloatString(0)
return strconv.ParseInt(float_string, 10, 64)
}
// BigratToInt converts a *big.Rat to a *big.Int (with truncation)
func BigratToBigint(bigrat *big.Rat) *big.Int {
int_string := bigrat.FloatString(0)
bigint := new(big.Int)
// no error scenario could be imagined in testing, so discard err
fmt.Sscan(int_string, bigint)
return bigint
}
// BigratToFloat converts a *big.Rat to a float64 (with loss of
// precision).
func BigratToFloat(bigrat *big.Rat) float64 {
float_string := bigrat.FloatString(10) // arbitrary largish precision
// no error scenario could be imagined in testing, so discard err
float, _ := strconv.ParseFloat(float_string, 64)
return float
}
// FloatToBigrat converts a float64 to a *big.Rat.
func FloatToBigrat(float float64) *big.Rat {
float_string := fmt.Sprintf("%g", float)
bigrat := new(big.Rat)
// no error scenario could be imagined in testing, so discard err
fmt.Sscan(float_string, bigrat)
return bigrat
}