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