# HG changeset patch # User Paul Boddie # Date 1479831656 -3600 # Node ID fe05975a4b54e3062c1e9a4a79574e6522e0c183 # Parent 1a64c775111a787b29d9a8cffaa54a43c690975d Added support for unary negation of integers. diff -r 1a64c775111a -r fe05975a4b54 lib/__builtins__/int.py --- a/lib/__builtins__/int.py Tue Nov 22 01:28:58 2016 +0100 +++ b/lib/__builtins__/int.py Tue Nov 22 17:20:56 2016 +0100 @@ -131,8 +131,14 @@ return _negate(self.__eq__(other)) def __invert__(self): pass - def __neg__(self): pass - def __pos__(self): pass + + def __neg__(self): + "Apply the unary negation operator." + return native._int_neg(self) + + def __pos__(self): + "Apply the unary positive operator." + return self def __str__(self): "Return a string representation." diff -r 1a64c775111a -r fe05975a4b54 lib/native.py --- a/lib/native.py Tue Nov 22 01:28:58 2016 +0100 +++ b/lib/native.py Tue Nov 22 17:20:56 2016 +0100 @@ -30,6 +30,7 @@ def _int_div(self, other): pass def _int_mod(self, other): pass def _int_mul(self, other): pass +def _int_neg(self): pass def _int_pow(self, other): pass def _int_sub(self, other): pass diff -r 1a64c775111a -r fe05975a4b54 templates/native.c --- a/templates/native.c Tue Nov 22 01:28:58 2016 +0100 +++ b/templates/native.c Tue Nov 22 17:20:56 2016 +0100 @@ -153,6 +153,17 @@ #undef other } +__attr __fn_native__int_neg(__attr __args[]) +{ + #define self (__args[1]) + /* self.__data__ interpreted as int */ + int i = __load_via_object(self.value, __pos___data__).intvalue; + + /* Return the new integer. */ + return __new_int(-i); + #undef self +} + __attr __fn_native__int_pow(__attr __args[]) { #define self (__args[1]) diff -r 1a64c775111a -r fe05975a4b54 templates/native.h --- a/templates/native.h Tue Nov 22 01:28:58 2016 +0100 +++ b/templates/native.h Tue Nov 22 17:20:56 2016 +0100 @@ -14,6 +14,7 @@ __attr __fn_native__int_div(__attr __args[]); __attr __fn_native__int_mod(__attr __args[]); __attr __fn_native__int_mul(__attr __args[]); +__attr __fn_native__int_neg(__attr __args[]); __attr __fn_native__int_pow(__attr __args[]); __attr __fn_native__int_sub(__attr __args[]); diff -r 1a64c775111a -r fe05975a4b54 tests/list.py --- a/tests/list.py Tue Nov 22 01:28:58 2016 +0100 +++ b/tests/list.py Tue Nov 22 17:20:56 2016 +0100 @@ -5,3 +5,7 @@ print l[1] print l[2] print l[3] +print l[-1] +print l[-2] +print l[-3] +print l[-4]