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
15abdd61
Commit
15abdd61
authored
Jan 24, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parentheses are now only added to sub-expressions when nescesary.
parent
84d65757
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
6 deletions
+38
-6
line.py
line.py
+22
-5
tests/test_line.py
tests/test_line.py
+16
-1
No files found.
line.py
View file @
15abdd61
...
...
@@ -82,11 +82,21 @@ def generate_line(root):
if
is_operator
(
node
):
if
arity
==
1
:
# Unary operator
sub_exp
=
traverse
(
node
[
0
])
sub
=
node
[
0
]
sub_exp
=
traverse
(
sub
)
if
' '
in
sub_exp
:
# Negated sub-expressions with spaces in them should be
# enclosed in parentheses
# enclosed in parentheses, unless they have a higher precedence
# than subtraction are rewritten to a factor of a subtraction:
# -(1 + 2)
# -(1 - 2)
# -4a
# -(4 * 5)
# 1 - 4 * 5
# 1 + -(4 * 5) -> 1 - 4 * 5
if
' '
in
sub_exp
and
not
(
not
isinstance
(
sub
,
Leaf
)
\
and
hasattr
(
node
,
'marked_negation'
)
and
pred
(
sub
)
>
0
):
sub_exp
=
'('
+
sub_exp
+
')'
result
=
op
+
sub_exp
...
...
@@ -97,6 +107,13 @@ def generate_line(root):
sep
=
' '
+
op
+
' '
e
=
[]
# Mark added and subtracted negations for later use when adding
# parentheses
if
op
in
(
'+'
,
'-'
):
for
child
in
node
:
if
child
.
title
()
==
'-'
and
len
(
child
)
==
1
:
child
.
marked_negation
=
True
for
i
,
child
in
enumerate
(
node
):
exp
=
traverse
(
child
)
...
...
tests/test_line.py
View file @
15abdd61
...
...
@@ -154,12 +154,27 @@ class TestLine(unittest.TestCase):
self
.
assertFalse
(
is_int
(
neg_a
))
self
.
assertFalse
(
is_int
(
plus
))
def
test_negated_
nary
(
self
):
def
test_negated_
addition_subtraction
(
self
):
neg
=
N
(
'-'
,
N
(
'+'
,
L
(
1
),
L
(
2
)))
self
.
assertEquals
(
generate_line
(
neg
),
'-(1 + 2)'
)
neg
=
N
(
'-'
,
N
(
'-'
,
L
(
1
),
L
(
2
)))
self
.
assertEquals
(
generate_line
(
neg
),
'-(1 - 2)'
)
neg
=
N
(
'+'
,
L
(
1
),
N
(
'-'
,
N
(
'+'
,
L
(
1
),
L
(
2
))))
self
.
assertEquals
(
generate_line
(
neg
),
'1 - (1 + 2)'
)
neg
=
N
(
'-'
,
N
(
'*'
,
L
(
4
),
L
(
'a'
)))
self
.
assertEquals
(
generate_line
(
neg
),
'-4a'
)
neg
=
N
(
'-'
,
N
(
'*'
,
L
(
4
),
L
(
5
)))
self
.
assertEquals
(
generate_line
(
neg
),
'-(4 * 5)'
)
plus
=
N
(
'+'
,
L
(
1
),
N
(
'-'
,
N
(
'*'
,
L
(
4
),
L
(
5
))))
self
.
assertEquals
(
generate_line
(
plus
),
'1 - 4 * 5'
)
plus
=
N
(
'+'
,
L
(
1
),
N
(
'-'
,
L
(
4
)))
self
.
assertEquals
(
generate_line
(
plus
),
'1 - 4'
)
neg
=
N
(
'-'
,
N
(
'-'
,
L
(
1
)))
self
.
assertEquals
(
generate_line
(
neg
),
'--1'
)
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