Commit e27de484 authored by Robert Bradshaw's avatar Robert Bradshaw

Support increment/decrement via Cython special methods. (May not be +/-1 for C++.)

parent 67d4ed42
......@@ -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.
#
......
......@@ -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']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment