321
|
1 import unittest
|
|
2 import io
|
|
3 import argparse
|
|
4
|
319
|
5 from tree import Tree
|
321
|
6 import pyburg
|
319
|
7
|
|
8
|
321
|
9 class testBURG(unittest.TestCase):
|
|
10 def testSample4(self):
|
|
11 """ Test sample4 burg system """
|
|
12 # Generate matcher from spec:
|
|
13 buf = io.StringIO()
|
|
14 args = argparse.Namespace(source=open('sample4.brg'), output=buf)
|
|
15 pyburg.main(args)
|
|
16
|
|
17 # Execute generated script into global scope:
|
|
18 exec(buf.getvalue(), globals())
|
|
19
|
|
20 # Sample tree:
|
|
21 t = Tree('ASGNI',
|
319
|
22 Tree('ADDRLP'),
|
|
23 Tree('ADDI',
|
|
24 Tree('CVCI', Tree('INDIRC', Tree('ADDRLP'))),
|
|
25 Tree('CNSTI')
|
|
26 )
|
|
27 )
|
321
|
28
|
|
29 # Subclass generated matcher:
|
|
30 class MyMatcher(Matcher):
|
|
31 def __init__(self):
|
|
32 super().__init__()
|
|
33 self.trace = []
|
|
34
|
|
35 def tr(self, r):
|
|
36 self.trace.append(r)
|
|
37
|
|
38 # Match tree:
|
|
39 mm = MyMatcher()
|
|
40 mm.gen(t)
|
|
41 self.assertSequenceEqual([8,8,4,11,9,3,1], mm.trace)
|
319
|
42
|
|
43 if __name__ == '__main__':
|
321
|
44 unittest.main()
|