Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
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
peephole
Commits
7b1f59f5
Commit
7b1f59f5
authored
Dec 19, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tested and debugged utility classes.
parent
60f1e223
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
21 deletions
+51
-21
src/utils.py
src/utils.py
+17
-15
tests/test_utils.py
tests/test_utils.py
+34
-6
No files found.
src/utils.py
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 = s
elf.s
tatements[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):
...
...
tests/test_utils.py
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'
)])
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