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 _binary_op, _negate 23 import native 24 25 class int(object): 26 27 "An integer abstraction." 28 29 def __init__(self, number_or_string=None): 30 31 "Initialise the integer with the given 'number_or_string'." 32 33 if isinstance(number_or_string, int): 34 self.__data__ = number_or_string.__data__ 35 else: 36 # NOTE: To be implemented. 37 self.__data__ = None 38 39 def __hash__(self): 40 "Return a value for hashing purposes." 41 return self 42 43 def __iadd__(self, other): 44 "Return a new int for the operation." 45 return _binary_op(self, other, native._int_add) 46 47 def __isub__(self, other): 48 "Return a new int for the operation." 49 return _binary_op(self, other, native._int_sub) 50 51 def __imul__(self, other): 52 "Return a new int for the operation." 53 return _binary_op(self, other, native._int_mul) 54 55 def __idiv__(self, other): 56 "Return a new int for the operation." 57 return _binary_op(self, other, native._int_div) 58 59 def __imod__(self, other): 60 "Return a new int for the operation." 61 return _binary_op(self, other, native._int_mod) 62 63 def __ipow__(self, other): 64 "Return a new int for the operation." 65 return _binary_op(self, other, native._int_pow) 66 67 def __iand__(self, other): 68 "Return a new int for the operation." 69 return _binary_op(self, other, native._int_and) 70 71 def __ior__(self, other): 72 "Return a new int for the operation." 73 return _binary_op(self, other, native._int_or) 74 75 def __ixor__(self, other): 76 "Return a new int for the operation." 77 return _binary_op(self, other, native._int_xor) 78 79 __add__ = __radd__ = __iadd__ 80 __sub__ = __isub__ 81 82 def __rsub__(self, other): 83 "Return a new int for the operation." 84 return _binary_op(other, self, native._int_sub) 85 86 __mul__ = __rmul__ = __imul__ 87 __div__ = __idiv__ 88 89 def __rdiv__(self, other): 90 "Return a new int for the operation." 91 return _binary_op(other, self, native._int_div) 92 93 def __floordiv__(self, other): pass 94 def __rfloordiv__(self, other): pass 95 def __ifloordiv__(self, other): pass 96 97 __mod__ = __imod__ 98 99 def __rmod__(self, other): 100 "Return a new int for the operation." 101 return _binary_op(other, self, native._int_mod) 102 103 __pow__ = __ipow__ 104 105 def __rpow__(self, other): 106 "Return a new int for the operation." 107 return _binary_op(other, self, native._int_pow) 108 109 __and__ = __rand__ = __iand__ 110 __or__ = __ror__ = __ior__ 111 __xor__ = __rxor__ = __ixor__ 112 113 def __lt__(self, other): 114 "Return a new boolean for the comparison." 115 return _binary_op(self, other, native._int_lt) 116 117 def __gt__(self, other): 118 "Return a new boolean for the comparison." 119 return _binary_op(self, other, native._int_gt) 120 121 def __le__(self, other): 122 "Return a new boolean for the comparison." 123 return _negate(self.__gt__(other)) 124 125 def __ge__(self, other): 126 "Return a new boolean for the comparison." 127 return _negate(self.__lt__(other)) 128 129 def __eq__(self, other): 130 "Return a new boolean for the comparison." 131 return _binary_op(self, other, native._int_eq) 132 133 def __ne__(self, other): 134 "Return a new boolean for the comparison." 135 return _negate(self.__eq__(other)) 136 137 def __invert__(self): pass 138 139 def __neg__(self): 140 "Apply the unary negation operator." 141 return native._int_neg(self) 142 143 def __pos__(self): 144 "Apply the unary positive operator." 145 return self 146 147 def __str__(self): 148 "Return a string representation." 149 return native._int_str(self) 150 151 __repr__ = __str__ 152 153 def __lshift__(self): pass 154 def __rlshift__(self): pass 155 def __rshift__(self): pass 156 def __rrshift__(self): pass 157 def __ilshift__(self): pass 158 def __irshift__(self): pass 159 160 def __bool__(self): 161 "Return whether this int is non-zero." 162 return native._int_ne(self, 0) 163 164 # vim: tabstop=4 expandtab shiftwidth=4