Permalink
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
May 19, 2017
Dec 18, 2015
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Dec 7, 2017
Jan 25, 2017
Jan 13, 2017
Jan 13, 2017
Jan 13, 2017
Jan 13, 2017
Jan 13, 2017
Jan 13, 2017
Newer
100644
695 lines (590 sloc)
21.1 KB
6
7
function $err(op,other){
8
var msg = "unsupported operand type(s) for "+op
9
msg += ": 'int' and '"+$B.get_class(other).__name__+"'"
14
var int = {__class__: $B.$type,
15
__name__: 'int',
16
__dir__: object.__dir__,
17
$is_class: true,
18
$native: true,
19
$descriptors: {
20
'numerator': true,
21
'denominator': true,
22
'imag': true,
23
'real': true
24
}
29
{bytes:null, byteorder:null, signed:null}, ['bytes', 'byteorder', 'signed'],
30
arguments, {signed:False}, null, null)
35
var _bytes, _len
36
if (isinstance(x, [_b_.list, _b_.tuple])) {
37
_bytes=x
38
_len=len(x)
43
_b_.TypeError.$factory("Error! " + _b_.type(x) + " is not supported in int.from_bytes. fix me!")
46
switch(byteorder) {
47
case 'big':
48
var num = _bytes[_len - 1];
49
var _mult=256
50
for (var i = (_len - 2); i >= 0; i--) {
51
// For operations, use the functions that can take or return
52
// big integers
53
num = $B.add($B.mul(_mult, _bytes[i]), num)
54
_mult = $B.mul(_mult,256)
59
case 'little':
60
var num = _bytes[0]
61
if (num >= 128) num = num - 256
62
var _mult=256
63
for (var i = 1; i < _len; i++) {
95
// compare object "self" to class "int"
96
if(other===undefined) return self===int
97
if(isinstance(other,int)) return self.valueOf()==other.valueOf()
113
function preformat(self, fmt){
114
if(fmt.empty){return _b_.str(self)}
115
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type)==-1){
120
switch(fmt.type){
121
case undefined:
122
case 'd':
123
return self.toString()
124
case 'b':
144
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type)!=-1){
145
// Call __format__ on float(self)
175
}
176
if(hasattr(other,'__rfloordiv__')){
177
return getattr(other,'__rfloordiv__')(self)
178
}
179
$err("//",other)
180
}
181
184
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
195
if(value===undefined){value=0}
196
self.toString = function(){return value}
197
//self.valueOf = function(){return value}
208
return int.$factory($B.LongInt.$dict.__lshift__($B.LongInt(self), $B.LongInt(other)))
209
}
210
var rlshift = getattr(other, '__rlshift__', null)
211
if(rlshift!==null){return rlshift(self)}
212
$err('<<', other)
213
}
214
216
// can't use Javascript % because it works differently for negative numbers
217
if(isinstance(other,_b_.tuple) && other.length==1) other=other[0]
218
if(isinstance(other,[int, _b_.float, bool])){
219
if(other===false){other=0}else if(other===true){other=1}
223
}
224
if(hasattr(other,'__rmod__')) return getattr(other,'__rmod__')(self)
225
$err('%',other)
226
}
227
233
234
// this will be quick check, so lets do it early.
235
if(typeof other==="string") {
236
return other.repeat(val)
237
}
238
239
if(isinstance(other,int)){
240
var res = self*other
241
if(res>$B.min_int && res<$B.max_int){return res}
253
return $B.make_complex(int.__mul__(self, other.$real),
254
int.__mul__(self, other.$imag))
255
}
256
if(isinstance(other,[_b_.list,_b_.tuple])){
257
var res = []
258
// make temporary copy of list
259
var $temp = other.slice(0,other.length)
260
for(var i=0;i<val;i++) res=res.concat($temp)
261
if(isinstance(other,_b_.tuple)) res=_b_.tuple(res)
262
return res
263
}
264
if(hasattr(other,'__rmul__')) return getattr(other,'__rmul__')(self)
265
$err("*",other)
266
}
267
273
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
287
if(z !== undefined && z !== null){
288
// If z is provided, the algorithm is faster than computing
289
// self ** other then applying the modulo z
290
var res = (self % z + z) % z
291
for(var i=1; i<other; i++){
292
res *= self
293
res = (res % z + z) % z
294
}
295
return res
296
}
306
if(self>=0){return new Number(Math.pow(self, other.valueOf()))}
307
else{
308
// use complex power
315
}
316
if(hasattr(other,'__rpow__')) return getattr(other,'__rpow__')(self)
317
$err("**",other)
318
}
319
328
return int.$factory($B.LongInt.$dict.__rshift__($B.LongInt(self),
329
$B.LongInt(other)))
330
}
331
var rrshift = getattr(other, '__rrshift__', null)
332
if(rrshift!==null){return rrshift(self)}
333
$err('>>', other)
334
}
341
throw _b_.AttributeError.$factory("'int' object attribute '"+attr+"' is read-only")
354
if(other.__class__==$B.LongInt.$dict){return new Number(self/parseInt(other.value))}
365
}
366
if(hasattr(other,'__rtruediv__')) return getattr(other,'__rtruediv__')(self)
367
$err("/",other)
368
}
369
373
s = bin(self)
374
s = getattr(s,'lstrip')('-0b') // remove leading zeros and minus sign
375
return s.length // len('100101') --> 6
376
}
377
379
int.numerator = function(self){return self}
380
int.denominator = function(self){return int.$factory(1)}
381
int.imag = function(self){return int.$factory(0)}
382
int.real = function(self){return self}
391
if(other.__class__===$B.LongInt.$dict){
392
return $B.LongInt.$dict.__sub__($B.LongInt(self), $B.LongInt(other))
393
}
395
other > $B.max_int32 || other < $B.min_int32) {
396
return $B.LongInt.$dict.__sub__($B.LongInt(self), $B.LongInt(other))
397
}
398
return self-other
400
if(isinstance(other,_b_.bool)) return self-other
401
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
402
$err("-",other)
403
}
404
405
$op_func += '' // source code
407
for(var $op in $ops){
408
var opf = $op_func.replace(/-/gm,$op)
409
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
411
}
412
413
// code for + and -
414
var $op_func = function(self,other){
415
if(isinstance(other,int)){
416
if(typeof other=='number'){
417
var res = self.valueOf()-other.valueOf()
418
if(res>=$B.min_int && res<=$B.max_int){return res}
433
}
434
if(isinstance(other,_b_.bool)){
435
var bool_value=0;
436
if(other.valueOf()) bool_value=1;
441
}
442
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
443
throw $err('-',other)
444
}
445
$op_func += '' // source code
446
var $ops = {'+':'add','-':'sub'}
447
for(var $op in $ops){
448
var opf = $op_func.replace(/-/gm,$op)
449
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
455
if (other.__class__ === $B.LongInt.$dict) {
456
return $B.LongInt.$dict.__lt__(other, $B.LongInt(self))
457
}
460
if(isinstance(other,_b_.bool)) {
461
return self.valueOf() > _b_.bool.$dict.__hash__(other)
462
}
467
// See if other has the opposite operator, eg < for >
468
var inv_op = getattr(other, '__lt__', null)
485
486
var $valid_digits=function(base) {
487
var digits=''
488
if (base === 0) return '0'
489
if (base < 10) {
490
for (var i=0; i < base; i++) digits+=String.fromCharCode(i+48)
491
return digits
492
}
493
494
var digits='0123456789'
495
// A = 65 (10 + 55)
496
for (var i=10; i < base; i++) digits+=String.fromCharCode(i+55)
497
return digits
498
}
499
508
if(base!==undefined){
509
if(!isinstance(value,[_b_.str,_b_.bytes,_b_.bytearray])){
524
if(value<$B.min_int || value>$B.max_int){
525
return $B.LongInt.$dict.$from_float(value)
526
}
536
537
if(base==10){
538
if(value < $B.min_int || value > $B.max_int) return $B.LongInt(value)
539
return value
540
}else if(value.toString().search('e')>-1){
544
var res=parseInt(value, base)
545
if(res < $B.min_int || res > $B.max_int) return $B.LongInt(value,base)
546
return res
552
if(value.__class__===$B.LongInt.$dict){
553
var z = parseInt(value.value)
554
if(z>$B.min_int && z<$B.max_int){return z}
555
else{return value}
556
}
559
560
if(isinstance(value, _b_.str)) value=value.valueOf()
561
if(typeof value=="string") {
562
var _value=value.trim() // remove leading/trailing whitespace
563
if (_value.length == 2 && base==0 && (_value=='0b' || _value=='0o' || _value=='0x')) {
568
if (base == 0) {
569
if (_pre == '0B') base=2
570
if (_pre == '0O') base=8
571
if (_pre == '0X') base=16
572
}
573
if (_pre=='0B' || _pre=='0O' || _pre=='0X') {
575
}
576
}
577
var _digits=$valid_digits(base)
578
var _re=new RegExp('^[+-]?['+_digits+']+$', 'i')
587
var res=parseInt(_value, base)
588
if(res < $B.min_int || res > $B.max_int) return $B.LongInt(_value, base)
589
return res
592
if(isinstance(value,[_b_.bytes,_b_.bytearray])){
593
var _digits = $valid_digits(base)
594
for(var i=0;i<value.source.length;i++){
595
if(_digits.indexOf(String.fromCharCode(value.source[i]))==-1){
597
base +": "+_b_.repr(value))
598
}
599
}
600
return Number(parseInt(getattr(value,'decode')('latin-1'), base))
601
}
603
if(hasattr(value, '__int__')) return getattr(value,'__int__')()
604
if(hasattr(value, '__index__')) return getattr(value,'__index__')()
605
if(hasattr(value, '__trunc__')) {
606
var res = getattr(value,'__trunc__')(),
607
int_func = _b_.getattr(res, '__int__', null)
608
if(int_func===null){
610
$B.get_class(res).__name__+')')
611
}
612
var res=int_func()
613
if(isinstance(res, int)){return res}
626
// Boolean type
627
var $BoolDict = _b_.bool.$dict
628
629
$BoolDict.__add__ = function(self,other){
651
}
652
653
$BoolDict.__hash__ = $BoolDict.__index__= $BoolDict.__int__=function(self) {
654
if(self.valueOf()) return 1
655
return 0
656
}
657
658
$BoolDict.__le__ = function(self,other){return !$BoolDict.__gt__(self,other)}
659
660
$BoolDict.__lshift__ = function(self,other){return self.valueOf() << other}
661
662
$BoolDict.__lt__ = function(self,other){return !$BoolDict.__ge__(self,other)}
663
664
$BoolDict.__mul__ = function(self,other){
666
}
667
668
$BoolDict.__neg__ = function(self){return -$B.int_or_bool(self)}
669
670
$BoolDict.__or__ = function(self, other){
672
}
673
674
$BoolDict.__pos__ = $B.int_or_bool
675
676
$BoolDict.__repr__ = $BoolDict.__str__ = function(self){
678
}
679
680
$BoolDict.__setattr__ = function(self, attr){
681
return no_set_attr($BoolDict, attr)
682
}
683
684
$BoolDict.__sub__ = function(self,other){
686
}
687
688
$BoolDict.__xor__ = function(self, other) {
689
return self.valueOf() != other.valueOf()
690
}
691
692