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
e62c02fc
Commit
e62c02fc
authored
Dec 28, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Applied pep8.
parent
effe7267
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
17 deletions
+29
-17
src/parser.py
src/parser.py
+10
-0
src/writer.py
src/writer.py
+5
-1
tests/test_dataflow.py
tests/test_dataflow.py
+14
-16
No files found.
src/parser.py
View file @
e62c02fc
...
@@ -3,11 +3,13 @@ import ply.yacc as yacc
...
@@ -3,11 +3,13 @@ import ply.yacc as yacc
from
statement
import
Statement
as
S
,
Block
from
statement
import
Statement
as
S
,
Block
# Global statements administration
# Global statements administration
statements
=
[]
statements
=
[]
tokens
=
(
'NEWLINE'
,
'WORD'
,
'COMMENT'
,
'DIRECTIVE'
,
'COMMA'
,
'COLON'
)
tokens
=
(
'NEWLINE'
,
'WORD'
,
'COMMENT'
,
'DIRECTIVE'
,
'COMMA'
,
'COLON'
)
# Tokens
# Tokens
def
t_NEWLINE
(
t
):
def
t_NEWLINE
(
t
):
r'\n+'
r'\n+'
...
@@ -50,6 +52,7 @@ def t_WORD(t):
...
@@ -50,6 +52,7 @@ def t_WORD(t):
r'
[
a
-
zA
-
Z0
-
9
$
_
.
+
()
-
]
+
'
r'
[
a
-
zA
-
Z0
-
9
$
_
.
+
()
-
]
+
'
return t
return t
# Ignore whitespaces
# Ignore whitespaces
t_ignore = '
\
t
'
t_ignore = '
\
t
'
...
@@ -57,9 +60,11 @@ def t_error(t):
...
@@ -57,9 +60,11 @@ def t_error(t):
print('
Illegal
character
"%s"' % t.value[0])
print('
Illegal
character
"%s"' % t.value[0])
t.lexer.skip(1)
t.lexer.skip(1)
# Build the lexer
# Build the lexer
lexer = lex.lex()
lexer = lex.lex()
# Parsing rules
# Parsing rules
start = '
input
'
start = '
input
'
...
@@ -102,9 +107,14 @@ def p_command(p):
...
@@ -102,9 +107,14 @@ def p_command(p):
def p_error(p):
def p_error(p):
print '
Syntax
error
at
"%s"
on
line
%
d
' % (p.value, lexer.lineno)
print '
Syntax
error
at
"%s"
on
line
%
d
' % (p.value, lexer.lineno)
# Build YACC
yacc.yacc()
yacc.yacc()
def parse_file(filename):
def parse_file(filename):
"""Parse a given Assembly file, return a Block with Statement objects
containing the parsed instructions."""
global statements
global statements
statements = []
statements = []
...
...
src/writer.py
View file @
e62c02fc
from
math
import
ceil
from
math
import
ceil
def
write_statements
(
statements
):
def
write_statements
(
statements
):
"""Write a list of statements to valid assembly code."""
"""Write a list of statements to valid assembly code."""
out
=
''
out
=
''
...
@@ -14,7 +15,7 @@ def write_statements(statements):
...
@@ -14,7 +15,7 @@ def write_statements(statements):
indent_level
=
1
indent_level
=
1
elif
s
.
is_comment
():
elif
s
.
is_comment
():
line
=
'#'
+
s
.
name
line
=
'#'
+
s
.
name
if
s
.
is_inline_comment
():
if
s
.
is_inline_comment
():
l
=
len
(
prevline
.
expandtabs
(
4
))
l
=
len
(
prevline
.
expandtabs
(
4
))
tabs
=
int
(
ceil
((
24
-
l
)
/
4.
))
+
1
tabs
=
int
(
ceil
((
24
-
l
)
/
4.
))
+
1
...
@@ -39,6 +40,9 @@ def write_statements(statements):
...
@@ -39,6 +40,9 @@ def write_statements(statements):
out
+=
newline
+
line
out
+=
newline
+
line
prevline
=
line
prevline
=
line
# Add newline at end of file
out
+=
'
\
n
'
return
out
return
out
def
write_to_file
(
filename
,
statements
):
def
write_to_file
(
filename
,
statements
):
...
...
tests/test_dataflow.py
View file @
e62c02fc
...
@@ -48,21 +48,20 @@ class TestDataflow(unittest.TestCase):
...
@@ -48,21 +48,20 @@ class TestDataflow(unittest.TestCase):
def
test_dag_unary
(
self
):
def
test_dag_unary
(
self
):
dag
=
Dag
(
B
([
S
(
'command'
,
'neg.d'
,
'$rd'
,
'$rs'
)]))
dag
=
Dag
(
B
([
S
(
'command'
,
'neg.d'
,
'$rd'
,
'$rs'
)]))
expect
=
Dag
([])
expect
=
Dag
([])
expect
.
nodes
=
[
DagLeaf
(
'$rs'
),
DagNode
(
'neg.d'
,
'$rd'
,
DagLeaf
(
'$rs'
))]
expect
.
nodes
=
[
DagLeaf
(
'$rs'
),
DagNode
(
'neg.d'
,
'$rd'
,
\
DagLeaf
(
'$rs'
))]
self
.
assertEqualDag
(
dag
,
expect
)
self
.
assertEqualDag
(
dag
,
expect
)
def
test_dag_binary
(
self
):
def
test_dag_binary
(
self
):
dag
=
Dag
(
B
([
S
(
'command'
,
'addu'
,
'$rd'
,
'$r1'
,
'$r2'
)]))
dag
=
Dag
(
B
([
S
(
'command'
,
'addu'
,
'$rd'
,
'$r1'
,
'$r2'
)]))
expect
=
Dag
([])
expect
=
Dag
([])
expect
.
nodes
=
[
DagLeaf
(
'$r1'
),
expect
.
nodes
=
[
DagLeaf
(
'$r1'
),
DagLeaf
(
'$r2'
),
DagLeaf
(
'$r2'
),
DagNode
(
'addu'
,
'$rd'
,
DagLeaf
(
'$r1'
),
DagLeaf
(
'$r2'
))]
DagNode
(
'addu'
,
'$rd'
,
DagLeaf
(
'$r1'
),
DagLeaf
(
'$r2'
))]
self
.
assertEqualDag
(
dag
,
expect
)
self
.
assertEqualDag
(
dag
,
expect
)
# def test_dag_combinednode(self):
# def test_dag_combinednode(self):
# dag = Dag(B([S('command', 'mult', '$rd1', '$r1', '$r2'),
# dag = Dag(B([S('command', 'mult', '$rd1', '$r1', '$r2'),
# S('command', 'mult', '$rd2', '$r1', '$r2')]))
# S('command', 'mult', '$rd2', '$r1', '$r2')]))
...
@@ -74,16 +73,15 @@ class TestDataflow(unittest.TestCase):
...
@@ -74,16 +73,15 @@ class TestDataflow(unittest.TestCase):
# expect.nodes = [DagLeaf('$r1'),
# expect.nodes = [DagLeaf('$r1'),
# DagLeaf('$r2'),
# DagLeaf('$r2'),
# multnode]
# multnode]
#
#
# self.assertEqualDag(dag, expect)
# self.assertEqualDag(dag, expect)
def
assertEqualDag
(
self
,
dag1
,
dag2
):
def
assertEqualDag
(
self
,
dag1
,
dag2
):
self
.
assertEqual
(
len
(
dag1
.
nodes
),
len
(
dag2
.
nodes
))
self
.
assertEqual
(
len
(
dag1
.
nodes
),
len
(
dag2
.
nodes
))
for
node1
,
node2
in
zip
(
dag1
.
nodes
,
dag2
.
nodes
):
for
node1
,
node2
in
zip
(
dag1
.
nodes
,
dag2
.
nodes
):
self
.
assertEqualNodes
(
node1
,
node2
)
self
.
assertEqualNodes
(
node1
,
node2
)
def
assertEqualNodes
(
self
,
node1
,
node2
):
def
assertEqualNodes
(
self
,
node1
,
node2
):
if
isinstance
(
node1
,
DagLeaf
):
if
isinstance
(
node1
,
DagLeaf
):
self
.
assertIsInstance
(
node2
,
DagLeaf
)
self
.
assertIsInstance
(
node2
,
DagLeaf
)
...
@@ -91,9 +89,9 @@ class TestDataflow(unittest.TestCase):
...
@@ -91,9 +89,9 @@ class TestDataflow(unittest.TestCase):
elif
isinstance
(
node2
,
DagLeaf
):
elif
isinstance
(
node2
,
DagLeaf
):
raise
AssertionError
raise
AssertionError
else
:
else
:
self
.
assertEqual
(
node1
.
op
,
node2
.
op
)
self
.
assertEqual
(
node1
.
op
,
node2
.
op
)
self
.
assertEqual
(
node1
.
labels
,
node2
.
labels
)
self
.
assertEqual
(
node1
.
labels
,
node2
.
labels
)
self
.
assertEqual
(
len
(
node1
.
nodes
),
len
(
node2
.
nodes
))
self
.
assertEqual
(
len
(
node1
.
nodes
),
len
(
node2
.
nodes
))
for
child1
,
child2
in
zip
(
node1
.
nodes
,
node2
.
nodes
):
for
child1
,
child2
in
zip
(
node1
.
nodes
,
node2
.
nodes
):
self
.
assertEqualNodes
(
child1
,
child2
)
self
.
assertEqualNodes
(
child1
,
child2
)
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