Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
peephole
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
peephole
Commits
7b1f59f5
Commit
7b1f59f5
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Tested and debugged utility classes.
parent
60f1e223
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/utils.py
+17
-15
17 additions, 15 deletions
src/utils.py
tests/test_utils.py
+34
-6
34 additions, 6 deletions
tests/test_utils.py
with
51 additions
and
21 deletions
src/utils.py
+
17
−
15
View file @
7b1f59f5
...
...
@@ -90,33 +90,35 @@ class Block:
def
__len__
(
self
):
return
len
(
self
.
statements
)
def
replace
(
self
,
start
,
end
,
replacement
):
"""
Replace the given range start-end with the given statement list, and
move the pointer to the first statement after the replacement.
"""
before
=
self
.
statements
[:
start
]
after
=
self
.
statements
[
end
:]
self
.
statements
=
before
+
replacement
+
after
self
.
pointer
=
start
+
len
(
replacement
)
def
read
(
self
,
count
=
1
):
"""
Read the statement at the current pointer position and move the
pointer one position to the right.
"""
s
=
statements
[
self
.
pointer
]
s
=
self
.
statements
[
self
.
pointer
]
self
.
pointer
+=
1
return
s
def
end
(
self
):
"""
Check if the pointer is at the end of the statement list.
"""
return
self
.
pointer
==
len
(
self
)
def
peek
(
self
,
count
=
1
):
"""
Read the statements until an offset from the current pointer
position.
"""
i
=
self
.
pointer
+
offset
return
self
.
statements
[
self
.
pointer
]
if
count
==
1
\
else
self
.
statements
[
self
.
pointer
:
self
.
pointer
+
count
]
if
i
<
len
(
self
.
statements
):
return
self
.
statements
[
self
.
pointer
:
i
]
def
replace
(
self
,
count
,
replacement
,
start
=
None
):
"""
Replace the given range start-(start + count) with the given
statement list, and move the pointer to the first statement after the
replacement.
"""
if
start
==
None
:
start
=
self
.
pointer
def
end
(
self
):
"""
Check if the pointer is at the end of the statement list.
"""
return
self
.
pointer
==
len
(
self
.
statements
)
-
1
before
=
self
.
statements
[:
start
]
after
=
self
.
statements
[
start
+
count
:]
self
.
statements
=
before
+
replacement
+
after
self
.
pointer
=
start
+
len
(
replacement
)
def
find_leaders
(
statements
):
...
...
This diff is collapsed.
Click to expand it.
tests/test_utils.py
+
34
−
6
View file @
7b1f59f5
...
...
@@ -7,16 +7,18 @@ from src.utils import Statement as S, Block as B, find_leaders, \
class
TestUtils
(
unittest
.
TestCase
):
def
setUp
(
self
):
pass
add
=
S
(
'
command
'
,
'
add
'
,
'
$1
'
,
'
$2
'
,
'
$3
'
)
self
.
statements
=
[
add
,
S
(
'
command
'
,
'
j
'
,
'
foo
'
),
add
,
add
,
\
S
(
'
label
'
,
'
foo
'
)]
self
.
block
=
B
([
S
(
'
command
'
,
'
foo
'
),
\
S
(
'
command
'
,
'
bar
'
),
S
(
'
command
'
,
'
baz
'
)])
def
test_find_leaders
(
self
):
add
=
S
(
'
command
'
,
'
add
'
,
'
$1
'
,
'
$2
'
,
'
$3
'
)
s
=
[
add
,
S
(
'
command
'
,
'
j
'
,
'
foo
'
),
add
,
add
,
S
(
'
label
'
,
'
foo
'
)]
self
.
assertEqual
(
find_leaders
(
s
),
[
0
,
2
,
4
])
self
.
assertEqual
(
find_leaders
(
self
.
statements
),
[
0
,
2
,
4
])
def
test_find_basic_blocks
(
self
):
add
=
S
(
'
command
'
,
'
add
'
,
'
$1
'
,
'
$2
'
,
'
$3
'
)
s
=
[
add
,
S
(
'
command
'
,
'
j
'
,
'
foo
'
),
add
,
add
,
S
(
'
label
'
,
'
foo
'
)]
s
=
self
.
statements
self
.
assertEqual
(
map
(
lambda
b
:
b
.
statements
,
find_basic_blocks
(
s
)),
\
[
B
(
s
[:
2
]).
statements
,
B
(
s
[
2
:
4
]).
statements
,
\
B
(
s
[
4
:]).
statements
])
...
...
@@ -43,3 +45,29 @@ class TestUtils(unittest.TestCase):
def
test_jump_target
(
self
):
self
.
assertEqual
(
S
(
'
command
'
,
'
j
'
,
'
foo
'
).
jump_target
(),
'
foo
'
)
self
.
assertRaises
(
Exception
,
S
(
'
command
'
,
'
foo
'
).
jump_target
)
def
test_read
(
self
):
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'
command
'
,
'
foo
'
))
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'
command
'
,
'
bar
'
))
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'
command
'
,
'
baz
'
))
def
test_end
(
self
):
self
.
assertFalse
(
self
.
block
.
end
())
self
.
block
.
read
()
self
.
block
.
read
()
self
.
block
.
read
()
self
.
assertTrue
(
self
.
block
.
end
())
def
test_peek
(
self
):
self
.
assertEqual
(
self
.
block
.
peek
(),
S
(
'
command
'
,
'
foo
'
))
self
.
assertEqual
(
self
.
block
.
peek
(
2
),
[
S
(
'
command
'
,
'
foo
'
),
\
S
(
'
command
'
,
'
bar
'
)])
self
.
block
.
read
()
self
.
assertEqual
(
self
.
block
.
peek
(),
S
(
'
command
'
,
'
bar
'
))
def
test_replace
(
self
):
self
.
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
foobar
'
)])
self
.
assertEqual
(
self
.
block
.
pointer
,
1
)
self
.
assertEqual
(
self
.
block
.
statements
,
[
S
(
'
command
'
,
'
foobar
'
),
\
S
(
'
command
'
,
'
bar
'
),
\
S
(
'
command
'
,
'
baz
'
)])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment