1.1 --- a/lib/__builtins__/int.py Sat Dec 03 16:44:55 2016 +0100
1.2 +++ b/lib/__builtins__/int.py Sat Dec 03 17:11:59 2016 +0100
1.3 @@ -114,6 +114,12 @@
1.4
1.5 return self._binary_op(native._int_xor, other)
1.6
1.7 + def __invert__(self):
1.8 +
1.9 + "Return the inversion of this int."
1.10 +
1.11 + return native._int_not(self.__data__)
1.12 +
1.13 __add__ = __radd__ = __iadd__
1.14 __sub__ = __isub__
1.15
1.16 @@ -192,8 +198,6 @@
1.17
1.18 return _negate(self.__eq__(other))
1.19
1.20 - def __invert__(self): pass
1.21 -
1.22 def __neg__(self):
1.23
1.24 "Apply the unary negation operator."
2.1 --- a/lib/native.py Sat Dec 03 16:44:55 2016 +0100
2.2 +++ b/lib/native.py Sat Dec 03 17:11:59 2016 +0100
2.3 @@ -42,6 +42,7 @@
2.4 def _int_sub(self, other): pass
2.5
2.6 def _int_and(self, other): pass
2.7 +def _int_not(self): pass
2.8 def _int_or(self, other): pass
2.9 def _int_xor(self, other): pass
2.10
3.1 --- a/lib/operator/unary.py Sat Dec 03 16:44:55 2016 +0100
3.2 +++ b/lib/operator/unary.py Sat Dec 03 17:11:59 2016 +0100
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Operator support.
3.6
3.7 -Copyright (C) 2010, 2013, 2015 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -25,9 +25,6 @@
3.13 # lambda functions. Thus, the appropriate methods are defined locally, but no
3.14 # attempt to obtain them is made until the generic function is called.
3.15
3.16 -# NOTE: The compiler should make it possible for the following functions to call
3.17 -# NOTE: the generic operator implementations with no additional call overhead.
3.18 -
3.19 # Unary operator functions.
3.20
3.21 def invert(a):
4.1 --- a/templates/native.c Sat Dec 03 16:44:55 2016 +0100
4.2 +++ b/templates/native.c Sat Dec 03 17:11:59 2016 +0100
4.3 @@ -243,6 +243,16 @@
4.4 return __new_int(i & j);
4.5 }
4.6
4.7 +__attr __fn_native__int_not(__attr __args[])
4.8 +{
4.9 + __attr * const _data = &__args[1];
4.10 + /* _data interpreted as int */
4.11 + int i = _data->intvalue;
4.12 +
4.13 + /* Return the new integer. */
4.14 + return __new_int(~i);
4.15 +}
4.16 +
4.17 __attr __fn_native__int_or(__attr __args[])
4.18 {
4.19 __attr * const _data = &__args[1];
5.1 --- a/templates/native.h Sat Dec 03 16:44:55 2016 +0100
5.2 +++ b/templates/native.h Sat Dec 03 17:11:59 2016 +0100
5.3 @@ -19,6 +19,7 @@
5.4 __attr __fn_native__int_sub(__attr __args[]);
5.5
5.6 __attr __fn_native__int_and(__attr __args[]);
5.7 +__attr __fn_native__int_not(__attr __args[]);
5.8 __attr __fn_native__int_or(__attr __args[]);
5.9 __attr __fn_native__int_xor(__attr __args[]);
5.10
6.1 --- a/tests/numbers.py Sat Dec 03 16:44:55 2016 +0100
6.2 +++ b/tests/numbers.py Sat Dec 03 17:11:59 2016 +0100
6.3 @@ -1,19 +1,28 @@
6.4 i = 2 ** 30
6.5 -print i
6.6 +print i # 1073741824
6.7
6.8 j = -2 ** 30
6.9 -print j
6.10 +print j # -1073741824
6.11
6.12 -print i + j
6.13 +print i + j # 0
6.14
6.15 try:
6.16 print i - j
6.17 except OverflowError:
6.18 print "i - j: overflow occurred"
6.19
6.20 -print i / j
6.21 +print i / i # 1
6.22 +print i / j # -1
6.23 +print j / j # 1
6.24 +print j / i # -1
6.25
6.26 try:
6.27 print i * j
6.28 except OverflowError:
6.29 print "i * j: overflow occurred"
6.30 +
6.31 +print i - i # 0
6.32 +print j - j # 0
6.33 +print ~j # 1073741823
6.34 +print i & ~j # 0
6.35 +print i - 1 & ~j # 1073741823