Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
320:84d67cce67b7 | 321:8c569fbe60e4 |
---|---|
1 import unittest | |
2 import io | |
3 import argparse | |
4 | |
5 from tree import Tree | |
6 import pyburg | |
7 | |
8 | |
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', | |
22 Tree('ADDRLP'), | |
23 Tree('ADDI', | |
24 Tree('CVCI', Tree('INDIRC', Tree('ADDRLP'))), | |
25 Tree('CNSTI') | |
26 ) | |
27 ) | |
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) | |
42 | |
43 if __name__ == '__main__': | |
44 unittest.main() |