Commit e62c02fc authored by Taddeus Kroes's avatar Taddeus Kroes

Applied pep8.

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