annotate test/test_burm.py @ 321:8c569fbe60e4

Load yacc and burg dynamic
author Windel Bouwman
date Sun, 19 Jan 2014 18:48:45 +0100
parents python/test_burm.py@8d07a4254f04
children
rev   line source
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
1 import unittest
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
2 import io
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
3 import argparse
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
4
319
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
5 from tree import Tree
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
6 import pyburg
319
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
7
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
8
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
9 class testBURG(unittest.TestCase):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
10 def testSample4(self):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
11 """ Test sample4 burg system """
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
12 # Generate matcher from spec:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
13 buf = io.StringIO()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
14 args = argparse.Namespace(source=open('sample4.brg'), output=buf)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
15 pyburg.main(args)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
16
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
17 # Execute generated script into global scope:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
18 exec(buf.getvalue(), globals())
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
19
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
20 # Sample tree:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
21 t = Tree('ASGNI',
319
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
22 Tree('ADDRLP'),
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
23 Tree('ADDI',
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
24 Tree('CVCI', Tree('INDIRC', Tree('ADDRLP'))),
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
25 Tree('CNSTI')
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
26 )
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
27 )
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
28
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
29 # Subclass generated matcher:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
30 class MyMatcher(Matcher):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
31 def __init__(self):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
32 super().__init__()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
33 self.trace = []
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
34
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
35 def tr(self, r):
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
36 self.trace.append(r)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
37
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
38 # Match tree:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
39 mm = MyMatcher()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
40 mm.gen(t)
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
41 self.assertSequenceEqual([8,8,4,11,9,3,1], mm.trace)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
42
8d07a4254f04 Work on burg
Windel Bouwman
parents:
diff changeset
43 if __name__ == '__main__':
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 319
diff changeset
44 unittest.main()