From 7187fa6285149e3b286443737ef5b2efda5b4893 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Mon, 21 Oct 2019 15:41:17 +0100 Subject: [PATCH] Refactor: move to dedicated module. --- bin/sixtypical | 33 ++------------------------------- src/sixtypical/callgraph.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 src/sixtypical/callgraph.py diff --git a/bin/sixtypical b/bin/sixtypical index 7f98e03..f16ffac 100755 --- a/bin/sixtypical +++ b/bin/sixtypical @@ -51,37 +51,8 @@ def process_input_files(filenames, options): sys.stdout.write("\n") if options.dump_callgraph: - graph = {} - - from sixtypical.model import RoutineType, VectorType - - def find_routines_matching_vector_type(program, type_): - return [] # dummy - - for routine in program.routines: - potentially_calls = [] - for (called_routine, called_routine_type) in routine.called_routines: - if isinstance(called_routine_type, RoutineType): - potentially_calls.append(called_routine.name) - elif isinstance(called_routine_type, VectorType): - for potentially_called in find_routines_matching_vector_type(program, called_routine_type): - potentially_calls.append(potentially_called.name) - else: - raise NotImplementedError - graph[routine.name] = { - 'potentially-calls': potentially_calls, - } - - # Reflexive closure - - for routine in program.routines: - potentially_called_by = [] - for (name, node) in graph.items(): - potential_calls = node['potentially-calls'] - if routine.name in potential_calls: - potentially_called_by.append(name) - graph[routine.name]['potentially-called-by'] = potentially_called_by - + from sixtypical.callgraph import construct_callgraph + graph = construct_callgraph(program) sys.stdout.write(json.dumps(graph, indent=4, sort_keys=True, separators=(',', ':'))) compilation_roster = None diff --git a/src/sixtypical/callgraph.py b/src/sixtypical/callgraph.py new file mode 100644 index 0000000..d7a9773 --- /dev/null +++ b/src/sixtypical/callgraph.py @@ -0,0 +1,35 @@ +from sixtypical.model import RoutineType, VectorType + + +def find_routines_matching_vector_type(program, type_): + return [] # dummy + + +def construct_callgraph(program): + graph = {} + + for routine in program.routines: + potentially_calls = [] + for (called_routine, called_routine_type) in routine.called_routines: + if isinstance(called_routine_type, RoutineType): + potentially_calls.append(called_routine.name) + elif isinstance(called_routine_type, VectorType): + for potentially_called in find_routines_matching_vector_type(program, called_routine_type): + potentially_calls.append(potentially_called.name) + else: + raise NotImplementedError + graph[routine.name] = { + 'potentially-calls': potentially_calls, + } + + # Reflexive closure + + for routine in program.routines: + potentially_called_by = [] + for (name, node) in graph.items(): + potential_calls = node['potentially-calls'] + if routine.name in potential_calls: + potentially_called_by.append(name) + graph[routine.name]['potentially-called-by'] = potentially_called_by + + return graph