paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Integer objects. |
paul@6 | 5 | |
paul@6 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@60 | 22 | from __builtins__.operator import _binary_op, _negate |
paul@6 | 23 | import native |
paul@6 | 24 | |
paul@6 | 25 | class int(object): |
paul@182 | 26 | |
paul@182 | 27 | "An integer abstraction." |
paul@182 | 28 | |
paul@6 | 29 | def __init__(self, number_or_string=None): |
paul@182 | 30 | |
paul@182 | 31 | "Initialise the integer with the given 'number_or_string'." |
paul@182 | 32 | |
paul@182 | 33 | if isinstance(number_or_string, int): |
paul@182 | 34 | self.__data__ = number_or_string.__data__ |
paul@182 | 35 | else: |
paul@182 | 36 | # NOTE: To be implemented. |
paul@182 | 37 | self.__data__ = None |
paul@6 | 38 | |
paul@6 | 39 | def __iadd__(self, other): |
paul@6 | 40 | "Return a new int for the operation." |
paul@6 | 41 | return _binary_op(self, other, native._int_add) |
paul@6 | 42 | |
paul@6 | 43 | def __isub__(self, other): |
paul@6 | 44 | "Return a new int for the operation." |
paul@6 | 45 | return _binary_op(self, other, native._int_sub) |
paul@6 | 46 | |
paul@6 | 47 | def __imul__(self, other): |
paul@6 | 48 | "Return a new int for the operation." |
paul@6 | 49 | return _binary_op(self, other, native._int_mul) |
paul@6 | 50 | |
paul@6 | 51 | def __idiv__(self, other): |
paul@6 | 52 | "Return a new int for the operation." |
paul@6 | 53 | return _binary_op(self, other, native._int_div) |
paul@6 | 54 | |
paul@6 | 55 | def __imod__(self, other): |
paul@6 | 56 | "Return a new int for the operation." |
paul@6 | 57 | return _binary_op(self, other, native._int_mod) |
paul@6 | 58 | |
paul@6 | 59 | def __ipow__(self, other): |
paul@6 | 60 | "Return a new int for the operation." |
paul@6 | 61 | return _binary_op(self, other, native._int_pow) |
paul@6 | 62 | |
paul@6 | 63 | def __iand__(self, other): |
paul@6 | 64 | "Return a new int for the operation." |
paul@6 | 65 | return _binary_op(self, other, native._int_and) |
paul@6 | 66 | |
paul@6 | 67 | def __ior__(self, other): |
paul@6 | 68 | "Return a new int for the operation." |
paul@6 | 69 | return _binary_op(self, other, native._int_or) |
paul@6 | 70 | |
paul@6 | 71 | def __ixor__(self, other): |
paul@6 | 72 | "Return a new int for the operation." |
paul@6 | 73 | return _binary_op(self, other, native._int_xor) |
paul@6 | 74 | |
paul@6 | 75 | __add__ = __radd__ = __iadd__ |
paul@6 | 76 | __sub__ = __isub__ |
paul@6 | 77 | |
paul@6 | 78 | def __rsub__(self, other): |
paul@6 | 79 | "Return a new int for the operation." |
paul@198 | 80 | return _binary_op(other, self, native._int_sub) |
paul@6 | 81 | |
paul@6 | 82 | __mul__ = __rmul__ = __imul__ |
paul@6 | 83 | __div__ = __idiv__ |
paul@6 | 84 | |
paul@6 | 85 | def __rdiv__(self, other): |
paul@6 | 86 | "Return a new int for the operation." |
paul@198 | 87 | return _binary_op(other, self, native._int_div) |
paul@6 | 88 | |
paul@6 | 89 | def __floordiv__(self, other): pass |
paul@6 | 90 | def __rfloordiv__(self, other): pass |
paul@6 | 91 | def __ifloordiv__(self, other): pass |
paul@6 | 92 | |
paul@6 | 93 | __mod__ = __imod__ |
paul@6 | 94 | |
paul@6 | 95 | def __rmod__(self, other): |
paul@6 | 96 | "Return a new int for the operation." |
paul@198 | 97 | return _binary_op(other, self, native._int_mod) |
paul@6 | 98 | |
paul@6 | 99 | __pow__ = __ipow__ |
paul@6 | 100 | |
paul@6 | 101 | def __rpow__(self, other): |
paul@6 | 102 | "Return a new int for the operation." |
paul@198 | 103 | return _binary_op(other, self, native._int_pow) |
paul@6 | 104 | |
paul@6 | 105 | __and__ = __rand__ = __iand__ |
paul@6 | 106 | __or__ = __ror__ = __ior__ |
paul@6 | 107 | __xor__ = __rxor__ = __ixor__ |
paul@6 | 108 | |
paul@6 | 109 | def __lt__(self, other): |
paul@6 | 110 | "Return a new boolean for the comparison." |
paul@6 | 111 | return _binary_op(self, other, native._int_lt) |
paul@6 | 112 | |
paul@6 | 113 | def __gt__(self, other): |
paul@6 | 114 | "Return a new boolean for the comparison." |
paul@6 | 115 | return _binary_op(self, other, native._int_gt) |
paul@6 | 116 | |
paul@6 | 117 | def __le__(self, other): |
paul@6 | 118 | "Return a new boolean for the comparison." |
paul@6 | 119 | return _negate(self.__gt__(other)) |
paul@6 | 120 | |
paul@6 | 121 | def __ge__(self, other): |
paul@6 | 122 | "Return a new boolean for the comparison." |
paul@6 | 123 | return _negate(self.__lt__(other)) |
paul@6 | 124 | |
paul@6 | 125 | def __eq__(self, other): |
paul@6 | 126 | "Return a new boolean for the comparison." |
paul@6 | 127 | return _binary_op(self, other, native._int_eq) |
paul@6 | 128 | |
paul@6 | 129 | def __ne__(self, other): |
paul@6 | 130 | "Return a new boolean for the comparison." |
paul@6 | 131 | return _negate(self.__eq__(other)) |
paul@6 | 132 | |
paul@6 | 133 | def __invert__(self): pass |
paul@207 | 134 | |
paul@207 | 135 | def __neg__(self): |
paul@207 | 136 | "Apply the unary negation operator." |
paul@207 | 137 | return native._int_neg(self) |
paul@207 | 138 | |
paul@207 | 139 | def __pos__(self): |
paul@207 | 140 | "Apply the unary positive operator." |
paul@207 | 141 | return self |
paul@198 | 142 | |
paul@198 | 143 | def __str__(self): |
paul@198 | 144 | "Return a string representation." |
paul@198 | 145 | return native._int_str(self) |
paul@198 | 146 | |
paul@222 | 147 | __repr__ = __str__ |
paul@222 | 148 | |
paul@6 | 149 | def __lshift__(self): pass |
paul@6 | 150 | def __rlshift__(self): pass |
paul@6 | 151 | def __rshift__(self): pass |
paul@6 | 152 | def __rrshift__(self): pass |
paul@6 | 153 | def __ilshift__(self): pass |
paul@6 | 154 | def __irshift__(self): pass |
paul@6 | 155 | |
paul@6 | 156 | def __bool__(self): |
paul@6 | 157 | "Return whether this int is non-zero." |
paul@198 | 158 | return native._int_ne(self, 0) |
paul@6 | 159 | |
paul@6 | 160 | # vim: tabstop=4 expandtab shiftwidth=4 |