Add a dedicated module for a dedicated FallthruAnalyzer.
Chris Pressey
4 years ago
59 | 59 |
|
60 | 60 |
if options.dump_fallthru_map:
|
61 | 61 |
import json
|
62 | |
sys.stdout.write(json.dumps(program.fallthru_map, indent=4, sort_keys=True))
|
|
62 |
from sixtypical.fallthru import FallthruAnalyzer
|
|
63 |
|
|
64 |
fa = FallthruAnalyzer(debug=options.debug)
|
|
65 |
fa.analyze_program(program)
|
|
66 |
|
|
67 |
sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True))
|
63 | 68 |
sys.stdout.write("\n")
|
64 | 69 |
|
65 | 70 |
if options.analyze_only:
|
|
155 | 160 |
help="Dump the fallthru map to stdout after analyzing the program."
|
156 | 161 |
)
|
157 | 162 |
argparser.add_argument(
|
|
163 |
"--dump-fallthru-ordering",
|
|
164 |
action="store_true",
|
|
165 |
help="Dump the fallthru ordering to stdout after analyzing the program."
|
|
166 |
)
|
|
167 |
argparser.add_argument(
|
158 | 168 |
"--parse-only",
|
159 | 169 |
action="store_true",
|
160 | 170 |
help="Only parse the program; do not analyze or compile it."
|
309 | 309 |
def analyze_program(self, program):
|
310 | 310 |
assert isinstance(program, Program)
|
311 | 311 |
self.routines = {r.location: r for r in program.routines}
|
312 | |
fallthru_map = {}
|
313 | 312 |
for routine in program.routines:
|
314 | 313 |
context = self.analyze_routine(routine)
|
315 | |
if context:
|
316 | |
encountered_gotos = list(context.encountered_gotos())
|
317 | |
if len(encountered_gotos) == 1 and isinstance(encountered_gotos[0].type, RoutineType):
|
318 | |
fallthru_map.setdefault(encountered_gotos[0].name, set()).add(routine.name)
|
319 | |
program.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()])
|
|
314 |
routine.encountered_gotos = list(context.encountered_gotos()) if context else []
|
320 | 315 |
|
321 | 316 |
def analyze_routine(self, routine):
|
322 | 317 |
assert isinstance(routine, Routine)
|
|
0 |
# encoding: UTF-8
|
|
1 |
|
|
2 |
from sixtypical.model import RoutineType
|
|
3 |
|
|
4 |
|
|
5 |
class FallthruAnalyzer(object):
|
|
6 |
|
|
7 |
def __init__(self, debug=False):
|
|
8 |
self.debug = debug
|
|
9 |
|
|
10 |
def analyze_program(self, program):
|
|
11 |
fallthru_map = {}
|
|
12 |
for routine in program.routines:
|
|
13 |
encountered_gotos = list(routine.encountered_gotos)
|
|
14 |
if len(encountered_gotos) == 1 and isinstance(encountered_gotos[0].type, RoutineType):
|
|
15 |
fallthru_map.setdefault(encountered_gotos[0].name, set()).add(routine.name)
|
|
16 |
self.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()])
|
|
17 |
|
|
18 |
def break_cycles(self):
|
|
19 |
raise NotImplementedError
|
|
20 |
|
|
21 |
def serialize(self):
|
|
22 |
raise NotImplementedError
|