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
693 lines (589 sloc)
21 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)
173
}
174
if(hasattr(other,'__rfloordiv__')){
175
return getattr(other,'__rfloordiv__')(self)
176
}
177
$err("//",other)
178
}
179
182
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
193
if(value===undefined){value=0}
194
self.toString = function(){return value}
195
//self.valueOf = function(){return value}
206
return int.$factory($B.LongInt.$dict.__lshift__($B.LongInt(self), $B.LongInt(other)))
207
}
208
var rlshift = getattr(other, '__rlshift__', null)
209
if(rlshift!==null){return rlshift(self)}
210
$err('<<', other)
211
}
212
214
// can't use Javascript % because it works differently for negative numbers
215
if(isinstance(other,_b_.tuple) && other.length==1) other=other[0]
216
if(isinstance(other,[int, _b_.float, bool])){
217
if(other===false){other=0}else if(other===true){other=1}
221
}
222
if(hasattr(other,'__rmod__')) return getattr(other,'__rmod__')(self)
223
$err('%',other)
224
}
225
231
232
// this will be quick check, so lets do it early.
233
if(typeof other==="string") {
234
return other.repeat(val)
235
}
236
237
if(isinstance(other,int)){
238
var res = self*other
239
if(res>$B.min_int && res<$B.max_int){return res}
251
return $B.make_complex(int.__mul__(self, other.$real),
252
int.__mul__(self, other.$imag))
253
}
254
if(isinstance(other,[_b_.list,_b_.tuple])){
255
var res = []
256
// make temporary copy of list
257
var $temp = other.slice(0,other.length)
258
for(var i=0;i<val;i++) res=res.concat($temp)
259
if(isinstance(other,_b_.tuple)) res=_b_.tuple(res)
260
return res
261
}
262
if(hasattr(other,'__rmul__')) return getattr(other,'__rmul__')(self)
263
$err("*",other)
264
}
265
271
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
285
if(z !== undefined && z !== null){
286
// If z is provided, the algorithm is faster than computing
287
// self ** other then applying the modulo z
288
var res = (self % z + z) % z
289
for(var i=1; i<other; i++){
290
res *= self
291
res = (res % z + z) % z
292
}
293
return res
294
}
304
if(self>=0){return new Number(Math.pow(self, other.valueOf()))}
305
else{
306
// use complex power
313
}
314
if(hasattr(other,'__rpow__')) return getattr(other,'__rpow__')(self)
315
$err("**",other)
316
}
317
326
return int.$factory($B.LongInt.$dict.__rshift__($B.LongInt(self),
327
$B.LongInt(other)))
328
}
329
var rrshift = getattr(other, '__rrshift__', null)
330
if(rrshift!==null){return rrshift(self)}
331
$err('>>', other)
332
}
339
throw _b_.AttributeError.$factory("'int' object attribute '"+attr+"' is read-only")
352
if(other.__class__==$B.LongInt.$dict){return new Number(self/parseInt(other.value))}
363
}
364
if(hasattr(other,'__rtruediv__')) return getattr(other,'__rtruediv__')(self)
365
$err("/",other)
366
}
367
371
s = bin(self)
372
s = getattr(s,'lstrip')('-0b') // remove leading zeros and minus sign
373
return s.length // len('100101') --> 6
374
}
375
377
int.numerator = function(self){return self}
378
int.denominator = function(self){return int.$factory(1)}
379
int.imag = function(self){return int.$factory(0)}
380
int.real = function(self){return self}
389
if(other.__class__===$B.LongInt.$dict){
390
return $B.LongInt.$dict.__sub__($B.LongInt(self), $B.LongInt(other))
391
}
393
other > $B.max_int32 || other < $B.min_int32) {
394
return $B.LongInt.$dict.__sub__($B.LongInt(self), $B.LongInt(other))
395
}
396
return self-other
398
if(isinstance(other,_b_.bool)) return self-other
399
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
400
$err("-",other)
401
}
402
403
$op_func += '' // source code
405
for(var $op in $ops){
406
var opf = $op_func.replace(/-/gm,$op)
407
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
409
}
410
411
// code for + and -
412
var $op_func = function(self,other){
413
if(isinstance(other,int)){
414
if(typeof other=='number'){
415
var res = self.valueOf()-other.valueOf()
416
if(res>=$B.min_int && res<=$B.max_int){return res}
431
}
432
if(isinstance(other,_b_.bool)){
433
var bool_value=0;
434
if(other.valueOf()) bool_value=1;
439
}
440
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
441
throw $err('-',other)
442
}
443
$op_func += '' // source code
444
var $ops = {'+':'add','-':'sub'}
445
for(var $op in $ops){
446
var opf = $op_func.replace(/-/gm,$op)
447
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
453
if (other.__class__ === $B.LongInt.$dict) {
454
return $B.LongInt.$dict.__lt__(other, $B.LongInt(self))
455
}
458
if(isinstance(other,_b_.bool)) {
459
return self.valueOf() > _b_.bool.$dict.__hash__(other)
460
}
465
// See if other has the opposite operator, eg < for >
466
var inv_op = getattr(other, '__lt__', null)
483
484
var $valid_digits=function(base) {
485
var digits=''
486
if (base === 0) return '0'
487
if (base < 10) {
488
for (var i=0; i < base; i++) digits+=String.fromCharCode(i+48)
489
return digits
490
}
491
492
var digits='0123456789'
493
// A = 65 (10 + 55)
494
for (var i=10; i < base; i++) digits+=String.fromCharCode(i+55)
495
return digits
496
}
497
506
if(base!==undefined){
507
if(!isinstance(value,[_b_.str,_b_.bytes,_b_.bytearray])){
522
if(value<$B.min_int || value>$B.max_int){
523
return $B.LongInt.$dict.$from_float(value)
524
}
534
535
if(base==10){
536
if(value < $B.min_int || value > $B.max_int) return $B.LongInt(value)
537
return value
538
}else if(value.toString().search('e')>-1){
542
var res=parseInt(value, base)
543
if(res < $B.min_int || res > $B.max_int) return $B.LongInt(value,base)
544
return res
550
if(value.__class__===$B.LongInt.$dict){
551
var z = parseInt(value.value)
552
if(z>$B.min_int && z<$B.max_int){return z}
553
else{return value}
554
}
557
558
if(isinstance(value, _b_.str)) value=value.valueOf()
559
if(typeof value=="string") {
560
var _value=value.trim() // remove leading/trailing whitespace
561
if (_value.length == 2 && base==0 && (_value=='0b' || _value=='0o' || _value=='0x')) {
566
if (base == 0) {
567
if (_pre == '0B') base=2
568
if (_pre == '0O') base=8
569
if (_pre == '0X') base=16
570
}
571
if (_pre=='0B' || _pre=='0O' || _pre=='0X') {
573
}
574
}
575
var _digits=$valid_digits(base)
576
var _re=new RegExp('^[+-]?['+_digits+']+$', 'i')
585
var res=parseInt(_value, base)
586
if(res < $B.min_int || res > $B.max_int) return $B.LongInt(_value, base)
587
return res
590
if(isinstance(value,[_b_.bytes,_b_.bytearray])){
591
var _digits = $valid_digits(base)
592
for(var i=0;i<value.source.length;i++){
593
if(_digits.indexOf(String.fromCharCode(value.source[i]))==-1){
595
base +": "+_b_.repr(value))
596
}
597
}
598
return Number(parseInt(getattr(value,'decode')('latin-1'), base))
599
}
601
if(hasattr(value, '__int__')) return getattr(value,'__int__')()
602
if(hasattr(value, '__index__')) return getattr(value,'__index__')()
603
if(hasattr(value, '__trunc__')) {
604
var res = getattr(value,'__trunc__')(),
605
int_func = _b_.getattr(res, '__int__', null)
606
if(int_func===null){
608
$B.get_class(res).__name__+')')
609
}
610
var res=int_func()
611
if(isinstance(res, int)){return res}
624
// Boolean type
625
var $BoolDict = _b_.bool.$dict
626
627
$BoolDict.__add__ = function(self,other){
649
}
650
651
$BoolDict.__hash__ = $BoolDict.__index__= $BoolDict.__int__=function(self) {
652
if(self.valueOf()) return 1
653
return 0
654
}
655
656
$BoolDict.__le__ = function(self,other){return !$BoolDict.__gt__(self,other)}
657
658
$BoolDict.__lshift__ = function(self,other){return self.valueOf() << other}
659
660
$BoolDict.__lt__ = function(self,other){return !$BoolDict.__ge__(self,other)}
661
662
$BoolDict.__mul__ = function(self,other){
664
}
665
666
$BoolDict.__neg__ = function(self){return -$B.int_or_bool(self)}
667
668
$BoolDict.__or__ = function(self, other){
670
}
671
672
$BoolDict.__pos__ = $B.int_or_bool
673
674
$BoolDict.__repr__ = $BoolDict.__str__ = function(self){
676
}
677
678
$BoolDict.__setattr__ = function(self, attr){
679
return no_set_attr($BoolDict, attr)
680
}
681
682
$BoolDict.__sub__ = function(self,other){
684
}
685
686
$BoolDict.__xor__ = function(self, other) {
687
return self.valueOf() != other.valueOf()
688
}
689
690