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