Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: lib/core/int.dart

Issue 1659163007: Rename fletch -> dartino (Closed) Base URL: https://github.com/dartino/sdk.git@master
Patch Set: address comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/core/embedded_core_patch.dart ('k') | lib/core/string.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE.md file. 3 // BSD-style license that can be found in the LICENSE.md file.
4 4
5 part of dart.core_patch; 5 part of dart.core_patch;
6 6
7 abstract class _IntBase implements int { 7 abstract class _IntBase implements int {
8 const _IntBase(); 8 const _IntBase();
9 9
10 bool get isNaN => false; 10 bool get isNaN => false;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 int modPow(int exponent, int modulus) { 127 int modPow(int exponent, int modulus) {
128 if (exponent is! int) { 128 if (exponent is! int) {
129 throw new ArgumentError.value(exponent, "exponent", "not an integer"); 129 throw new ArgumentError.value(exponent, "exponent", "not an integer");
130 } 130 }
131 if (modulus is! int) { 131 if (modulus is! int) {
132 throw new ArgumentError.value(modulus, "modulus", "not an integer"); 132 throw new ArgumentError.value(modulus, "modulus", "not an integer");
133 } 133 }
134 if (exponent < 0) throw new RangeError.range(exponent, 0, null, "exponent"); 134 if (exponent < 0) throw new RangeError.range(exponent, 0, null, "exponent");
135 if (modulus <= 0) throw new RangeError.range(modulus, 1, null, "modulus"); 135 if (modulus <= 0) throw new RangeError.range(modulus, 1, null, "modulus");
136 if (exponent == 0) return 1; 136 if (exponent == 0) return 1;
137 if (fletch.enableBigint) { 137 if (dartino.enableBigint) {
138 return this._toBigint().modPow(exponent, modulus); 138 return this._toBigint().modPow(exponent, modulus);
139 } else { 139 } else {
140 throw new UnimplementedError(); 140 throw new UnimplementedError();
141 } 141 }
142 } 142 }
143 143
144 int modInverse(int modulus) { 144 int modInverse(int modulus) {
145 if (modulus is! int) { 145 if (modulus is! int) {
146 throw new ArgumentError.value(modulus, "modulus", "not an integer"); 146 throw new ArgumentError.value(modulus, "modulus", "not an integer");
147 } 147 }
148 if (modulus <= 0) throw new RangeError.range(modulus, 1, null, "modulus"); 148 if (modulus <= 0) throw new RangeError.range(modulus, 1, null, "modulus");
149 if (modulus == 1) return 0; 149 if (modulus == 1) return 0;
150 if (fletch.enableBigint) { 150 if (dartino.enableBigint) {
151 return this._toBigint().modInverse(modulus); 151 return this._toBigint().modInverse(modulus);
152 } else { 152 } else {
153 throw new UnimplementedError(); 153 throw new UnimplementedError();
154 } 154 }
155 } 155 }
156 156
157 int gcd(int other) { 157 int gcd(int other) {
158 if (other is! int) { 158 if (other is! int) {
159 throw new ArgumentError.value(other, "other", "not an integer"); 159 throw new ArgumentError.value(other, "other", "not an integer");
160 } 160 }
161 int x = this.abs(); 161 int x = this.abs();
162 int y = other.abs(); 162 int y = other.abs();
163 if (x == 0) return y; 163 if (x == 0) return y;
164 if (y == 0) return x; 164 if (y == 0) return x;
165 if ((x == 1) || (y == 1)) return 1; 165 if ((x == 1) || (y == 1)) return 1;
166 if (fletch.enableBigint) { 166 if (dartino.enableBigint) {
167 return this._toBigint().gcd(other); 167 return this._toBigint().gcd(other);
168 } else { 168 } else {
169 throw new UnimplementedError(); 169 throw new UnimplementedError();
170 } 170 }
171 } 171 }
172 172
173 bool get isEven => (this & 1) == 0; 173 bool get isEven => (this & 1) == 0;
174 174
175 bool get isOdd => (this & 1) == 1; 175 bool get isOdd => (this & 1) == 1;
176 176
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 bool _compareGtFromDouble(double other) => other > toDouble(); 247 bool _compareGtFromDouble(double other) => other > toDouble();
248 248
249 bool _compareGeFromDouble(double other) => other >= toDouble(); 249 bool _compareGeFromDouble(double other) => other >= toDouble();
250 250
251 double _remainderFromDouble(double other) => other.remainder(toDouble()); 251 double _remainderFromDouble(double other) => other.remainder(toDouble());
252 } 252 }
253 253
254 class _Smi extends _IntBase { 254 class _Smi extends _IntBase {
255 int get hashCode => identityHashCode(this); 255 int get hashCode => identityHashCode(this);
256 256
257 @fletch.native external String toString(); 257 @dartino.native external String toString();
258 258
259 @fletch.native external double toDouble(); 259 @dartino.native external double toDouble();
260 260
261 @fletch.native external int _toMint(); 261 @dartino.native external int _toMint();
262 262
263 int _toBigint() { 263 int _toBigint() {
264 if (fletch.enableBigint) { 264 if (dartino.enableBigint) {
265 return new _Bigint._fromInt(this); 265 return new _Bigint._fromInt(this);
266 } else { 266 } else {
267 throw new UnimplementedError(); 267 throw new UnimplementedError();
268 } 268 }
269 } 269 }
270 270
271 int _toBigintOrDouble() { 271 int _toBigintOrDouble() {
272 if (fletch.enableBigint) { 272 if (dartino.enableBigint) {
273 return new _Bigint._fromInt(this); 273 return new _Bigint._fromInt(this);
274 } else { 274 } else {
275 throw new UnimplementedError(); 275 throw new UnimplementedError();
276 } 276 }
277 } 277 }
278 278
279 @fletch.native num operator -() { 279 @dartino.native num operator -() {
280 return -_toMint(); 280 return -_toMint();
281 } 281 }
282 282
283 @fletch.native num operator +(other) { 283 @dartino.native num operator +(other) {
284 // TODO(kasperl): Check error. 284 // TODO(kasperl): Check error.
285 return other._addFromInteger(this); 285 return other._addFromInteger(this);
286 } 286 }
287 287
288 @fletch.native num operator -(other) { 288 @dartino.native num operator -(other) {
289 // TODO(kasperl): Check error. 289 // TODO(kasperl): Check error.
290 return other._subFromInteger(this); 290 return other._subFromInteger(this);
291 } 291 }
292 292
293 @fletch.native num operator *(other) { 293 @dartino.native num operator *(other) {
294 // TODO(kasperl): Check error. 294 // TODO(kasperl): Check error.
295 return other._mulFromInteger(this); 295 return other._mulFromInteger(this);
296 } 296 }
297 297
298 @fletch.native num operator %(other) { 298 @dartino.native num operator %(other) {
299 switch (fletch.nativeError) { 299 switch (dartino.nativeError) {
300 case fletch.wrongArgumentType: 300 case dartino.wrongArgumentType:
301 return other._modFromInteger(this); 301 return other._modFromInteger(this);
302 case fletch.indexOutOfBounds: 302 case dartino.indexOutOfBounds:
303 throw new IntegerDivisionByZeroException(); 303 throw new IntegerDivisionByZeroException();
304 } 304 }
305 } 305 }
306 306
307 @fletch.native num operator /(other) { 307 @dartino.native num operator /(other) {
308 // TODO(kasperl): Check error. 308 // TODO(kasperl): Check error.
309 return other._divFromInteger(this); 309 return other._divFromInteger(this);
310 } 310 }
311 311
312 @fletch.native int operator ~/(other) { 312 @dartino.native int operator ~/(other) {
313 switch (fletch.nativeError) { 313 switch (dartino.nativeError) {
314 case fletch.wrongArgumentType: 314 case dartino.wrongArgumentType:
315 return other._truncDivFromInteger(this); 315 return other._truncDivFromInteger(this);
316 case fletch.indexOutOfBounds: 316 case dartino.indexOutOfBounds:
317 throw new IntegerDivisionByZeroException(); 317 throw new IntegerDivisionByZeroException();
318 } 318 }
319 } 319 }
320 320
321 @fletch.native external int operator ~(); 321 @dartino.native external int operator ~();
322 322
323 @fletch.native int operator &(other) { 323 @dartino.native int operator &(other) {
324 // TODO(kasperl): Check error. 324 // TODO(kasperl): Check error.
325 return other._bitAndFromInteger(this); 325 return other._bitAndFromInteger(this);
326 } 326 }
327 327
328 @fletch.native int operator |(other) { 328 @dartino.native int operator |(other) {
329 // TODO(kasperl): Check error. 329 // TODO(kasperl): Check error.
330 return other._bitOrFromInteger(this); 330 return other._bitOrFromInteger(this);
331 } 331 }
332 332
333 @fletch.native int operator ^(other) { 333 @dartino.native int operator ^(other) {
334 // TODO(kasperl): Check error. 334 // TODO(kasperl): Check error.
335 return other._bitXorFromInteger(this); 335 return other._bitXorFromInteger(this);
336 } 336 }
337 337
338 @fletch.native int operator >>(other) { 338 @dartino.native int operator >>(other) {
339 // TODO(kasperl): Check error. 339 // TODO(kasperl): Check error.
340 return other._bitShrFromInteger(this); 340 return other._bitShrFromInteger(this);
341 } 341 }
342 342
343 @fletch.native int operator <<(other) { 343 @dartino.native int operator <<(other) {
344 // TODO(kasperl): Check error. 344 // TODO(kasperl): Check error.
345 return other._bitShlFromInteger(this); 345 return other._bitShlFromInteger(this);
346 } 346 }
347 347
348 @fletch.native bool operator ==(other) { 348 @dartino.native bool operator ==(other) {
349 if (other is! num) return false; 349 if (other is! num) return false;
350 // TODO(kasperl): Check error. 350 // TODO(kasperl): Check error.
351 return other._compareEqFromInteger(this); 351 return other._compareEqFromInteger(this);
352 } 352 }
353 353
354 @fletch.native bool operator <(other) { 354 @dartino.native bool operator <(other) {
355 // TODO(kasperl): Check error. 355 // TODO(kasperl): Check error.
356 return other._compareLtFromInteger(this); 356 return other._compareLtFromInteger(this);
357 } 357 }
358 358
359 @fletch.native bool operator <=(other) { 359 @dartino.native bool operator <=(other) {
360 // TODO(kasperl): Check error. 360 // TODO(kasperl): Check error.
361 return other._compareLeFromInteger(this); 361 return other._compareLeFromInteger(this);
362 } 362 }
363 363
364 @fletch.native bool operator >(other) { 364 @dartino.native bool operator >(other) {
365 // TODO(kasperl): Check error. 365 // TODO(kasperl): Check error.
366 return other._compareGtFromInteger(this); 366 return other._compareGtFromInteger(this);
367 } 367 }
368 368
369 @fletch.native bool operator >=(other) { 369 @dartino.native bool operator >=(other) {
370 // TODO(kasperl): Check error. 370 // TODO(kasperl): Check error.
371 return other._compareGeFromInteger(this); 371 return other._compareGeFromInteger(this);
372 } 372 }
373 373
374 // In the following double-dispatch helpers, [other] might 374 // In the following double-dispatch helpers, [other] might
375 // be a small integer in case of overflows. Calling toMint() 375 // be a small integer in case of overflows. Calling toMint()
376 // on [other] isn't strictly necessary but it makes the 376 // on [other] isn't strictly necessary but it makes the
377 // overflow case a litte bit faster. 377 // overflow case a litte bit faster.
378 int _addFromInteger(other) => other._toMint() + _toMint(); 378 int _addFromInteger(other) => other._toMint() + _toMint();
379 379
(...skipping 24 matching lines...) Expand all
404 bool _compareLeFromInteger(other) => other._toMint() <= _toMint(); 404 bool _compareLeFromInteger(other) => other._toMint() <= _toMint();
405 405
406 bool _compareGtFromInteger(other) => other._toMint() > _toMint(); 406 bool _compareGtFromInteger(other) => other._toMint() > _toMint();
407 407
408 bool _compareGeFromInteger(other) => other._toMint() >= _toMint(); 408 bool _compareGeFromInteger(other) => other._toMint() >= _toMint();
409 } 409 }
410 410
411 class _Mint extends _IntBase { 411 class _Mint extends _IntBase {
412 int get hashCode => identityHashCode(this); 412 int get hashCode => identityHashCode(this);
413 413
414 @fletch.native external String toString(); 414 @dartino.native external String toString();
415 415
416 @fletch.native external double toDouble(); 416 @dartino.native external double toDouble();
417 417
418 int _toMint() => this; 418 int _toMint() => this;
419 419
420 int _toBigint() { 420 int _toBigint() {
421 if (fletch.enableBigint) { 421 if (dartino.enableBigint) {
422 return new _Bigint._fromInt(this); 422 return new _Bigint._fromInt(this);
423 } else { 423 } else {
424 throw new UnimplementedError(); 424 throw new UnimplementedError();
425 } 425 }
426 } 426 }
427 427
428 int _toBigintOrDouble() { 428 int _toBigintOrDouble() {
429 if (fletch.enableBigint) { 429 if (dartino.enableBigint) {
430 return new _Bigint._fromInt(this); 430 return new _Bigint._fromInt(this);
431 } else { 431 } else {
432 throw new UnimplementedError(); 432 throw new UnimplementedError();
433 } 433 }
434 } 434 }
435 435
436 @fletch.native num operator -() { 436 @dartino.native num operator -() {
437 if (fletch.enableBigint) { 437 if (dartino.enableBigint) {
438 return -_toBigint(); 438 return -_toBigint();
439 } else { 439 } else {
440 throw new UnimplementedError('Overflow to big integer'); 440 throw new UnimplementedError('Overflow to big integer');
441 } 441 }
442 } 442 }
443 443
444 @fletch.native num operator +(other) { 444 @dartino.native num operator +(other) {
445 switch (fletch.nativeError) { 445 switch (dartino.nativeError) {
446 case fletch.wrongArgumentType: 446 case dartino.wrongArgumentType:
447 return other._addFromInteger(this); 447 return other._addFromInteger(this);
448 case fletch.indexOutOfBounds: 448 case dartino.indexOutOfBounds:
449 if (fletch.enableBigint) { 449 if (dartino.enableBigint) {
450 return other._toBigint()._addFromInteger(this); 450 return other._toBigint()._addFromInteger(this);
451 } else { 451 } else {
452 throw new UnimplementedError('Overflow to big integer'); 452 throw new UnimplementedError('Overflow to big integer');
453 } 453 }
454 } 454 }
455 } 455 }
456 456
457 @fletch.native num operator -(other) { 457 @dartino.native num operator -(other) {
458 switch (fletch.nativeError) { 458 switch (dartino.nativeError) {
459 case fletch.wrongArgumentType: 459 case dartino.wrongArgumentType:
460 return other._subFromInteger(this); 460 return other._subFromInteger(this);
461 case fletch.indexOutOfBounds: 461 case dartino.indexOutOfBounds:
462 if (fletch.enableBigint) { 462 if (dartino.enableBigint) {
463 return other._toBigint()._subFromInteger(this); 463 return other._toBigint()._subFromInteger(this);
464 } else { 464 } else {
465 throw new UnimplementedError('Overflow to big integer'); 465 throw new UnimplementedError('Overflow to big integer');
466 } 466 }
467 } 467 }
468 } 468 }
469 469
470 @fletch.native num operator *(other) { 470 @dartino.native num operator *(other) {
471 switch (fletch.nativeError) { 471 switch (dartino.nativeError) {
472 case fletch.wrongArgumentType: 472 case dartino.wrongArgumentType:
473 return other._mulFromInteger(this); 473 return other._mulFromInteger(this);
474 case fletch.indexOutOfBounds: 474 case dartino.indexOutOfBounds:
475 if (fletch.enableBigint) { 475 if (dartino.enableBigint) {
476 return other._toBigint()._mulFromInteger(this); 476 return other._toBigint()._mulFromInteger(this);
477 } else { 477 } else {
478 throw new UnimplementedError('Overflow to big integer'); 478 throw new UnimplementedError('Overflow to big integer');
479 } 479 }
480 } 480 }
481 } 481 }
482 482
483 @fletch.native num operator %(other) { 483 @dartino.native num operator %(other) {
484 switch (fletch.nativeError) { 484 switch (dartino.nativeError) {
485 case fletch.wrongArgumentType: 485 case dartino.wrongArgumentType:
486 return other._modFromInteger(this); 486 return other._modFromInteger(this);
487 case fletch.indexOutOfBounds: 487 case dartino.indexOutOfBounds:
488 if (fletch.enableBigint) { 488 if (dartino.enableBigint) {
489 return other._toBigint()._modFromInteger(this); 489 return other._toBigint()._modFromInteger(this);
490 } else { 490 } else {
491 throw new UnimplementedError('Overflow to big integer'); 491 throw new UnimplementedError('Overflow to big integer');
492 } 492 }
493 case fletch.illegalState: 493 case dartino.illegalState:
494 throw new IntegerDivisionByZeroException(); 494 throw new IntegerDivisionByZeroException();
495 } 495 }
496 } 496 }
497 497
498 @fletch.native num operator /(other) { 498 @dartino.native num operator /(other) {
499 // TODO(kasperl): Check error. 499 // TODO(kasperl): Check error.
500 return other._divFromInteger(this); 500 return other._divFromInteger(this);
501 } 501 }
502 502
503 @fletch.native int operator ~/(other) { 503 @dartino.native int operator ~/(other) {
504 switch (fletch.nativeError) { 504 switch (dartino.nativeError) {
505 case fletch.wrongArgumentType: 505 case dartino.wrongArgumentType:
506 return other._truncDivFromInteger(this); 506 return other._truncDivFromInteger(this);
507 case fletch.indexOutOfBounds: 507 case dartino.indexOutOfBounds:
508 if (fletch.enableBigint) { 508 if (dartino.enableBigint) {
509 return other._toBigint()._truncDivFromInteger(this); 509 return other._toBigint()._truncDivFromInteger(this);
510 } else { 510 } else {
511 throw new UnimplementedError('Overflow to big integer'); 511 throw new UnimplementedError('Overflow to big integer');
512 } 512 }
513 case fletch.illegalState: 513 case dartino.illegalState:
514 throw new IntegerDivisionByZeroException(); 514 throw new IntegerDivisionByZeroException();
515 } 515 }
516 } 516 }
517 517
518 @fletch.native external int operator ~(); 518 @dartino.native external int operator ~();
519 519
520 @fletch.native int operator &(other) { 520 @dartino.native int operator &(other) {
521 // TODO(kasperl): Check error. 521 // TODO(kasperl): Check error.
522 return other._bitAndFromInteger(this); 522 return other._bitAndFromInteger(this);
523 } 523 }
524 524
525 @fletch.native int operator |(other) { 525 @dartino.native int operator |(other) {
526 // TODO(kasperl): Check error. 526 // TODO(kasperl): Check error.
527 return other._bitOrFromInteger(this); 527 return other._bitOrFromInteger(this);
528 } 528 }
529 529
530 @fletch.native int operator ^(other) { 530 @dartino.native int operator ^(other) {
531 // TODO(kasperl): Check error. 531 // TODO(kasperl): Check error.
532 return other._bitXorFromInteger(this); 532 return other._bitXorFromInteger(this);
533 } 533 }
534 534
535 @fletch.native int operator >>(other) { 535 @dartino.native int operator >>(other) {
536 // TODO(kasperl): Check error. 536 // TODO(kasperl): Check error.
537 return other._bitShrFromInteger(this); 537 return other._bitShrFromInteger(this);
538 } 538 }
539 539
540 @fletch.native int operator <<(other) { 540 @dartino.native int operator <<(other) {
541 switch (fletch.nativeError) { 541 switch (dartino.nativeError) {
542 case fletch.wrongArgumentType: 542 case dartino.wrongArgumentType:
543 return other._bitShlFromInteger(this); 543 return other._bitShlFromInteger(this);
544 case fletch.indexOutOfBounds: 544 case dartino.indexOutOfBounds:
545 if (fletch.enableBigint) { 545 if (dartino.enableBigint) {
546 return other._toBigint()._bitShlFromInteger(this); 546 return other._toBigint()._bitShlFromInteger(this);
547 } else { 547 } else {
548 throw new UnimplementedError('Overflow to big integer'); 548 throw new UnimplementedError('Overflow to big integer');
549 } 549 }
550 } 550 }
551 } 551 }
552 552
553 @fletch.native bool operator ==(other) { 553 @dartino.native bool operator ==(other) {
554 if (other is! num) return false; 554 if (other is! num) return false;
555 // TODO(kasperl): Check error. 555 // TODO(kasperl): Check error.
556 return other._compareEqFromInteger(this); 556 return other._compareEqFromInteger(this);
557 } 557 }
558 558
559 @fletch.native bool operator <(other) { 559 @dartino.native bool operator <(other) {
560 // TODO(kasperl): Check error. 560 // TODO(kasperl): Check error.
561 return other._compareLtFromInteger(this); 561 return other._compareLtFromInteger(this);
562 } 562 }
563 563
564 @fletch.native bool operator <=(other) { 564 @dartino.native bool operator <=(other) {
565 // TODO(kasperl): Check error. 565 // TODO(kasperl): Check error.
566 return other._compareLeFromInteger(this); 566 return other._compareLeFromInteger(this);
567 } 567 }
568 568
569 @fletch.native bool operator >(other) { 569 @dartino.native bool operator >(other) {
570 // TODO(kasperl): Check error. 570 // TODO(kasperl): Check error.
571 return other._compareGtFromInteger(this); 571 return other._compareGtFromInteger(this);
572 } 572 }
573 573
574 @fletch.native bool operator >=(other) { 574 @dartino.native bool operator >=(other) {
575 // TODO(kasperl): Check error. 575 // TODO(kasperl): Check error.
576 return other._compareGeFromInteger(this); 576 return other._compareGeFromInteger(this);
577 } 577 }
578 578
579 int _addFromInteger(other) => other._toMint() + this; 579 int _addFromInteger(other) => other._toMint() + this;
580 580
581 int _subFromInteger(other) => other._toMint() - this; 581 int _subFromInteger(other) => other._toMint() - this;
582 582
583 int _mulFromInteger(other) => other._toMint() * this; 583 int _mulFromInteger(other) => other._toMint() * this;
584 584
(...skipping 16 matching lines...) Expand all
601 bool _compareEqFromInteger(other) => other._toMint() == this; 601 bool _compareEqFromInteger(other) => other._toMint() == this;
602 602
603 bool _compareLtFromInteger(other) => other._toMint() < this; 603 bool _compareLtFromInteger(other) => other._toMint() < this;
604 604
605 bool _compareLeFromInteger(other) => other._toMint() <= this; 605 bool _compareLeFromInteger(other) => other._toMint() <= this;
606 606
607 bool _compareGtFromInteger(other) => other._toMint() > this; 607 bool _compareGtFromInteger(other) => other._toMint() > this;
608 608
609 bool _compareGeFromInteger(other) => other._toMint() >= this; 609 bool _compareGeFromInteger(other) => other._toMint() >= this;
610 } 610 }
OLDNEW
« no previous file with comments | « lib/core/embedded_core_patch.dart ('k') | lib/core/string.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698