import logging
import os

import jenga.compile
import jenga.crossreference
import jenga.interpret
import jenga.lex
import jenga.preprocess
from jenga.types import Word


def gen_ir(fn: str, include_paths: list[str]) -> list[Word]:
    """Generate the intermediate representation (list of Words) from a program"""
    logging.info("Lexing file")
    words = jenga.lex.lex_file(fn)

    logging.info("Preprocessing program")
    words = jenga.preprocess.preprocess_program(words, fn, include_paths)

    logging.info("Crossreferencing program")
    words = jenga.crossreference.crossreference_program(words)

    return words


def simulate_file(fn: str, include_paths: list[str]):
    """Simulate (interpret) a Jenga program"""
    words = gen_ir(fn, include_paths)

    logging.info("Interpreting file")
    jenga.interpret.interpret(words)


def compile_file(fn: str, src_folder: str, base_file: str, outpath: str, run: bool, nocleanup: bool, include_paths: list[str]):
    """Compile a Jenga program"""
    words = gen_ir(fn, include_paths)

    logging.info("Compiling file")
    tmp_fn = os.path.join(src_folder, base_file)
    asm_path = tmp_fn + '.asm'
    o_path = tmp_fn + '.o'

    jenga.compile.compile(words, asm_path)

    if jenga.util.is_cmd("nasm"):
        res = jenga.util.run_cmd(["nasm", "-fmacho64", "-o", o_path, asm_path])
        if res.returncode != 0:
            logging.critical(f"Subcommand failed with code {res.returncode}, exitting")
            exit(res.returncode)
        res = jenga.util.run_cmd(["ld", "-o", outpath, o_path, "-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib", "-lSystem"])
        if res.returncode != 0:
            logging.critical(f"Subcommand failed with code {res.returncode}, exitting")
            exit(res.returncode)

        if run:
            res = jenga.util.run_cmd([outpath])
            logging.info(f"File ran with exit code {res.returncode}")

        if not nocleanup:
            os.remove(asm_path)
            os.remove(o_path)
    else:
        logging.error("'nasm' not found, use your own assembler")

import logging
import os

import jenga.compile
import jenga.crossreference
import jenga.interpret
import jenga.lex
import jenga.preprocess
from jenga.types import Word


def gen_ir(fn: str, include_paths: list[str]) -> list[Word]:
    """Generate the intermediate representation (list of Words) from a program"""
    logging.info("Lexing file")
    words = jenga.lex.lex_file(fn)

    logging.info("Preprocessing program")
    words = jenga.preprocess.preprocess_program(words, fn, include_paths)

    logging.info("Crossreferencing program")
    words = jenga.crossreference.crossreference_program(words)

    return words


def simulate_file(fn: str, include_paths: list[str]):
    """Simulate (interpret) a Jenga program"""
    words = gen_ir(fn, include_paths)

    logging.info("Interpreting file")
    jenga.interpret.interpret(words)


def compile_file(fn: str, src_folder: str, base_file: str, outpath: str, run: bool, nocleanup: bool, include_paths: list[str]):
    """Compile a Jenga program"""
    words = gen_ir(fn, include_paths)

    logging.info("Compiling file")
    tmp_fn = os.path.join(src_folder, base_file)
    asm_path = tmp_fn + '.asm'
    o_path = tmp_fn + '.o'

    jenga.compile.compile(words, asm_path)

    if jenga.util.is_cmd("nasm"):
        res = jenga.util.run_cmd(["nasm", "-fmacho64", "-o", o_path, asm_path])
        if res.returncode != 0:
            logging.critical(f"Subcommand failed with code {res.returncode}, exitting")
            exit(res.returncode)
        res = jenga.util.run_cmd(["ld", "-o", outpath, o_path, "-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib", "-lSystem"])
        if res.returncode != 0:
            logging.critical(f"Subcommand failed with code {res.returncode}, exitting")
            exit(res.returncode)

        if run:
            res = jenga.util.run_cmd([outpath])
            logging.info(f"File ran with exit code {res.returncode}")

        if not nocleanup:
            os.remove(asm_path)
            os.remove(o_path)
    else:
        logging.error("'nasm' not found, use your own assembler")

Generate the intermediate representation (list of Words) from a program Simulate (interpret) a Jenga program Compile a Jenga program