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 string: 452 def __add__(self, other): 453 if isinstance(other, string): 454 return string() 455 else: 456 raise TypeError 457 458 def __radd__(self, other): 459 if isinstance(other, string): 460 return string() 461 else: 462 raise TypeError 463 464 def __len__(self): 465 return int() 466 467 def __str__(self): 468 return self 469 470 def __true__(self): 471 return self.__len__() != 0 472 473 def join(self, l): 474 total = 0 475 first = 1 476 self_len = self.__len__() 477 for i in l: 478 if not first: 479 total += self_len 480 total += len(str(i)) 481 first = 0 482 b = buffer(total) 483 first = 1 484 for i in l: 485 if not first: 486 b.append(self) 487 b.append(str(i)) 488 first = 0 489 s = str(b) 490 return s 491 492 class buffer: 493 def __init__(self, size): 494 if not isinstance(size, int): 495 raise TypeError 496 497 def append(self, s): 498 if not isinstance(s, string): 499 raise TypeError 500 pass 501 502 def __str__(self): 503 return string() 504 505 class list: 506 def __init__(self, *args): 507 self.next = None 508 self.last = self 509 for arg in args: 510 self.append(arg) 511 512 def __getitem__(self, index): 513 i = 0 514 n = self 515 # NOTE: Support negative indices using last. 516 while i < index and n.next is not None: 517 n = n.next 518 i += 1 519 if n.next is not None: 520 return n.value 521 else: 522 raise IndexError() # NOTE: Make this compliant with Python! 523 524 def __setitem__(self, index, value): 525 i = 0 526 n = self 527 # NOTE: Support negative indices using last. 528 while i < index and n.next is not None: 529 n = n.next 530 i += 1 531 if n.next is not None: 532 n.value = value 533 else: 534 raise IndexError() # NOTE: Make this compliant with Python! 535 536 def __getslice__(self, start, end=None): 537 slice = [] 538 i = 0 539 n = self 540 # NOTE: Support negative indices using last. 541 while (end is None or i < end) and n.next is not None: 542 if i >= start: 543 slice.append(n.value) 544 n = n.next 545 i += 1 546 return slice 547 548 def __setslice__(self, start, end, slice): 549 i = 0 550 n = self 551 j = 0 552 p = slice 553 # NOTE: Support negative indices using last. 554 # NOTE: Support appending when start >= len(self). 555 while i < end and n is not None and p is not None: 556 if i >= start: 557 n.value = p.value 558 p = p.next 559 j += 1 560 n = n.next 561 i += 1 562 563 def append(self, value): 564 n = self.last 565 n.value = value 566 n.next = self.__class__() 567 self.last = n.next 568 569 def __len__(self): 570 i = 0 571 n = self 572 while n.next is not None: 573 n = n.next 574 i += 1 575 return i 576 577 def __add__(self, other): 578 result = self.__class__() 579 for value in self: 580 result.append(value) 581 for value in other: 582 result.append(value) 583 return result 584 585 def __iadd__(self, other): 586 for value in other: 587 self.append(value) 588 return self 589 590 def __str__(self): 591 output = ["["] 592 n = self 593 first = 1 594 while n.next is not None: 595 if not first: 596 output.append(", ") 597 else: 598 first = 0 599 output.append(str(n.value)) 600 n = n.next 601 output.append("]") 602 return "".join(output) 603 604 def __iter__(self): 605 return listiterator(self) 606 607 def __true__(self): 608 return self.__len__() != 0 609 610 class listiterator: 611 def __init__(self, l): 612 self.l = l 613 614 def next(self): 615 l = self.l 616 next = l.next 617 if next is not None: 618 self.l = next 619 return l.value 620 else: 621 raise StopIteration 622 623 def __true__(self): 624 return boolean() 625 626 class tuple: 627 def __init__(self, *args): 628 self.next = None 629 self.last = self 630 for arg in args: 631 self.append(arg) 632 633 def __getitem__(self, index): 634 i = 0 635 n = self 636 # NOTE: Support negative indices using last. 637 while i < index and n.next is not None: 638 n = n.next 639 i += 1 640 if n.next is not None: 641 return n.value 642 else: 643 raise IndexError() # NOTE: Make this compliant with Python! 644 645 def __getslice__(self, start, end=None): 646 # NOTE: Should probably return a tuple. 647 slice = [] 648 i = 0 649 n = self 650 # NOTE: Support negative indices using last. 651 while (end is None or i < end) and n.next is not None: 652 if i >= start: 653 slice.append(n.value) 654 n = n.next 655 i += 1 656 return slice 657 658 # NOTE: The append method should be internal at most. 659 660 def append(self, value): 661 n = self.last 662 n.value = value 663 n.next = self.__class__() 664 self.last = n.next 665 666 def __len__(self): 667 i = 0 668 n = self 669 while n.next is not None: 670 n = n.next 671 i += 1 672 return i 673 674 def __add__(self, other): 675 result = self.__class__() 676 for value in self: 677 result.append(value) 678 for value in other: 679 result.append(value) 680 return result 681 682 def __str__(self): 683 output = ["("] 684 n = self 685 first = 1 686 while n.next is not None: 687 if not first: 688 output.append(", ") 689 else: 690 first = 0 691 output.append(str(n.value)) 692 n = n.next 693 output.append(")") 694 return "".join(output) 695 696 def __iter__(self): 697 return tupleiterator(self) 698 699 def __true__(self): 700 return self.__len__() != 0 701 702 class tupleiterator: 703 def __init__(self, l): 704 self.l = l 705 706 def next(self): 707 l = self.l 708 next = l.next 709 if next is not None: 710 self.l = next 711 return l.value 712 else: 713 raise StopIteration 714 715 def __true__(self): 716 return boolean() 717 718 class dict: 719 pass 720 721 class Exception: 722 pass 723 724 class StopIteration(Exception): 725 pass 726 727 class IndexError(Exception): 728 pass 729 730 class AttributeError(Exception): 731 pass 732 733 class TypeError(Exception): 734 pass 735 736 class none: 737 def __true__(self): 738 return False 739 740 def __str__(self): 741 return "None" 742 743 class undefined: 744 pass 745 746 class xrange: 747 def __init__(self, start, end, step=1): 748 self.start = start 749 self.end = end 750 self.step = step 751 self.current = start 752 753 def __iter__(self): 754 return self 755 756 def next(self): 757 if self.current >= self.end: 758 raise StopIteration 759 current = self.current 760 self.current += self.step 761 return current 762 763 # General functions. 764 765 def isinstance(obj, cls): 766 return boolean() 767 768 def issubclass(cls1, cls2): 769 return boolean() 770 771 def len(x): 772 return x.__len__() 773 774 def max(*l): 775 max_so_far = l[0] 776 for i in l[1:]: 777 if i > max_so_far: 778 max_so_far = i 779 return max_so_far 780 781 def range(start, end, step=None): 782 if start == end: 783 return [] 784 elif start > end: 785 step = step or 1 786 i = start 787 result = [] 788 while i < end: 789 result.append(i) 790 i += 1 791 return result 792 else: 793 step = step or -1 794 i = start 795 result = [] 796 while i > end: 797 result.append(i) 798 i -= 1 799 return result 800 801 def str(x): 802 return x.__str__() 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