Exemplo n.º 1
0
    def __init__(self, input_file: str, output_file: str, debug: bool):
        '''
        Inicializa las variables a utilizar y 
        ejecuta el pipeline del proyecto.
        '''
        self.input_file = Path(input_file)
        self.output_file = Path(output_file)
        self.debug = debug
        self.code = ''
        self.lexer = None
        self.parser = None
        self.ast = None
        self.context = None
        self.scope = None

        name = Utils.GetName(input_file)
        self.debug_path = f'./debug/{name}'

        if not str(self.input_file).endswith('.cl'):
            error_text = CompilerError.WRONG_EXTENTION
            print(CompilerError(0, 0, error_text))
            exit(1)

        try:
            with open(self.input_file, encoding='utf-8') as file:
                self.code += file.read()
        except (IOError, FileNotFoundError):
            error_text = CompilerError.UNKNOWN_FILE % str(self.input_file)
            print(CompilerError(0, 0, error_text))
            exit(1)

        Utils.Write(self.debug_path, '.cl', self.code) if self.debug else None
        self.steps = [
            self.lexing, self.parsing, self.semantics, self.code_generation
        ]