Permalink
Feb 10, 2018
Feb 10, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 10, 2018
Feb 10, 2018
May 19, 2017
Feb 11, 2018
Dec 18, 2015
Feb 11, 2018
Feb 10, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Jan 25, 2017
Feb 10, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Newer
100644
729 lines (624 sloc)
22 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()
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.long_int.__lshift__($B.long_int.$factory(self), $B.long_int.$factory(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}
240
else{return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
241
$B.long_int.$factory(other)))}
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)
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
}
299
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
300
$B.long_int.$factory(other)))
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.long_int.__rshift__($B.long_int.$factory(self),
327
$B.long_int.$factory(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")
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.long_int){
390
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(other))
394
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(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}
417
else{return $B.long_int.__sub__($B.long_int.$factory(self),
418
$B.long_int.$factory(other))}
422
return $B.long_int.__sub__($B.long_int.$factory(self),
423
$B.long_int.$factory(other))
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.long_int) {
454
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
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])){
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')
579
"invalid literal for int() with base "+base +": '"+
580
_b_.str.$factory(value)+"'")
584
"invalid literal for int() with base "+base +": '"+
585
_b_.str.$factory(value)+"'")
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
$B.$bool = function(obj){ // return true or false
627
if(obj===null || obj === undefined ) return false
628
switch(typeof obj) {
629
case 'boolean':
630
return obj
631
case 'number':
632
case 'string':
633
if(obj) return true
634
return false
635
default:
636
var ce = $B.current_exception
637
try{return getattr(obj,'__bool__')()}
638
catch(err){
639
$B.current_exception = ce
640
try{return getattr(obj,'__len__')()>0}
641
catch(err){return true}
642
}
643
}// switch
644
}
645
646
var bool = {
647
__class__: $B.$type,
648
__module__: "builtins",
649
__mro__: [int, object],
650
__name__: "bool",
651
$is_class: true,
652
$native: true
653
}
659
bool.__and__ = function(self, other){
660
return $B.$bool(int.__and__(self, other))
663
bool.__eq__ = function(self,other){
664
return self ? $B.$bool(other) : !$B.$bool(other)
667
bool.__ne__ = function(self,other){
668
return self ? !$B.$bool(other) : $B.$bool(other)
671
bool.__ge__ = function(self,other){
672
return _b_.int.__ge__(bool.__hash__(self),other)
675
bool.__gt__ = function(self,other){
676
return _b_.int.__gt__(bool.__hash__(self),other)
718
bool.$factory = function(){
719
// Calls $B.$bool, which is used inside the generated JS code and skips
720
// arguments control.
721
var $=$B.args('bool', 1, {x:null}, ['x'], arguments,{x:false},null,null)
722
return $B.$bool($.x)
723
}
724
725
_b_.bool = bool