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