# HG changeset patch # User Paul Boddie # Date 1480781519 -3600 # Node ID fc291b59f67b14a2429cec0d34120d88ad577c27 # Parent 95b8056f3a79920c2676d863936c38506f6caa20 Implemented the inversion operator for integers. diff -r 95b8056f3a79 -r fc291b59f67b lib/__builtins__/int.py --- a/lib/__builtins__/int.py Sat Dec 03 16:44:55 2016 +0100 +++ b/lib/__builtins__/int.py Sat Dec 03 17:11:59 2016 +0100 @@ -114,6 +114,12 @@ return self._binary_op(native._int_xor, other) + def __invert__(self): + + "Return the inversion of this int." + + return native._int_not(self.__data__) + __add__ = __radd__ = __iadd__ __sub__ = __isub__ @@ -192,8 +198,6 @@ return _negate(self.__eq__(other)) - def __invert__(self): pass - def __neg__(self): "Apply the unary negation operator." diff -r 95b8056f3a79 -r fc291b59f67b lib/native.py --- a/lib/native.py Sat Dec 03 16:44:55 2016 +0100 +++ b/lib/native.py Sat Dec 03 17:11:59 2016 +0100 @@ -42,6 +42,7 @@ def _int_sub(self, other): pass def _int_and(self, other): pass +def _int_not(self): pass def _int_or(self, other): pass def _int_xor(self, other): pass diff -r 95b8056f3a79 -r fc291b59f67b lib/operator/unary.py --- a/lib/operator/unary.py Sat Dec 03 16:44:55 2016 +0100 +++ b/lib/operator/unary.py Sat Dec 03 17:11:59 2016 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -25,9 +25,6 @@ # lambda functions. Thus, the appropriate methods are defined locally, but no # attempt to obtain them is made until the generic function is called. -# NOTE: The compiler should make it possible for the following functions to call -# NOTE: the generic operator implementations with no additional call overhead. - # Unary operator functions. def invert(a): diff -r 95b8056f3a79 -r fc291b59f67b templates/native.c --- a/templates/native.c Sat Dec 03 16:44:55 2016 +0100 +++ b/templates/native.c Sat Dec 03 17:11:59 2016 +0100 @@ -243,6 +243,16 @@ return __new_int(i & j); } +__attr __fn_native__int_not(__attr __args[]) +{ + __attr * const _data = &__args[1]; + /* _data interpreted as int */ + int i = _data->intvalue; + + /* Return the new integer. */ + return __new_int(~i); +} + __attr __fn_native__int_or(__attr __args[]) { __attr * const _data = &__args[1]; diff -r 95b8056f3a79 -r fc291b59f67b templates/native.h --- a/templates/native.h Sat Dec 03 16:44:55 2016 +0100 +++ b/templates/native.h Sat Dec 03 17:11:59 2016 +0100 @@ -19,6 +19,7 @@ __attr __fn_native__int_sub(__attr __args[]); __attr __fn_native__int_and(__attr __args[]); +__attr __fn_native__int_not(__attr __args[]); __attr __fn_native__int_or(__attr __args[]); __attr __fn_native__int_xor(__attr __args[]); diff -r 95b8056f3a79 -r fc291b59f67b tests/numbers.py --- a/tests/numbers.py Sat Dec 03 16:44:55 2016 +0100 +++ b/tests/numbers.py Sat Dec 03 17:11:59 2016 +0100 @@ -1,19 +1,28 @@ i = 2 ** 30 -print i +print i # 1073741824 j = -2 ** 30 -print j +print j # -1073741824 -print i + j +print i + j # 0 try: print i - j except OverflowError: print "i - j: overflow occurred" -print i / j +print i / i # 1 +print i / j # -1 +print j / j # 1 +print j / i # -1 try: print i * j except OverflowError: print "i * j: overflow occurred" + +print i - i # 0 +print j - j # 0 +print ~j # 1073741823 +print i & ~j # 0 +print i - 1 & ~j # 1073741823