Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
ad201451
Commit
ad201451
authored
Nov 29, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic graph rewrite prototype.
parent
ae546156
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
0 deletions
+127
-0
src/node.py
src/node.py
+127
-0
No files found.
src/node.py
0 → 100644
View file @
ad201451
import
os.path
import
sys
sys
.
path
.
insert
(
0
,
os
.
path
.
realpath
(
'external'
))
from
graph_drawing.graph
import
generate_graph
class
ExpressionNode
(
object
):
def
__init__
(
self
,
operator
,
*
args
):
super
(
ExpressionNode
,
self
).
__init__
()
self
.
operator
,
self
.
args
=
operator
,
list
(
args
)
for
a
in
self
.
args
:
a
.
parent
=
self
def
title
(
self
):
return
self
.
operator
def
replace
(
self
,
node
):
pos
=
self
.
parent
.
args
.
index
(
self
)
self
.
parent
.
args
[
pos
]
=
node
node
.
parent
=
self
.
parent
self
.
parent
=
None
def
__iter__
(
self
):
return
iter
(
self
.
args
)
def
__len__
(
self
):
return
len
(
self
.
args
)
def
__getitem__
(
self
,
n
):
return
self
.
args
[
n
]
def
__setitem__
(
self
,
n
,
arg
):
self
.
args
[
n
]
=
arg
def
__str__
(
self
):
return
generate_graph
(
self
,
ExpressionNode
)
class
ExpressionLeaf
(
object
):
def
__init__
(
self
,
value
):
super
(
ExpressionLeaf
,
self
).
__init__
()
self
.
value
=
value
def
replace
(
self
,
node
):
if
not
hasattr
(
self
,
'parent'
):
return
pos
=
self
.
parent
.
args
.
index
(
self
)
self
.
parent
.
args
[
pos
]
=
node
node
.
parent
=
self
.
parent
self
.
parent
=
None
def
title
(
self
):
return
str
(
self
.
value
)
def
__add__
(
self
,
b
):
return
self
.
value
+
b
.
value
def
__repr__
(
self
):
return
repr
(
self
.
value
)
def
__str__
(
self
):
return
str
(
self
.
value
)
l0
=
ExpressionLeaf
(
3
)
l1
=
ExpressionLeaf
(
4
)
l2
=
ExpressionLeaf
(
5
)
l3
=
ExpressionLeaf
(
7
)
n0
=
ExpressionNode
(
'+'
,
l0
,
l1
)
n1
=
ExpressionNode
(
'+'
,
l2
,
l3
)
n2
=
ExpressionNode
(
'*'
,
n0
,
n1
)
print
n2
N
=
ExpressionNode
def
rewrite_multiply
(
node
):
a
,
b
=
node
[
0
]
c
,
d
=
node
[
1
]
ac
=
N
(
'*'
,
a
,
c
)
ad
=
N
(
'*'
,
a
,
d
)
bc
=
N
(
'*'
,
b
,
c
)
bd
=
N
(
'*'
,
b
,
d
)
res
=
N
(
'+'
,
N
(
'+'
,
N
(
'+'
,
ac
,
ad
),
bc
),
bd
)
return
res
possibilities
=
[
(
n0
,
lambda
(
x
,
y
):
ExpressionLeaf
(
x
+
y
)),
(
n1
,
lambda
(
x
,
y
):
ExpressionLeaf
(
x
+
y
)),
(
n2
,
rewrite_multiply
),
]
print
'
\
n
--- after rule 2 ---
\
n
'
n_
,
method
=
possibilities
[
2
]
new
=
method
(
n_
)
print
new
print
'
\
n
--- original graph ---
\
n
'
print
n2
print
'
\
n
--- apply rule 0 ---
\
n
'
n_
,
method
=
possibilities
[
0
]
new
=
method
(
n_
)
n_
.
replace
(
new
)
print
n2
# Revert rule 0
new
.
replace
(
n_
)
print
'
\
n
--- apply rule 1 ---
\
n
'
n_
,
method
=
possibilities
[
1
]
new
=
method
(
n_
)
n_
.
replace
(
new
)
print
n2
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