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