# Import classes and functions from RPE_Utils
from UtilsRPE.MainFunctions import parse_sources, postprocess_sources, assign_sbits_to_rpe_variables, \
    create_read_precisions_module_and_add_to_file, replace_wrkalloc, preprocess_sources
from UtilsRPE.Functions import load_vault, load_list_of_files_to_keep_unmodified

if __name__ == "__main__":
    # Paths to source folders
    path_to_input_sources = "./ModelSources/Input/"
    path_to_preprocessed_sources = "./ModelSources/Processed/"
    path_to_output_sources = "./ModelSources/Output/"

    # Starting
    print("Starting the implementation of the reduced precision emulator in:\n   %s\n\n" % path_to_input_sources)

    # !!!!!!Only for NEMO!!!!!!! Probably should be removed from the main tool.
    # Remove the use of wrkalloc calls and fix it with allocates
    replace_wrkalloc(path_to_input_sources, path_to_preprocessed_sources)

    # Blacklist of files that should not be processed. Is read from file "list_of_files_to_keep_unmodified.txt"
    preprocess_blacklist = load_list_of_files_to_keep_unmodified()

    # The code is first processed to replace real declarations with type(rpe_var)
    preprocess_sources(path_to_preprocessed_sources, path_to_preprocessed_sources, blacklist=preprocess_blacklist)

    # The code is parsed to obtain source information
    vault = parse_sources(path_to_preprocessed_sources, save_vault=True)

    # We load the code information into the vault
    try:
        vault
    except NameError:
        vault = load_vault()

    # Process the sources to fix call to functions, read/write, ...
    postprocess_sources(path_to_preprocessed_sources, path_to_output_sources, vault=vault)

    # Insert precision specification
    assign_sbits_to_rpe_variables(path_to_output_sources, vault=vault)

    # Add the read_precisions module into a file (should be accessible for all the other modules)
    file_to_add_read_precisions_module = None
    create_read_precisions_module_and_add_to_file(path_to_output_sources, file_to_add_read_precisions_module,
                                                  vault=vault)

