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