Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
graph_drawing
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
graph_drawing
Commits
35dfadad
Commit
35dfadad
authored
Dec 01, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Built in support for writing n-ary operator nodes.
parent
00abd696
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
22 deletions
+23
-22
line.py
line.py
+14
-18
tests/test_line.py
tests/test_line.py
+9
-4
No files found.
line.py
View file @
35dfadad
...
...
@@ -31,11 +31,9 @@ def generate_line(root):
int(x, -oo, oo)
"""
# FIXME: Binding order
operators
=
[
(
'+'
,
'-'
),
(
'mod'
,
),
(
'*'
,
'/'
),
(
'*'
,
'/'
,
'mod'
),
(
'^'
,
)
]
max_assoc
=
len
(
operators
)
...
...
@@ -77,26 +75,24 @@ def generate_line(root):
if
is_operator
(
node
):
if
arity
==
1
:
# Unary
expression
# Unary
operator
s
+=
traverse
(
node
[
0
])
elif
arity
==
2
:
# Binary expression
left
,
right
=
map
(
traverse
,
node
)
# Check if there is an assiociativity conflict on either side.
# If so, add parentheses
else
:
# N-ary operator
node_assoc
=
assoc
(
node
)
e
=
[]
if
assoc
(
node
[
0
])
<
node_assoc
:
left
=
'('
+
left
+
')'
for
child
in
node
:
exp
=
traverse
(
child
)
# Check if there is an assiociativity conflict.
# If so, add parentheses
if
assoc
(
child
)
<
node_assoc
:
exp
=
'('
+
exp
+
')'
if
assoc
(
node
[
1
])
<
node_assoc
:
right
=
'('
+
right
+
')'
e
.
append
(
exp
)
s
=
left
+
' '
+
s
+
' '
+
right
else
:
# pragma: nocover
raise
ValueError
(
'arity = %d is currently not supported.'
\
%
arity
)
s
=
(
' '
+
s
+
' '
).
join
(
e
)
else
:
# Function
s
+=
'('
+
', '
.
join
(
map
(
traverse
,
node
))
+
')'
...
...
tests/test_line.py
View file @
35dfadad
...
...
@@ -15,22 +15,27 @@ class TestLine(unittest.TestCase):
def
test_simple
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
plus
=
Node
(
'+'
,
l0
,
l1
)
assert
generate_line
(
plus
)
==
'1 + 2'
self
.
assertEquals
(
generate_line
(
plus
),
'1 + 2'
)
def
test_parentheses
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
plus
=
Node
(
'+'
,
l0
,
l1
)
times
=
Node
(
'*'
,
plus
,
plus
)
assert
generate_line
(
times
)
==
'(1 + 2) * (1 + 2)'
self
.
assertEquals
(
generate_line
(
times
),
'(1 + 2) * (1 + 2)'
)
def
test_function
(
self
):
exp
=
Leaf
(
'x'
)
inf
=
Leaf
(
'oo'
)
minus_inf
=
Node
(
'-'
,
inf
)
integral
=
Node
(
'int'
,
exp
,
minus_inf
,
inf
)
assert
generate_line
(
integral
)
==
'int(x, -oo, oo)'
self
.
assertEquals
(
generate_line
(
integral
),
'int(x, -oo, oo)'
)
def
test_mod
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
mod
=
Node
(
'mod'
,
l1
,
l0
)
assert
generate_line
(
mod
)
==
'2 mod 1'
self
.
assertEquals
(
generate_line
(
mod
),
'2 mod 1'
)
def
test_n_ary
(
self
):
l0
,
l1
,
l2
=
Leaf
(
1
),
Leaf
(
2
),
Leaf
(
3
)
plus
=
Node
(
'+'
,
l0
,
l1
,
l2
)
self
.
assertEquals
(
generate_line
(
plus
),
'1 + 2 + 3'
)
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