git @ Cat's Eye Technologies SixtyPical / a0d3ea8
Add a dedicated module for a dedicated FallthruAnalyzer. Chris Pressey 3 years ago
3 changed file(s) with 35 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
5959
6060 if options.dump_fallthru_map:
6161 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))
6368 sys.stdout.write("\n")
6469
6570 if options.analyze_only:
155160 help="Dump the fallthru map to stdout after analyzing the program."
156161 )
157162 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(
158168 "--parse-only",
159169 action="store_true",
160170 help="Only parse the program; do not analyze or compile it."
309309 def analyze_program(self, program):
310310 assert isinstance(program, Program)
311311 self.routines = {r.location: r for r in program.routines}
312 fallthru_map = {}
313312 for routine in program.routines:
314313 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 []
320315
321316 def analyze_routine(self, routine):
322317 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