From e27de4846b1395738b9438dc33cfdd71769f618b Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 14 Jan 2010 15:40:39 -0800 Subject: [PATCH] Support increment/decrement via Cython special methods. (May not be +/-1 for C++.) --- Cython/Compiler/ExprNodes.py | 27 +++++++++++++++++++++++--- Cython/Compiler/ParseTreeTransforms.py | 4 ++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index a9de0cee..57175661 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -4261,12 +4261,14 @@ class TildeNode(UnopNode): return "(~%s)" % self.operand.result() -class DereferenceNode(UnopNode): - # unary '*' operator - +class CUnopNode(UnopNode): + def is_py_operation(self): return False +class DereferenceNode(CUnopNode): + # unary * operator + def analyse_c_operation(self, env): if self.operand.type.is_ptr: self.type = self.operand.type.base_type @@ -4277,6 +4279,25 @@ class DereferenceNode(UnopNode): return "(*%s)" % self.operand.result() +class DecrementIncrementNode(CUnopNode): + # unary ++/-- operator + + def analyse_c_operation(self, env): + if self.operand.type.is_ptr or self.operand.type.is_numeric: + self.type = self.operand.type + else: + self.type_error() + + def calculate_result_code(self): + if self.is_prefix: + return "(%s%s)" % (self.operator, self.operand.result()) + else: + return "(%s%s)" % (self.operand.result(), self.operator) + +def inc_dec_constructor(is_prefix, operator): + return lambda pos, **kwds: DecrementIncrementNode(pos, is_prefix=is_prefix, operator=operator, **kwds) + + class AmpersandNode(ExprNode): # The C address-of operator. # diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index 87963075..f8da55b0 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -327,6 +327,10 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations): 'typeof': TypeofNode, 'address': AmpersandNode, 'dereference': DereferenceNode, + 'preincrement' : inc_dec_constructor(True, '++'), + 'predecrement' : inc_dec_constructor(True, '--'), + 'postincrement': inc_dec_constructor(False, '++'), + 'postdecrement': inc_dec_constructor(False, '--'), } special_methods = set(['declare', 'union', 'struct', 'typedef', 'sizeof', 'cast', 'pointer', 'compiled', 'NULL'] -- 2.26.2