simplify

lib/builtins.py

26:41d4b988c94d
2006-07-24 paulb Introduced subprograms for If node bodies so that local merging may be more obvious to the annotator.
     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     25 The docstring annotations have the following meanings:    26     27     NAME            Indicates a "stable" name used by callers of a function    28                     instead of a generated name which would distinguish    29                     different specialisations.    30     31     NATIVE          Means that the class or function body details are not    32                     accurate representations of the actual code and should not    33                     be generated by a compiler.    34     35     INTERCHANGEABLE Indicates that instances of a class are not sufficiently or    36                     meaningfully distinguishable from each other with regard to    37                     instantiation.    38     39     SPECIAL         Indicates that the compiler should try and optimise calls to    40                     the annotated function.    41     42     ATOMIC          Indicates that the class and its instance(s) cannot store    43                     attributes, although some may be predefined (such as the    44                     __true__ method).    45     46 --------    47 NATIVE    48 """    49     50 class boolean:    51     """    52     NATIVE    53     INTERCHANGEABLE    54     """    55     def __true__(self):    56         return self    57     58     def __str__(self):    59         if self:    60             return "True"    61         else:    62             return "False"    63     64 class int:    65     """    66     NATIVE    67     INTERCHANGEABLE    68     """    69     def __iadd__(self, other):    70         """    71         NAME: IMPL.builtins.int.__iadd__    72         NATIVE    73         """    74         if isinstance(other, int):    75             return int()    76         else:    77             TypeConstraintError    78     79     def __isub__(self, other):    80         """    81         NAME: IMPL.builtins.int.__isub__    82         NATIVE    83         """    84         if isinstance(other, int):    85             return int()    86         else:    87             TypeConstraintError    88     89     def __add__(self, other):    90         """    91         NAME: IMPL.builtins.int.__add__    92         NATIVE    93         """    94         if isinstance(other, int):    95             return int()    96         else:    97             TypeConstraintError    98     99     def __radd__(self, other):   100         """   101         NAME: IMPL.builtins.int.__radd__   102         NATIVE   103         """   104         if isinstance(other, int):   105             return int()   106         else:   107             TypeConstraintError   108    109     def __sub__(self, other):   110         """   111         NAME: IMPL.builtins.int.__sub__   112         NATIVE   113         """   114         if isinstance(other, int):   115             return int()   116         else:   117             TypeConstraintError   118    119     def __rsub__(self, other):   120         """   121         NAME: IMPL.builtins.int.__rsub__   122         NATIVE   123         """   124         if isinstance(other, int):   125             return int()   126         else:   127             TypeConstraintError   128    129     def __mul__(self, other):   130         """   131         NAME: IMPL.builtins.int.__mul__   132         NATIVE   133         """   134         if isinstance(other, int):   135             return int()   136         else:   137             TypeConstraintError   138    139     def __rmul__(self, other):   140         """   141         NAME: IMPL.builtins.int.__rmul__   142         NATIVE   143         """   144         if isinstance(other, int):   145             return int()   146         else:   147             TypeConstraintError   148    149     def __div__(self, other):   150         """   151         NAME: IMPL.builtins.int.__div__   152         NATIVE   153         """   154         if isinstance(other, int):   155             return int()   156         else:   157             TypeConstraintError   158    159     def __rdiv__(self, other):   160         """   161         NAME: IMPL.builtins.int.__rdiv__   162         NATIVE   163         """   164         if isinstance(other, int):   165             return int()   166         else:   167             TypeConstraintError   168    169     def __pow__(self, other):   170         """   171         NAME: IMPL.builtins.int.__pow__   172         NATIVE   173         """   174         if isinstance(other, int):   175             return int()   176         else:   177             TypeConstraintError   178    179     def __lt__(self, other):   180         """   181         NAME: IMPL.builtins.int.__lt__   182         NATIVE   183         """   184         if isinstance(other, int):   185             return boolean()   186         else:   187             TypeConstraintError   188    189     def __gt__(self, other):   190         """   191         NAME: IMPL.builtins.int.__gt__   192         NATIVE   193         """   194         if isinstance(other, int):   195             return boolean()   196         else:   197             TypeConstraintError   198    199     def __le__(self, other):   200         """   201         NAME: IMPL.builtins.int.__le__   202         NATIVE   203         """   204         if isinstance(other, int):   205             return boolean()   206         else:   207             TypeConstraintError   208    209     def __ge__(self, other):   210         """   211         NAME: IMPL.builtins.int.__ge__   212         NATIVE   213         """   214         if isinstance(other, int):   215             return boolean()   216         else:   217             TypeConstraintError   218    219     def __eq__(self, other):   220         """   221         NAME: IMPL.builtins.int.__eq__   222         NATIVE   223         """   224         if isinstance(other, int):   225             return boolean()   226         else:   227             TypeConstraintError   228    229     def __ne__(self, other):   230         """   231         NAME: IMPL.builtins.int.__ne__   232         NATIVE   233         """   234         if isinstance(other, int):   235             return boolean()   236         else:   237             TypeConstraintError   238    239     def __neg__(self):   240         """   241         NAME: IMPL.builtins.int.__neg__   242         NATIVE   243         """   244         return int()   245    246     def __pos__(self):   247         return self   248    249     def __str__(self):   250         """   251         NAME: IMPL.builtins.int.__str__   252         NATIVE   253         """   254         return string()   255    256     def __true__(self):   257         return self != 0   258    259 class long:   260     """   261     NATIVE   262     INTERCHANGEABLE   263     """   264     def __iadd__(self, other):   265         """   266         NAME: IMPL.builtins.long.__iadd__   267         NATIVE   268         """   269         if isinstance(other, int):   270             return long()   271         elif isinstance(other, long):   272             return long()   273         else:   274             TypeConstraintError   275    276     def __isub__(self, other):   277         """   278         NAME: IMPL.builtins.long.__isub__   279         NATIVE   280         """   281         if isinstance(other, int):   282             return long()   283         elif isinstance(other, long):   284             return long()   285         else:   286             TypeConstraintError   287    288     def __add__(self, other):   289         """   290         NAME: IMPL.builtins.long.__add__   291         NATIVE   292         """   293         if isinstance(other, int):   294             return long()   295         elif isinstance(other, long):   296             return long()   297         else:   298             TypeConstraintError   299    300     def __radd__(self, other):   301         """   302         NAME: IMPL.builtins.long.__radd__   303         NATIVE   304         """   305         if isinstance(other, int):   306             return long()   307         elif isinstance(other, long):   308             return long()   309         else:   310             TypeConstraintError   311    312     def __sub__(self, other):   313         """   314         NAME: IMPL.builtins.long.__sub__   315         NATIVE   316         """   317         if isinstance(other, int):   318             return long()   319         elif isinstance(other, long):   320             return long()   321         else:   322             TypeConstraintError   323    324     def __rsub__(self, other):   325         """   326         NAME: IMPL.builtins.long.__rsub__   327         NATIVE   328         """   329         if isinstance(other, int):   330             return long()   331         elif isinstance(other, long):   332             return long()   333         else:   334             TypeConstraintError   335    336     def __lt__(self, other):   337         """   338         NAME: IMPL.builtins.long.__lt__   339         NATIVE   340         """   341         if isinstance(other, int):   342             return boolean()   343         elif isinstance(other, long):   344             return boolean()   345         else:   346             TypeConstraintError   347    348     def __gt__(self, other):   349         """   350         NAME: IMPL.builtins.long.__gt__   351         NATIVE   352         """   353         if isinstance(other, int):   354             return boolean()   355         elif isinstance(other, long):   356             return boolean()   357         else:   358             TypeConstraintError   359    360     def __le__(self, other):   361         """   362         NAME: IMPL.builtins.long.__le__   363         NATIVE   364         """   365         if isinstance(other, int):   366             return boolean()   367         elif isinstance(other, long):   368             return boolean()   369         else:   370             TypeConstraintError   371    372     def __ge__(self, other):   373         """   374         NAME: IMPL.builtins.long.__ge__   375         NATIVE   376         """   377         if isinstance(other, int):   378             return boolean()   379         elif isinstance(other, long):   380             return boolean()   381         else:   382             TypeConstraintError   383    384     def __eq__(self, other):   385         """   386         NAME: IMPL.builtins.long.__eq__   387         NATIVE   388         """   389         if isinstance(other, int):   390             return boolean()   391         elif isinstance(other, long):   392             return boolean()   393         else:   394             TypeConstraintError   395    396     def __ne__(self, other):   397         """   398         NAME: IMPL.builtins.long.__ne__   399         NATIVE   400         """   401         if isinstance(other, int):   402             return boolean()   403         elif isinstance(other, long):   404             return boolean()   405         else:   406             TypeConstraintError   407    408     def __neg__(self):   409         """   410         NAME: IMPL.builtins.long.__neg__   411         NATIVE   412         """   413         return long()   414    415     def __pos__(self):   416         return self   417    418     def __str__(self):   419         """   420         NAME: IMPL.builtins.long.__str__   421         NATIVE   422         """   423         return string()   424    425     def __true__(self):   426         return self != 0   427    428 class float:   429     """   430     NATIVE   431     INTERCHANGEABLE   432     """   433     def __iadd__(self, other):   434         """   435         NAME: IMPL.builtins.float.__iadd__   436         NATIVE   437         """   438         if isinstance(other, int):   439             return float()   440         elif isinstance(other, long):   441             return float()   442         elif isinstance(other, float):   443             return float()   444         else:   445             TypeConstraintError   446    447     def __isub__(self, other):   448         """   449         NAME: IMPL.builtins.float.__isub__   450         NATIVE   451         """   452         if isinstance(other, int):   453             return float()   454         elif isinstance(other, long):   455             return float()   456         elif isinstance(other, float):   457             return float()   458         else:   459             TypeConstraintError   460    461     def __add__(self, other):   462         """   463         NAME: IMPL.builtins.float.__add__   464         NATIVE   465         """   466         if isinstance(other, int):   467             return float()   468         elif isinstance(other, long):   469             return float()   470         elif isinstance(other, float):   471             return float()   472         else:   473             TypeConstraintError   474    475     def __radd__(self, other):   476         """   477         NAME: IMPL.builtins.float.__radd__   478         NATIVE   479         """   480         if isinstance(other, int):   481             return float()   482         elif isinstance(other, long):   483             return float()   484         elif isinstance(other, float):   485             return float()   486         else:   487             TypeConstraintError   488    489     def __sub__(self, other):   490         """   491         NAME: IMPL.builtins.float.__sub__   492         NATIVE   493         """   494         if isinstance(other, int):   495             return float()   496         elif isinstance(other, long):   497             return float()   498         elif isinstance(other, float):   499             return float()   500         else:   501             TypeConstraintError   502    503     def __rsub__(self, other):   504         """   505         NAME: IMPL.builtins.float.__rsub__   506         NATIVE   507         """   508         if isinstance(other, int):   509             return float()   510         elif isinstance(other, long):   511             return float()   512         elif isinstance(other, float):   513             return float()   514         else:   515             TypeConstraintError   516    517     def __mul__(self, other):   518         """   519         NAME: IMPL.builtins.float.__mul__   520         NATIVE   521         """   522         if isinstance(other, int):   523             return float()   524         elif isinstance(other, long):   525             return float()   526         elif isinstance(other, float):   527             return float()   528         else:   529             TypeConstraintError   530    531     def __rmul__(self, other):   532         """   533         NAME: IMPL.builtins.float.__rmul__   534         NATIVE   535         """   536         if isinstance(other, int):   537             return float()   538         elif isinstance(other, long):   539             return float()   540         elif isinstance(other, float):   541             return float()   542         else:   543             TypeConstraintError   544    545     def __div__(self, other):   546         """   547         NAME: IMPL.builtins.float.__div__   548         NATIVE   549         """   550         if isinstance(other, int):   551             return float()   552         elif isinstance(other, long):   553             return float()   554         elif isinstance(other, float):   555             return float()   556         else:   557             TypeConstraintError   558    559     def __rdiv__(self, other):   560         """   561         NAME: IMPL.builtins.float.__rdiv__   562         NATIVE   563         """   564         if isinstance(other, int):   565             return float()   566         elif isinstance(other, long):   567             return float()   568         elif isinstance(other, float):   569             return float()   570         else:   571             TypeConstraintError   572    573     def __pow__(self, other):   574         """   575         NAME: IMPL.builtins.float.__pow__   576         NATIVE   577         """   578         if isinstance(other, int):   579             return float()   580         elif isinstance(other, long):   581             return float()   582         elif isinstance(other, float):   583             return float()   584         else:   585             TypeConstraintError   586    587     def __rpow__(self, other):   588         """   589         NAME: IMPL.builtins.float.__rpow__   590         NATIVE   591         """   592         if isinstance(other, int):   593             return float()   594         elif isinstance(other, long):   595             return float()   596         elif isinstance(other, float):   597             return float()   598         else:   599             TypeConstraintError   600    601     def __lt__(self, other):   602         """   603         NAME: IMPL.builtins.float.__lt__   604         NATIVE   605         """   606         if isinstance(other, int):   607             return boolean()   608         elif isinstance(other, long):   609             return boolean()   610         elif isinstance(other, float):   611             return boolean()   612         else:   613             TypeConstraintError   614    615     def __gt__(self, other):   616         """   617         NAME: IMPL.builtins.float.__gt__   618         NATIVE   619         """   620         if isinstance(other, int):   621             return boolean()   622         elif isinstance(other, long):   623             return boolean()   624         elif isinstance(other, float):   625             return boolean()   626         else:   627             TypeConstraintError   628    629     def __le__(self, other):   630         """   631         NAME: IMPL.builtins.float.__le__   632         NATIVE   633         """   634         if isinstance(other, int):   635             return boolean()   636         elif isinstance(other, long):   637             return boolean()   638         elif isinstance(other, float):   639             return boolean()   640         else:   641             TypeConstraintError   642    643     def __ge__(self, other):   644         """   645         NAME: IMPL.builtins.float.__ge__   646         NATIVE   647         """   648         if isinstance(other, int):   649             return boolean()   650         elif isinstance(other, long):   651             return boolean()   652         elif isinstance(other, float):   653             return boolean()   654         else:   655             TypeConstraintError   656    657     def __eq__(self, other):   658         """   659         NAME: IMPL.builtins.float.__eq__   660         NATIVE   661         """   662         if isinstance(other, int):   663             return boolean()   664         elif isinstance(other, long):   665             return boolean()   666         elif isinstance(other, float):   667             return boolean()   668         else:   669             TypeConstraintError   670    671     def __ne__(self, other):   672         """   673         NAME: IMPL.builtins.float.__ne__   674         NATIVE   675         """   676         if isinstance(other, int):   677             return boolean()   678         elif isinstance(other, long):   679             return boolean()   680         elif isinstance(other, float):   681             return boolean()   682         else:   683             TypeConstraintError   684    685     def __neg__(self):   686         """   687         NAME: IMPL.builtins.float.__neg__   688         NATIVE   689         """   690         return float()   691    692     def __pos__(self):   693         return self   694    695     def __str__(self):   696         """   697         NAME: IMPL.builtins.float.__str__   698         NATIVE   699         """   700         return string()   701    702     def __true__(self):   703         return self != 0   704    705 class string:   706     """   707     NATIVE   708     INTERCHANGEABLE   709     """   710     def __add__(self, other):   711         """   712         NAME: IMPL.builtins.string.__add__   713         NATIVE   714         """   715         if isinstance(other, string):   716             return string()   717         else:   718             TypeConstraintError   719    720     def __radd__(self, other):   721         """   722         NAME: IMPL.builtins.string.__radd__   723         NATIVE   724         """   725         if isinstance(other, string):   726             return string()   727         else:   728             TypeConstraintError   729    730     def __len__(self):   731         """   732         NAME: IMPL.builtins.string.__len__   733         NATIVE   734         """   735         return int()   736    737     def __str__(self):   738         return self   739    740     def __true__(self):   741         return self.__len__() != 0   742    743     def join(self, l):   744         total = 0   745         first = 1   746         self_len = self.__len__()   747         for i in l:   748             if not first:   749                 total += self_len   750             total += len(str(i))   751             first = 0   752         b = buffer(total)   753         first = 1   754         for i in l:   755             if not first:   756                 b.append(self)   757             b.append(str(i))   758             first = 0   759         s = str(b)   760         return s   761    762 class buffer:   763     """   764     NATIVE   765     INTERCHANGEABLE   766     """   767     def __init__(self, size):   768         """   769         NAME: IMPL.builtins.buffer.__init__   770         NATIVE   771         """   772         if not isinstance(size, int):   773             TypeConstraintError   774    775     def append(self, s):   776         """   777         NAME: IMPL.builtins.buffer.append   778         NATIVE   779         """   780         if not isinstance(s, string):   781             TypeConstraintError   782         pass   783    784     def __str__(self):   785         """   786         NAME: IMPL.builtins.buffer.__str__   787         NATIVE   788         """   789         return string()   790    791 class list:   792     def __init__(self):   793         self.next = None   794         self.last = self   795    796     def __getitem__(self, index):   797         i = 0   798         n = self   799         # NOTE: Support negative indices using last.   800         while i < index and n.next is not None:   801             n = n.next   802             i += 1   803         if n.next is not None:   804             return n.value   805         else:   806             raise IndexError() # NOTE: Make this compliant with Python!   807    808     def __setitem__(self, index, value):   809         i = 0   810         n = self   811         # NOTE: Support negative indices using last.   812         while i < index and n.next is not None:   813             n = n.next   814             i += 1   815         if n.next is not None:   816             n.value = value   817         else:   818             raise IndexError() # NOTE: Make this compliant with Python!   819    820     def __getslice__(self, start, end=None):   821         slice = []   822         i = 0   823         n = self   824         # NOTE: Support negative indices using last.   825         while (end is None or i < end) and n.next is not None:   826             if i >= start:   827                 slice.append(n.value)   828             n = n.next   829             i += 1   830         return slice   831    832     def __setslice__(self, start, end, slice):   833         i = 0   834         n = self   835         j = 0   836         p = slice   837         # NOTE: Support negative indices using last.   838         # NOTE: Support appending when start >= len(self).   839         while i < end and n is not None and p is not None:   840             if i >= start:   841                 n.value = p.value   842                 p = p.next   843                 j += 1   844             n = n.next   845             i += 1   846    847     def append(self, value):   848         n = self.last   849         n.value = value   850         n.next = self.__class__()   851         self.last = n.next   852    853     def __len__(self):   854         i = 0   855         n = self   856         while n.next is not None:   857             n = n.next   858             i += 1   859         return i   860    861     def __add__(self, other):   862         result = self.__class__()   863         for value in self:   864             result.append(value)   865         for value in other:   866             result.append(value)   867         return result   868    869     def __str__(self):   870         output = ["["]   871         n = self   872         first = 1   873         while n.next is not None:   874             if not first:   875                 output.append(", ")   876             else:   877                 first = 0   878             output.append(str(n.value))   879             n = n.next   880         output.append("]")   881         return "".join(output)   882    883     def __iter__(self):   884         return listiterator(self)   885    886     def __true__(self):   887         return self.__len__() != 0   888    889 class listiterator:   890     def __init__(self, l):   891         self.l = l   892    893     def next(self):   894         l = self.l   895         next = l.next   896         if next is not None:   897             self.l = next   898             return l.value   899         else:   900             raise StopIteration() # NOTE: Make this compliant with Python!   901    902     def __true__(self):   903         """   904         NAME: IMPL.builtins.int.__true__   905         NATIVE   906         """   907         return boolean()   908    909 class tuple:   910     def __init__(self):   911         self.next = None   912         self.last = self   913    914     def __getitem__(self, index):   915         i = 0   916         n = self   917         # NOTE: Support negative indices using last.   918         while i < index and n.next is not None:   919             n = n.next   920             i += 1   921         if n.next is not None:   922             return n.value   923         else:   924             raise IndexError() # NOTE: Make this compliant with Python!   925    926     def __getslice__(self, start, end=None):   927         # NOTE: Should probably return a tuple.   928         slice = []   929         i = 0   930         n = self   931         # NOTE: Support negative indices using last.   932         while (end is None or i < end) and n.next is not None:   933             if i >= start:   934                 slice.append(n.value)   935             n = n.next   936             i += 1   937         return slice   938    939     # NOTE: The append method should be internal at most.   940    941     def append(self, value):   942         n = self.last   943         n.value = value   944         n.next = self.__class__()   945         self.last = n.next   946    947     def __len__(self):   948         i = 0   949         n = self   950         while n.next is not None:   951             n = n.next   952             i += 1   953         return i   954    955     def __add__(self, other):   956         result = self.__class__()   957         for value in self:   958             result.append(value)   959         for value in other:   960             result.append(value)   961         return result   962    963     def __str__(self):   964         output = ["("]   965         n = self   966         first = 1   967         while n.next is not None:   968             if not first:   969                 output.append(", ")   970             else:   971                 first = 0   972             output.append(str(n.value))   973             n = n.next   974         output.append(")")   975         return "".join(output)   976    977     def __iter__(self):   978         return tupleiterator(self)   979    980     def __true__(self):   981         return self.__len__() != 0   982    983 class tupleiterator:   984     def __init__(self, l):   985         self.l = l   986    987     def next(self):   988         l = self.l   989         next = l.next   990         if next is not None:   991             self.l = next   992             return l.value   993         else:   994             raise StopIteration() # NOTE: Make this compliant with Python!   995    996     def __true__(self):   997         """   998         NAME: IMPL.builtins.int.__true__   999         NATIVE  1000         """  1001         return boolean()  1002   1003 class Exception:  1004     pass  1005   1006 class StopIteration(Exception):  1007     pass  1008   1009 class IndexError(Exception):  1010     pass  1011   1012 class AttributeError(Exception):  1013     pass  1014   1015 class TypeError(Exception):  1016     pass  1017   1018 class none:  1019     """  1020     NATIVE  1021     INTERCHANGEABLE  1022     ATOMIC  1023     """  1024     def __true__(self):  1025         return False  1026   1027     def __str__(self):  1028         return "None"  1029   1030 class undefined:  1031     """  1032     NATIVE  1033     INTERCHANGEABLE  1034     ATOMIC  1035     """  1036     pass  1037   1038 def isinstance(obj, cls):  1039     """  1040     NAME: IMPL.builtins.isinstance  1041     NATIVE  1042     SPECIAL  1043     """  1044     return boolean()  1045   1046 def issubclass(cls1, cls2):  1047     """  1048     NAME: IMPL.builtins.isinstance  1049     NATIVE  1050     SPECIAL  1051     """  1052     return boolean()  1053   1054 def len(x):  1055     return x.__len__()  1056   1057 def max(*l):  1058     max_so_far = l[0]  1059     for i in l[1:]:  1060         if i > max_so_far:  1061             max_so_far = i  1062     return max_so_far  1063   1064 def str(x):  1065     return x.__str__()  1066   1067 class xrange:  1068     def __init__(self, start, end, step=1):  1069         self.start = start  1070         self.end = end  1071         self.step = step  1072         self.current = start  1073   1074     def __iter__(self):  1075         return self  1076   1077     def next(self):  1078         if self.current >= self.end:  1079             raise StopIteration()  1080         current = self.current  1081         self.current += self.step  1082         return current  1083   1084 # Special values. None of these definitions should be generated by the compiler.  1085 # All such definitions should be made in the underlying implementation.  1086   1087 True = boolean()  1088 False = boolean()  1089 None = none()  1090 Undefined = undefined()  1091   1092 # Special functions. These all operate on references at run-time.  1093   1094 def __is__(a, b):  1095     """  1096     NAME: IMPL.builtins.__is__  1097     NATIVE  1098     """  1099     return boolean()  1100   1101 def __is_not__(a, b):  1102     """  1103     NAME: IMPL.builtins.__is_not__  1104     NATIVE  1105     """  1106     return boolean()  1107   1108 def __not__(a):  1109     """  1110     NAME: IMPL.builtins.__not__  1111     NATIVE  1112     """  1113     return boolean()  1114   1115 # vim: tabstop=4 expandtab shiftwidth=4