@@ -17,6 +17,7 @@ use super::objstr::{PyString, PyStringRef};
17
17
use super :: objtype;
18
18
use crate :: obj:: objtype:: PyClassRef ;
19
19
20
+ #[ pyclass( __inside_vm) ]
20
21
#[ derive( Debug ) ]
21
22
pub struct PyInt {
22
23
value : BigInt ,
@@ -95,7 +96,9 @@ impl_try_from_object_int!(
95
96
( u64 , to_u64) ,
96
97
) ;
97
98
99
+ #[ pyimpl( __inside_vm) ]
98
100
impl PyInt {
101
+ #[ pymethod( name = "__eq__" ) ]
99
102
fn eq ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
100
103
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
101
104
vm. ctx . new_bool ( self . value == * get_value ( & other) )
@@ -104,6 +107,7 @@ impl PyInt {
104
107
}
105
108
}
106
109
110
+ #[ pymethod( name = "__ne__" ) ]
107
111
fn ne ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
108
112
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
109
113
vm. ctx . new_bool ( self . value != * get_value ( & other) )
@@ -112,6 +116,7 @@ impl PyInt {
112
116
}
113
117
}
114
118
119
+ #[ pymethod( name = "__lt__" ) ]
115
120
fn lt ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
116
121
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
117
122
vm. ctx . new_bool ( self . value < * get_value ( & other) )
@@ -120,6 +125,7 @@ impl PyInt {
120
125
}
121
126
}
122
127
128
+ #[ pymethod( name = "__le__" ) ]
123
129
fn le ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
124
130
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
125
131
vm. ctx . new_bool ( self . value <= * get_value ( & other) )
@@ -128,6 +134,7 @@ impl PyInt {
128
134
}
129
135
}
130
136
137
+ #[ pymethod( name = "__gt__" ) ]
131
138
fn gt ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
132
139
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
133
140
vm. ctx . new_bool ( self . value > * get_value ( & other) )
@@ -136,6 +143,7 @@ impl PyInt {
136
143
}
137
144
}
138
145
146
+ #[ pymethod( name = "__ge__" ) ]
139
147
fn ge ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
140
148
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
141
149
vm. ctx . new_bool ( self . value >= * get_value ( & other) )
@@ -144,6 +152,7 @@ impl PyInt {
144
152
}
145
153
}
146
154
155
+ #[ pymethod( name = "__add__" ) ]
147
156
fn add ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
148
157
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
149
158
vm. ctx . new_int ( ( & self . value ) + get_value ( & other) )
@@ -152,6 +161,7 @@ impl PyInt {
152
161
}
153
162
}
154
163
164
+ #[ pymethod( name = "__sub__" ) ]
155
165
fn sub ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
156
166
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
157
167
vm. ctx . new_int ( ( & self . value ) - get_value ( & other) )
@@ -160,6 +170,7 @@ impl PyInt {
160
170
}
161
171
}
162
172
173
+ #[ pymethod( name = "__rsub__" ) ]
163
174
fn rsub ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
164
175
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
165
176
vm. ctx . new_int ( get_value ( & other) - ( & self . value ) )
@@ -168,6 +179,7 @@ impl PyInt {
168
179
}
169
180
}
170
181
182
+ #[ pymethod( name = "__mul__" ) ]
171
183
fn mul ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
172
184
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
173
185
vm. ctx . new_int ( ( & self . value ) * get_value ( & other) )
@@ -176,6 +188,7 @@ impl PyInt {
176
188
}
177
189
}
178
190
191
+ #[ pymethod( name = "__truediv__" ) ]
179
192
fn truediv ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
180
193
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
181
194
div_ints ( vm, & self . value , & get_value ( & other) )
@@ -184,6 +197,7 @@ impl PyInt {
184
197
}
185
198
}
186
199
200
+ #[ pymethod( name = "__rtruediv__" ) ]
187
201
fn rtruediv ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
188
202
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
189
203
div_ints ( vm, & get_value ( & other) , & self . value )
@@ -192,6 +206,7 @@ impl PyInt {
192
206
}
193
207
}
194
208
209
+ #[ pymethod( name = "__floordiv__" ) ]
195
210
fn floordiv ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
196
211
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
197
212
let v2 = get_value ( & other) ;
@@ -205,6 +220,7 @@ impl PyInt {
205
220
}
206
221
}
207
222
223
+ #[ pymethod( name = "__lshift__" ) ]
208
224
fn lshift ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
209
225
if !objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
210
226
return Ok ( vm. ctx . not_implemented ( ) ) ;
@@ -224,6 +240,7 @@ impl PyInt {
224
240
}
225
241
}
226
242
243
+ #[ pymethod( name = "__rshift__" ) ]
227
244
fn rshift ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
228
245
if !objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
229
246
return Ok ( vm. ctx . not_implemented ( ) ) ;
@@ -243,6 +260,7 @@ impl PyInt {
243
260
}
244
261
}
245
262
263
+ #[ pymethod( name = "__xor__" ) ]
246
264
fn xor ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
247
265
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
248
266
vm. ctx . new_int ( ( & self . value ) ^ get_value ( & other) )
@@ -251,6 +269,7 @@ impl PyInt {
251
269
}
252
270
}
253
271
272
+ #[ pymethod( name = "__rxor__" ) ]
254
273
fn rxor ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
255
274
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
256
275
vm. ctx . new_int ( get_value ( & other) ^ ( & self . value ) )
@@ -259,6 +278,7 @@ impl PyInt {
259
278
}
260
279
}
261
280
281
+ #[ pymethod( name = "__or__" ) ]
262
282
fn or ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
263
283
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
264
284
vm. ctx . new_int ( ( & self . value ) | get_value ( & other) )
@@ -267,6 +287,7 @@ impl PyInt {
267
287
}
268
288
}
269
289
290
+ #[ pymethod( name = "__and__" ) ]
270
291
fn and ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
271
292
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
272
293
let v2 = get_value ( & other) ;
@@ -276,6 +297,7 @@ impl PyInt {
276
297
}
277
298
}
278
299
300
+ #[ pymethod( name = "__pow__" ) ]
279
301
fn pow ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
280
302
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
281
303
let v2 = get_value ( & other) . to_u32 ( ) . unwrap ( ) ;
@@ -288,6 +310,7 @@ impl PyInt {
288
310
}
289
311
}
290
312
313
+ #[ pymethod( name = "__mod__" ) ]
291
314
fn mod_ ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
292
315
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
293
316
let v2 = get_value ( & other) ;
@@ -301,6 +324,7 @@ impl PyInt {
301
324
}
302
325
}
303
326
327
+ #[ pymethod( name = "__divmod__" ) ]
304
328
fn divmod ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
305
329
if objtype:: isinstance ( & other, & vm. ctx . int_type ( ) ) {
306
330
let v2 = get_value ( & other) ;
@@ -317,20 +341,24 @@ impl PyInt {
317
341
}
318
342
}
319
343
344
+ #[ pymethod( name = "__neg__" ) ]
320
345
fn neg ( & self , _vm : & VirtualMachine ) -> BigInt {
321
346
-( & self . value )
322
347
}
323
348
349
+ #[ pymethod( name = "__hash__" ) ]
324
350
fn hash ( & self , _vm : & VirtualMachine ) -> u64 {
325
351
let mut hasher = std:: collections:: hash_map:: DefaultHasher :: new ( ) ;
326
352
self . value . hash ( & mut hasher) ;
327
353
hasher. finish ( )
328
354
}
329
355
356
+ #[ pymethod( name = "__abs__" ) ]
330
357
fn abs ( & self , _vm : & VirtualMachine ) -> BigInt {
331
358
self . value . abs ( )
332
359
}
333
360
361
+ #[ pymethod( name = "__round__" ) ]
334
362
fn round (
335
363
zelf : PyRef < Self > ,
336
364
_precision : OptionalArg < PyObjectRef > ,
@@ -339,45 +367,55 @@ impl PyInt {
339
367
zelf
340
368
}
341
369
370
+ #[ pymethod( name = "__int__" ) ]
342
371
fn int ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
343
372
zelf
344
373
}
345
374
375
+ #[ pymethod( name = "__pos__" ) ]
346
376
fn pos ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
347
377
zelf
348
378
}
349
379
380
+ #[ pymethod( name = "__float__" ) ]
350
381
fn float ( & self , vm : & VirtualMachine ) -> PyResult < PyFloat > {
351
382
self . value
352
383
. to_f64 ( )
353
384
. map ( PyFloat :: from)
354
385
. ok_or_else ( || vm. new_overflow_error ( "int too large to convert to float" . to_string ( ) ) )
355
386
}
356
387
388
+ #[ pymethod( name = "__trunc__" ) ]
357
389
fn trunc ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
358
390
zelf
359
391
}
360
392
393
+ #[ pymethod( name = "__floor__" ) ]
361
394
fn floor ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
362
395
zelf
363
396
}
364
397
398
+ #[ pymethod( name = "__ceil__" ) ]
365
399
fn ceil ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
366
400
zelf
367
401
}
368
402
403
+ #[ pymethod( name = "__index__" ) ]
369
404
fn index ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
370
405
zelf
371
406
}
372
407
408
+ #[ pymethod( name = "__invert__" ) ]
373
409
fn invert ( & self , _vm : & VirtualMachine ) -> BigInt {
374
410
!( & self . value )
375
411
}
376
412
413
+ #[ pymethod( name = "__repr__" ) ]
377
414
fn repr ( & self , _vm : & VirtualMachine ) -> String {
378
415
self . value . to_string ( )
379
416
}
380
417
418
+ #[ pymethod( name = "__format__" ) ]
381
419
fn format ( & self , spec : PyStringRef , vm : & VirtualMachine ) -> PyResult < String > {
382
420
let format_spec = FormatSpec :: parse ( & spec. value ) ;
383
421
match format_spec. format_int ( & self . value ) {
@@ -386,22 +424,27 @@ impl PyInt {
386
424
}
387
425
}
388
426
427
+ #[ pymethod( name = "__bool__" ) ]
389
428
fn bool ( & self , _vm : & VirtualMachine ) -> bool {
390
429
!self . value . is_zero ( )
391
430
}
392
431
432
+ #[ pymethod]
393
433
fn bit_length ( & self , _vm : & VirtualMachine ) -> usize {
394
434
self . value . bits ( )
395
435
}
396
436
437
+ #[ pymethod]
397
438
fn conjugate ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
398
439
zelf
399
440
}
400
441
442
+ #[ pyproperty]
401
443
fn real ( zelf : PyRef < Self > , _vm : & VirtualMachine ) -> PyIntRef {
402
444
zelf
403
445
}
404
446
447
+ #[ pyproperty]
405
448
fn imag ( & self , _vm : & VirtualMachine ) -> usize {
406
449
0
407
450
}
0 commit comments