Skip to content
Permalink
Newer
Older
100644 854 lines (768 sloc) 26.1 KB
Sep 5, 2014
1
;(function($B){
2
3
var _b_ = $B.builtins
Sep 5, 2014
4
Mar 10, 2018
5
function $err(op, other){
6
var msg = "unsupported operand type(s) for " + op +
7
": 'int' and '" + $B.class_name(other) + "'"
8
throw _b_.TypeError.$factory(msg)
Sep 5, 2014
9
}
10
11
function int_value(obj){
12
// Instances of int subclasses that call int.__new__(cls, value)
13
// have an attribute $value set
14
return obj.$value !== undefined ? obj.$value : obj
15
}
16
17
// dictionary for built-in class 'int'
18
var int = {
19
__class__: _b_.type,
20
__dir__: _b_.object.__dir__,
21
$infos: {
22
__module__: "builtins",
23
__name__: "int"
24
},
25
$is_class: true,
26
$native: true,
27
$descriptors: {
Mar 10, 2018
28
"numerator": true,
29
"denominator": true,
30
"imag": true,
31
"real": true
Sep 5, 2014
33
}
34
35
int.from_bytes = function() {
Mar 10, 2018
36
var $ = $B.args("from_bytes", 3,
37
{bytes:null, byteorder:null, signed:null},
Mar 10, 2018
38
["bytes", "byteorder", "signed"],
39
arguments, {signed: false}, null, null)
Sep 5, 2014
40
41
var x = $.bytes,
42
byteorder = $.byteorder,
Mar 10, 2018
43
signed = $.signed,
44
_bytes, _len
45
if(_b_.isinstance(x, [_b_.bytes, _b_.bytearray])){
Mar 10, 2018
46
_bytes = x.source
47
_len = x.source.length
48
}else{
49
_bytes = _b_.list.$factory(x)
50
_len = _bytes.length
Mar 10, 2018
51
for(var i = 0; i < _len; i++){
52
_b_.bytes.$factory([_bytes[i]])
53
}
Sep 5, 2014
54
}
Mar 10, 2018
56
case "big":
57
var num = _bytes[_len - 1]
58
var _mult = 256
59
for(var i = _len - 2; i >= 0; i--){
60
// For operations, use the functions that can take or return
61
// big integers
62
num = $B.add($B.mul(_mult, _bytes[i]), num)
63
_mult = $B.mul(_mult,256)
64
}
65
if(! signed){return num}
66
if(_bytes[0] < 128){return num}
67
return $B.sub(num, _mult)
68
case "little":
69
var num = _bytes[0]
70
if(num >= 128){num = num - 256}
71
var _mult = 256
72
for(var i = 1; i < _len; i++){
73
num = $B.add($B.mul(_mult, _bytes[i]), num)
74
_mult = $B.mul(_mult, 256)
75
}
76
if(! signed){return num}
77
if(_bytes[_len - 1] < 128){return num}
78
return $B.sub(num, _mult)
Sep 5, 2014
79
}
80
Mar 10, 2018
81
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
Sep 5, 2014
82
}
83
84
int.to_bytes = function(){
85
var $ = $B.args("to_bytes", 3,
86
{self: null, len: null, byteorder: null},
87
["self", "len", "byteorder"],
88
arguments, {}, "args", "kw"),
89
self = $.self,
90
len = $.len,
91
byteorder = $.byteorder,
92
kwargs = $.kw
93
if(! _b_.isinstance(len, _b_.int)){
94
throw _b_.TypeError.$factory("integer argument expected, got " +
96
}
97
if(["little", "big"].indexOf(byteorder) == -1){
98
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
99
}
100
var signed = kwargs.$string_dict["signed"] || false,
101
res = []
102
103
if(self < 0){
104
if(! signed){
105
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
106
}
107
self = Math.pow(256, len) + self
108
}
109
var value = self
110
while(true){
111
var quotient = Math.floor(value / 256),
112
rest = value - 256 * quotient
113
res.push(rest)
114
if(quotient == 0){
115
break
116
}
117
value = quotient
118
}
119
if(res.length > len){
120
throw _b_.OverflowError.$factory("int too big to convert")
121
}
122
if(byteorder == "big"){res = res.reverse()}
123
return {
124
__class__: _b_.bytes,
125
source: res
126
}
Sep 5, 2014
127
}
128
129
int.__abs__ = function(self){return _b_.abs(self)}
131
int.__bool__ = function(self){
132
return int_value(self).valueOf() == 0 ? false : true
133
}
Sep 5, 2014
134
135
int.__ceil__ = function(self){return Math.ceil(int_value(self))}
137
int.__divmod__ = function(self, other){return _b_.divmod(self, other)}
Mar 10, 2018
139
int.__eq__ = function(self, other){
Sep 5, 2014
140
// compare object "self" to class "int"
Mar 10, 2018
141
if(other === undefined){return self === int}
142
if(_b_.isinstance(other, int)){
143
return self.valueOf() == int_value(other).valueOf()
144
}
145
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
146
if(_b_.isinstance(other, _b_.complex)){
Mar 10, 2018
147
if(other.$imag != 0){return False}
148
return self.valueOf() == other.$real
Sep 5, 2014
149
}
150
return _b_.NotImplemented
Sep 5, 2014
151
}
152
153
int.__float__ = function(self){
154
return new Number(self)
155
}
156
157
function preformat(self, fmt){
str
Feb 10, 2018
158
if(fmt.empty){return _b_.str.$factory(self)}
Mar 10, 2018
159
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
160
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
161
"' for object of type 'int'")
162
}
164
switch(fmt.type){
165
case undefined:
Mar 10, 2018
166
case "d":
167
res = self.toString()
168
break
Mar 10, 2018
169
case "b":
170
res = (fmt.alternate ? "0b" : "") + self.toString(2)
171
break
Mar 10, 2018
172
case "c":
173
res = _b_.chr(self)
174
break
Mar 10, 2018
175
case "o":
176
res = (fmt.alternate ? "0o" : "") + self.toString(8)
177
break
Mar 10, 2018
178
case "x":
179
res = (fmt.alternate ? "0x" : "") + self.toString(16)
180
break
Mar 10, 2018
181
case "X":
182
res = (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
183
break
Mar 10, 2018
184
case "n":
185
return self // fix me
186
}
188
if(fmt.sign !== undefined){
189
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
190
res = fmt.sign + res
191
}
192
}
193
return res
194
}
195
196
Mar 10, 2018
197
int.__format__ = function(self, format_spec){
198
var fmt = new $B.parse_format_spec(format_spec)
Mar 10, 2018
199
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type) != -1){
200
// Call __format__ on float(self)
201
return _b_.float.__format__(self, format_spec)
Mar 10, 2018
203
fmt.align = fmt.align || ">"
204
var res = preformat(self, fmt)
205
if(fmt.comma){
Mar 10, 2018
206
var sign = res[0] == "-" ? "-" : "",
207
rest = res.substr(sign.length),
208
len = rest.length,
209
nb = Math.ceil(rest.length/3),
210
chunks = []
Mar 10, 2018
211
for(var i = 0; i < nb; i++){
212
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
213
}
214
chunks.reverse()
Mar 10, 2018
215
res = sign + chunks.join(",")
216
}
217
return $B.format_width(res, fmt)
Sep 5, 2014
218
}
219
220
int.__floordiv__ = function(self, other){
221
if(other.__class__ === $B.long_int){
222
return $B.long_int.__floordiv__($B.long_int.$factory(self), other)
223
}
224
if(_b_.isinstance(other, int)){
226
if(other == 0){throw _b_.ZeroDivisionError.$factory("division by zero")}
Mar 10, 2018
227
return Math.floor(self / other)
Sep 5, 2014
228
}
229
if(_b_.isinstance(other, _b_.float)){
Mar 10, 2018
230
if(!other.valueOf()){
231
throw _b_.ZeroDivisionError.$factory("division by zero")
Mar 10, 2018
232
}
233
return Math.floor(self / other)
Sep 5, 2014
234
}
Mar 10, 2018
235
if(hasattr(other, "__rfloordiv__")){
236
return $B.$getattr(other, "__rfloordiv__")(self)
Sep 5, 2014
237
}
Mar 10, 2018
238
$err("//", other)
Sep 5, 2014
239
}
240
241
int.__hash__ = function(self){
Mar 23, 2018
242
if(self === undefined){
243
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
244
}
245
return self.valueOf()
246
}
Sep 5, 2014
247
248
//int.__ior__ = function(self,other){return self | other} // bitwise OR
Sep 5, 2014
249
250
int.__index__ = function(self){
251
return int_value(self)
252
}
Sep 5, 2014
253
254
int.__init__ = function(self, value){
Mar 10, 2018
255
if(value === undefined){value = 0}
Sep 5, 2014
256
self.toString = function(){return value}
257
return _b_.None
Sep 5, 2014
258
}
259
260
int.__int__ = function(self){return self}
Sep 5, 2014
261
262
int.__invert__ = function(self){return ~self}
Sep 5, 2014
263
Mar 10, 2018
265
int.__lshift__ = function(self, other){
266
if(_b_.isinstance(other, int)){
Mar 10, 2018
268
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
269
$B.long_int.$factory(other)))
271
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
272
if(rlshift !== _b_.None){return rlshift(self)}
Mar 10, 2018
273
$err("<<", other)
Mar 10, 2018
276
int.__mod__ = function(self, other) {
Sep 5, 2014
277
// can't use Javascript % because it works differently for negative numbers
278
if(_b_.isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
279
if(other.__class__ === $B.long_int){
280
return $B.long_int.__mod__($B.long_int.$factory(self), other)
281
}
282
if(_b_.isinstance(other, [int, _b_.float, bool])){
Mar 10, 2018
284
if(other === false){other = 0}
285
else if(other === true){other = 1}
286
if(other == 0){throw _b_.ZeroDivisionError.$factory(
287
"integer division or modulo by zero")}
Mar 10, 2018
288
return (self % other + other) % other
Sep 5, 2014
289
}
290
if(hasattr(other, "__rmod__")){return $B.$getattr(other, "__rmod__")(self)}
Mar 10, 2018
291
$err("%", other)
Sep 5, 2014
292
}
293
294
int.__mro__ = [_b_.object]
Sep 5, 2014
295
Mar 10, 2018
296
int.__mul__ = function(self, other){
297
Sep 5, 2014
298
var val = self.valueOf()
Jan 22, 2015
299
300
// this will be quick check, so lets do it early.
Mar 10, 2018
301
if(typeof other === "string") {
Jan 22, 2015
302
return other.repeat(val)
303
}
304
305
if(_b_.isinstance(other, int)){
Mar 10, 2018
307
var res = self * other
308
if(res > $B.min_int && res < $B.max_int){return res}
309
else{
310
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
311
$B.long_int.$factory(other)))
312
}
314
if(_b_.isinstance(other, _b_.float)){
Mar 10, 2018
315
return new Number(self * other)
317
if(_b_.isinstance(other, _b_.bool)){
Mar 10, 2018
318
if(other.valueOf()){return self}
319
return int.$factory(0)
Sep 5, 2014
320
}
321
if(_b_.isinstance(other, _b_.complex)){
322
return $B.make_complex(int.__mul__(self, other.$real),
323
int.__mul__(self, other.$imag))
Sep 5, 2014
324
}
325
if(_b_.isinstance(other, [_b_.list, _b_.tuple])){
Sep 5, 2014
326
var res = []
327
// make temporary copy of list
Mar 10, 2018
328
var $temp = other.slice(0, other.length)
329
for(var i = 0; i < val; i++){res = res.concat($temp)}
330
if(_b_.isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
Sep 5, 2014
331
return res
332
}
333
if(_b_.hasattr(other, "__rmul__")){
334
return $B.$getattr(other, "__rmul__")(self)
335
}
Mar 10, 2018
336
$err("*", other)
Sep 5, 2014
337
}
338
Feb 22, 2019
339
int.__ne__ = function(self, other){
340
var res = int.__eq__(self, other)
341
return (res === _b_.NotImplemented) ? res : !res
342
}
343
344
int.__neg__ = function(self){return -self}
Sep 5, 2014
345
346
int.__new__ = function(cls, value){
Mar 10, 2018
347
if(cls === undefined){
348
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
349
}else if(! _b_.isinstance(cls, _b_.type)){
350
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
351
}
352
if(cls === int){return int.$factory(value)}
353
return {
354
__class__: cls,
355
__dict__: _b_.dict.$factory(),
Mar 10, 2018
357
}
Sep 5, 2014
358
}
359
360
int.__pos__ = function(self){return self}
Sep 5, 2014
361
Mar 10, 2018
362
int.__pow__ = function(self, other, z){
363
if(_b_.isinstance(other, int)){
364
other = int_value(other)
365
switch(other.valueOf()) {
366
case 0:
367
return int.$factory(1)
368
case 1:
369
return int.$factory(self.valueOf())
Feb 9, 2015
370
}
May 19, 2017
371
if(z !== undefined && z !== null){
372
// If z is provided, the algorithm is faster than computing
373
// self ** other then applying the modulo z
374
if(z == 1){return 0}
375
var result = 1,
376
base = self % z,
377
exponent = other,
378
long_int = $B.long_int
379
while(exponent > 0){
380
if(exponent % 2 == 1){
381
if(result * base > $B.max_int){
382
result = long_int.__mul__(
383
long_int.$factory(result),
384
long_int.$factory(base))
385
result = long_int.__mod__(result, z)
386
}else{
387
result = (result * base) % z
388
}
389
}
390
exponent = exponent >> 1
391
if(base * base > $B.max_int){
392
base = long_int.__mul__(long_int.$factory(base),
393
long_int.$factory(base))
394
base = long_int.__mod__(base, z)
395
}else{
396
base = (base * base) % z
397
}
May 19, 2017
398
}
May 19, 2017
400
}
Mar 10, 2018
401
var res = Math.pow(self.valueOf(), other.valueOf())
402
if(res > $B.min_int && res < $B.max_int){return res}
May 19, 2017
403
else if(res !== Infinity && !isFinite(res)){return res}
Feb 11, 2018
405
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
406
$B.long_int.$factory(other)))
May 19, 2017
407
}
Sep 5, 2014
408
}
409
if(_b_.isinstance(other, _b_.float)) {
Mar 10, 2018
410
if(self >= 0){return new Number(Math.pow(self, other.valueOf()))}
411
else{
412
// use complex power
413
return _b_.complex.__pow__($B.make_complex(self, 0), other)
415
}else if(_b_.isinstance(other, _b_.complex)){
Mar 10, 2018
416
var preal = Math.pow(self, other.$real),
417
ln = Math.log(self)
Mar 10, 2018
418
return $B.make_complex(preal * Math.cos(ln), preal * Math.sin(ln))
Sep 5, 2014
419
}
420
if(hasattr(other, "__rpow__")){return $B.$getattr(other, "__rpow__")(self)}
Mar 10, 2018
421
$err("**", other)
Sep 5, 2014
422
}
423
424
int.__repr__ = function(self){
Mar 10, 2018
425
if(self === int){return "<class 'int'>"}
Sep 5, 2014
426
return self.toString()
427
}
428
429
// bitwise right shift
Mar 10, 2018
430
int.__rshift__ = function(self, other){
431
if(_b_.isinstance(other, int)){
Feb 11, 2018
433
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
434
$B.long_int.$factory(other)))
436
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
437
if(rrshift !== _b_.None){return rrshift(self)}
438
$err('>>', other)
439
}
Sep 5, 2014
440
441
int.__setattr__ = function(self, attr, value){
Mar 10, 2018
442
if(typeof self == "number"){
443
if(int.$factory[attr] === undefined){
444
throw _b_.AttributeError.$factory(
445
"'int' object has no attribute '" + attr + "'")
Mar 10, 2018
447
throw _b_.AttributeError.$factory(
448
"'int' object attribute '" + attr + "' is read-only")
Sep 5, 2014
450
}
451
// subclasses of int can have attributes set
452
self.__dict__.$string_dict[attr] = value
453
return _b_.None
Sep 5, 2014
454
}
455
456
int.__str__ = int.__repr__
Sep 5, 2014
457
Mar 10, 2018
458
int.__truediv__ = function(self, other){
459
if(_b_.isinstance(other, int)){
461
if(other == 0){
462
throw _b_.ZeroDivisionError.$factory("division by zero")
463
}
Mar 10, 2018
464
if(other.__class__ === $B.long_int){
465
return new Number(self / parseInt(other.value))
466
}
467
return new Number(self / other)
Sep 5, 2014
468
}
469
if(_b_.isinstance(other, _b_.float)){
Mar 10, 2018
470
if(!other.valueOf()){
471
throw _b_.ZeroDivisionError.$factory("division by zero")
Mar 10, 2018
472
}
473
return new Number(self / other)
Sep 5, 2014
474
}
475
if(_b_.isinstance(other, _b_.complex)){
Mar 10, 2018
476
var cmod = other.$real * other.$real + other.$imag * other.$imag
477
if(cmod == 0){throw _b_.ZeroDivisionError.$factory("division by zero")}
Mar 10, 2018
478
return $B.make_complex(self * other.$real / cmod,
479
-self * other.$imag / cmod)
Sep 5, 2014
480
}
481
if(_b_.hasattr(other, "__rtruediv__")){
482
return $B.$getattr(other, "__rtruediv__")(self)
Mar 10, 2018
483
}
484
$err("/", other)
Sep 5, 2014
485
}
486
487
int.bit_length = function(self){
488
s = _b_.bin(self)
489
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
Sep 5, 2014
490
return s.length // len('100101') --> 6
491
}
492
493
// descriptors
494
int.numerator = function(self){return self}
495
int.denominator = function(self){return int.$factory(1)}
496
int.imag = function(self){return int.$factory(0)}
497
int.real = function(self){return self}
498
Mar 10, 2018
499
$B.max_int32 = (1 << 30) * 2 - 1
500
$B.min_int32 = - $B.max_int32
502
// code for operands & | ^
Mar 10, 2018
503
var $op_func = function(self, other){
504
if(_b_.isinstance(other, int)) {
Mar 10, 2018
505
if(other.__class__ === $B.long_int){
506
return $B.long_int.__sub__($B.long_int.$factory(self),
507
$B.long_int.$factory(other))
Mar 23, 2018
510
if(self > $B.max_int32 || self < $B.min_int32 ||
511
other > $B.max_int32 || other < $B.min_int32){
Mar 10, 2018
512
return $B.long_int.__sub__($B.long_int.$factory(self),
513
$B.long_int.$factory(other))
Mar 21, 2018
515
return self - other
Jun 7, 2015
516
}
517
if(_b_.isinstance(other, _b_.bool)){return self - other}
518
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
519
if(rsub !== _b_.None){return rsub(self)}
Mar 10, 2018
520
$err("-", other)
Sep 5, 2014
521
}
522
Mar 10, 2018
523
$op_func += "" // source code
524
var $ops = {"&": "and", "|": "or", "^": "xor"}
Sep 5, 2014
525
for(var $op in $ops){
Mar 10, 2018
526
var opf = $op_func.replace(/-/gm, $op)
527
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
528
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
529
}
530
531
// code for + and -
Mar 10, 2018
532
var $op_func = function(self, other){
533
if(_b_.isinstance(other, int)){
Mar 10, 2018
535
if(typeof other == "number"){
536
var res = self.valueOf() - other.valueOf()
537
if(res > $B.min_int && res < $B.max_int){return res}
Feb 11, 2018
538
else{return $B.long_int.__sub__($B.long_int.$factory(self),
539
$B.long_int.$factory(other))}
Mar 10, 2018
540
}else if(typeof other == "boolean"){
Mar 21, 2018
541
return other ? self - 1 : self
542
}else{
Feb 11, 2018
543
return $B.long_int.__sub__($B.long_int.$factory(self),
544
$B.long_int.$factory(other))
Sep 5, 2014
546
}
547
if(_b_.isinstance(other, _b_.float)){
Mar 10, 2018
548
return new Number(self - other)
Sep 5, 2014
549
}
550
if(_b_.isinstance(other, _b_.complex)){
Mar 10, 2018
551
return $B.make_complex(self - other.$real, -other.$imag)
Sep 5, 2014
552
}
553
if(_b_.isinstance(other, _b_.bool)){
Mar 10, 2018
554
var bool_value = 0;
555
if(other.valueOf()){bool_value = 1}
556
return self - bool_value
Sep 5, 2014
557
}
558
if(_b_.isinstance(other, _b_.complex)){
559
return $B.make_complex(self.valueOf() - other.$real, other.$imag)
Sep 5, 2014
560
}
561
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
562
if(rsub !== _b_.None){return rsub(self)}
Mar 10, 2018
563
throw $err("-", other)
Sep 5, 2014
564
}
Mar 10, 2018
565
$op_func += "" // source code
566
var $ops = {"+": "add", "-": "sub"}
Sep 5, 2014
567
for(var $op in $ops){
Mar 10, 2018
568
var opf = $op_func.replace(/-/gm, $op)
569
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
570
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
571
}
572
573
// comparison methods
Mar 10, 2018
574
var $comp_func = function(self, other){
Mar 23, 2018
575
if(other.__class__ === $B.long_int){
Feb 11, 2018
576
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
578
if(_b_.isinstance(other, int)){
579
other = int_value(other)
580
return self.valueOf() > other.valueOf()
581
}else if(_b_.isinstance(other, _b_.float)){
582
return self.valueOf() > other.valueOf()
583
}else if(_b_.isinstance(other, _b_.bool)) {
Feb 11, 2018
584
return self.valueOf() > _b_.bool.__hash__(other)
Sep 5, 2014
585
}
586
if(_b_.hasattr(other, "__int__") || _b_.hasattr(other, "__index__")){
587
return int.__gt__(self, $B.$GetInt(other))
Sep 5, 2014
591
}
Mar 10, 2018
592
$comp_func += "" // source code
Sep 5, 2014
594
for(var $op in $B.$comps){
Mar 10, 2018
595
eval("int.__"+$B.$comps[$op] + "__ = " +
596
$comp_func.replace(/>/gm, $op).
597
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
598
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
Sep 5, 2014
599
}
600
601
// add "reflected" methods
602
$B.make_rmethods(int)
Sep 5, 2014
603
Mar 10, 2018
604
var $valid_digits = function(base) {
605
var digits = ""
606
if(base === 0){return "0"}
607
if(base < 10){
Mar 21, 2018
608
for(var i = 0; i < base; i++){digits += String.fromCharCode(i + 48)}
Sep 5, 2014
609
return digits
610
}
611
Mar 10, 2018
612
var digits = "0123456789"
Sep 5, 2014
613
// A = 65 (10 + 55)
Mar 21, 2018
614
for (var i = 10; i < base; i++) {digits += String.fromCharCode(i + 55)}
Sep 5, 2014
615
return digits
616
}
617
618
int.$factory = function(value, base){
619
// int() with no argument returns 0
Mar 10, 2018
620
if(value === undefined){return 0}
622
// int() of an integer returns the integer if base is undefined
Mar 10, 2018
623
if(typeof value == "number" &&
624
(base === undefined || base == 10)){return parseInt(value)}
Mar 10, 2018
626
if(base !== undefined){
627
if(! _b_.isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
Mar 10, 2018
628
throw TypeError.$factory(
629
"int() can't convert non-string with explicit base")
Dec 28, 2014
630
}
631
}
632
633
if(_b_.isinstance(value, _b_.complex)){
634
throw TypeError.$factory("can't convert complex to int")
Dec 28, 2014
635
}
Mar 10, 2018
636
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
637
{"base": 10}, null, null),
638
value = $ns["x"],
639
base = $ns["base"]
641
if(_b_.isinstance(value, _b_.float) && base == 10){
Mar 10, 2018
642
if(value < $B.min_int || value > $B.max_int){
Feb 11, 2018
643
return $B.long_int.$from_float(value)
Mar 10, 2018
645
else{return value > 0 ? Math.floor(value) : Math.ceil(value)}
Sep 5, 2014
647
Mar 10, 2018
648
if(! (base >=2 && base <= 36)){
Dec 26, 2014
649
// throw error (base must be 0, or 2-36)
Mar 10, 2018
650
if(base != 0){throw _b_.ValueError.$factory("invalid base")}
Dec 26, 2014
651
}
652
Mar 10, 2018
653
if(typeof value == "number"){
Mar 10, 2018
655
if(base == 10){
656
if(value < $B.min_int || value > $B.max_int){
657
return $B.long_int.$factory(value)
658
}
Mar 10, 2018
660
}else if(value.toString().search("e") > -1){
Dec 26, 2014
661
// can't convert to another base if value is too big
Mar 10, 2018
662
throw _b_.OverflowError.$factory("can't convert to base " + base)
Dec 26, 2014
663
}else{
Mar 10, 2018
664
var res = parseInt(value, base)
665
if(value < $B.min_int || value > $B.max_int){
666
return $B.long_int.$factory(value, base)
667
}
Dec 26, 2014
669
}
670
}
Sep 5, 2014
671
Mar 10, 2018
672
if(value === true){return Number(1)}
673
if(value === false){return Number(0)}
674
if(value.__class__ === $B.long_int){
675
var z = parseInt(value.value)
Mar 10, 2018
676
if(z > $B.min_int && z < $B.max_int){return z}
677
else{return value}
678
}
Sep 5, 2014
679
Mar 10, 2018
680
base = $B.$GetInt(base)
681
function invalid(value, base){
682
throw _b_.ValueError.$factory("invalid literal for int() with base " +
683
base + ": '" + _b_.str.$factory(value) + "'")
684
}
Sep 5, 2014
685
686
if(_b_.isinstance(value, _b_.str)){value = value.valueOf()}
Mar 10, 2018
687
if(typeof value == "string") {
688
var _value = value.trim() // remove leading/trailing whitespace
689
if(_value.length == 2 && base == 0 &&
690
(_value == "0b" || _value == "0o" || _value == "0x")){
691
throw _b_.ValueError.$factory("invalid value")
692
}
693
if(_value.length >2) {
694
var _pre = _value.substr(0, 2).toUpperCase()
695
if(base == 0){
696
if(_pre == "0B"){base = 2}
697
if(_pre == "0O"){base = 8}
698
if(_pre == "0X"){base = 16}
699
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
700
else if(_pre == "0O" && base != 8){invalid(_value, base)}
701
if((_pre == "0B" && base == 2) || _pre == "0O" || _pre == "0X"){
Mar 10, 2018
702
_value = _value.substr(2)
703
while(_value.startsWith("_")){
704
_value = _value.substr(1)
705
}
Mar 10, 2018
706
}
707
}else if(base == 0){
708
// eg int("1\n", 0)
709
base = 10
Mar 10, 2018
710
}
711
var _digits = $valid_digits(base),
712
_re = new RegExp("^[+-]?[" + _digits + "]" +
713
"[" + _digits + "_]*$", "i"),
714
match = _re.exec(_value)
715
if(match === null){
716
invalid(value, base)
717
}else{
718
value = _value.replace(/_/g, "")
Mar 10, 2018
719
}
720
if(base <= 10 && ! isFinite(value)){invalid(_value, base)}
721
var res = parseInt(value, base)
Mar 10, 2018
722
if(res < $B.min_int || res > $B.max_int){
723
return $B.long_int.$factory(value, base)
Mar 10, 2018
724
}
725
return res
Sep 5, 2014
726
}
728
if(_b_.isinstance(value, [_b_.bytes, _b_.bytearray])){
729
return int.$factory($B.$getattr(value, "decode")("latin-1"), base)
730
}
731
var $int = $B.$getattr(value, "__int__", _b_.None)
732
if($int !== _b_.None){return $int()}
733
734
var $index = $B.$getattr(value, "__index__", _b_.None)
735
if($index !== _b_.None){return $index()}
Sep 5, 2014
736
737
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
738
if($trunc !== _b_.None){
739
var res = $truc(),
740
int_func = $int
741
if(int_func === _b_.None){
742
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 10, 2018
745
var res = int_func()
746
if(_b_.isinstance(res, int)){return int_value(res)}
747
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 10, 2018
750
throw _b_.TypeError.$factory(
751
"int() argument must be a string, a bytes-like " +
752
"object or a number, not '" + $B.class_name(value) + "'")
Sep 5, 2014
753
}
754
755
$B.set_func_names(int, "builtins")
Sep 5, 2014
757
_b_.int = int
758
Feb 11, 2018
760
$B.$bool = function(obj){ // return true or false
Mar 10, 2018
761
if(obj === null || obj === undefined ){ return false}
762
switch(typeof obj){
763
case "boolean":
764
return obj
765
case "number":
766
case "string":
767
if(obj){return true}
768
return false
769
default:
770
if(obj.$is_class){return true}
771
var missing = {},
772
bool_func = $B.$getattr(obj, "__bool__", missing)
773
if(bool_func === missing){
774
try{return $B.$getattr(obj, "__len__")() > 0}
Mar 10, 2018
775
catch(err){return true}
776
}else{
777
return bool_func()
Mar 10, 2018
778
}
779
}
Feb 11, 2018
780
}
781
782
var bool = {
783
__bases__: [int],
Feb 11, 2018
784
__class__: _b_.type,
785
__mro__: [int, _b_.object],
786
$infos:{
787
__name__: "bool",
788
__module__: "builtins"
789
},
Feb 11, 2018
790
$is_class: true,
791
$native: true
792
}
Feb 22, 2019
794
var methods = $B.op2method.subset("operations", "binary", "comparisons",
795
"boolean")
796
for(var op in methods){
797
var method = "__" + methods[op] + "__"
798
bool[method] = (function(op){
799
return function(self, other){
800
var value = self ? 1 : 0
801
if(int[op] !== undefined){
802
return int[op](value, other)
803
}
804
}
805
})(method)
Feb 11, 2018
808
bool.__and__ = function(self, other){
809
return $B.$bool(int.__and__(self, other))
Mar 10, 2018
812
bool.__hash__ = bool.__index__ = bool.__int__ = function(self){
813
if(self.valueOf()) return 1
814
return 0
815
}
816
Feb 11, 2018
817
bool.__neg__ = function(self){return -$B.int_or_bool(self)}
Feb 11, 2018
819
bool.__or__ = function(self, other){
820
return $B.$bool(int.__or__(self, other))
Feb 11, 2018
823
bool.__pos__ = $B.int_or_bool
Feb 11, 2018
825
bool.__repr__ = bool.__str__ = function(self){
826
return self ? "True" : "False"
Feb 11, 2018
829
bool.__setattr__ = function(self, attr){
830
if(_b_.dir(self).indexOf(attr) > -1){
831
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
832
}else{
833
var msg = "'bool' object has no attribute '" + attr + "'"
834
}
835
throw _b_.AttributeError.$factory(msg)
Feb 11, 2018
838
bool.__xor__ = function(self, other) {
839
return self.valueOf() != other.valueOf()
840
}
841
Feb 11, 2018
842
bool.$factory = function(){
843
// Calls $B.$bool, which is used inside the generated JS code and skips
844
// arguments control.
Mar 10, 2018
845
var $ = $B.args("bool", 1, {x: null}, ["x"],
846
arguments,{x: false}, null, null)
Feb 11, 2018
847
return $B.$bool($.x)
848
}
849
850
_b_.bool = bool
Feb 11, 2018
852
$B.set_func_names(bool, "builtins")
Sep 5, 2014
854
})(__BRYTHON__)