sm64coopdx/autogen/extract_functions.py

69 lines
1.3 KiB
Python

import os
import re
import sys
with open('./src/audio/external.h') as file:
lines = file.readlines()
# strip directives and comments
txt = ''
for line in lines:
if line.strip().startswith('#'):
continue
if '//' in line:
line = line.split('//', 1)[0]
txt += line
while ('/*' in txt):
s1 = txt.split('/*', 1)
s2 = s1[1].split('*/', 1)
txt = s1[0] + s2[-1]
# normalize newlines
txt = txt.replace('\n', ' ')
txt = txt.replace(';', ';\n')
txt = txt.replace('{', '{\n')
while (' ' in txt):
txt = txt.replace(' ', ' ')
# strip macros
txt = re.sub(r'[^a-zA-Z0-9_][A-Z0-9_]+\(.*\)', '', txt)
# strip blocks
tmp = txt
txt = ''
inside = 0
for character in tmp:
if inside == 0:
txt += character
if character == '{':
txt += '\n'
inside += 1
if character == '}':
inside -= 1
# cull obvious non-functions, statics, and externs
tmp = txt
txt = ''
for line in tmp.splitlines():
line = line.strip()
if '(' not in line:
continue
if ')' not in line:
continue
if '=' in line:
continue
#if '{' not in line:
# continue
if line.startswith('static '):
continue
if line.startswith('extern '):
continue
txt += line + '\n'
# normalize function ending
txt = txt.replace(' {', ';')
print('-'* 80 + '\n' + txt)