1 #!/usr/bin/env python 2 3 """ 4 Operator support. 5 6 Copyright (C) 2010, 2013 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 def binary_op(a, b, left_accessor, right_accessor): 23 24 """ 25 A single parameterised function providing the binary operator mechanism for 26 arguments 'a' and 'b' using accessors given as 'left_accessor' and 27 'right_accessor' which provide the methods for the operands. 28 """ 29 30 # First, try and get a method for the left argument, and then call it with 31 # the right argument. 32 33 try: 34 fn = left_accessor(a) 35 except AttributeError: 36 pass 37 else: 38 result = fn(b) 39 if result is not NotImplemented: 40 return result 41 42 # Otherwise, try and get a method for the right argument, and then call it 43 # with the left argument. 44 45 try: 46 fn = right_accessor(b) 47 except AttributeError: 48 pass 49 else: 50 result = fn(a) 51 if result is not NotImplemented: 52 return result 53 54 # Where no methods were available, or if neither method could support the 55 # operation, raise an exception. 56 57 raise TypeError 58 59 def unary_op(a, accessor): 60 61 """ 62 A single parameterised function providing the unary operator mechanism for 63 the argument 'a' using the given 'accessor' to provide the method for the 64 operand. 65 """ 66 67 # First, try and get a method for the argument, and then call it. 68 69 try: 70 fn = accessor(a) 71 except AttributeError: 72 pass 73 else: 74 result = fn() 75 if result is not NotImplemented: 76 return result 77 78 # Where no method was available, or if the method could not support the 79 # operation, raise an exception. 80 81 raise TypeError 82 83 def augassign(a, b, augmented_accessor, left_accessor, right_accessor): 84 85 """ 86 A single parameterised function providing the augmented assignment mechanism 87 for arguments 'a' and 'b' either using 'augmented_accessor' (directly 88 affecting 'a') or using 'left_accessor' and 'right_accessor' (conventional 89 operator method accessors). 90 91 The result of the assignment is returned. 92 """ 93 94 # First, try and get a method that directly affects the assignment target. 95 96 try: 97 fn = augmented_accessor(a) 98 except AttributeError: 99 pass 100 else: 101 result = fn(b) 102 if result is not NotImplemented: 103 return result 104 105 # Otherwise, attempt a conventional binary operation. 106 107 return binary_op(a, b, left_accessor, right_accessor) 108 109 # These functions defer method lookup by wrapping the attribute access in 110 # lambda functions. Thus, the appropriate methods are defined locally, but no 111 # attempt to obtain them is made until the generic function is called. 112 113 # NOTE: The compiler should make it possible for the following functions to call 114 # NOTE: the generic operator implementations with no additional call overhead. 115 116 # Binary operator functions. 117 118 def add(a, b): 119 return binary_op(a, b, lambda a: a.__add__, lambda b: b.__radd__) 120 121 def and_(a, b): 122 return binary_op(a, b, lambda a: a.__and__, lambda b: b.__rand__) 123 124 def contains(a, b): 125 return b in a 126 127 def div(a, b): 128 return binary_op(a, b, lambda a: a.__div__, lambda b: b.__rdiv__) 129 130 def floordiv(a, b): 131 return binary_op(a, b, lambda a: a.__floordiv__, lambda b: b.__rfloordiv__) 132 133 def lshift(a, b): 134 return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__) 135 136 def mod(a, b): 137 return binary_op(a, b, lambda a: a.__mod__, lambda b: b.__rmod__) 138 139 def mul(a, b): 140 return binary_op(a, b, lambda a: a.__mul__, lambda b: b.__rmul__) 141 142 def or_(a, b): 143 return binary_op(a, b, lambda a: a.__or__, lambda b: b.__ror__) 144 145 def pow(a, b): 146 return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__) 147 148 def rshift(a, b): 149 return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__) 150 151 def sub(a, b): 152 return binary_op(a, b, lambda a: a.__sub__, lambda b: b.__rsub__) 153 154 def xor(a, b): 155 return binary_op(a, b, lambda a: a.__xor__, lambda b: b.__rxor__) 156 157 # Unary operator functions. 158 159 def invert(a): 160 return unary_op(a, lambda a: a.__invert__) 161 162 def neg(a): 163 return unary_op(a, lambda a: a.__neg__) 164 165 def not_(a): 166 return not a 167 168 def pos(a): 169 return unary_op(a, lambda a: a.__pos__) 170 171 # Augmented assignment functions. 172 173 def iadd(a, b): 174 return augassign(a, b, lambda a: a.__iadd__, lambda a: a.__add__, lambda b: b.__radd__) 175 176 def iand_(a, b): 177 return augassign(a, b, lambda a: a.__iand__, lambda a: a.__and__, lambda b: b.__rand__) 178 179 def idiv(a, b): 180 return augassign(a, b, lambda a: a.__idiv__, lambda a: a.__div__, lambda b: b.__rdiv__) 181 182 def ifloordiv(a, b): 183 return augassign(a, b, lambda a: a.__ifloordiv__, lambda a: a.__floordiv__, lambda b: b.__rfloordiv__) 184 185 def ilshift(a, b): 186 return augassign(a, b, lambda a: a.__ilshift__, lambda a: a.__lshift__, lambda b: b.__rlshift__) 187 188 def imod(a, b): 189 return augassign(a, b, lambda a: a.__imod__, lambda a: a.__mod__, lambda b: b.__rmod__) 190 191 def imul(a, b): 192 return augassign(a, b, lambda a: a.__imul__, lambda a: a.__mul__, lambda b: b.__rmul__) 193 194 def ior_(a, b): 195 return augassign(a, b, lambda a: a.__ior__, lambda a: a.__or__, lambda b: b.__ror__) 196 197 def ipow(a, b): 198 return augassign(a, b, lambda a: a.__ipow__, lambda a: a.__pow__, lambda b: b.__rpow__) 199 200 def irshift(a, b): 201 return augassign(a, b, lambda a: a.__irshift__, lambda a: a.__rshift__, lambda b: b.__rrshift__) 202 203 def isub(a, b): 204 return augassign(a, b, lambda a: a.__isub__, lambda a: a.__sub__, lambda b: b.__rsub__) 205 206 def ixor(a, b): 207 return augassign(a, b, lambda a: a.__ixor__, lambda a: a.__xor__, lambda b: b.__rxor__) 208 209 # Comparison functions. 210 211 def eq(a, b): 212 return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__) 213 214 def ge(a, b): 215 return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__) 216 217 def gt(a, b): 218 return binary_op(a, b, lambda a: a.__gt__, lambda b: b.__lt__) 219 220 def le(a, b): 221 return binary_op(a, b, lambda a: a.__le__, lambda b: b.__ge__) 222 223 def lt(a, b): 224 return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__) 225 226 def ne(a, b): 227 return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__) 228 229 # Access and slicing functions. 230 231 def getitem(a, b): 232 return a.__getitem__(b) 233 234 def setitem(a, b, c): 235 a.__setitem__(b, c) 236 237 # NOTE: Should be able to optimise temporary instance allocations for slices. 238 239 def getslice(a, b, c): 240 return a.__getitem__(slice(b, c)) 241 242 def setslice(a, b, c, d): 243 a.__setitem__(slice(b, c), d) 244 245 # vim: tabstop=4 expandtab shiftwidth=4