-
Notifications
You must be signed in to change notification settings - Fork 7
/
constnumber.go
192 lines (168 loc) · 5.07 KB
/
constnumber.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
package eval
import "strconv"
type ConstNumber struct {
Value BigComplex
Type ConstType
}
// Use with token.INT ast.BasicLit
func NewConstInteger(i string) (*ConstNumber, bool) {
z := new(ConstNumber)
z.Type = ConstInt
z.Value.Re.Denom().SetInt64(1)
_, ok := z.Value.Re.Num().SetString(i, 0)
return z, ok
}
// Use with token.FLOAT ast.BasicLit
func NewConstFloat(r string) (*ConstNumber, bool) {
z := new(ConstNumber)
z.Type = ConstFloat
_, ok := z.Value.Re.SetString(r)
return z, ok
}
// Use with token.IMAG ast.BasicLit
func NewConstImag(i string) (*ConstNumber, bool) {
z := new(ConstNumber)
z.Type = ConstComplex
ok := i[len(i)-1] == 'i'
if ok {
_, ok = z.Value.Im.SetString(i[:len(i)-1])
}
return z, ok
}
// Use with token.CHAR ast.BasicLit
func NewConstRune(n rune) *ConstNumber {
z := new(ConstNumber)
z.Type = ConstRune
z.Value.Re.Denom().SetInt64(1)
z.Value.Re.Num().SetInt64(int64(n))
return z
}
func NewConstInt64(i int64) *ConstNumber {
z := new(ConstNumber)
z.Type = ConstInt
z.Value.Re.Denom().SetInt64(1)
z.Value.Re.Num().SetInt64(i)
return z
}
func NewConstUint64(u uint64) *ConstNumber {
z := new(ConstNumber)
z.Type = ConstInt
z.Value.Re.Denom().SetInt64(1)
z.Value.Re.Num().SetUint64(u)
return z
}
func NewConstFloat64(f float64) *ConstNumber {
z := new(ConstNumber)
z.Type = ConstFloat
z.Value.Re.SetFloat64(f)
return z
}
func NewConstComplex128(c complex128) *ConstNumber {
z := new(ConstNumber)
z.Type = ConstComplex
z.Value.Re.SetFloat64(real(c))
z.Value.Im.SetFloat64(imag(c))
return z
}
func (z *ConstNumber) String() string {
return z.StringShow0i(true)
}
func (z *ConstNumber) StringShow0i(show0i bool) string {
if z.Type == ConstRune && z.Value.Re.Num().BitLen() <= 32 {
r, _, _ := z.Value.Int(32)
return strconv.QuoteRuneToASCII(rune(r))
} else if z.Type == ConstComplex {
// Hack to show 0i instead of 0
if z.Value.IsZero() {
return "0i"
} else {
return z.Value.StringShow0i(show0i)
}
} else {
return z.Value.StringShow0i(false)
}
}
// Add two ConstNumbers, promoting the type automatically.
func (z *ConstNumber) Add(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Add(&x.Value, &y.Value)
return z
}
// z.Sub() subtracts two ConstNumbers, promoting the type
// automatically.
func (z *ConstNumber) Sub(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Sub(&x.Value, &y.Value)
return z
}
// z.Mul multiplies two ConstNumbers, promoting the type
// automatically.
func (z *ConstNumber) Mul(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Mul(&x.Value, &y.Value)
return z
}
// z.Quo divides two ConstNumbers, promoting the type
// automatically. If both operands are of ConstInt, then integer
// division is performed.
func (z *ConstNumber) Quo(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
if z.Type.IsIntegral() {
z.Value.Re.Num().Div(x.Value.Re.Num(), y.Value.Re.Num())
} else {
z.Value.Quo(&x.Value, &y.Value)
}
return z
}
// z.Rem computes remainder of two ConstNumbers, promoting the type
// automatically. The result is undefined if both x and y are not
// integral types.
func (z *ConstNumber) Rem(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Re.Num().Rem(x.Value.Re.Num(), y.Value.Re.Num())
return z
}
// z.And compute logical "and" of two ConstNumbers, promoting the type
// automatically. The result is undefined if both x and y are not
// integral types.
func (z *ConstNumber) And(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Re.Num().And(x.Value.Re.Num(), y.Value.Re.Num())
return z
}
// z.Or computes the logical "o"r of two ConstNumbers, promoting the
// type automatically. The result is undefined if both x and y are not
// integral types.
func (z *ConstNumber) Or(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Re.Num().Or(x.Value.Re.Num(), y.Value.Re.Num())
return z
}
// z.Xor computes the exclusive "or" of two ConstNumbers, promoting
// the type automatically. The result is undefined if both x and y are
// not integral types.
func (z *ConstNumber) Xor(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Re.Num().Xor(x.Value.Re.Num(), y.Value.Re.Num())
return z
}
// z.AndNot computes "and not" of two ConstNumbers, promoting the type
// automatically. The result is undefined if both x and y are not
// integral types.
func (z *ConstNumber) AndNot(x, y *ConstNumber) *ConstNumber {
z.Type = promoteConstNumbers(x.Type, y.Type)
z.Value.Re.Num().AndNot(x.Value.Re.Num(), y.Value.Re.Num())
return z
}
// z.Shl() shifts x left by count. Type is set to ConstShiftedInt
func (z *ConstNumber) Lsh(x *ConstNumber, count uint) *ConstNumber {
z.Type = ConstShiftedInt
z.Value.Lsh(&x.Value, count)
return z
}
// z.Shl() shifts x right by count. Type is set to ConstShiftedInt
func (z *ConstNumber) Rsh(x *ConstNumber, count uint) *ConstNumber {
z.Type = ConstShiftedInt
z.Value.Rsh(&x.Value, count)
return z
}