def cpp( path: str ) -> str: defined = set() out = '' emit : list[ bool ] = [] def process( line: str ) -> None: if line.startswith( '#ifdef' ): cmd, macro = line.split() emit.append( macro in defined ) if line.startswith( '#endif' ): emit.pop() if not emit or emit[ -1 ]: if line.startswith( '#define' ): cmd, macro = line.split() defined.add( macro ) if line.startswith( '#undef' ): cmd, macro = line.split() defined.remove( macro ) if line.startswith( '#include' ): cmd, path = line.split() read( path[ 1: -1 ] ) def read( path: str ) -> None: nonlocal out with open( path, 'r' ) as f: for line in f: if line[ 0 ] == '#': process( line ) else: if not emit or emit[ -1 ]: out += line read( path ) return out if __name__ == '__main__': import sys if len( sys.argv ) >= 2: print( end = cpp( sys.argv[ 1 ] ) ) exit( 0 ) import run_tests