-
Notifications
You must be signed in to change notification settings - Fork 9
/
number.go
343 lines (298 loc) · 10.2 KB
/
number.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
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
import "C"
import "unsafe"
// NumberProtocol is a 0-sized type that can be embedded in concrete types after
// the AbstractObject to provide access to the suite of methods that Python
// calls the "Number Protocol".
type NumberProtocol struct{}
// Number is an interface that defines the Python "Number Protocol".
type Number interface {
Object
Add(obj Object) (Object, error)
Subtract(obj Object) (Object, error)
Multiply(obj Object) (Object, error)
FloorDivide(obj Object) (Object, error)
TrueDivide(obj Object) (Object, error)
Remainder(obj Object) (Object, error)
Divmod(obj Object) (Object, error)
Power(obj1, obj2 Object) (Object, error)
Negative() (Object, error)
Positive() (Object, error)
Absolute() (Object, error)
Invert() (Object, error)
Lshift(obj Object) (Object, error)
Rshift(obj Object) (Object, error)
And(obj Object) (Object, error)
Xor(obj Object) (Object, error)
Or(obj Object) (Object, error)
InPlaceAdd(obj Object) (Object, error)
InPlaceSubtract(obj Object) (Object, error)
InPlaceMultiply(obj Object) (Object, error)
InPlaceFloorDivide(obj Object) (Object, error)
InPlaceTrueDivide(obj Object) (Object, error)
InPlaceRemainder(obj Object) (Object, error)
InPlacePower(obj1, obj2 Object) (Object, error)
InPlaceLshift(obj Object) (Object, error)
InPlaceRshift(obj Object) (Object, error)
InPlaceAnd(obj Object) (Object, error)
InPlaceXor(obj Object) (Object, error)
InPlaceOr(obj Object) (Object, error)
}
// number is a concrete realisation of the Number Protocol. A type that
// implements the "Number Protocol" but doesn't embed NumberProtocol can be
// turned into a Number by calling AsNumber.
type number struct {
AbstractObject
NumberProtocol
o C.PyObject
}
func cnp(n *NumberProtocol) *C.PyObject {
return (*C.PyObject)(unsafe.Pointer(n))
}
// AsNumber returns a struct pointer that satisfies the Number interface. It
// will refer to the same underlying object as obj. If obj doesn't implement
// the "Number Protocol", then nil is returned.
func AsNumber(obj Object) Number {
if C.PyNumber_Check(c(obj)) != 1 {
return nil
}
if n, ok := obj.(Number); ok {
return n
}
return (*number)(unsafe.Pointer(obj.Base()))
}
// Add returns the result of adding n and obj. The equivalent Python is "n +
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Add(obj Object) (Object, error) {
ret := C.PyNumber_Add(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Subtract returns the result of subtracting obj from n. The equivalent Python
// is "n - obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Subtract(obj Object) (Object, error) {
ret := C.PyNumber_Subtract(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Multiply returns the result of multiplying n by obj. The equivalent Python
// is "n * obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Multiply(obj Object) (Object, error) {
ret := C.PyNumber_Multiply(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// FloorDivide returns the floor of dividing n obj obj.
//
// Return value: New Reference.
func (n *NumberProtocol) FloorDivide(obj Object) (Object, error) {
ret := C.PyNumber_FloorDivide(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// TrueDivide returns the ... TODO
//
// Return value: New Reference.
func (n *NumberProtocol) TrueDivide(obj Object) (Object, error) {
ret := C.PyNumber_TrueDivide(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Remainder returns the remainder of dividing n by obj. The equivalent Python
// is "n % obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Remainder(obj Object) (Object, error) {
ret := C.PyNumber_Remainder(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Divmod returns the result of the Python "divmod(n, obj)".
//
// Return value: New Reference.
func (n *NumberProtocol) Divmod(obj Object) (Object, error) {
ret := C.PyNumber_Divmod(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Power returns the result of the Python "pow(n, obj1, obj2)".
//
// Return value: New Reference.
func (n *NumberProtocol) Power(obj1, obj2 Object) (Object, error) {
ret := C.PyNumber_Power(cnp(n), c(obj1), c(obj2))
return obj2ObjErr(ret)
}
// Negative returns the negation of n. The equivalent Python is "-n".
//
// Return value: New Reference.
func (n *NumberProtocol) Negative() (Object, error) {
ret := C.PyNumber_Negative(cnp(n))
return obj2ObjErr(ret)
}
// Positive returns the positive of n. The equivalent Python is "+n".
//
// Return value: New Reference.
func (n *NumberProtocol) Positive() (Object, error) {
ret := C.PyNumber_Positive(cnp(n))
return obj2ObjErr(ret)
}
// Absolute returns the absolute value of n. The equivalent Python is "abs(n)".
//
// Return value: New Reference.
func (n *NumberProtocol) Absolute() (Object, error) {
ret := C.PyNumber_Absolute(cnp(n))
return obj2ObjErr(ret)
}
// Invert returns the bitwise negation of n. The equivalent Python is "-n".
//
// Return value: New Reference.
func (n *NumberProtocol) Invert() (Object, error) {
ret := C.PyNumber_Invert(cnp(n))
return obj2ObjErr(ret)
}
// Lshift returns the result of left shifting n by obj. The equivalent Python
// is "n << obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Lshift(obj Object) (Object, error) {
ret := C.PyNumber_Lshift(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Rshift returns the result of right shifting n by obj. The equivalent Python
// is "n << obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Rshift(obj Object) (Object, error) {
ret := C.PyNumber_Rshift(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// And returns the bitwise and of n and obj. The equivalent Python is "n &
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) And(obj Object) (Object, error) {
ret := C.PyNumber_And(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Xor returns the bitwise xor of n and obj. The equivalent Python is "n ^
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Xor(obj Object) (Object, error) {
ret := C.PyNumber_Xor(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// Or returns the bitwise or of n and obj. The equivalent Python is "n | obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Or(obj Object) (Object, error) {
ret := C.PyNumber_Or(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceAdd returns the result of adding n and obj. This is done in place if
// supported by n. The equivalent Python is "n += obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceAdd(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceAdd(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceSubtract returns the result of subtracting obj from n. This is done
// in place if supported by n. The equivalent Python is "n -= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceSubtract(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceSubtract(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceMultiply returns the result of multiplying n by obj. This is done in
// place if supported by n. The equivalent Python is "n *= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceMultiply(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceMultiply(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// TODO returns the ...
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceFloorDivide(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceFloorDivide(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// TODO returns the ...
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceTrueDivide(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceTrueDivide(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceRemainder returns the remainder of n divided by obj. This is done in
// place if supported by n. The equivalent Python is "n %= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceRemainder(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceRemainder(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlacePower returns the result of the Python "pow(n, obj1, obj2)". This is
// done in place if supported by n. If obj2 is None, then the Python "n **=
// obj" is also equivalent, if obj2 is not None, there is no equivalent in
// Python.
//
// Return value: New Reference.
func (n *NumberProtocol) InPlacePower(obj1, obj2 Object) (Object, error) {
ret := C.PyNumber_InPlacePower(cnp(n), c(obj1), c(obj2))
return obj2ObjErr(ret)
}
// InPlaceLshift returns the result of left shifting n by obj. This is done in
// place if supported by n. The equivalent Python is "n <<= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceLshift(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceLshift(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceRshift returns the result of right shifting n by obj. This is done in
// place if supported by n. The equivalent Python is "n >>= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceRshift(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceRshift(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceAnd returns the bitwise and of n and obj. This is done in place if
// supported by n. The equivalent Python is "n &= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceAnd(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceAnd(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceXor returns the bitwise xor of n and obj. This is done in place if
// supported by n. The equivalent Python is "n ^= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceXor(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceXor(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// InPlaceOr returns the bitwise or of n and obj. This is done in place if
// supported by n. The equivalent Python is "n |= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceOr(obj Object) (Object, error) {
ret := C.PyNumber_InPlaceOr(cnp(n), c(obj))
return obj2ObjErr(ret)
}
// PyNumber_Coerce: TODO
// PyNumber_CoerceEx: TODO
// PyNumber_Int: TODO
// PyNumber_Long: TODO
// PyNumber_Float: TODO
// PyNumber_Index: TODO
// PyNumber_ToBase: TODO
// PyNumber_AsSsize_t: TODO