diff --git a/src/cc_lang_interpreter/application.py b/src/cc_lang_interpreter/application.py index e52b16f..252d142 100644 --- a/src/cc_lang_interpreter/application.py +++ b/src/cc_lang_interpreter/application.py @@ -6,6 +6,7 @@ from cpl_core.console import Console from cpl_core.dependency_injection import ServiceProviderABC from lexer.abc.lexer_abc import LexerABC +from parser.abc.parser_abc import ParserABC from runtime.abc.runtime_service_abc import RuntimeServiceABC @@ -15,19 +16,20 @@ class Application(ApplicationABC): 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') - Console.write_line('t', self._path) def _interpret(self, line: str): tokens = self._lexer.tokenize(line) + 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_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) def _console(self): i = 0 diff --git a/src/cc_lang_interpreter/startup.py b/src/cc_lang_interpreter/startup.py index 5e9f1d0..64f4ecd 100644 --- a/src/cc_lang_interpreter/startup.py +++ b/src/cc_lang_interpreter/startup.py @@ -5,6 +5,8 @@ from cpl_core.environment import ApplicationEnvironment from lexer.abc.lexer_abc import LexerABC from lexer.service.lexer_service import LexerService +from parser.abc.parser_abc import ParserABC +from parser.service.parser_service import ParserService from runtime.abc.runtime_service_abc import RuntimeServiceABC from runtime.service.runtime_service import RuntimeService @@ -22,6 +24,7 @@ class Startup(StartupABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironment) -> ServiceProviderABC: services.add_singleton(LexerABC, LexerService) + services.add_singleton(ParserABC, ParserService) services.add_singleton(RuntimeServiceABC, RuntimeService) return services.build_service_provider() diff --git a/src/lexer/model/token.py b/src/lexer/model/token.py index 64493ab..34729c8 100644 --- a/src/lexer/model/token.py +++ b/src/lexer/model/token.py @@ -18,3 +18,6 @@ class Token: @value.setter def value(self, value: str): self._value = value + + def __str__(self) -> str: + return f'Token: Type: {self._type}, Value: {self._value}' diff --git a/src/parser/__init__.py b/src/parser/__init__.py index ad5eca3..425ab6c 100644 --- a/src/parser/__init__.py +++ b/src/parser/__init__.py @@ -1 +1 @@ -# imports: +# imports diff --git a/src/parser/abc/__init__.py b/src/parser/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/parser/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/parser/abc/ast_abc.py b/src/parser/abc/ast_abc.py new file mode 100644 index 0000000..4b66da6 --- /dev/null +++ b/src/parser/abc/ast_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class AST(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/parser/abc/parser_abc.py b/src/parser/abc/parser_abc.py new file mode 100644 index 0000000..f0423e3 --- /dev/null +++ b/src/parser/abc/parser_abc.py @@ -0,0 +1,14 @@ +from abc import ABC, abstractmethod + +from cpl_query.extension.list import List + +from parser.abc.ast_abc import AST +from lexer.model.token import Token + +class ParserABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def create_ast(self, tokens: List[Token]) -> List[AST]: pass diff --git a/src/parser/service/__init__.py b/src/parser/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/parser/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/parser/service/parser_service.py b/src/parser/service/parser_service.py new file mode 100644 index 0000000..811a4ab --- /dev/null +++ b/src/parser/service/parser_service.py @@ -0,0 +1,19 @@ +from cpl_core.console.console import Console +from cpl_query.extension.list import List +from lexer.model.token import Token +from parser.abc.ast_abc import AST +from parser.abc.parser_abc import ParserABC + + +class ParserService(ParserABC): + + def __init__(self): + pass + + def create_ast(self, tokens: List[Token]) -> List[AST]: + for i in range(0, tokens.count()): + prev_token = tokens[i-1] if i-1 > 0 else None + token = tokens[i] + next_token = tokens[i+1] if i+1 < tokens.count() else None + + Console.write_line(token)