git @ Cat's Eye Technologies SixtyPical / 448849a
Successfully break cycle. Chris Pressey 4 years ago
3 changed file(s) with 22 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
6565 fa.analyze_program(program)
6666
6767 if options.dump_fallthru_info:
68 fallthru_map = dict(fa.fallthru_map)
69 sys.stdout.write(json.dumps(fallthru_map, indent=4, sort_keys=True))
68 sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True))
7069 sys.stdout.write("\n")
7170
72 fa.find_ancestors()
73
74 if options.debug and options.dump_fallthru_info:
75 sys.stdout.write("*** ancestors:\n")
76 sys.stdout.write(json.dumps(fa.ancestor_map, indent=4, sort_keys=True))
77 sys.stdout.write("\n")
78
7971 fa.find_cycles()
8072
81 if fa.cycles_found:
73 while fa.cycles_found:
8274 if options.dump_fallthru_info:
75
76 if options.debug:
77 sys.stdout.write("*** ancestors:\n")
78 sys.stdout.write(json.dumps(fa.ancestor_map, indent=4, sort_keys=True))
79 sys.stdout.write("\n")
80
8381 sys.stdout.write("*** cycles found:\n")
8482 sys.stdout.write(json.dumps(sorted(fa.cycles_found), indent=4, sort_keys=True))
8583 sys.stdout.write("\n")
8684
87 fa.break_cycles()
88
89 if options.dump_fallthru_info and fallthru_map != fa.fallthru_map:
90 sys.stdout.write("*** after breaking cycles:\n")
85 fa.break_cycle()
86
87 if options.dump_fallthru_info:
88 sys.stdout.write("*** after breaking cycle:\n")
9189 sys.stdout.write(json.dumps(fa.fallthru_map, indent=4, sort_keys=True))
9290 sys.stdout.write("\n")
91
92 fa.find_cycles()
9393
9494 if options.analyze_only:
9595 return
2323 self.fallthru_map = dict([(k, sorted(v)) for k, v in fallthru_map.iteritems()])
2424 return self.fallthru_map
2525
26 def find_ancestors(self):
26 def find_cycles(self):
2727 self.ancestor_map = {}
2828 for key in self.fallthru_map:
2929 ancestors = set()
3030 make_transitive_closure(self.fallthru_map, key, ancestors)
3131 self.ancestor_map[key] = sorted(ancestors)
32 return self.ancestor_map
3332
34 def find_cycles(self):
3533 self.cycles_found = set()
3634 for key in self.ancestor_map:
3735 if key in self.ancestor_map[key]:
3836 self.cycles_found.add(key)
37
3938 return self.cycles_found
4039
41 def break_cycles(self):
40 def break_cycle(self):
41 cycle_to_break = sorted(self.cycles_found)[0]
42 cycles_to_break = set([cycle_to_break])
43
4244 new_fallthru_map = {}
4345 for key in self.fallthru_map:
44 values = set(self.fallthru_map[key]) - self.cycles_found
46 values = set(self.fallthru_map[key]) - cycles_to_break
4547 if values:
4648 new_fallthru_map[key] = sorted(values)
4749 self.fallthru_map = new_fallthru_map
138138 = "bar",
139139 = "foo"
140140 = ]
141 = *** after breaking cycles:
141 = *** after breaking cycle:
142142 = {
143143 = "bar": [
144144 = "foo"