paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Integer objects. |
paul@6 | 5 | |
paul@920 | 6 | Copyright (C) 2015, 2016, 2017, 2018, 2021 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@472 | 22 | from __builtins__.unicode import utf8string |
paul@787 | 23 | from native import get_maxint, get_minint, is_int, \ |
paul@848 | 24 | int_add, int_and, int_div, int_eq, int_ge, int_gt, \ |
paul@848 | 25 | int_lshift, int_le, int_lt, int_mod, int_mul, int_ne, \ |
paul@848 | 26 | int_neg, int_not, int_or, int_pow, int_rshift, int_str, \ |
paul@848 | 27 | int_sub, int_xor |
paul@6 | 28 | |
paul@920 | 29 | def int(number_or_string): |
paul@920 | 30 | |
paul@920 | 31 | "Initialise the integer with the given 'number_or_string'." |
paul@920 | 32 | |
paul@920 | 33 | if is_int(number_or_string): |
paul@920 | 34 | return number_or_string |
paul@920 | 35 | else: |
paul@920 | 36 | raise TypeError |
paul@920 | 37 | |
paul@920 | 38 | class integer: |
paul@182 | 39 | |
paul@182 | 40 | "An integer abstraction." |
paul@182 | 41 | |
paul@283 | 42 | def __hash__(self): |
paul@303 | 43 | |
paul@283 | 44 | "Return a value for hashing purposes." |
paul@303 | 45 | |
paul@283 | 46 | return self |
paul@283 | 47 | |
paul@303 | 48 | def _binary_op(self, op, other): |
paul@303 | 49 | |
paul@303 | 50 | "Perform 'op' on this int and 'other' if appropriate." |
paul@303 | 51 | |
paul@787 | 52 | if is_int(other): |
paul@758 | 53 | return op(self, other) |
paul@303 | 54 | else: |
paul@303 | 55 | return NotImplemented |
paul@303 | 56 | |
paul@303 | 57 | def _binary_op_rev(self, op, other): |
paul@303 | 58 | |
paul@303 | 59 | "Perform 'op' on 'other' and this int if appropriate." |
paul@303 | 60 | |
paul@787 | 61 | if is_int(other): |
paul@758 | 62 | return op(other, self) |
paul@303 | 63 | else: |
paul@303 | 64 | return NotImplemented |
paul@303 | 65 | |
paul@6 | 66 | def __iadd__(self, other): |
paul@303 | 67 | |
paul@303 | 68 | "Return a new int for the addition of this int and 'other'." |
paul@303 | 69 | |
paul@356 | 70 | return self._binary_op(int_add, other) |
paul@6 | 71 | |
paul@6 | 72 | def __isub__(self, other): |
paul@303 | 73 | |
paul@303 | 74 | "Return a new int for the subtraction of this int and 'other'." |
paul@303 | 75 | |
paul@356 | 76 | return self._binary_op(int_sub, other) |
paul@6 | 77 | |
paul@6 | 78 | def __imul__(self, other): |
paul@303 | 79 | |
paul@303 | 80 | "Return a new int for the multiplication of this int and 'other'." |
paul@303 | 81 | |
paul@356 | 82 | return self._binary_op(int_mul, other) |
paul@6 | 83 | |
paul@6 | 84 | def __idiv__(self, other): |
paul@303 | 85 | |
paul@303 | 86 | "Return a new int for the division of this int and 'other'." |
paul@303 | 87 | |
paul@356 | 88 | return self._binary_op(int_div, other) |
paul@6 | 89 | |
paul@6 | 90 | def __imod__(self, other): |
paul@303 | 91 | |
paul@303 | 92 | "Return a new int for the modulo of this int by 'other'." |
paul@303 | 93 | |
paul@356 | 94 | return self._binary_op(int_mod, other) |
paul@6 | 95 | |
paul@6 | 96 | def __ipow__(self, other): |
paul@303 | 97 | |
paul@303 | 98 | "Return a new int for the exponentiation of this int by 'other'." |
paul@303 | 99 | |
paul@356 | 100 | return self._binary_op(int_pow, other) |
paul@6 | 101 | |
paul@6 | 102 | def __iand__(self, other): |
paul@303 | 103 | |
paul@303 | 104 | "Return a new int for the binary-and of this int and 'other'." |
paul@303 | 105 | |
paul@356 | 106 | return self._binary_op(int_and, other) |
paul@6 | 107 | |
paul@6 | 108 | def __ior__(self, other): |
paul@303 | 109 | |
paul@303 | 110 | "Return a new int for the binary-or of this int and 'other'." |
paul@303 | 111 | |
paul@356 | 112 | return self._binary_op(int_or, other) |
paul@6 | 113 | |
paul@6 | 114 | def __ixor__(self, other): |
paul@303 | 115 | |
paul@303 | 116 | "Return a new int for the exclusive-or of this int and 'other'." |
paul@303 | 117 | |
paul@356 | 118 | return self._binary_op(int_xor, other) |
paul@6 | 119 | |
paul@307 | 120 | def __invert__(self): |
paul@307 | 121 | |
paul@307 | 122 | "Return the inversion of this int." |
paul@307 | 123 | |
paul@758 | 124 | return int_not(self) |
paul@307 | 125 | |
paul@6 | 126 | __add__ = __radd__ = __iadd__ |
paul@6 | 127 | __sub__ = __isub__ |
paul@6 | 128 | |
paul@6 | 129 | def __rsub__(self, other): |
paul@303 | 130 | |
paul@303 | 131 | "Return a new int for the subtraction of this int from 'other'." |
paul@303 | 132 | |
paul@356 | 133 | return self._binary_op_rev(int_sub, other) |
paul@6 | 134 | |
paul@6 | 135 | __mul__ = __rmul__ = __imul__ |
paul@6 | 136 | __div__ = __idiv__ |
paul@6 | 137 | |
paul@6 | 138 | def __rdiv__(self, other): |
paul@303 | 139 | |
paul@303 | 140 | "Return a new int for the division of this int into 'other'." |
paul@303 | 141 | |
paul@356 | 142 | return self._binary_op_rev(int_div, other) |
paul@6 | 143 | |
paul@848 | 144 | # NOTE: To be implemented. |
paul@848 | 145 | |
paul@6 | 146 | def __floordiv__(self, other): pass |
paul@6 | 147 | def __rfloordiv__(self, other): pass |
paul@6 | 148 | def __ifloordiv__(self, other): pass |
paul@6 | 149 | |
paul@6 | 150 | __mod__ = __imod__ |
paul@6 | 151 | |
paul@6 | 152 | def __rmod__(self, other): |
paul@303 | 153 | |
paul@303 | 154 | "Return a new int for the modulo of 'other' by this int." |
paul@303 | 155 | |
paul@356 | 156 | return self._binary_op_rev(int_mod, other) |
paul@6 | 157 | |
paul@6 | 158 | __pow__ = __ipow__ |
paul@6 | 159 | |
paul@6 | 160 | def __rpow__(self, other): |
paul@303 | 161 | |
paul@303 | 162 | "Return a new int for the exponentiation of 'other' by this int." |
paul@303 | 163 | |
paul@356 | 164 | return self._binary_op_rev(int_pow, other) |
paul@6 | 165 | |
paul@6 | 166 | __and__ = __rand__ = __iand__ |
paul@6 | 167 | __or__ = __ror__ = __ior__ |
paul@6 | 168 | __xor__ = __rxor__ = __ixor__ |
paul@6 | 169 | |
paul@798 | 170 | def __lshift__(self, other): |
paul@798 | 171 | |
paul@798 | 172 | "Return a new int left-shifted by 'other'." |
paul@798 | 173 | |
paul@798 | 174 | return self._binary_op(int_lshift, other) |
paul@798 | 175 | |
paul@798 | 176 | def __rlshift__(self, other): |
paul@798 | 177 | |
paul@798 | 178 | "Return a new int left-shifted by 'other'." |
paul@798 | 179 | |
paul@798 | 180 | return self._binary_op_rev(int_lshift, other) |
paul@798 | 181 | |
paul@798 | 182 | def __rshift__(self, other): |
paul@798 | 183 | |
paul@798 | 184 | "Return a new int right-shifted by 'other'." |
paul@798 | 185 | |
paul@798 | 186 | return self._binary_op(int_rshift, other) |
paul@798 | 187 | |
paul@798 | 188 | def __rrshift__(self, other): |
paul@798 | 189 | |
paul@798 | 190 | "Return a new int right-shifted by 'other'." |
paul@798 | 191 | |
paul@798 | 192 | return self._binary_op_rev(int_rshift, other) |
paul@798 | 193 | |
paul@798 | 194 | __ilshift__ = __lshift__ |
paul@798 | 195 | __irshift__ = __rshift__ |
paul@798 | 196 | |
paul@6 | 197 | def __lt__(self, other): |
paul@303 | 198 | |
paul@303 | 199 | "Return whether this int is less than 'other'." |
paul@303 | 200 | |
paul@356 | 201 | return self._binary_op(int_lt, other) |
paul@6 | 202 | |
paul@6 | 203 | def __gt__(self, other): |
paul@303 | 204 | |
paul@303 | 205 | "Return whether this int is greater than 'other'." |
paul@303 | 206 | |
paul@356 | 207 | return self._binary_op(int_gt, other) |
paul@6 | 208 | |
paul@6 | 209 | def __le__(self, other): |
paul@303 | 210 | |
paul@303 | 211 | "Return whether this int is less than or equal to 'other'." |
paul@303 | 212 | |
paul@848 | 213 | return self._binary_op(int_le, other) |
paul@6 | 214 | |
paul@6 | 215 | def __ge__(self, other): |
paul@303 | 216 | |
paul@303 | 217 | "Return whether this int is greater than or equal to 'other'." |
paul@303 | 218 | |
paul@848 | 219 | return self._binary_op(int_ge, other) |
paul@6 | 220 | |
paul@6 | 221 | def __eq__(self, other): |
paul@303 | 222 | |
paul@303 | 223 | "Return whether this int is equal to 'other'." |
paul@303 | 224 | |
paul@356 | 225 | return self._binary_op(int_eq, other) |
paul@6 | 226 | |
paul@6 | 227 | def __ne__(self, other): |
paul@303 | 228 | |
paul@303 | 229 | "Return whether this int is not equal to 'other'." |
paul@303 | 230 | |
paul@848 | 231 | return self._binary_op(int_ne, other) |
paul@6 | 232 | |
paul@207 | 233 | def __neg__(self): |
paul@303 | 234 | |
paul@207 | 235 | "Apply the unary negation operator." |
paul@303 | 236 | |
paul@758 | 237 | return int_neg(self) |
paul@207 | 238 | |
paul@207 | 239 | def __pos__(self): |
paul@303 | 240 | |
paul@207 | 241 | "Apply the unary positive operator." |
paul@303 | 242 | |
paul@207 | 243 | return self |
paul@198 | 244 | |
paul@198 | 245 | def __str__(self): |
paul@303 | 246 | |
paul@198 | 247 | "Return a string representation." |
paul@303 | 248 | |
paul@758 | 249 | return utf8string(int_str(self)) |
paul@198 | 250 | |
paul@222 | 251 | __repr__ = __str__ |
paul@222 | 252 | |
paul@6 | 253 | def __bool__(self): |
paul@303 | 254 | |
paul@6 | 255 | "Return whether this int is non-zero." |
paul@303 | 256 | |
paul@758 | 257 | return int_ne(self, 0) |
paul@6 | 258 | |
paul@311 | 259 | # Limits. |
paul@311 | 260 | |
paul@356 | 261 | maxint = get_maxint() |
paul@356 | 262 | minint = get_minint() |
paul@311 | 263 | |
paul@6 | 264 | # vim: tabstop=4 expandtab shiftwidth=4 |