I need a python script that works on C\C++, python and javascript code and do the following:
-creates 'functions' list - a list of functions defined in the code;
-creates "callers" list of lists where first index corresponds to the index of a function in 'functions' list and we have callers of that function at this index;
-creates "calls" list of lists where first index corresponds to the index of a function in 'functions' list and we have calls of that function to other functions;
Give me a quick and dirty solution. Focus on the core functionality, ignore edge cases, and prioritize brevity and simplicity over completeness. I'm looking for a clever hack rather than a comprehensive implementation
Assistant
Here's a quick and dirty Python script that processes C/C++, Python, and JavaScript code to extract functions, their callers, and the functions they call:
python
import re
import sys
def extract_functions_and_calls(code):
functions = []
function_bodies = {}
# Regex patterns for function definitions
func_def_patterns = [
# C/C++ function definitions
r'^\s*(?:[\w\*\&]+\s+)+(\w+)\s*\(.*?\)\s*\{',
# Python function definitions
r'^\s*def\s+(\w+)\s*\(',
# JavaScript function definitions
r'^\s*function\s+(\w+)\s*\(',
r'^\s*var\s+(\w+)\s*=\s*function\s*\(',
r'^\s*(\w+)\s*=\s*function\s*\(',
r'^\s*(\w+)\s*\([^)]*\)\s*\{', # For JS function declarations without 'function' keyword
]
# Pattern for function calls (simplified)
func_call_pattern = r'(\b\w+\b)\s*\('
lines = code.split('\n')
in_function = False
current_function = ''
brace_count = 0
functions_set = set()
for i, line in enumerate(lines):
# Check for function definition
for pattern in func_def_patterns:
match = re.match(pattern, line)
if match:
func_name = match.group(1)
functions.append(func_name)
functions_set.add(func_name)
current_function = func_name
in_function = True
function_bodies[current_function] = ''
# For languages with braces, reset brace count
brace_count = line.count('{') - line.count('}')
break
else:
if in_function:
function_bodies[current_function] += line + '\n'
# Update brace count for C/C++ and JavaScript
brace_count += line.count('{') - line.count('}')
if brace_count == 0 and ('{' in function_bodies[current_function]):
in_function = False
# For Python, end function definition on dedent
if re.match(r'^\s*\S', line) and 'def ' not in line and ':' not in line and not line.strip().startswith('#'):
in_function = False
# Build calls list
calls = [[] for _ in functions]
func_name_to_index = {name: idx for idx, name in enumerate(functions)}
for func_name, body in function_bodies.items():
idx = func_name_to_index[func_name]
called_funcs = re.findall(func_call_pattern, body)
# Keep only functions that are in our functions list
called_funcs = [fn for fn in called_funcs if fn in functions_set and fn != func_name]
calls[idx] = called_funcs
# Build callers list
callers = [[] for _ in functions]
for idx, called_funcs in enumerate(calls):
for called_func in called_funcs:
called_idx = func_name_to_index[called_func]
callers[called_idx].append(functions[idx])
return functions, callers, calls
if __name__ == '__main__':
# Read code from a file specified in the command line, or use an example code string
if len(sys.argv) > 1:
with open(sys.argv[1], 'r') as f:
code = f.read()
else:
code = '''
// Example code
function foo() {
bar();
baz();
}
function bar() {
// do something
}
function baz() {
bar();
}
'''
functions, callers, calls = extract_functions_and_calls(code)
print('Functions:', functions)
print('Callers:', callers)
print('Calls:', calls)
Usage:
Save this script to a file, e.g., function_analyzer.py.