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 __str__(self): 586 output = ["["] 587 n = self 588 first = 1 589 while n.next is not None: 590 if not first: 591 output.append(", ") 592 else: 593 first = 0 594 output.append(str(n.value)) 595 n = n.next 596 output.append("]") 597 return "".join(output) 598 599 def __iter__(self): 600 return listiterator(self) 601 602 def __true__(self): 603 return self.__len__() != 0 604 605 class listiterator: 606 def __init__(self, l): 607 self.l = l 608 609 def next(self): 610 l = self.l 611 next = l.next 612 if next is not None: 613 self.l = next 614 return l.value 615 else: 616 raise StopIteration 617 618 def __true__(self): 619 return boolean() 620 621 class tuple: 622 def __init__(self, *args): 623 self.next = None 624 self.last = self 625 for arg in args: 626 self.append(arg) 627 628 def __getitem__(self, index): 629 i = 0 630 n = self 631 # NOTE: Support negative indices using last. 632 while i < index and n.next is not None: 633 n = n.next 634 i += 1 635 if n.next is not None: 636 return n.value 637 else: 638 raise IndexError() # NOTE: Make this compliant with Python! 639 640 def __getslice__(self, start, end=None): 641 # NOTE: Should probably return a tuple. 642 slice = [] 643 i = 0 644 n = self 645 # NOTE: Support negative indices using last. 646 while (end is None or i < end) and n.next is not None: 647 if i >= start: 648 slice.append(n.value) 649 n = n.next 650 i += 1 651 return slice 652 653 # NOTE: The append method should be internal at most. 654 655 def append(self, value): 656 n = self.last 657 n.value = value 658 n.next = self.__class__() 659 self.last = n.next 660 661 def __len__(self): 662 i = 0 663 n = self 664 while n.next is not None: 665 n = n.next 666 i += 1 667 return i 668 669 def __add__(self, other): 670 result = self.__class__() 671 for value in self: 672 result.append(value) 673 for value in other: 674 result.append(value) 675 return result 676 677 def __str__(self): 678 output = ["("] 679 n = self 680 first = 1 681 while n.next is not None: 682 if not first: 683 output.append(", ") 684 else: 685 first = 0 686 output.append(str(n.value)) 687 n = n.next 688 output.append(")") 689 return "".join(output) 690 691 def __iter__(self): 692 return tupleiterator(self) 693 694 def __true__(self): 695 return self.__len__() != 0 696 697 class tupleiterator: 698 def __init__(self, l): 699 self.l = l 700 701 def next(self): 702 l = self.l 703 next = l.next 704 if next is not None: 705 self.l = next 706 return l.value 707 else: 708 raise StopIteration 709 710 def __true__(self): 711 return boolean() 712 713 class dict: 714 pass 715 716 class Exception: 717 pass 718 719 class StopIteration(Exception): 720 pass 721 722 class IndexError(Exception): 723 pass 724 725 class AttributeError(Exception): 726 pass 727 728 class TypeError(Exception): 729 pass 730 731 class none: 732 def __true__(self): 733 return False 734 735 def __str__(self): 736 return "None" 737 738 class undefined: 739 pass 740 741 def isinstance(obj, cls): 742 return boolean() 743 744 def issubclass(cls1, cls2): 745 return boolean() 746 747 def len(x): 748 return x.__len__() 749 750 def max(*l): 751 max_so_far = l[0] 752 for i in l[1:]: 753 if i > max_so_far: 754 max_so_far = i 755 return max_so_far 756 757 def str(x): 758 return x.__str__() 759 760 class xrange: 761 def __init__(self, start, end, step=1): 762 self.start = start 763 self.end = end 764 self.step = step 765 self.current = start 766 767 def __iter__(self): 768 return self 769 770 def next(self): 771 if self.current >= self.end: 772 raise StopIteration 773 current = self.current 774 self.current += self.step 775 return current 776 777 # Special values. None of these definitions should be generated by the compiler. 778 # All such definitions should be made in the underlying implementation. 779 780 True = boolean() 781 False = boolean() 782 None = none() 783 Undefined = undefined() 784 785 # Special functions. These all operate on references at run-time. 786 787 def __is__(a, b): 788 return boolean() 789 790 def __is_not__(a, b): 791 return boolean() 792 793 def __not__(a): 794 return boolean() 795 796 # vim: tabstop=4 expandtab shiftwidth=4