import os from cpl_core.application import ApplicationABC from cpl_core.configuration import ConfigurationABC from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension.list import List from lexer.abc.lexer_abc import LexerABC from lexer.model.token import Token from parser.abc.ast import AST from parser.abc.parser_abc import ParserABC from runtime.abc.runtime_service_abc import RuntimeServiceABC from runtime.model.error import Error from runtime.model.error_codes_enum import ErrorCodesEnum class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) self._lexer: LexerABC = services.get_service(LexerABC) self._parser: ParserABC = services.get_service(ParserABC) self._runtime: RuntimeServiceABC = services.get_service(RuntimeServiceABC) self._path = config.get_configuration('p') def _interpret(self, line: str): tokens: List[Token] = self._lexer.tokenize(line) ast: List[AST] = self._parser.create_ast(tokens) line.replace("\n", "").replace("\t", "") Console.write_line(f'<{self._runtime.line_count}> LINE: {line}') # header, values = ['Type', 'Value'], [] # tokens.for_each(lambda t: values.append([t.type, t.value])) # Console.table(header, values) Console.write(ast, '\n') def _console(self): i = 0 while True: self._runtime.line_count = i + 1 self._interpret(Console.read('> ')) i += 1 def _files(self): if not os.path.isdir(self._path): raise FileNotFoundError(self._path) # r=root, d=directories, f=files for r, d, f in os.walk(self._path): for file in f: if file.endswith('.ccl'): self._read_file(os.path.join(r, file)) def _read_file(self, file: str): if not os.path.isfile(file): self._runtime.error(Error(ErrorCodesEnum.FileNotFound)) if not file.endswith('.ccl'): self._runtime.error(Error(ErrorCodesEnum.WrongFileType)) self._runtime.file = file f = open(file, 'r', encoding='utf-8').readlines() for i in range(0, len(f)): self._runtime.line_count = i + 1 self._interpret(f[i]) self._runtime.file = '' def configure(self): pass def main(self): if self._path is None: self._console() return if os.path.isfile(self._path): self._read_file(self._path) else: self._files()