-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoperators.go
157 lines (141 loc) · 4.02 KB
/
operators.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
// Copyright 2016 Steven Oud. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package mathcat
import (
"errors"
"fmt"
"math"
"math/big"
)
type association int
type operator struct {
prec int
assoc association
unary bool
}
const (
AssocLeft association = iota
AssocRight
)
var ErrDivisionByZero = errors.New("Division by zero")
var operators = map[TokenType]operator{
// Assignment operators
Eq: {0, AssocRight, false}, // =
AddEq: {0, AssocRight, false}, // +=
SubEq: {0, AssocRight, false}, // -=
DivEq: {0, AssocRight, false}, // /=
MulEq: {0, AssocRight, false}, // *=
PowEq: {0, AssocRight, false}, // **=
RemEq: {0, AssocRight, false}, // %=
AndEq: {0, AssocRight, false}, // &=
OrEq: {0, AssocRight, false}, // |=
XorEq: {0, AssocRight, false}, // ^=
LshEq: {0, AssocRight, false}, // <<=
RshEq: {0, AssocRight, false}, // >>=
// Relational operators
EqEq: {1, AssocRight, false}, // ==
NotEq: {1, AssocRight, false}, // !=
Gt: {1, AssocRight, false}, // >
GtEq: {1, AssocRight, false}, // >=
Lt: {1, AssocRight, false}, // <
LtEq: {1, AssocRight, false}, // <=
// Bitwise operators
Or: {2, AssocRight, false}, // |
Xor: {3, AssocRight, false}, // ^
And: {4, AssocRight, false}, // &
Lsh: {5, AssocRight, false}, // <<
Rsh: {5, AssocRight, false}, // >>
Not: {9, AssocLeft, true}, // ~
// Mathematical operators
Add: {6, AssocLeft, false}, // +
Sub: {6, AssocLeft, false}, // -
Mul: {7, AssocLeft, false}, // *
Div: {7, AssocLeft, false}, // /
Pow: {8, AssocLeft, false}, // **
Rem: {7, AssocLeft, false}, // %
UnaryMin: {10, AssocLeft, true}, // -
}
// Determine if operator 1 has higher precedence than operator 2
func (o1 operator) hasHigherPrecThan(o2 operator) bool {
return (o2.assoc == AssocLeft && o2.prec <= o1.prec) ||
(o2.assoc == AssocRight && o2.prec < o1.prec)
}
// Execute a binary or unary expression
func executeExpression(operator *Token, lhs, rhs *big.Rat) (*big.Rat, error) {
result := new(big.Rat)
// Both lhs and rhs have to be integers for bitwise operations
if operator.IsBitwise() {
if (lhs == nil && !rhs.IsInt()) || (lhs != nil && (!rhs.IsInt() || !lhs.IsInt())) {
return nil, fmt.Errorf("Expecting integers for ‘%s’", operator)
}
}
switch operator.Type {
case Add, AddEq:
result.Add(lhs, rhs)
case Sub, SubEq:
result.Sub(lhs, rhs)
case UnaryMin:
result.Neg(rhs)
case Div, DivEq:
if rhs.Sign() == 0 {
return nil, ErrDivisionByZero
}
result.Quo(lhs, rhs)
case Mul, MulEq:
result.Mul(lhs, rhs)
case Pow, PowEq:
if lhs.IsInt() && rhs.IsInt() {
intResult := new(big.Int)
intResult.Set(lhs.Num())
intResult.Exp(intResult, rhs.Num(), nil)
result.SetInt(intResult)
} else {
lhsFloat, _ := lhs.Float64()
rhsFloat, _ := rhs.Float64()
result.SetFloat64(math.Pow(lhsFloat, rhsFloat))
}
case Rem, RemEq:
if rhs.Sign() == 0 {
return nil, ErrDivisionByZero
}
result.Set(Mod(lhs, rhs))
case And, AndEq:
result.SetInt(new(big.Int).And(lhs.Num(), rhs.Num()))
case Or, OrEq:
result.SetInt(new(big.Int).Or(lhs.Num(), rhs.Num()))
case Xor, XorEq:
result.SetInt(new(big.Int).Xor(lhs.Num(), rhs.Num()))
case Lsh, LshEq:
shift := uint(rhs.Num().Uint64())
result.SetInt(new(big.Int).Lsh(lhs.Num(), shift))
case Rsh, RshEq:
shift := uint(rhs.Num().Uint64())
result.SetInt(new(big.Int).Rsh(lhs.Num(), shift))
case Not:
result.SetInt(new(big.Int).Not(rhs.Num()))
case Eq:
result = rhs
case EqEq:
result = boolToRat(lhs.Cmp(rhs) == 0)
case NotEq:
result = boolToRat(lhs.Cmp(rhs) != 0)
case Gt:
result = boolToRat(lhs.Cmp(rhs) == 1)
case GtEq:
result = boolToRat(lhs.Cmp(rhs) == 1 || lhs.Cmp(rhs) == 0)
case Lt:
result = boolToRat(lhs.Cmp(rhs) == -1)
case LtEq:
result = boolToRat(lhs.Cmp(rhs) == -1 || lhs.Cmp(rhs) == 0)
default:
return nil, fmt.Errorf("Invalid operator ‘%s’", operator)
}
return result, nil
}
func boolToRat(b bool) *big.Rat {
if b {
return RatTrue
}
return RatFalse
}