Lichen

lib/__builtins__/int.py

307:fc291b59f67b
2016-12-03 Paul Boddie Implemented the inversion operator for integers.
     1 #!/usr/bin/env python     2      3 """     4 Integer objects.     5      6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.operator import _negate    23 import native    24     25 class int(object):    26     27     "An integer abstraction."    28     29     def __init__(self, number_or_string=None):    30     31         "Initialise the integer with the given 'number_or_string'."    32     33         if native._isinstance(number_or_string, int):    34             self.__data__ = number_or_string.__data__    35         else:    36             # NOTE: To be implemented.    37             self.__data__ = None    38     39     def __hash__(self):    40     41         "Return a value for hashing purposes."    42     43         return self    44     45     def _binary_op(self, op, other):    46     47         "Perform 'op' on this int and 'other' if appropriate."    48     49         if native._isinstance(other, int):    50             return op(self.__data__, other.__data__)    51         else:    52             return NotImplemented    53     54     def _binary_op_rev(self, op, other):    55     56         "Perform 'op' on 'other' and this int if appropriate."    57     58         if native._isinstance(other, int):    59             return op(other.__data__, self.__data__)    60         else:    61             return NotImplemented    62     63     def __iadd__(self, other):    64     65         "Return a new int for the addition of this int and 'other'."    66     67         return self._binary_op(native._int_add, other)    68     69     def __isub__(self, other):    70     71         "Return a new int for the subtraction of this int and 'other'."    72     73         return self._binary_op(native._int_sub, other)    74     75     def __imul__(self, other):    76     77         "Return a new int for the multiplication of this int and 'other'."    78     79         return self._binary_op(native._int_mul, other)    80     81     def __idiv__(self, other):    82     83         "Return a new int for the division of this int and 'other'."    84     85         return self._binary_op(native._int_div, other)    86     87     def __imod__(self, other):    88     89         "Return a new int for the modulo of this int by 'other'."    90     91         return self._binary_op(native._int_mod, other)    92     93     def __ipow__(self, other):    94     95         "Return a new int for the exponentiation of this int by 'other'."    96     97         return self._binary_op(native._int_pow, other)    98     99     def __iand__(self, other):   100    101         "Return a new int for the binary-and of this int and 'other'."   102    103         return self._binary_op(native._int_and, other)   104    105     def __ior__(self, other):   106    107         "Return a new int for the binary-or of this int and 'other'."   108    109         return self._binary_op(native._int_or, other)   110    111     def __ixor__(self, other):   112    113         "Return a new int for the exclusive-or of this int and 'other'."   114    115         return self._binary_op(native._int_xor, other)   116    117     def __invert__(self):   118    119         "Return the inversion of this int."   120    121         return native._int_not(self.__data__)   122    123     __add__ = __radd__ = __iadd__   124     __sub__ = __isub__   125    126     def __rsub__(self, other):   127    128         "Return a new int for the subtraction of this int from 'other'."   129    130         return self._binary_op_rev(native._int_sub, other)   131    132     __mul__ = __rmul__ = __imul__   133     __div__ = __idiv__   134    135     def __rdiv__(self, other):   136    137         "Return a new int for the division of this int into 'other'."   138    139         return self._binary_op_rev(native._int_div, other)   140    141     def __floordiv__(self, other): pass   142     def __rfloordiv__(self, other): pass   143     def __ifloordiv__(self, other): pass   144    145     __mod__ = __imod__   146    147     def __rmod__(self, other):   148    149         "Return a new int for the modulo of 'other' by this int."   150    151         return self._binary_op_rev(native._int_mod, other)   152    153     __pow__ = __ipow__   154    155     def __rpow__(self, other):   156    157         "Return a new int for the exponentiation of 'other' by this int."   158    159         return self._binary_op_rev(native._int_pow, other)   160    161     __and__ = __rand__ = __iand__   162     __or__ = __ror__ = __ior__   163     __xor__ = __rxor__ = __ixor__   164    165     def __lt__(self, other):   166    167         "Return whether this int is less than 'other'."   168    169         return self._binary_op(native._int_lt, other)   170    171     def __gt__(self, other):   172    173         "Return whether this int is greater than 'other'."   174    175         return self._binary_op(native._int_gt, other)   176    177     def __le__(self, other):   178    179         "Return whether this int is less than or equal to 'other'."   180    181         return _negate(self.__gt__(other))   182    183     def __ge__(self, other):   184    185         "Return whether this int is greater than or equal to 'other'."   186    187         return _negate(self.__lt__(other))   188    189     def __eq__(self, other):   190    191         "Return whether this int is equal to 'other'."   192    193         return self._binary_op(native._int_eq, other)   194    195     def __ne__(self, other):   196    197         "Return whether this int is not equal to 'other'."   198    199         return _negate(self.__eq__(other))   200    201     def __neg__(self):   202    203         "Apply the unary negation operator."   204    205         return native._int_neg(self.__data__)   206    207     def __pos__(self):   208    209         "Apply the unary positive operator."   210    211         return self   212    213     def __str__(self):   214    215         "Return a string representation."   216    217         return native._int_str(self.__data__)   218    219     __repr__ = __str__   220    221     def __lshift__(self): pass   222     def __rlshift__(self): pass   223     def __rshift__(self): pass   224     def __rrshift__(self): pass   225     def __ilshift__(self): pass   226     def __irshift__(self): pass   227    228     def __bool__(self):   229    230         "Return whether this int is non-zero."   231    232         zero = 0   233         return native._int_ne(self.__data__, zero.__data__)   234    235 # vim: tabstop=4 expandtab shiftwidth=4