1 #!/usr/bin/env python 2 3 """ 4 Simple built-in classes and functions. 5 6 Copyright (C) 2005, 2006 Paul Boddie <paul@boddie.org.uk> 7 8 This software is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License as 10 published by the Free Software Foundation; either version 2 of 11 the License, or (at your option) any later version. 12 13 This software is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; see the file LICENCE.txt 20 If not, write to the Free Software Foundation, Inc., 21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 22 """ 23 24 class boolean: 25 def __true__(self): 26 return self 27 28 def __str__(self): 29 if self: 30 return "True" 31 else: 32 return "False" 33 34 class int: 35 def __iadd__(self, other): 36 if isinstance(other, int): 37 return int() 38 else: 39 raise TypeError 40 41 def __isub__(self, other): 42 if isinstance(other, int): 43 return int() 44 else: 45 raise TypeError 46 47 def __add__(self, other): 48 if isinstance(other, int): 49 return int() 50 else: 51 raise TypeError 52 53 def __radd__(self, other): 54 if isinstance(other, int): 55 return int() 56 else: 57 raise TypeError 58 59 def __sub__(self, other): 60 if isinstance(other, int): 61 return int() 62 else: 63 raise TypeError 64 65 def __rsub__(self, other): 66 if isinstance(other, int): 67 return int() 68 else: 69 raise TypeError 70 71 def __mul__(self, other): 72 if isinstance(other, int): 73 return int() 74 else: 75 raise TypeError 76 77 def __rmul__(self, other): 78 if isinstance(other, int): 79 return int() 80 else: 81 raise TypeError 82 83 def __div__(self, other): 84 if isinstance(other, int): 85 return int() 86 else: 87 raise TypeError 88 89 def __rdiv__(self, other): 90 if isinstance(other, int): 91 return int() 92 else: 93 raise TypeError 94 95 def __pow__(self, other): 96 if isinstance(other, int): 97 return int() 98 else: 99 raise TypeError 100 101 def __lt__(self, other): 102 if isinstance(other, int): 103 return boolean() 104 else: 105 raise TypeError 106 107 def __gt__(self, other): 108 if isinstance(other, int): 109 return boolean() 110 else: 111 raise TypeError 112 113 def __le__(self, other): 114 if isinstance(other, int): 115 return boolean() 116 else: 117 raise TypeError 118 119 def __ge__(self, other): 120 if isinstance(other, int): 121 return boolean() 122 else: 123 raise TypeError 124 125 def __eq__(self, other): 126 if isinstance(other, int): 127 return boolean() 128 else: 129 raise TypeError 130 131 def __ne__(self, other): 132 if isinstance(other, int): 133 return boolean() 134 else: 135 raise TypeError 136 137 def __neg__(self): 138 return int() 139 140 def __pos__(self): 141 return self 142 143 def __str__(self): 144 return string() 145 146 def __true__(self): 147 return self != 0 148 149 class long: 150 def __iadd__(self, other): 151 if isinstance(other, int): 152 return long() 153 elif isinstance(other, long): 154 return long() 155 else: 156 raise TypeError 157 158 def __isub__(self, other): 159 if isinstance(other, int): 160 return long() 161 elif isinstance(other, long): 162 return long() 163 else: 164 raise TypeError 165 166 def __add__(self, other): 167 if isinstance(other, int): 168 return long() 169 elif isinstance(other, long): 170 return long() 171 else: 172 raise TypeError 173 174 def __radd__(self, other): 175 if isinstance(other, int): 176 return long() 177 elif isinstance(other, long): 178 return long() 179 else: 180 raise TypeError 181 182 def __sub__(self, other): 183 if isinstance(other, int): 184 return long() 185 elif isinstance(other, long): 186 return long() 187 else: 188 raise TypeError 189 190 def __rsub__(self, other): 191 if isinstance(other, int): 192 return long() 193 elif isinstance(other, long): 194 return long() 195 else: 196 raise TypeError 197 198 def __lt__(self, other): 199 if isinstance(other, int): 200 return boolean() 201 elif isinstance(other, long): 202 return boolean() 203 else: 204 raise TypeError 205 206 def __gt__(self, other): 207 if isinstance(other, int): 208 return boolean() 209 elif isinstance(other, long): 210 return boolean() 211 else: 212 raise TypeError 213 214 def __le__(self, other): 215 if isinstance(other, int): 216 return boolean() 217 elif isinstance(other, long): 218 return boolean() 219 else: 220 raise TypeError 221 222 def __ge__(self, other): 223 if isinstance(other, int): 224 return boolean() 225 elif isinstance(other, long): 226 return boolean() 227 else: 228 raise TypeError 229 230 def __eq__(self, other): 231 if isinstance(other, int): 232 return boolean() 233 elif isinstance(other, long): 234 return boolean() 235 else: 236 raise TypeError 237 238 def __ne__(self, other): 239 if isinstance(other, int): 240 return boolean() 241 elif isinstance(other, long): 242 return boolean() 243 else: 244 raise TypeError 245 246 def __neg__(self): 247 return long() 248 249 def __pos__(self): 250 return self 251 252 def __str__(self): 253 return string() 254 255 def __true__(self): 256 return self != 0 257 258 class float: 259 def __iadd__(self, other): 260 if isinstance(other, int): 261 return float() 262 elif isinstance(other, long): 263 return float() 264 elif isinstance(other, float): 265 return float() 266 else: 267 raise TypeError 268 269 def __isub__(self, other): 270 if isinstance(other, int): 271 return float() 272 elif isinstance(other, long): 273 return float() 274 elif isinstance(other, float): 275 return float() 276 else: 277 raise TypeError 278 279 def __add__(self, other): 280 if isinstance(other, int): 281 return float() 282 elif isinstance(other, long): 283 return float() 284 elif isinstance(other, float): 285 return float() 286 else: 287 raise TypeError 288 289 def __radd__(self, other): 290 if isinstance(other, int): 291 return float() 292 elif isinstance(other, long): 293 return float() 294 elif isinstance(other, float): 295 return float() 296 else: 297 raise TypeError 298 299 def __sub__(self, other): 300 if isinstance(other, int): 301 return float() 302 elif isinstance(other, long): 303 return float() 304 elif isinstance(other, float): 305 return float() 306 else: 307 raise TypeError 308 309 def __rsub__(self, other): 310 if isinstance(other, int): 311 return float() 312 elif isinstance(other, long): 313 return float() 314 elif isinstance(other, float): 315 return float() 316 else: 317 raise TypeError 318 319 def __mul__(self, other): 320 if isinstance(other, int): 321 return float() 322 elif isinstance(other, long): 323 return float() 324 elif isinstance(other, float): 325 return float() 326 else: 327 raise TypeError 328 329 def __rmul__(self, other): 330 if isinstance(other, int): 331 return float() 332 elif isinstance(other, long): 333 return float() 334 elif isinstance(other, float): 335 return float() 336 else: 337 raise TypeError 338 339 def __div__(self, other): 340 if isinstance(other, int): 341 return float() 342 elif isinstance(other, long): 343 return float() 344 elif isinstance(other, float): 345 return float() 346 else: 347 raise TypeError 348 349 def __rdiv__(self, other): 350 if isinstance(other, int): 351 return float() 352 elif isinstance(other, long): 353 return float() 354 elif isinstance(other, float): 355 return float() 356 else: 357 raise TypeError 358 359 def __pow__(self, other): 360 if isinstance(other, int): 361 return float() 362 elif isinstance(other, long): 363 return float() 364 elif isinstance(other, float): 365 return float() 366 else: 367 raise TypeError 368 369 def __rpow__(self, other): 370 if isinstance(other, int): 371 return float() 372 elif isinstance(other, long): 373 return float() 374 elif isinstance(other, float): 375 return float() 376 else: 377 raise TypeError 378 379 def __lt__(self, other): 380 if isinstance(other, int): 381 return boolean() 382 elif isinstance(other, long): 383 return boolean() 384 elif isinstance(other, float): 385 return boolean() 386 else: 387 raise TypeError 388 389 def __gt__(self, other): 390 if isinstance(other, int): 391 return boolean() 392 elif isinstance(other, long): 393 return boolean() 394 elif isinstance(other, float): 395 return boolean() 396 else: 397 raise TypeError 398 399 def __le__(self, other): 400 if isinstance(other, int): 401 return boolean() 402 elif isinstance(other, long): 403 return boolean() 404 elif isinstance(other, float): 405 return boolean() 406 else: 407 raise TypeError 408 409 def __ge__(self, other): 410 if isinstance(other, int): 411 return boolean() 412 elif isinstance(other, long): 413 return boolean() 414 elif isinstance(other, float): 415 return boolean() 416 else: 417 raise TypeError 418 419 def __eq__(self, other): 420 if isinstance(other, int): 421 return boolean() 422 elif isinstance(other, long): 423 return boolean() 424 elif isinstance(other, float): 425 return boolean() 426 else: 427 raise TypeError 428 429 def __ne__(self, other): 430 if isinstance(other, int): 431 return boolean() 432 elif isinstance(other, long): 433 return boolean() 434 elif isinstance(other, float): 435 return boolean() 436 else: 437 raise TypeError 438 439 def __neg__(self): 440 return float() 441 442 def __pos__(self): 443 return self 444 445 def __str__(self): 446 return string() 447 448 def __true__(self): 449 return self != 0 450 451 class str: 452 def __init__(self, x=None): 453 x.__str__() 454 455 def __add__(self, other): 456 if isinstance(other, string): 457 return string() 458 else: 459 raise TypeError 460 461 def __radd__(self, other): 462 if isinstance(other, string): 463 return string() 464 else: 465 raise TypeError 466 467 def __len__(self): 468 return int() 469 470 def __str__(self): 471 return self 472 473 def __true__(self): 474 return self.__len__() != 0 475 476 def join(self, l): 477 total = 0 478 first = 1 479 self_len = self.__len__() 480 for i in l: 481 if not first: 482 total += self_len 483 total += len(str(i)) 484 first = 0 485 b = buffer(total) 486 first = 1 487 for i in l: 488 if not first: 489 b.append(self) 490 b.append(str(i)) 491 first = 0 492 s = str(b) 493 return s 494 495 class buffer: 496 def __init__(self, size): 497 if not isinstance(size, int): 498 raise TypeError 499 500 def append(self, s): 501 if not isinstance(s, string): 502 raise TypeError 503 pass 504 505 def __str__(self): 506 return string() 507 508 class list: 509 def __init__(self, *args): 510 self.next = None 511 self.last = self 512 for arg in args: 513 self.append(arg) 514 515 def __getitem__(self, index): 516 i = 0 517 n = self 518 # NOTE: Support negative indices using last. 519 while i < index and n.next is not None: 520 n = n.next 521 i += 1 522 if n.next is not None: 523 return n.value 524 else: 525 raise IndexError() # NOTE: Make this compliant with Python! 526 527 def __setitem__(self, index, value): 528 i = 0 529 n = self 530 # NOTE: Support negative indices using last. 531 while i < index and n.next is not None: 532 n = n.next 533 i += 1 534 if n.next is not None: 535 n.value = value 536 else: 537 raise IndexError() # NOTE: Make this compliant with Python! 538 539 def __getslice__(self, start, end=None): 540 slice = [] 541 i = 0 542 n = self 543 # NOTE: Support negative indices using last. 544 while (end is None or i < end) and n.next is not None: 545 if i >= start: 546 slice.append(n.value) 547 n = n.next 548 i += 1 549 return slice 550 551 def __setslice__(self, start, end, slice): 552 i = 0 553 n = self 554 j = 0 555 p = slice 556 # NOTE: Support negative indices using last. 557 # NOTE: Support appending when start >= len(self). 558 while i < end and n is not None and p is not None: 559 if i >= start: 560 n.value = p.value 561 p = p.next 562 j += 1 563 n = n.next 564 i += 1 565 566 def append(self, value): 567 n = self.last 568 n.value = value 569 n.next = self.__class__() 570 self.last = n.next 571 572 def __len__(self): 573 i = 0 574 n = self 575 while n.next is not None: 576 n = n.next 577 i += 1 578 return i 579 580 def __add__(self, other): 581 result = self.__class__() 582 for value in self: 583 result.append(value) 584 for value in other: 585 result.append(value) 586 return result 587 588 def __iadd__(self, other): 589 for value in other: 590 self.append(value) 591 return self 592 593 def __str__(self): 594 output = ["["] 595 n = self 596 first = 1 597 while n.next is not None: 598 if not first: 599 output.append(", ") 600 else: 601 first = 0 602 output.append(str(n.value)) 603 n = n.next 604 output.append("]") 605 return "".join(output) 606 607 def __iter__(self): 608 return listiterator(self) 609 610 def __true__(self): 611 return self.__len__() != 0 612 613 class listiterator: 614 def __init__(self, l): 615 self.l = l 616 617 def next(self): 618 l = self.l 619 next = l.next 620 if next is not None: 621 self.l = next 622 return l.value 623 else: 624 raise StopIteration 625 626 def __true__(self): 627 return boolean() 628 629 class tuple: 630 def __init__(self, *args): 631 self.next = None 632 self.last = self 633 for arg in args: 634 self.append(arg) 635 636 def __getitem__(self, index): 637 i = 0 638 n = self 639 # NOTE: Support negative indices using last. 640 while i < index and n.next is not None: 641 n = n.next 642 i += 1 643 if n.next is not None: 644 return n.value 645 else: 646 raise IndexError() # NOTE: Make this compliant with Python! 647 648 def __getslice__(self, start, end=None): 649 # NOTE: Should probably return a tuple. 650 slice = [] 651 i = 0 652 n = self 653 # NOTE: Support negative indices using last. 654 while (end is None or i < end) and n.next is not None: 655 if i >= start: 656 slice.append(n.value) 657 n = n.next 658 i += 1 659 return slice 660 661 # NOTE: The append method should be internal at most. 662 663 def append(self, value): 664 n = self.last 665 n.value = value 666 n.next = self.__class__() 667 self.last = n.next 668 669 def __len__(self): 670 i = 0 671 n = self 672 while n.next is not None: 673 n = n.next 674 i += 1 675 return i 676 677 def __add__(self, other): 678 result = self.__class__() 679 for value in self: 680 result.append(value) 681 for value in other: 682 result.append(value) 683 return result 684 685 def __str__(self): 686 output = ["("] 687 n = self 688 first = 1 689 while n.next is not None: 690 if not first: 691 output.append(", ") 692 else: 693 first = 0 694 output.append(str(n.value)) 695 n = n.next 696 output.append(")") 697 return "".join(output) 698 699 def __iter__(self): 700 return tupleiterator(self) 701 702 def __true__(self): 703 return self.__len__() != 0 704 705 class tupleiterator: 706 def __init__(self, l): 707 self.l = l 708 709 def next(self): 710 l = self.l 711 next = l.next 712 if next is not None: 713 self.l = next 714 return l.value 715 else: 716 raise StopIteration 717 718 def __true__(self): 719 return boolean() 720 721 class dict: 722 pass 723 724 class Exception: 725 pass 726 727 class StopIteration(Exception): 728 pass 729 730 class IndexError(Exception): 731 pass 732 733 class AttributeError(Exception): 734 pass 735 736 class TypeError(Exception): 737 pass 738 739 class none: 740 def __true__(self): 741 return False 742 743 def __str__(self): 744 return "None" 745 746 class undefined: 747 pass 748 749 class xrange: 750 def __init__(self, start, end, step=1): 751 self.start = start 752 self.end = end 753 self.step = step 754 self.current = start 755 756 def __iter__(self): 757 return self 758 759 def next(self): 760 if self.current >= self.end: 761 raise StopIteration 762 current = self.current 763 self.current += self.step 764 return current 765 766 # General functions. 767 768 def isinstance(obj, cls): 769 return boolean() 770 771 def issubclass(cls1, cls2): 772 return boolean() 773 774 def len(x): 775 return x.__len__() 776 777 def max(*l): 778 max_so_far = l[0] 779 for i in l[1:]: 780 if i > max_so_far: 781 max_so_far = i 782 return max_so_far 783 784 def range(start, end, step=None): 785 if start == end: 786 return [] 787 elif start > end: 788 step = step or 1 789 i = start 790 result = [] 791 while i < end: 792 result.append(i) 793 i += 1 794 return result 795 else: 796 step = step or -1 797 i = start 798 result = [] 799 while i > end: 800 result.append(i) 801 i -= 1 802 return result 803 804 # Special values. None of these definitions should be generated by the compiler. 805 # All such definitions should be made in the underlying implementation. 806 807 True = boolean() 808 False = boolean() 809 None = none() 810 Undefined = undefined() 811 812 # Special functions. These all operate on references at run-time. 813 814 def __is__(a, b): 815 return boolean() 816 817 def __is_not__(a, b): 818 return boolean() 819 820 def __not__(a): 821 return boolean() 822 823 # vim: tabstop=4 expandtab shiftwidth=4