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