### Configuration for creating the bootstrap model using conformance_bottom.
import glob
import hashlib
import tempfile
import gzip

def bootstrap():
    root = ["__hierarchy"]

    task_manager = "task_manager"
    initial_code_manager = "bootstrap/initial_code_manager.alb"
    initial_code_task = "bootstrap/initial_code_task.alb"
    bootstrap_files = glob.glob("bootstrap/*.alc") + [initial_code_manager, initial_code_task]

    task_data = [   "input",
                    "output",
                    "globals",
                    "frame",
                    ]

    task_frame = [  "evalstack",
                    "symbols",
                    "returnvalue",
                    ]

    primitives = {  "integer_addition": ["Integer", "Integer", "Integer"],
                    "integer_subtraction": ["Integer", "Integer", "Integer"],
                    "integer_multiplication": ["Integer", "Integer", "Integer"],
                    "integer_division": ["Integer", "Integer", "Integer"],
                    "integer_gt": ["Boolean", "Integer", "Integer"],
                    "integer_lt": ["Boolean", "Integer", "Integer"],
                    "integer_neg": ["Integer", "Integer"],
                    "float_addition": ["Float", "Float", "Float"],
                    "float_subtraction": ["Float", "Float", "Float"],
                    "float_multiplication": ["Float", "Float", "Float"],
                    "float_division": ["Float", "Float", "Float"],
                    "float_gt": ["Boolean", "Float", "Float"],
                    "float_lt": ["Boolean", "Float", "Float"],
                    "float_neg": ["Float", "Float"],
                    "bool_and": ["Boolean", "Boolean", "Boolean"],
                    "bool_or": ["Boolean", "Boolean", "Boolean"],
                    "bool_not": ["Boolean", "Boolean"],
                    "string_join": ["String", "String", "String"],
                    "string_get": ["String", "String", "Integer"],
                    "string_len": ["Integer", "String"],
                    "string_split": ["Element", "String", "String"],
                    "value_eq":  ["Boolean", "Element", "Element"],
                    "value_neq": ["Boolean", "Element", "Element"],
                    "cast_i2f": ["Float", "Integer"],
                    "cast_i2s": ["String", "Integer"],
                    "cast_i2b": ["Boolean", "Integer"],
                    "cast_f2i": ["Integer", "Float"],
                    "cast_f2b": ["Boolean", "Float"],
                    "cast_f2s": ["String", "Float"],
                    "cast_s2i": ["Integer", "String"],
                    "cast_s2f": ["Float", "String"],
                    "cast_s2b": ["Boolean", "String"],
                    "cast_b2i": ["Integer", "Boolean"],
                    "cast_b2f": ["Float", "Boolean"],
                    "cast_b2s": ["String", "Boolean"],
                    "cast_e2s": ["String", "Element"],
                    "cast_a2s": ["String", "Action"],
                    "cast_v2s": ["String", "Element"],
                    "cast_id2s": ["String", "Element"],
                    "list_read": ["Element", "Element", "Integer"],
                    "list_append": ["Element", "Element", "Element"],
                    "list_insert": ["Element", "Element", "Integer", "Element"],
                    "list_delete": ["Element", "Element", "Integer"],
                    "list_len": ["Integer", "Element"],
                    "dict_add": ["Element", "Element", "Element", "Element"],
                    "dict_delete": ["Element", "Element", "Element"],
                    "dict_delete_node": ["Element", "Element", "Element"],
                    "dict_read": ["Element", "Element", "Element"],
                    "dict_read_edge": ["Element", "Element", "Element"],
                    "dict_read_node": ["Element", "Element", "Element"],
                    "dict_len": ["Integer", "Element"],
                    "dict_in": ["Boolean", "Element", "Element"],
                    "dict_in_node": ["Boolean", "Element", "Element"],
                    "dict_keys": ["Element", "Element"],
                    "set_add": ["Element", "Element", "Element"],
                    "set_pop": ["Element", "Element"],
                    "set_remove": ["Element", "Element", "Element"],
                    "set_remove_node": ["Element", "Element", "Element"],
                    "set_in": ["Boolean", "Element", "Element"],
                    "set_in_node": ["Boolean", "Element", "Element"],
                    "is_physical_int": ["Boolean", "Element"],
                    "is_physical_boolean": ["Boolean", "Element"],
                    "is_physical_string": ["Boolean", "Element"],
                    "is_physical_action": ["Boolean", "Element"],
                    "is_physical_float": ["Boolean", "Element"],
                    "create_node": ["Element"],
                    "create_edge": ["Element", "Element", "Element"],
                    "create_value": ["Element", "Element"],
                    "is_edge": ["Boolean", "Element"],
                    "read_nr_out": ["Integer", "Element"],
                    "read_out": ["Element", "Element", "Integer"],
                    "read_nr_in": ["Integer", "Element"],
                    "read_in": ["Element", "Element", "Integer"],
                    "read_edge_src": ["Element", "Element"],
                    "read_edge_dst": ["Element", "Element"],
                    "delete_element": ["Element", "Element"],
                    "element_eq": ["Boolean", "Element", "Element"],
                    "element_neq": ["Boolean", "Element", "Element"],
                    "read_root": ["Element"],
                    "read_taskroot": ["Element"],
                    "deserialize": ["Element", "String"],
                    "log": ["String", "String"],
                    "time": ["Float"],
                    "hash": ["String", "String"],
                }

    jit_primitives = {
        "get_jit_enabled": ["Boolean"],
        "set_jit_enabled": ["Void", "Boolean"]
    }

    ### Actual script to generate the file
    import os
    import sys

    class Writer(object):
        def __init__(self, file_a, file_b):
            self.file_a = file_a
            self.file_b = file_b

        def write(self, text, both=True):
            self.file_a.write(text)
            if both:
                self.file_b.write(text)

    try:
        with gzip.GzipFile("bootstrap/bootstrap.m.gz", "wb", mtime=0) as fa:
            with gzip.GzipFile("bootstrap/minimal.m.gz", "wb", mtime=0) as fb:
                f = Writer(fa, fb)
                # Create the root first
                f.write("Node root()\n")

                # Create all children of the root
                for node in root:
                    f.write("Node %s()\n" % node)
                    f.write("Edge _%s(root, %s)\n" % (node, node))
                    f.write('Node __%s("%s")\n' % (node, node))
                    f.write("Edge ___%s(_%s, __%s)\n" % (node, node, node))

                def declare_primitive_class(primitive_class_name, primitive_decls):
                    f.write("Node %s()\n" % primitive_class_name)
                    f.write("Edge _%s(__hierarchy, %s)\n" % (primitive_class_name, primitive_class_name))
                    f.write('Node __%s("%s")\n' % (primitive_class_name, primitive_class_name))
                    f.write("Edge ___%s(_%s, __%s)\n" % (primitive_class_name, primitive_class_name, primitive_class_name))

                    # Define all primitive functions
                    for function, parameters in primitive_decls.iteritems():
                        f.write("Node _func_signature_%s()\n" % function)
                        f.write("Node _func_params_%s()\n" % function)
                        f.write("Node _func_body_%s()\n" % function)
                        f.write("Edge _%s_%s(%s, _func_signature_%s)\n" % (primitive_class_name, function, primitive_class_name, function))
                        f.write('Node _name_%s("%s")\n' % (function, function))
                        f.write("Edge _%s_name_%s(_%s_%s, _name_%s)\n" % (primitive_class_name, function, primitive_class_name, function, function))

                        f.write('Node _body_%s("body")\n' % function)
                        f.write("Edge _signature_body_%s(_func_signature_%s, _func_body_%s)\n" % (function, function, function))
                        f.write("Edge _signature_body_str_%s(_signature_body_%s, _body_%s)\n" % (function, function, function))

                        f.write('Node _params_%s("params")\n' % function)
                        f.write("Edge _signature_params_%s(_func_signature_%s, _func_params_%s)\n" % (function, function, function))
                        f.write("Edge _signature_params_str_%s(_signature_params_%s, _params_%s)\n" % (function, function, function))

                        parameter_names = "abcdefghijklmnopqrstuvwxyz"
                        for number, param in enumerate(parameters[1:]):
                            param_encoding = "%s_%s" % (function, parameter_names[number])
                            f.write("Node _func_params_%s()\n" % (param_encoding))
                            f.write('Node _name_%s("%s")\n' % (param_encoding, parameter_names[number]))
                            f.write("Edge _param_link_%s(_func_params_%s, _func_params_%s)\n" % (param_encoding, function, param_encoding))
                            f.write("Edge _param_link_str_%s(_param_link_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
                            f.write('Node _name_str_%s("name")\n' % param_encoding)
                            f.write("Edge _param_name_%s(_func_params_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
                            f.write("Edge _param_name_str_%s(_param_name_%s, _name_str_%s)\n" % (param_encoding, param_encoding, param_encoding))

                declare_primitive_class('primitives', primitives)
                declare_primitive_class('jit', jit_primitives)

                # Create the initial task
                f.write("Node task_root()\n")

                for data in task_data:
                    f.write("Node task_%s()\n" % data)
                    f.write('Node ___task_%s("%s")\n' % (data, data))
                    f.write("Edge _task_%s(task_root, task_%s)\n" % (data, data))
                    f.write("Edge __task_%s(_task_%s, ___task_%s)\n" % (data, data, data))

                for data in task_frame:
                    f.write("Node task_%s()\n" % data)
                    f.write('Node ___task_%s("%s")\n' % (data, data))
                    f.write("Edge _task_%s(task_frame, task_%s)\n" % (data, data))
                    f.write("Edge __task_%s(_task_%s, ___task_%s)\n" % (data, data, data))

                # Add last_input and last_output links
                for data in ["input", "output"]:
                    f.write('Node ___task_last_%s("last_%s")\n' % (data, data))
                    f.write("Edge _task_last_%s(task_root, task_%s)\n" % (data, data))
                    f.write("Edge __task_last_%s(_task_last_%s, ___task_last_%s)\n" % (data, data, data))
                    
                # Bind task to the root
                f.write('Node ___new_task("%s")\n' % task_manager)
                f.write("Edge _new_task(root, task_root)\n")
                f.write("Edge __new_task(_new_task, ___new_task)\n")

                def compile_code_AL(filename, target, prepend="", main=False, symbols=None):
                    import sys
                    sys.path.append("interface/HUTN/")
                    from hutn_compiler.compiler import main as compile_code
                    code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"], symbols=symbols)
                    return code.replace("auto_initial_IP", target)

                # Create all library code
                # But first create the structure to hold compiled data
                f.write("Node __objects()\n", both=False)
                f.write('Node __objects_name("objects")\n', both=False)
                f.write("Edge __obj_link(__hierarchy, __objects)\n", both=False)
                f.write("Edge _name_obj_link(__obj_link, __objects_name)\n", both=False)

                # Compile all files and add to structure manually
                for bootstrap_file in bootstrap_files:
                    # Compile the subfile
                    bootstrap_file = bootstrap_file.replace("\\", "/")
                    print("[COMP] %s" % bootstrap_file)
                    symbols = {}
                    f.write(compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, symbols=symbols, main = bootstrap_file in [initial_code_manager, initial_code_task]), both=False)

                    # Now link the code with the compilation manager structure
                    f.write("Node elem()\n", both=False)
                    f.write('Node initializers("initializers")\n', both=False)
                    f.write('Node hash("hash_md5")\n', both=False)
                    f.write("Edge _(__objects, elem)\n", both=False)
                    f.write('Node filename("%s")\n' % bootstrap_file, both=False)
                    f.write("Edge _(_, filename)\n", both=False)
                    f.write("Edge _(elem, %s_initial_IP)\n" % bootstrap_file, both=False)
                    f.write("Edge _(_, initializers)\n", both=False)
                    md5 = hashlib.md5()
                    md5.update(open(bootstrap_file, 'r').read())
                    f.write('Node hash_value("%s")\n' % md5.hexdigest(), both=False)
                    f.write("Edge _(elem, hash_value)\n", both=False)
                    f.write("Edge _(_, hash)\n", both=False)

                    f.write('Node symbols("symbols")\n', both=False)
                    f.write('Node __symbols()\n', both=False)
                    f.write('Edge _(elem, __symbols)\n', both=False)
                    f.write('Edge _(_, symbols)\n', both=False)

                    for k, v in symbols.items():
                        f.write('Node v(%s)\n' % v, both=False)
                        f.write('Node k("%s")\n' % k, both=False)
                        f.write('Edge _(__symbols, v)\n', both=False)
                        f.write('Edge _(_, k)\n', both=False)

                # Create code for initial task
                print("[BOOT] task_manager")
                f.write('Node _IP_str("IP")\n', both=False)
                f.write("Edge _task_frame(task_frame, %s_initial_IP)\n" % initial_code_manager, both=False)
                f.write("Edge __task_frame(_task_frame, _IP_str)\n", both=False)

                f.write('Node __phase("init")\n', both=False)
                f.write('Node __phase_str("phase")\n', both=False)
                f.write("Edge _task_phase(task_frame, __phase)\n", both=False)
                f.write("Edge __task_phase(_task_phase, __phase_str)\n", both=False)

                # Create code for new tasks to start at
                print("[BOOT] new_task")
                f.write('Node __IP_str("__IP")\n', both=False)
                f.write("Edge _task_IP(__hierarchy, %s_initial_IP)\n" % initial_code_task, both=False)
                f.write("Edge __task_IP(_task_IP, __IP_str)\n", both=False)
    except:
        os.remove("bootstrap/bootstrap.m.gz")
        os.remove("bootstrap/minimal.m.gz")
        raise
