Commit e62c02fc authored by Taddeus Kroes's avatar Taddeus Kroes

Applied pep8.

parent effe7267
...@@ -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 = []
......
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):
......
...@@ -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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment