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
9ffea24c
Commit
9ffea24c
authored
Jul 14, 2009
by
DaniloFreitas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More on overloading operators (+ and -)
parent
a598f9e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
62 deletions
+67
-62
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+67
-62
No files found.
Cython/Compiler/ExprNodes.py
View file @
9ffea24c
...
@@ -2392,7 +2392,6 @@ class SimpleCallNode(CallNode):
...
@@ -2392,7 +2392,6 @@ class SimpleCallNode(CallNode):
def
best_match
(
self
):
def
best_match
(
self
):
entries
=
[
self
.
function
.
entry
]
+
self
.
function
.
entry
.
overloaded_alternatives
entries
=
[
self
.
function
.
entry
]
+
self
.
function
.
entry
.
overloaded_alternatives
#print self.function.entry.name, self.function.entry.type, self.function.entry.overloaded_alternatives
actual_nargs
=
len
(
self
.
args
)
actual_nargs
=
len
(
self
.
args
)
possibilities
=
[]
possibilities
=
[]
for
entry
in
entries
:
for
entry
in
entries
:
...
@@ -2433,12 +2432,9 @@ class SimpleCallNode(CallNode):
...
@@ -2433,12 +2432,9 @@ class SimpleCallNode(CallNode):
self
.
type
=
PyrexTypes
.
error_type
self
.
type
=
PyrexTypes
.
error_type
self
.
result_code
=
"<error>"
self
.
result_code
=
"<error>"
return
None
return
None
#for (score, entry) in possibilities:
#print entry.name, entry.type, score
return
possibilities
[
0
][
1
]
return
possibilities
[
0
][
1
]
error
(
self
.
pos
,
error
(
self
.
pos
,
"Call with wrong arguments"
)
# (expected %s, got %s)"
"Call with wrong arguments"
)
#% (expected_str, actual_nargs))
self
.
args
=
None
self
.
args
=
None
self
.
type
=
PyrexTypes
.
error_type
self
.
type
=
PyrexTypes
.
error_type
self
.
result_code
=
"<error>"
self
.
result_code
=
"<error>"
...
@@ -4334,69 +4330,18 @@ class NumBinopNode(BinopNode):
...
@@ -4334,69 +4330,18 @@ class NumBinopNode(BinopNode):
def
py_operation_function
(
self
):
def
py_operation_function
(
self
):
return
self
.
py_functions
[
self
.
operator
]
return
self
.
py_functions
[
self
.
operator
]
py_functions
=
{
"|"
:
"PyNumber_Or"
,
"^"
:
"PyNumber_Xor"
,
"&"
:
"PyNumber_And"
,
"<<"
:
"PyNumber_Lshift"
,
">>"
:
"PyNumber_Rshift"
,
"+"
:
"PyNumber_Add"
,
"-"
:
"PyNumber_Subtract"
,
"*"
:
"PyNumber_Multiply"
,
"/"
:
"__Pyx_PyNumber_Divide"
,
"//"
:
"PyNumber_FloorDivide"
,
"%"
:
"PyNumber_Remainder"
,
"**"
:
"PyNumber_Power"
}
class
IntBinopNode
(
NumBinopNode
):
# Binary operation taking integer arguments.
def
c_types_okay
(
self
,
type1
,
type2
):
#print "IntBinopNode.c_types_okay:", type1, type2 ###
return
(
type1
.
is_int
or
type1
.
is_enum
)
\
and
(
type2
.
is_int
or
type2
.
is_enum
)
class
AddNode
(
NumBinopNode
):
def
best_match
(
self
,
env
):
# '+' operator.
entries
=
[
env
]
+
env
.
overloaded_alternatives
def
analyse_cpp_operation
(
self
,
env
):
type1
=
self
.
operand1
.
type
type2
=
self
.
operand2
.
type
if
type1
.
is_ptr
:
type1
=
type1
.
base_type
if
type2
.
is_ptr
:
type2
=
type2
.
base_type
entry1
=
env
.
lookup
(
type1
.
name
)
entry2
=
env
.
lookup
(
type2
.
name
)
entry
=
entry1
.
scope
.
lookup_here
(
"__add__"
)
if
not
entry
:
error
(
self
.
pos
,
"'+' operator not defined for '%s + %s'"
%
(
self
.
operand1
.
type
,
self
.
operand2
.
type
))
self
.
type_error
()
return
self
.
type
=
self
.
best_match
(
entry
)
def
best_match
(
self
,
entry
):
entries
=
[
entry
]
+
entry
.
overloaded_alternatives
actual_nargs
=
2
possibilities
=
[]
possibilities
=
[]
args
=
[
self
.
operand1
,
self
.
operand2
]
for
entry
in
entries
:
for
entry
in
entries
:
type
=
entry
.
type
type
=
entry
.
type
if
type
.
is_ptr
:
if
type
.
is_ptr
:
type
=
type
.
base_type
type
=
type
.
base_type
# Check no. of args
max_nargs
=
len
(
type
.
args
)
expected_nargs
=
max_nargs
-
type
.
optional_arg_count
if
actual_nargs
<
expected_nargs
\
or
(
not
type
.
has_varargs
and
actual_nargs
>
max_nargs
):
continue
score
=
[
0
,
0
,
0
]
score
=
[
0
,
0
,
0
]
for
i
in
range
(
len
(
self
.
args
)):
for
i
in
range
(
len
(
args
)):
src_type
=
self
.
args
[
i
].
type
src_type
=
args
[
i
].
type
if
entry
.
type
.
is_ptr
:
if
entry
.
type
.
is_ptr
:
dst_type
=
entry
.
type
.
base_type
.
args
[
i
].
type
dst_type
=
entry
.
type
.
base_type
.
args
[
i
].
type
else
:
else
:
...
@@ -4422,7 +4367,7 @@ class AddNode(NumBinopNode):
...
@@ -4422,7 +4367,7 @@ class AddNode(NumBinopNode):
self
.
type
=
PyrexTypes
.
error_type
self
.
type
=
PyrexTypes
.
error_type
self
.
result_code
=
"<error>"
self
.
result_code
=
"<error>"
return
None
return
None
return
possibilities
[
0
][
1
]
return
possibilities
[
0
][
1
]
.
type
error
(
self
.
pos
,
error
(
self
.
pos
,
"Call with wrong arguments"
)
# (expected %s, got %s)"
"Call with wrong arguments"
)
# (expected %s, got %s)"
#% (expected_str, actual_nargs))
#% (expected_str, actual_nargs))
...
@@ -4430,6 +4375,50 @@ class AddNode(NumBinopNode):
...
@@ -4430,6 +4375,50 @@ class AddNode(NumBinopNode):
self
.
type
=
PyrexTypes
.
error_type
self
.
type
=
PyrexTypes
.
error_type
self
.
result_code
=
"<error>"
self
.
result_code
=
"<error>"
return
None
return
None
py_functions
=
{
"|"
:
"PyNumber_Or"
,
"^"
:
"PyNumber_Xor"
,
"&"
:
"PyNumber_And"
,
"<<"
:
"PyNumber_Lshift"
,
">>"
:
"PyNumber_Rshift"
,
"+"
:
"PyNumber_Add"
,
"-"
:
"PyNumber_Subtract"
,
"*"
:
"PyNumber_Multiply"
,
"/"
:
"__Pyx_PyNumber_Divide"
,
"//"
:
"PyNumber_FloorDivide"
,
"%"
:
"PyNumber_Remainder"
,
"**"
:
"PyNumber_Power"
}
class
IntBinopNode
(
NumBinopNode
):
# Binary operation taking integer arguments.
def
c_types_okay
(
self
,
type1
,
type2
):
#print "IntBinopNode.c_types_okay:", type1, type2 ###
return
(
type1
.
is_int
or
type1
.
is_enum
)
\
and
(
type2
.
is_int
or
type2
.
is_enum
)
class
AddNode
(
NumBinopNode
):
# '+' operator.
def
analyse_cpp_operation
(
self
,
env
):
type1
=
self
.
operand1
.
type
type2
=
self
.
operand2
.
type
if
type1
.
is_ptr
:
type1
=
type1
.
base_type
if
type2
.
is_ptr
:
type2
=
type2
.
base_type
entry
=
env
.
lookup
(
type1
.
name
)
function
=
entry
.
type
.
scope
.
lookup
(
u'__add__'
)
if
not
function
:
error
(
self
.
pos
,
"'+' operator not defined for '%s + %s'"
%
(
type1
,
type2
))
self
.
type_error
()
return
self
.
type
=
self
.
best_match
(
function
)
def
is_py_operation
(
self
):
def
is_py_operation
(
self
):
if
self
.
operand1
.
type
.
is_string
\
if
self
.
operand1
.
type
.
is_string
\
...
@@ -4451,6 +4440,22 @@ class AddNode(NumBinopNode):
...
@@ -4451,6 +4440,22 @@ class AddNode(NumBinopNode):
class
SubNode
(
NumBinopNode
):
class
SubNode
(
NumBinopNode
):
# '-' operator.
# '-' operator.
def
analyse_cpp_operation
(
self
,
env
):
type1
=
self
.
operand1
.
type
type2
=
self
.
operand2
.
type
if
type1
.
is_ptr
:
type1
=
type1
.
base_type
if
type2
.
is_ptr
:
type2
=
type2
.
base_type
entry
=
env
.
lookup
(
type1
.
name
)
function
=
entry
.
type
.
scope
.
lookup
(
u'__sub__'
)
if
not
function
:
error
(
self
.
pos
,
"'-' operator not defined for '%s - %s'"
%
(
type1
,
type2
))
self
.
type_error
()
return
self
.
type
=
self
.
best_match
(
function
)
def
compute_c_result_type
(
self
,
type1
,
type2
):
def
compute_c_result_type
(
self
,
type1
,
type2
):
if
(
type1
.
is_ptr
or
type1
.
is_array
)
and
(
type2
.
is_int
or
type2
.
is_enum
):
if
(
type1
.
is_ptr
or
type1
.
is_array
)
and
(
type2
.
is_int
or
type2
.
is_enum
):
...
...
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