Beispiel #1
0
def compileMcScript(config: Config, callback: Callable = None) -> Datapack:
    """
    compiles a mcscript string and returns the generated datapack.

    Args:
        callback: a callback function that accepts the current state, the progress and the temporary object
        config: the config. Should contain the input file and the target path

    Returns:
        A datapack
    """
    steps = (
        (_parseCode, "Parsing"),
        (lambda tree: Analyzer().analyze(tree), "Analyzing context"),
        (lambda tree: get_compiler().compile(tree[0], tree[1], text, config), "Compiling"),
        (lambda ir_master: get_default_backend()(config, ir_master).generate(), "Running ir backend")
    )

    text = config.input_string

    if Logger.isEnabledFor(DEBUG):
        debug_log_text(text, "[Compile] parsing the following code: ")

    global_start_time = perf_counter()

    arg = text
    for index, step in enumerate(steps):
        if callback is not None:
            callback(step[1], index / len(steps), arg)
        start_time = perf_counter()
        try:
            arg = step[0](arg)
        except Exception as e:
            if not isinstance(e, McScriptError):
                Logger.critical(f"Internal compiler error occurred: {repr(e)}")
            raise e
        Logger.info(f"{step[1]} finished in {perf_counter() - start_time:.4f} seconds")
        if isinstance(arg, Tree):
            _debug_log_tree(arg)

    if callback is not None:
        callback("Done", 1, arg)

    Logger.info(f"Run all steps in {perf_counter() - global_start_time:.4f} seconds")

    # noinspection PyTypeChecker
    return arg
Beispiel #2
0
def _debug_log_tree(tree: Tree):
    if Logger.isEnabledFor(DEBUG):
        debug_log_text(tree.pretty(), "[Compile] Intermediate Tree:")