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
Kirill Smelkov
cython
Commits
5ae0cccf
Commit
5ae0cccf
authored
Dec 14, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generic aggregation of a constant BinopNode into a ConstNode (in simple cases)
parent
c7b928a2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
4 deletions
+39
-4
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+25
-0
tests/run/consts.pyx
tests/run/consts.pyx
+14
-4
No files found.
Cython/Compiler/Optimize.py
View file @
5ae0cccf
...
...
@@ -427,9 +427,34 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
self
.
_calculate_const
(
node
)
return
node
# def visit_NumBinopNode(self, node):
def
visit_BinopNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
if
node
.
type
is
PyrexTypes
.
py_object_type
:
return
node
if
node
.
constant_result
is
ExprNodes
.
not_a_constant
:
return
node
# print node.constant_result, node.operand1, node.operand2, node.pos
if
isinstance
(
node
.
operand1
,
ExprNodes
.
ConstNode
)
and
\
node
.
type
is
node
.
operand1
.
type
:
new_node
=
node
.
operand1
elif
isinstance
(
node
.
operand2
,
ExprNodes
.
ConstNode
)
and
\
node
.
type
is
node
.
operand2
.
type
:
new_node
=
node
.
operand2
else
:
return
node
new_node
.
value
=
new_node
.
constant_result
=
node
.
constant_result
new_node
=
new_node
.
coerce_to
(
node
.
type
,
self
.
module_scope
)
return
new_node
# in the future, other nodes can have their own handler method here
# that can replace them with a constant result node
def
visit_ModuleNode
(
self
,
node
):
self
.
module_scope
=
node
.
scope
self
.
visitchildren
(
node
)
return
node
def
visit_Node
(
self
,
node
):
self
.
visitchildren
(
node
)
return
node
...
...
tests/run/consts.pyx
View file @
5ae0cccf
__doc__
=
u"""
>>> add()
10
>>> add_var(10)
20
>>> add() == 1+2+3+4
True
>>> add_var(10) == 1+2+10+3+4
True
>>> mul() == 1*60*1000
True
>>> arithm() == 9*2+3*8/6-10
True
"""
def
add
():
...
...
@@ -10,3 +14,9 @@ def add():
def
add_var
(
a
):
return
1
+
2
+
a
+
3
+
4
def
mul
():
return
1
*
60
*
1000
def
arithm
():
return
9
*
2
+
3
*
8
/
6
-
10
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