Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
4e3b2f43
Commit
4e3b2f43
authored
Jun 28, 2012
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cython bang operator overloading.
parent
1a6fcd39
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
25 additions
and
5 deletions
+25
-5
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+14
-0
Cython/Compiler/Lexicon.py
Cython/Compiler/Lexicon.py
+1
-1
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+1
-0
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+2
-2
docs/src/userguide/wrapping_CPlusPlus.rst
docs/src/userguide/wrapping_CPlusPlus.rst
+4
-2
tests/run/cpp_operators.pyx
tests/run/cpp_operators.pyx
+3
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
4e3b2f43
...
...
@@ -7021,6 +7021,20 @@ class CUnopNode(UnopNode):
def
is_py_operation
(
self
):
return
False
class
BangNode
(
CUnopNode
):
# unary ! operator
operator
=
'!'
def
analyse_c_operation
(
self
,
env
):
if
self
.
operand
.
type
.
is_ptr
or
self
.
operand
.
type
.
is_numeric
:
self
.
type
=
PyrexTypes
.
c_bint_type
else
:
self
.
type_error
()
def
calculate_result_code
(
self
):
return
"(!%s)"
%
self
.
operand
.
result
()
class
DereferenceNode
(
CUnopNode
):
# unary * operator
...
...
Cython/Compiler/Lexicon.py
View file @
4e3b2f43
...
...
@@ -73,7 +73,7 @@ def make_lexicon():
deco
=
Str
(
"@"
)
bra
=
Any
(
"([{"
)
ket
=
Any
(
")]}"
)
punct
=
Any
(
":,;+-*/|&<>=.%`~^?"
)
punct
=
Any
(
":,;+-*/|&<>=.%`~^?
!
"
)
diphthong
=
Str
(
"=="
,
"<>"
,
"!="
,
"<="
,
">="
,
"<<"
,
">>"
,
"**"
,
"//"
,
"+="
,
"-="
,
"*="
,
"/="
,
"%="
,
"|="
,
"^="
,
"&="
,
"<<="
,
">>="
,
"**="
,
"//="
,
"->"
)
...
...
Cython/Compiler/ParseTreeTransforms.py
View file @
4e3b2f43
...
...
@@ -614,6 +614,7 @@ class InterpretCompilerDirectives(CythonTransform, SkipDeclarations):
'typeof'
:
ExprNodes
.
TypeofNode
,
'operator.address'
:
ExprNodes
.
AmpersandNode
,
'operator.bang'
:
ExprNodes
.
BangNode
,
'operator.dereference'
:
ExprNodes
.
DereferenceNode
,
'operator.preincrement'
:
ExprNodes
.
inc_dec_constructor
(
True
,
'++'
),
'operator.predecrement'
:
ExprNodes
.
inc_dec_constructor
(
True
,
'--'
),
...
...
Cython/Compiler/Parsing.py
View file @
4e3b2f43
...
...
@@ -2297,7 +2297,7 @@ supported_overloaded_operators = set([
'+'
,
'-'
,
'*'
,
'/'
,
'%'
,
'++'
,
'--'
,
'~'
,
'|'
,
'&'
,
'^'
,
'<<'
,
'>>'
,
','
,
'=='
,
'!='
,
'>='
,
'>'
,
'<='
,
'<'
,
'[]'
,
'()'
,
'[]'
,
'()'
,
'!'
,
])
def
p_c_simple_declarator
(
s
,
ctx
,
empty
,
is_type
,
cmethod_flag
,
...
...
@@ -2345,7 +2345,7 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
cname
=
ctx
.
namespace
+
"::"
+
name
if
name
==
'operator'
and
ctx
.
visibility
==
'extern'
and
nonempty
:
op
=
s
.
sy
if
[
1
for
c
in
op
if
c
in
'+-*/<=>!%&|([^~,'
]:
if
[
1
for
c
in
op
if
c
in
'+-*/<=>!%&|([^~,
!
'
]:
s
.
next
()
# Handle diphthong operators.
if
op
==
'('
:
...
...
docs/src/userguide/wrapping_CPlusPlus.rst
View file @
4e3b2f43
...
...
@@ -332,9 +332,11 @@ 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.bang`` for not. ``bang(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
...
...
tests/run/cpp_operators.pyx
View file @
4e3b2f43
...
...
@@ -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
(
cython
.
operator
.
bang
(
t
[
0
]))
del
t
def
test_incdec
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment