diff --git a/bin/sixtypical b/bin/sixtypical index 80fc8e6..ab714f7 100755 --- a/bin/sixtypical +++ b/bin/sixtypical @@ -60,7 +60,12 @@ if options.dump_fallthru_map: import json - sys.stdout.write(json.dumps(program.fallthru_map, indent=4, sort_keys=True)) + from sixtypical.fallthru import FallthruAnalyzer + + fa = FallthruAnalyzer(debug=options.debug) + fa.analyze_program(program) + + sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True)) sys.stdout.write("\n") if options.analyze_only: @@ -156,6 +161,11 @@ help="Dump the fallthru map to stdout after analyzing the program." ) argparser.add_argument( + "--dump-fallthru-ordering", + action="store_true", + help="Dump the fallthru ordering to stdout after analyzing the program." + ) + argparser.add_argument( "--parse-only", action="store_true", help="Only parse the program; do not analyze or compile it." diff --git a/src/sixtypical/analyzer.py b/src/sixtypical/analyzer.py index b107ed7..df76933 100644 --- a/src/sixtypical/analyzer.py +++ b/src/sixtypical/analyzer.py @@ -310,14 +310,9 @@ def analyze_program(self, program): assert isinstance(program, Program) self.routines = {r.location: r for r in program.routines} - fallthru_map = {} for routine in program.routines: context = self.analyze_routine(routine) - if context: - encountered_gotos = list(context.encountered_gotos()) - if len(encountered_gotos) == 1 and isinstance(encountered_gotos[0].type, RoutineType): - fallthru_map.setdefault(encountered_gotos[0].name, set()).add(routine.name) - program.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()]) + routine.encountered_gotos = list(context.encountered_gotos()) if context else [] def analyze_routine(self, routine): assert isinstance(routine, Routine) diff --git a/src/sixtypical/fallthru.py b/src/sixtypical/fallthru.py new file mode 100644 index 0000000..d7face9 --- /dev/null +++ b/src/sixtypical/fallthru.py @@ -0,0 +1,23 @@ +# encoding: UTF-8 + +from sixtypical.model import RoutineType + + +class FallthruAnalyzer(object): + + def __init__(self, debug=False): + self.debug = debug + + def analyze_program(self, program): + fallthru_map = {} + for routine in program.routines: + encountered_gotos = list(routine.encountered_gotos) + if len(encountered_gotos) == 1 and isinstance(encountered_gotos[0].type, RoutineType): + fallthru_map.setdefault(encountered_gotos[0].name, set()).add(routine.name) + self.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()]) + + def break_cycles(self): + raise NotImplementedError + + def serialize(self): + raise NotImplementedError