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