I can get this test out of DVCS history if I ever need it again.
Cat's Eye Technologies
11 years ago
175 | 175 | |
176 | 176 | ### performance ### |
177 | 177 | |
178 | * IR: map program a map from prod name -> [prod AST]. | |
179 | 178 | * a second implementation, in C |
180 | 179 | * a compiler (in Python) *to* C |
6 | 6 | import codecs |
7 | 7 | import sys |
8 | 8 | |
9 | from tamsin.term import Term | |
10 | 9 | from tamsin.event import DebugEventListener |
11 | from tamsin.scanner import ( | |
12 | Scanner, | |
13 | TamsinScannerEngine, CharScannerEngine, ProductionScannerEngine | |
14 | ) | |
10 | from tamsin.scanner import Scanner, CharScannerEngine | |
15 | 11 | from tamsin.parser import Parser |
16 | 12 | from tamsin.interpreter import Interpreter |
17 | ||
18 | ||
19 | ||
20 | def test_peek_is_idempotent(): | |
21 | ast = ('PROGRAM', [('PROD', u'main', [], ('USING', ('CALL', u'program', [], None), u'scanner')), ('PROD', u'scanner', [], ('USING', ('CALL', u'scan', [], None), u'char')), ('PROD', u'scan', [], ('AND', ('LITERAL', u'X'), ('OR', ('AND', ('AND', ('AND', ('LITERAL', u'c'), ('LITERAL', u'a')), ('LITERAL', u't')), ('RETURN', Term('cat'))), ('AND', ('AND', ('AND', ('LITERAL', u'd'), ('LITERAL', u'o')), ('LITERAL', u'g')), ('RETURN', Term('dog')))))), ('PROD', u'program', [], ('AND', ('LITERAL', u'cat'), ('LITERAL', u'dog')))]) | |
22 | scanner = Scanner('XdogXcat') | |
23 | scanner.push_engine(TamsinScannerEngine()) | |
24 | interpreter = Interpreter(ast, scanner) | |
25 | ||
26 | prod = interpreter.find_productions('scanner')[0] | |
27 | print repr(prod) | |
28 | ||
29 | new_engine = ProductionScannerEngine(interpreter, prod) | |
30 | scanner.push_engine(new_engine) | |
31 | ||
32 | print "---INITIAL STATE---" | |
33 | scanner.dump() | |
34 | print repr(interpreter) | |
35 | ||
36 | ||
37 | print "---INITIAL CALL TO peek---" | |
38 | token = scanner.peek() | |
39 | print token | |
40 | scanner.dump() | |
41 | print repr(interpreter) | |
42 | ||
43 | ||
44 | for i in xrange(0, 4): | |
45 | sav_tok = token | |
46 | print "---SUBSEQUENT CALL TO peek---" | |
47 | token = scanner.peek() | |
48 | print token | |
49 | scanner.dump() | |
50 | print repr(interpreter) | |
51 | ||
52 | ||
53 | if sav_tok != token: | |
54 | print "FAILED" | |
55 | break | |
56 | 13 | |
57 | 14 | |
58 | 15 | def main(args): |
60 | 17 | if args[0] == '--debug': |
61 | 18 | listeners.append(DebugEventListener()) |
62 | 19 | args = args[1:] |
63 | if args[0] == 'test': | |
64 | test_peek_is_idempotent() | |
65 | return | |
66 | 20 | if args[0] == 'parse': |
67 | 21 | with codecs.open(args[1], 'r', 'UTF-8') as f: |
68 | 22 | contents = f.read() |