/
TC_func_stdlib_math.go
45 lines (41 loc) · 1012 Bytes
/
TC_func_stdlib_math.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
package ThreadComputation
import (
"fmt"
"errors"
"math"
"github.com/gammazero/deque"
)
func TCSimpleMathFunction(l *TCExecListener, name string, q *deque.Deque) (interface{}, error) {
var acc interface{}
if q.Len() == 0 {
return math.NaN(), nil
}
acc = q.PopFront()
if q.Len() == 0 {
return acc, nil
}
for q.Len() > 0 {
e := q.PopFront()
fun := GetMathCallback(acc, e)
if fun == nil {
return nil, errors.New(fmt.Sprintf("Operator non-existing for %T %v %T", acc, name, e))
}
res := fun(name, acc, e)
switch res.(type) {
case *TCError:
l.TC.RegisterError(res.(*TCError))
return nil, res.(*TCError).Err
}
if res == nil {
return nil, errors.New(fmt.Sprintf("%T %v %T returns nil", acc, name, e))
}
acc = res
}
return acc, nil
}
func init() {
SetFunction("+", TCSimpleMathFunction)
SetFunction("*", TCSimpleMathFunction)
SetFunction("/", TCSimpleMathFunction)
SetFunction("-", TCSimpleMathFunction)
}