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