Untitled

 avatar
unknown
plain_text
4 months ago
2.5 kB
3
Indexable
def resolve_functions_recursive(
    prog_txt, 
    functions_list, 
    replacements_dict, 
    processed_files, 
    cpy_files
):
    """
    Recursively resolve functions and merge them into the program text.

    Args:
        prog_txt: The main program text.
        functions_list: List of functions to process.
        replacements_dict: Replacement rules for COPY statements.
        processed_files: A set of already processed files to avoid circular references.
        cpy_files: List of available function files.

    Returns:
        Tuple containing:
        - The updated program text with all functions resolved.
        - A list of functions that were not found.
    """
    not_found_functions = []

    for fun in functions_list:
        if fun in processed_files:
            logging.warning(f"Circular reference detected for function: {fun}")
            continue

        logging.debug(f"Processing function: {fun}")
        processed_files.add(fun)

        # Locate the file for the function
        function_path = next((i for i in cpy_files if f"{fun}.cpy" in i), None)
        if not function_path:
            logging.error(f"Function file not found for: {fun}")
            not_found_functions.append(fun)  # Add to the list of not found functions
            continue

        # Read the function file and parse its content
        logging.debug(f"Reading function file: {function_path}")
        sub_functions_list, sub_prog_txt = parser.get_functions(function_path)
        if sub_functions_list:
            logging.info(f"Got sub-functions: {sub_functions_list}")

        # Apply `func_replace` and `func_do_task` to the content
        sub_prog_txt = "".join(sub_prog_txt)
        sub_prog_txt = parser.combine_copy_statements(sub_prog_txt)

        # Recursively resolve nested functions
        sub_replacements_dict = parser.parse_cobol_replacements_sub(sub_prog_txt)
        sub_prog_txt, sub_not_found = resolve_functions_recursive(
            sub_prog_txt, sub_functions_list, sub_replacements_dict, processed_files, cpy_files
        )
        not_found_functions.extend(sub_not_found)  # Add sub-not-found functions to the main list

        # Merge the resolved function into the main program text
        prog_txt = parser.merge_functions(
            replacement_dict=replacements_dict.get(fun),
            program_text=prog_txt,
            function_name=fun,
            function_path=function_path,
        )

    return prog_txt, not_found_functions
Editor is loading...
Leave a Comment