Commit 946cfc01 authored by scoder's avatar scoder

Merge pull request #135 from robertwb/bang

Cython bang operator overloading.
parents 391a336b c96fc2de
......@@ -6939,7 +6939,20 @@ class NotNode(ExprNode):
def analyse_types(self, env):
self.operand.analyse_types(env)
self.operand = self.operand.coerce_to_boolean(env)
if self.operand.type.is_cpp_class:
type = self.operand.type
function = type.scope.lookup("operator!")
if not function:
error(self.pos, "'!' operator not defined for %s"
% (type))
self.type = PyrexTypes.error_type
return
func_type = function.type
if func_type.is_ptr:
func_type = func_type.base_type
self.type = func_type.return_type
else:
self.operand = self.operand.coerce_to_boolean(env)
def calculate_result_code(self):
return "(!%s)" % self.operand.result()
......
......@@ -73,7 +73,7 @@ def make_lexicon():
deco = Str("@")
bra = Any("([{")
ket = Any(")]}")
punct = Any(":,;+-*/|&<>=.%`~^?")
punct = Any(":,;+-*/|&<>=.%`~^?!")
diphthong = Str("==", "<>", "!=", "<=", ">=", "<<", ">>", "**", "//",
"+=", "-=", "*=", "/=", "%=", "|=", "^=", "&=",
"<<=", ">>=", "**=", "//=", "->")
......
......@@ -2297,7 +2297,7 @@ supported_overloaded_operators = set([
'+', '-', '*', '/', '%',
'++', '--', '~', '|', '&', '^', '<<', '>>', ',',
'==', '!=', '>=', '>', '<=', '<',
'[]', '()',
'[]', '()', '!',
])
def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
......
......@@ -337,9 +337,9 @@ syntax as C++. Cython provides functions replacing these operators in
a special module ``cython.operator``. The functions provided are:
* ``cython.operator.dereference`` for dereferencing. ``dereference(foo)``
will produce the C++ code ``*foo``
will produce the C++ code ``*(foo)``
* ``cython.operator.preincrement`` for pre-incrementation. ``preincrement(foo)``
will produce the C++ code ``++foo``
will produce the C++ code ``++(foo)``
* ...
These functions need to be cimported. Of course, one can use a
......
......@@ -13,6 +13,7 @@ cdef extern from "cpp_operators_helper.h":
char* operator-()
char* operator*()
char* operator~()
char* operator!()
char* operator++()
char* operator--()
......@@ -50,12 +51,14 @@ def test_unops():
unary -
unary ~
unary *
unary !
"""
cdef TestOps* t = new TestOps()
out(+t[0])
out(-t[0])
out(~t[0])
out(deref(t[0]))
out(not t[0])
del t
def test_incdec():
......
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