From 9c6034f7aecad2b7251590b06e659e93c3ea76a7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 2 Aug 2021 14:55:04 +0200 Subject: [PATCH 01/11] Added CPL workspace --- .gitignore | 3 +- src/CCLang_sly/__init__.py => LICENSE | 0 src/Interpreter/__init__.py => README.md | 0 appsettings.json | 15 +++++++ cpl-workspace.json | 10 +++++ {src => old/src}/Basic.py | 0 {src => old/src}/CCLang_sly/Interpreter.py | 0 {src => old/src}/CCLang_sly/Lexer.py | 0 {src => old/src}/CCLang_sly/Parser.py | 0 {src => old/src}/First.cc | 0 {src => old/src}/Interpreter/Interpreter.py | 0 {src => old/src}/Interpreter/Lexer.py | 0 {src => old/src}/Interpreter/Parser.py | 0 {src => old/src}/Interpreter/Repo.py | 0 {src => old/src}/Interpreter/Utils.py | 0 {src => old/src}/Interpreter/Validator.py | 0 .../AbstractSyntaxTree/AbstractSyntaxTree.py | 0 .../src}/Models/CCLang/TokenDefinition.py | 0 .../src}/Models/Interpreter/Datatypes.py | 0 {src => old/src}/Models/Interpreter/Error.py | 0 {src => old/src}/Models/Token/Token.py | 0 {src => old/src}/Models/Token/TokenTypes.py | 0 .../src}/Models/Token/TokenValueTypes.py | 0 {src => old/src}/ServiceInitializer.py | 0 {src => old/src}/cclang.py | 0 old/src/test.cc | 27 ++++++++++++ {src => old/src}/test.py | 0 src/cc_lang/__init__.py | 1 + src/cc_lang/application.py | 16 +++++++ src/cc_lang/cc-lang.json | 43 +++++++++++++++++++ src/cc_lang/main.py | 14 ++++++ src/cc_lang/startup.py | 19 ++++++++ src/lexer/__init__.py | 1 + src/lexer/lexer.json | 43 +++++++++++++++++++ src/parser/__init__.py | 1 + src/parser/parser.json | 43 +++++++++++++++++++ src/tests/__init__.py | 1 + 37 files changed, 236 insertions(+), 1 deletion(-) rename src/CCLang_sly/__init__.py => LICENSE (100%) rename src/Interpreter/__init__.py => README.md (100%) create mode 100644 appsettings.json create mode 100644 cpl-workspace.json rename {src => old/src}/Basic.py (100%) rename {src => old/src}/CCLang_sly/Interpreter.py (100%) rename {src => old/src}/CCLang_sly/Lexer.py (100%) rename {src => old/src}/CCLang_sly/Parser.py (100%) rename {src => old/src}/First.cc (100%) rename {src => old/src}/Interpreter/Interpreter.py (100%) rename {src => old/src}/Interpreter/Lexer.py (100%) rename {src => old/src}/Interpreter/Parser.py (100%) rename {src => old/src}/Interpreter/Repo.py (100%) rename {src => old/src}/Interpreter/Utils.py (100%) rename {src => old/src}/Interpreter/Validator.py (100%) rename {src => old/src}/Models/AbstractSyntaxTree/AbstractSyntaxTree.py (100%) rename {src => old/src}/Models/CCLang/TokenDefinition.py (100%) rename {src => old/src}/Models/Interpreter/Datatypes.py (100%) rename {src => old/src}/Models/Interpreter/Error.py (100%) rename {src => old/src}/Models/Token/Token.py (100%) rename {src => old/src}/Models/Token/TokenTypes.py (100%) rename {src => old/src}/Models/Token/TokenValueTypes.py (100%) rename {src => old/src}/ServiceInitializer.py (100%) rename {src => old/src}/cclang.py (100%) create mode 100644 old/src/test.cc rename {src => old/src}/test.py (100%) create mode 100644 src/cc_lang/__init__.py create mode 100644 src/cc_lang/application.py create mode 100644 src/cc_lang/cc-lang.json create mode 100644 src/cc_lang/main.py create mode 100644 src/cc_lang/startup.py create mode 100644 src/lexer/__init__.py create mode 100644 src/lexer/lexer.json create mode 100644 src/parser/__init__.py create mode 100644 src/parser/parser.json create mode 100644 src/tests/__init__.py diff --git a/.gitignore b/.gitignore index 274bcbb..ccae4b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc -/.idea/ \ No newline at end of file +/.idea/ +venv \ No newline at end of file diff --git a/src/CCLang_sly/__init__.py b/LICENSE similarity index 100% rename from src/CCLang_sly/__init__.py rename to LICENSE diff --git a/src/Interpreter/__init__.py b/README.md similarity index 100% rename from src/Interpreter/__init__.py rename to README.md diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..629e6eb --- /dev/null +++ b/appsettings.json @@ -0,0 +1,15 @@ +{ + "TimeFormatSettings": { + "DateFormat": "%Y-%m-%d", + "TimeFormat": "%H:%M:%S", + "DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f", + "DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S" + }, + + "LoggingSettings": { + "Path": "logs/", + "Filename": "log_$start_time.log", + "ConsoleLogLevel": "ERROR", + "FileLogLevel": "WARN" + } +} diff --git a/cpl-workspace.json b/cpl-workspace.json new file mode 100644 index 0000000..5294190 --- /dev/null +++ b/cpl-workspace.json @@ -0,0 +1,10 @@ +{ + "WorkspaceSettings": { + "DefaultProject": "cc-lang", + "Projects": { + "cc-lang": "src/cc_lang/cc-lang.json", + "parser": "src/parser/parser.json", + "lexer": "src/lexer/lexer.json" + } + } +} \ No newline at end of file diff --git a/src/Basic.py b/old/src/Basic.py similarity index 100% rename from src/Basic.py rename to old/src/Basic.py diff --git a/src/CCLang_sly/Interpreter.py b/old/src/CCLang_sly/Interpreter.py similarity index 100% rename from src/CCLang_sly/Interpreter.py rename to old/src/CCLang_sly/Interpreter.py diff --git a/src/CCLang_sly/Lexer.py b/old/src/CCLang_sly/Lexer.py similarity index 100% rename from src/CCLang_sly/Lexer.py rename to old/src/CCLang_sly/Lexer.py diff --git a/src/CCLang_sly/Parser.py b/old/src/CCLang_sly/Parser.py similarity index 100% rename from src/CCLang_sly/Parser.py rename to old/src/CCLang_sly/Parser.py diff --git a/src/First.cc b/old/src/First.cc similarity index 100% rename from src/First.cc rename to old/src/First.cc diff --git a/src/Interpreter/Interpreter.py b/old/src/Interpreter/Interpreter.py similarity index 100% rename from src/Interpreter/Interpreter.py rename to old/src/Interpreter/Interpreter.py diff --git a/src/Interpreter/Lexer.py b/old/src/Interpreter/Lexer.py similarity index 100% rename from src/Interpreter/Lexer.py rename to old/src/Interpreter/Lexer.py diff --git a/src/Interpreter/Parser.py b/old/src/Interpreter/Parser.py similarity index 100% rename from src/Interpreter/Parser.py rename to old/src/Interpreter/Parser.py diff --git a/src/Interpreter/Repo.py b/old/src/Interpreter/Repo.py similarity index 100% rename from src/Interpreter/Repo.py rename to old/src/Interpreter/Repo.py diff --git a/src/Interpreter/Utils.py b/old/src/Interpreter/Utils.py similarity index 100% rename from src/Interpreter/Utils.py rename to old/src/Interpreter/Utils.py diff --git a/src/Interpreter/Validator.py b/old/src/Interpreter/Validator.py similarity index 100% rename from src/Interpreter/Validator.py rename to old/src/Interpreter/Validator.py diff --git a/src/Models/AbstractSyntaxTree/AbstractSyntaxTree.py b/old/src/Models/AbstractSyntaxTree/AbstractSyntaxTree.py similarity index 100% rename from src/Models/AbstractSyntaxTree/AbstractSyntaxTree.py rename to old/src/Models/AbstractSyntaxTree/AbstractSyntaxTree.py diff --git a/src/Models/CCLang/TokenDefinition.py b/old/src/Models/CCLang/TokenDefinition.py similarity index 100% rename from src/Models/CCLang/TokenDefinition.py rename to old/src/Models/CCLang/TokenDefinition.py diff --git a/src/Models/Interpreter/Datatypes.py b/old/src/Models/Interpreter/Datatypes.py similarity index 100% rename from src/Models/Interpreter/Datatypes.py rename to old/src/Models/Interpreter/Datatypes.py diff --git a/src/Models/Interpreter/Error.py b/old/src/Models/Interpreter/Error.py similarity index 100% rename from src/Models/Interpreter/Error.py rename to old/src/Models/Interpreter/Error.py diff --git a/src/Models/Token/Token.py b/old/src/Models/Token/Token.py similarity index 100% rename from src/Models/Token/Token.py rename to old/src/Models/Token/Token.py diff --git a/src/Models/Token/TokenTypes.py b/old/src/Models/Token/TokenTypes.py similarity index 100% rename from src/Models/Token/TokenTypes.py rename to old/src/Models/Token/TokenTypes.py diff --git a/src/Models/Token/TokenValueTypes.py b/old/src/Models/Token/TokenValueTypes.py similarity index 100% rename from src/Models/Token/TokenValueTypes.py rename to old/src/Models/Token/TokenValueTypes.py diff --git a/src/ServiceInitializer.py b/old/src/ServiceInitializer.py similarity index 100% rename from src/ServiceInitializer.py rename to old/src/ServiceInitializer.py diff --git a/src/cclang.py b/old/src/cclang.py similarity index 100% rename from src/cclang.py rename to old/src/cclang.py diff --git a/old/src/test.cc b/old/src/test.cc new file mode 100644 index 0000000..e478a04 --- /dev/null +++ b/old/src/test.cc @@ -0,0 +1,27 @@ +# olc test 1 +// olc test 2 +/* mlc Test 1 */ +/* + mlc Test 2 +*/ + + +public lib Main { + class Program { + var test: bool = false; + var test2: Program2 = empty; + var test3: Program2 = Test(34); + + func Main(): void { + #func Main { + var hallo: any; + #output('Hello'); + #output(this.isTrue(false)); + } + + func isTrue(value: bool): bool { + #func isTrue { + #return value; + } + } +} \ No newline at end of file diff --git a/src/test.py b/old/src/test.py similarity index 100% rename from src/test.py rename to old/src/test.py diff --git a/src/cc_lang/__init__.py b/src/cc_lang/__init__.py new file mode 100644 index 0000000..ad5eca3 --- /dev/null +++ b/src/cc_lang/__init__.py @@ -0,0 +1 @@ +# imports: diff --git a/src/cc_lang/application.py b/src/cc_lang/application.py new file mode 100644 index 0000000..0a98353 --- /dev/null +++ b/src/cc_lang/application.py @@ -0,0 +1,16 @@ +from cpl.application import ApplicationABC +from cpl.configuration import ConfigurationABC +from cpl.console import Console +from cpl.dependency_injection import ServiceProviderABC + + +class Application(ApplicationABC): + + def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): + ApplicationABC.__init__(self, config, services) + + def configure(self): + pass + + def main(self): + Console.write_line('Hello World') diff --git a/src/cc_lang/cc-lang.json b/src/cc_lang/cc-lang.json new file mode 100644 index 0000000..bd23212 --- /dev/null +++ b/src/cc_lang/cc-lang.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "cc-lang", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl==2021.4.0.post2" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "console", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "cc_lang.main", + "EntryPoint": "cc-lang", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/cc_lang/main.py b/src/cc_lang/main.py new file mode 100644 index 0000000..23aedce --- /dev/null +++ b/src/cc_lang/main.py @@ -0,0 +1,14 @@ +from cpl.application import ApplicationBuilder + +from cc_lang.application import Application +from cc_lang.startup import Startup + + +def main(): + app_builder = ApplicationBuilder(Application) + app_builder.use_startup(Startup) + app_builder.build().run() + + +if __name__ == '__main__': + main() diff --git a/src/cc_lang/startup.py b/src/cc_lang/startup.py new file mode 100644 index 0000000..8988cf3 --- /dev/null +++ b/src/cc_lang/startup.py @@ -0,0 +1,19 @@ +from cpl.application import StartupABC +from cpl.configuration import ConfigurationABC +from cpl.dependency_injection import ServiceProviderABC, ServiceCollectionABC + + +class Startup(StartupABC): + + def __init__(self, config: ConfigurationABC, services: ServiceCollectionABC): + StartupABC.__init__(self) + + self._configuration = config + self._environment = self._configuration.environment + self._services = services + + def configure_configuration(self) -> ConfigurationABC: + return self._configuration + + def configure_services(self) -> ServiceProviderABC: + return self._services.build_service_provider() diff --git a/src/lexer/__init__.py b/src/lexer/__init__.py new file mode 100644 index 0000000..ad5eca3 --- /dev/null +++ b/src/lexer/__init__.py @@ -0,0 +1 @@ +# imports: diff --git a/src/lexer/lexer.json b/src/lexer/lexer.json new file mode 100644 index 0000000..db992e1 --- /dev/null +++ b/src/lexer/lexer.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "lexer", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl==2021.4.0.post2" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "lexer.main", + "EntryPoint": "lexer", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/parser/__init__.py b/src/parser/__init__.py new file mode 100644 index 0000000..ad5eca3 --- /dev/null +++ b/src/parser/__init__.py @@ -0,0 +1 @@ +# imports: diff --git a/src/parser/parser.json b/src/parser/parser.json new file mode 100644 index 0000000..61f93ca --- /dev/null +++ b/src/parser/parser.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "parser", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl==2021.4.0.post2" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "parser.main", + "EntryPoint": "parser", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..ad5eca3 --- /dev/null +++ b/src/tests/__init__.py @@ -0,0 +1 @@ +# imports: -- 2.45.2 From 1577c78c5106c654324f473007fd55d4b514f422 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 2 Aug 2021 14:55:10 +0200 Subject: [PATCH 02/11] Added CPL workspace --- {src/Models/AbstractSyntaxTree => old/src/CCLang_sly}/__init__.py | 0 {src/Models/CCLang => old/src/Interpreter}/__init__.py | 0 .../Interpreter => old/src/Models/AbstractSyntaxTree}/__init__.py | 0 {src/Models/Token => old/src/Models/CCLang}/__init__.py | 0 {src/Models => old/src/Models/Interpreter}/__init__.py | 0 old/src/Models/Token/__init__.py | 0 old/src/Models/__init__.py | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename {src/Models/AbstractSyntaxTree => old/src/CCLang_sly}/__init__.py (100%) rename {src/Models/CCLang => old/src/Interpreter}/__init__.py (100%) rename {src/Models/Interpreter => old/src/Models/AbstractSyntaxTree}/__init__.py (100%) rename {src/Models/Token => old/src/Models/CCLang}/__init__.py (100%) rename {src/Models => old/src/Models/Interpreter}/__init__.py (100%) create mode 100644 old/src/Models/Token/__init__.py create mode 100644 old/src/Models/__init__.py diff --git a/src/Models/AbstractSyntaxTree/__init__.py b/old/src/CCLang_sly/__init__.py similarity index 100% rename from src/Models/AbstractSyntaxTree/__init__.py rename to old/src/CCLang_sly/__init__.py diff --git a/src/Models/CCLang/__init__.py b/old/src/Interpreter/__init__.py similarity index 100% rename from src/Models/CCLang/__init__.py rename to old/src/Interpreter/__init__.py diff --git a/src/Models/Interpreter/__init__.py b/old/src/Models/AbstractSyntaxTree/__init__.py similarity index 100% rename from src/Models/Interpreter/__init__.py rename to old/src/Models/AbstractSyntaxTree/__init__.py diff --git a/src/Models/Token/__init__.py b/old/src/Models/CCLang/__init__.py similarity index 100% rename from src/Models/Token/__init__.py rename to old/src/Models/CCLang/__init__.py diff --git a/src/Models/__init__.py b/old/src/Models/Interpreter/__init__.py similarity index 100% rename from src/Models/__init__.py rename to old/src/Models/Interpreter/__init__.py diff --git a/old/src/Models/Token/__init__.py b/old/src/Models/Token/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/old/src/Models/__init__.py b/old/src/Models/__init__.py new file mode 100644 index 0000000..e69de29 -- 2.45.2 From a97de91fc420b1e28c6f2b5512ccacd3ad4280a6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 2 Aug 2021 15:58:05 +0200 Subject: [PATCH 03/11] Improved workspace structure --- cc_code_preview/Preview/classes.cc | 13 ++++++ cc_code_preview/Preview/functions.cc | 5 +++ cc_code_preview/Preview/variables.cc | 8 ++++ cc_code_preview/Program/main.cc | 19 ++++++++ cpl-workspace.json | 4 +- old/src/ServiceInitializer.py | 4 +- old/src/cclang.py | 10 ++--- src/cc_lang/cc-lang.json | 6 +-- src/cc_lang_interpreter/__init__.py | 1 + .../application.py | 16 ++++++- .../cc-lang-interpreter.json | 43 +++++++++++++++++++ src/{cc_lang => cc_lang_interpreter}/main.py | 4 +- .../startup.py | 10 ++++- src/lexer/__init__.py | 2 +- src/lexer/abc/__init__.py | 1 + src/lexer/abc/lexer_abc.py | 7 +++ src/lexer/lexer.json | 4 +- src/lexer/service/__init__.py | 1 + src/lexer/service/lexer_service.py | 7 +++ src/parser/parser.json | 4 +- src/runtime/__init__.py | 1 + src/runtime/abc/__init__.py | 1 + src/runtime/abc/class_stack_abc.py | 7 +++ src/runtime/abc/function_stack_abc.py | 7 +++ src/runtime/abc/library_stack_abc.py | 7 +++ src/runtime/abc/variable_stack_abc.py | 7 +++ src/runtime/runtime.json | 43 +++++++++++++++++++ 27 files changed, 222 insertions(+), 20 deletions(-) create mode 100644 cc_code_preview/Preview/classes.cc create mode 100644 cc_code_preview/Preview/functions.cc create mode 100644 cc_code_preview/Preview/variables.cc create mode 100644 cc_code_preview/Program/main.cc create mode 100644 src/cc_lang_interpreter/__init__.py rename src/{cc_lang => cc_lang_interpreter}/application.py (50%) create mode 100644 src/cc_lang_interpreter/cc-lang-interpreter.json rename src/{cc_lang => cc_lang_interpreter}/main.py (67%) rename src/{cc_lang => cc_lang_interpreter}/startup.py (62%) create mode 100644 src/lexer/abc/__init__.py create mode 100644 src/lexer/abc/lexer_abc.py create mode 100644 src/lexer/service/__init__.py create mode 100644 src/lexer/service/lexer_service.py create mode 100644 src/runtime/__init__.py create mode 100644 src/runtime/abc/__init__.py create mode 100644 src/runtime/abc/class_stack_abc.py create mode 100644 src/runtime/abc/function_stack_abc.py create mode 100644 src/runtime/abc/library_stack_abc.py create mode 100644 src/runtime/abc/variable_stack_abc.py create mode 100644 src/runtime/runtime.json diff --git a/cc_code_preview/Preview/classes.cc b/cc_code_preview/Preview/classes.cc new file mode 100644 index 0000000..621a9e9 --- /dev/null +++ b/cc_code_preview/Preview/classes.cc @@ -0,0 +1,13 @@ +public lib Preview.Classes { + public class Test { + private var _name: string; + + public constructor(name: string) { + this._name = name; + } + + public getName(): string { + return this._name; + } + } +} \ No newline at end of file diff --git a/cc_code_preview/Preview/functions.cc b/cc_code_preview/Preview/functions.cc new file mode 100644 index 0000000..3deeddc --- /dev/null +++ b/cc_code_preview/Preview/functions.cc @@ -0,0 +1,5 @@ +public lib Preview.Functions { + public func isTrue(value: bool): bool { + return value; + } +} \ No newline at end of file diff --git a/cc_code_preview/Preview/variables.cc b/cc_code_preview/Preview/variables.cc new file mode 100644 index 0000000..2140abe --- /dev/null +++ b/cc_code_preview/Preview/variables.cc @@ -0,0 +1,8 @@ + + +public lib Preview.Variables { + public var str: string = "Hello World"; + public var test: bool = false; + public var test2: Test = empty; + public var test3: Test = Test(34); +} \ No newline at end of file diff --git a/cc_code_preview/Program/main.cc b/cc_code_preview/Program/main.cc new file mode 100644 index 0000000..f271c5e --- /dev/null +++ b/cc_code_preview/Program/main.cc @@ -0,0 +1,19 @@ +use Preview.Variables; +use Preview.Functions; +use Preview.Classes; + +public lib Main { + public class Program { + private var test: Test; + + public constructor() { + this.test = Test(str); + } + + public func Main(): void { + output(str, test, test2, test3); + output(isTrue(test)); + output(this.test.getName()); + } + } +} \ No newline at end of file diff --git a/cpl-workspace.json b/cpl-workspace.json index 5294190..c3c5d8e 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -4,7 +4,9 @@ "Projects": { "cc-lang": "src/cc_lang/cc-lang.json", "parser": "src/parser/parser.json", - "lexer": "src/lexer/lexer.json" + "lexer": "src/lexer/lexer.json", + "runtime": "src/runtime/runtime.json", + "cc-lang-interpreter": "src/cc_lang_interpreter/cc-lang-interpreter.json" } } } \ No newline at end of file diff --git a/old/src/ServiceInitializer.py b/old/src/ServiceInitializer.py index a2faea2..becc5ff 100644 --- a/old/src/ServiceInitializer.py +++ b/old/src/ServiceInitializer.py @@ -1,5 +1,5 @@ from Interpreter.Interpreter import Interpreter -from CCLang_sly.Interpreter import Interpreter as SlyCCLangInterpreter +# from CCLang_sly.Interpreter import Interpreter as SlyCCLangInterpreter from Interpreter.Utils import Utils from Interpreter.Repo import Repo @@ -10,4 +10,4 @@ class ServiceInitializer: self.repo = Repo() self.utils = Utils(self.repo) self.interpreter = Interpreter(self.repo, self.utils) - self.sly_cclang_interpreter = SlyCCLangInterpreter(self.repo, self.utils) + # self.sly_cclang_interpreter = SlyCCLangInterpreter(self.repo, self.utils) diff --git a/old/src/cclang.py b/old/src/cclang.py index e33045d..f31825d 100644 --- a/old/src/cclang.py +++ b/old/src/cclang.py @@ -12,7 +12,7 @@ class Main: self.__utils = self.__services.utils self.__repo = self.__services.repo self.__interpreter = self.__services.interpreter - self.__sly_cclang_interpreter = self.__services.sly_cclang_interpreter + # self.__sly_cclang_interpreter = self.__services.sly_cclang_interpreter def console(self) -> None: """ @@ -22,8 +22,8 @@ class Main: i = 0 while self.__repo.error is None: self.__repo.line_number = i + 1 - #self.__interpreter.interpret(input('> ')) - self.__sly_cclang_interpreter.interpret(input('> ')) + self.__interpreter.interpret(input('> ')) + # self.__sly_cclang_interpreter.interpret(input('> ')) i += 1 def files(self, file: str) -> None: @@ -43,8 +43,8 @@ class Main: f = open(file, 'r', encoding='utf-8').readlines() for i in range(0, len(f)): self.__repo.line_number = i + 1 - # self.__interpreter.interpret(f[i]) - self.__sly_cclang_interpreter.interpret(f[i]) + self.__interpreter.interpret(f[i]) + # self.__sly_cclang_interpreter.interpret(f[i]) if __name__ == '__main__': diff --git a/src/cc_lang/cc-lang.json b/src/cc_lang/cc-lang.json index bd23212..34a6b80 100644 --- a/src/cc_lang/cc-lang.json +++ b/src/cc_lang/cc-lang.json @@ -25,11 +25,11 @@ "Classifiers": [] }, "BuildSettings": { - "ProjectType": "console", + "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "cc_lang.main", - "EntryPoint": "cc-lang", + "Main": "", + "EntryPoint": "", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/cc_lang_interpreter/__init__.py b/src/cc_lang_interpreter/__init__.py new file mode 100644 index 0000000..ad5eca3 --- /dev/null +++ b/src/cc_lang_interpreter/__init__.py @@ -0,0 +1 @@ +# imports: diff --git a/src/cc_lang/application.py b/src/cc_lang_interpreter/application.py similarity index 50% rename from src/cc_lang/application.py rename to src/cc_lang_interpreter/application.py index 0a98353..bddf84d 100644 --- a/src/cc_lang/application.py +++ b/src/cc_lang_interpreter/application.py @@ -3,14 +3,28 @@ from cpl.configuration import ConfigurationABC from cpl.console import Console from cpl.dependency_injection import ServiceProviderABC +from lexer.abc.lexer_abc import LexerABC + class Application(ApplicationABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): ApplicationABC.__init__(self, config, services) + self._lexer: LexerABC = services.get_service(LexerABC) + self._path = config.get_configuration('p') + + def _console(self): pass + + def _files(self): pass + def configure(self): pass def main(self): - Console.write_line('Hello World') + Console.write_line(self._configuration.additional_arguments, self._path) + if self._path is None: + self._console() + return + + self._files() diff --git a/src/cc_lang_interpreter/cc-lang-interpreter.json b/src/cc_lang_interpreter/cc-lang-interpreter.json new file mode 100644 index 0000000..3f2ea2b --- /dev/null +++ b/src/cc_lang_interpreter/cc-lang-interpreter.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "cc-lang-interpreter", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl==2021.4.0.post2" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "console", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "cc_lang_interpreter.main", + "EntryPoint": "cc-lang-interpreter", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/src/cc_lang/main.py b/src/cc_lang_interpreter/main.py similarity index 67% rename from src/cc_lang/main.py rename to src/cc_lang_interpreter/main.py index 23aedce..808d05d 100644 --- a/src/cc_lang/main.py +++ b/src/cc_lang_interpreter/main.py @@ -1,7 +1,7 @@ from cpl.application import ApplicationBuilder -from cc_lang.application import Application -from cc_lang.startup import Startup +from cc_lang_interpreter.application import Application +from cc_lang_interpreter.startup import Startup def main(): diff --git a/src/cc_lang/startup.py b/src/cc_lang_interpreter/startup.py similarity index 62% rename from src/cc_lang/startup.py rename to src/cc_lang_interpreter/startup.py index 8988cf3..f84d94a 100644 --- a/src/cc_lang/startup.py +++ b/src/cc_lang_interpreter/startup.py @@ -1,7 +1,10 @@ from cpl.application import StartupABC -from cpl.configuration import ConfigurationABC +from cpl.configuration import ConfigurationABC, ConsoleArgument from cpl.dependency_injection import ServiceProviderABC, ServiceCollectionABC +from lexer.abc.lexer_abc import LexerABC +from lexer.service.lexer_service import LexerService + class Startup(StartupABC): @@ -13,7 +16,12 @@ class Startup(StartupABC): self._services = services def configure_configuration(self) -> ConfigurationABC: + self._configuration.add_console_argument(ConsoleArgument('-', 'p', [], ' ')) + self._configuration.add_console_arguments() + return self._configuration def configure_services(self) -> ServiceProviderABC: + self._services.add_singleton(LexerABC, LexerService) + return self._services.build_service_provider() diff --git a/src/lexer/__init__.py b/src/lexer/__init__.py index ad5eca3..425ab6c 100644 --- a/src/lexer/__init__.py +++ b/src/lexer/__init__.py @@ -1 +1 @@ -# imports: +# imports diff --git a/src/lexer/abc/__init__.py b/src/lexer/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/lexer/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/lexer/abc/lexer_abc.py b/src/lexer/abc/lexer_abc.py new file mode 100644 index 0000000..4c892e5 --- /dev/null +++ b/src/lexer/abc/lexer_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class LexerABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/lexer/lexer.json b/src/lexer/lexer.json index db992e1..ece0188 100644 --- a/src/lexer/lexer.json +++ b/src/lexer/lexer.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "lexer.main", - "EntryPoint": "lexer", + "Main": "", + "EntryPoint": "", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/lexer/service/__init__.py b/src/lexer/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/lexer/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/lexer/service/lexer_service.py b/src/lexer/service/lexer_service.py new file mode 100644 index 0000000..397174e --- /dev/null +++ b/src/lexer/service/lexer_service.py @@ -0,0 +1,7 @@ +from lexer.abc.lexer_abc import LexerABC + + +class LexerService(LexerABC): + + def __init__(self): + pass diff --git a/src/parser/parser.json b/src/parser/parser.json index 61f93ca..16923bd 100644 --- a/src/parser/parser.json +++ b/src/parser/parser.json @@ -28,8 +28,8 @@ "ProjectType": "library", "SourcePath": "", "OutputPath": "../../dist", - "Main": "parser.main", - "EntryPoint": "parser", + "Main": "", + "EntryPoint": "", "IncludePackageData": false, "Included": [], "Excluded": [ diff --git a/src/runtime/__init__.py b/src/runtime/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/runtime/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/runtime/abc/__init__.py b/src/runtime/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/runtime/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/runtime/abc/class_stack_abc.py b/src/runtime/abc/class_stack_abc.py new file mode 100644 index 0000000..d1ef3e6 --- /dev/null +++ b/src/runtime/abc/class_stack_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class ClassStackABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/runtime/abc/function_stack_abc.py b/src/runtime/abc/function_stack_abc.py new file mode 100644 index 0000000..c2df524 --- /dev/null +++ b/src/runtime/abc/function_stack_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class FunctionStackABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/runtime/abc/library_stack_abc.py b/src/runtime/abc/library_stack_abc.py new file mode 100644 index 0000000..836e7cd --- /dev/null +++ b/src/runtime/abc/library_stack_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class LibraryStackABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/runtime/abc/variable_stack_abc.py b/src/runtime/abc/variable_stack_abc.py new file mode 100644 index 0000000..06b3a7e --- /dev/null +++ b/src/runtime/abc/variable_stack_abc.py @@ -0,0 +1,7 @@ +from abc import ABC, abstractmethod + + +class VariableStackABC(ABC): + + @abstractmethod + def __init__(self): pass diff --git a/src/runtime/runtime.json b/src/runtime/runtime.json new file mode 100644 index 0000000..436f695 --- /dev/null +++ b/src/runtime/runtime.json @@ -0,0 +1,43 @@ +{ + "ProjectSettings": { + "Name": "runtime", + "Version": { + "Major": "0", + "Minor": "0", + "Micro": "0" + }, + "Author": "", + "AuthorEmail": "", + "Description": "", + "LongDescription": "", + "URL": "", + "CopyrightDate": "", + "CopyrightName": "", + "LicenseName": "", + "LicenseDescription": "", + "Dependencies": [ + "sh_cpl==2021.4.0.post2" + ], + "PythonVersion": ">=3.9.2", + "PythonPath": { + "linux": "" + }, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "", + "EntryPoint": "", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file -- 2.45.2 From 4a3f9b4b9d2fdfc620940baefaba6fc7b17e8b13 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 2 Aug 2021 16:02:29 +0200 Subject: [PATCH 04/11] Added model files --- src/cc_lang/__init__.py | 2 +- src/cc_lang/model/__init__.py | 1 + src/cc_lang/model/abc/__init__.py | 1 + src/cc_lang/model/abc/type.py | 4 ++++ src/cc_lang/model/class.py | 4 ++++ src/cc_lang/model/function.py | 4 ++++ src/cc_lang/model/library.py | 4 ++++ src/cc_lang/model/type/__init__.py | 1 + src/cc_lang/model/type/bool.py | 4 ++++ src/cc_lang/model/type/number.py | 4 ++++ src/cc_lang/model/type/string.py | 4 ++++ src/cc_lang/model/variable.py | 4 ++++ 12 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/cc_lang/model/__init__.py create mode 100644 src/cc_lang/model/abc/__init__.py create mode 100644 src/cc_lang/model/abc/type.py create mode 100644 src/cc_lang/model/class.py create mode 100644 src/cc_lang/model/function.py create mode 100644 src/cc_lang/model/library.py create mode 100644 src/cc_lang/model/type/__init__.py create mode 100644 src/cc_lang/model/type/bool.py create mode 100644 src/cc_lang/model/type/number.py create mode 100644 src/cc_lang/model/type/string.py create mode 100644 src/cc_lang/model/variable.py diff --git a/src/cc_lang/__init__.py b/src/cc_lang/__init__.py index ad5eca3..425ab6c 100644 --- a/src/cc_lang/__init__.py +++ b/src/cc_lang/__init__.py @@ -1 +1 @@ -# imports: +# imports diff --git a/src/cc_lang/model/__init__.py b/src/cc_lang/model/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/cc_lang/model/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/cc_lang/model/abc/__init__.py b/src/cc_lang/model/abc/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/cc_lang/model/abc/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/cc_lang/model/abc/type.py b/src/cc_lang/model/abc/type.py new file mode 100644 index 0000000..f211a84 --- /dev/null +++ b/src/cc_lang/model/abc/type.py @@ -0,0 +1,4 @@ +class Type: + + def __init__(self): + pass diff --git a/src/cc_lang/model/class.py b/src/cc_lang/model/class.py new file mode 100644 index 0000000..4321280 --- /dev/null +++ b/src/cc_lang/model/class.py @@ -0,0 +1,4 @@ +class Class: + + def __init__(self): + pass diff --git a/src/cc_lang/model/function.py b/src/cc_lang/model/function.py new file mode 100644 index 0000000..7efaf8c --- /dev/null +++ b/src/cc_lang/model/function.py @@ -0,0 +1,4 @@ +class Function: + + def __init__(self): + pass diff --git a/src/cc_lang/model/library.py b/src/cc_lang/model/library.py new file mode 100644 index 0000000..214952b --- /dev/null +++ b/src/cc_lang/model/library.py @@ -0,0 +1,4 @@ +class Library: + + def __init__(self): + pass diff --git a/src/cc_lang/model/type/__init__.py b/src/cc_lang/model/type/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/cc_lang/model/type/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/cc_lang/model/type/bool.py b/src/cc_lang/model/type/bool.py new file mode 100644 index 0000000..a3fee62 --- /dev/null +++ b/src/cc_lang/model/type/bool.py @@ -0,0 +1,4 @@ +class Bool: + + def __init__(self): + pass diff --git a/src/cc_lang/model/type/number.py b/src/cc_lang/model/type/number.py new file mode 100644 index 0000000..72e1887 --- /dev/null +++ b/src/cc_lang/model/type/number.py @@ -0,0 +1,4 @@ +class Number: + + def __init__(self): + pass diff --git a/src/cc_lang/model/type/string.py b/src/cc_lang/model/type/string.py new file mode 100644 index 0000000..3693e76 --- /dev/null +++ b/src/cc_lang/model/type/string.py @@ -0,0 +1,4 @@ +class String: + + def __init__(self): + pass diff --git a/src/cc_lang/model/variable.py b/src/cc_lang/model/variable.py new file mode 100644 index 0000000..b174c45 --- /dev/null +++ b/src/cc_lang/model/variable.py @@ -0,0 +1,4 @@ +class Variable: + + def __init__(self): + pass -- 2.45.2 From c909ce31d85d9692a9689a829d8f7a84a2044971 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 14 Aug 2021 16:41:25 +0200 Subject: [PATCH 05/11] Added lexer --- cc_code_preview/Program/main.cc | 7 + old/src/CCLang_sly/Interpreter.py | 31 ---- old/src/CCLang_sly/Lexer.py | 108 ------------ old/src/CCLang_sly/Parser.py | 61 ------- old/src/cclang.py | 3 - src/cc_lang/model/datatypes.py | 12 ++ src/cc_lang/model/language_definition.py | 65 +++++++ src/cc_lang_interpreter/application.py | 49 +++++- src/cc_lang_interpreter/startup.py | 5 +- src/lexer/abc/lexer_abc.py | 7 + .../lexer/model}/__init__.py | 0 src/lexer/model/token.py | 20 +++ src/lexer/model/token_types.py | 24 +++ src/lexer/model/token_value_types.py | 62 +++++++ src/lexer/service/lexer_service.py | 163 +++++++++++++++++- src/runtime/abc/runtime_service_abc.py | 15 ++ src/runtime/service/__init__.py | 1 + src/runtime/service/runtime_service.py | 15 ++ 18 files changed, 438 insertions(+), 210 deletions(-) delete mode 100644 old/src/CCLang_sly/Interpreter.py delete mode 100644 old/src/CCLang_sly/Lexer.py delete mode 100644 old/src/CCLang_sly/Parser.py create mode 100644 src/cc_lang/model/datatypes.py create mode 100644 src/cc_lang/model/language_definition.py rename {old/src/CCLang_sly => src/lexer/model}/__init__.py (100%) create mode 100644 src/lexer/model/token.py create mode 100644 src/lexer/model/token_types.py create mode 100644 src/lexer/model/token_value_types.py create mode 100644 src/runtime/abc/runtime_service_abc.py create mode 100644 src/runtime/service/__init__.py create mode 100644 src/runtime/service/runtime_service.py diff --git a/cc_code_preview/Program/main.cc b/cc_code_preview/Program/main.cc index f271c5e..d172d88 100644 --- a/cc_code_preview/Program/main.cc +++ b/cc_code_preview/Program/main.cc @@ -15,5 +15,12 @@ public lib Main { output(isTrue(test)); output(this.test.getName()); } + + private func testForEach(): void { + var list = []; + list.forEach(e => { + output(e); + }); + } } } \ No newline at end of file diff --git a/old/src/CCLang_sly/Interpreter.py b/old/src/CCLang_sly/Interpreter.py deleted file mode 100644 index 18fde1a..0000000 --- a/old/src/CCLang_sly/Interpreter.py +++ /dev/null @@ -1,31 +0,0 @@ -from typing import Optional - -from CCLang_sly.Parser import Parser -from Interpreter.Validator import Validator -from CCLang_sly.Lexer import Lexer -from Interpreter.Repo import Repo -from Interpreter.Utils import Utils -from Models.AbstractSyntaxTree.AbstractSyntaxTree import AbstractSyntaxTree - - -class Interpreter: - - def __init__(self, repo: Repo, utils: Utils) -> None: - self.__repo = repo - self.__utils = utils - # self.__lexer = Lexer(repo, utils) - # self.__parser = Parser(repo, utils) - # self.__validator = Validator(repo, utils) - - def interpret(self, line_str: str) -> None: - """ - Interprets code line - :param line_str: - :return: - """ - lexer = Lexer() - parser = Parser() - env = {} - ast = parser.parse(lexer.tokenize(line_str)) - if ast is not None: - print(ast) diff --git a/old/src/CCLang_sly/Lexer.py b/old/src/CCLang_sly/Lexer.py deleted file mode 100644 index 4f7bdfc..0000000 --- a/old/src/CCLang_sly/Lexer.py +++ /dev/null @@ -1,108 +0,0 @@ -from sly import Lexer as SlyLexer - -from Models.CCLang.TokenDefinition import TokenDefinition - - -class Lexer(SlyLexer): - # Ignored pattern - ignore = '\t ' - # ignore_comment = r'(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)|([#].*)' - ignore_comment = r'([#].*|(//.*))' - ignore_newline = r'\n+' - - tokens = { - LIBRARY, - CLASS, - FUNCTION, - VARIABLE, - USE, - FROM, - OUTPUT, - INPUT, - LENGTH, - RANGE, - EXIT, - IF, - ELSEIF, - ELSE, - CONTINUE, - IN, - RETURN, - WHILE, - FOR, - PUBLIC, - THIS, - LBRACE, - RBRACE, - LPARAN, - RPARAN, - LBRACKET, - RBRACKET, - SEMICOLON, - COLON, - COMMA, - POINT, - PLUS, - MINUS, - ASTERIK, - SLASH, - EQUAL, - CARET, - TRUE, - FALSE, - STRING, - NUMBER, - EMPTY, - - NAME - } - - # token definition - LIBRARY = TokenDefinition.Library.value - CLASS = TokenDefinition.Class.value - FUNCTION = TokenDefinition.Function.value - VARIABLE = TokenDefinition.Variable.value - USE = TokenDefinition.Use.value - FROM = TokenDefinition.From.value - OUTPUT = TokenDefinition.Output.value - INPUT = TokenDefinition.Input.value - LENGTH = TokenDefinition.Length.value - RANGE = TokenDefinition.Range.value - EXIT = TokenDefinition.Exit.value - IF = TokenDefinition.If.value - ELSEIF = TokenDefinition.ElseIf.value - ELSE = TokenDefinition.Else.value - CONTINUE = TokenDefinition.Continue.value - IN = TokenDefinition.In.value - RETURN = TokenDefinition.Return.value - WHILE = TokenDefinition.While.value - FOR = TokenDefinition.For.value - PUBLIC = TokenDefinition.Public.value - THIS = TokenDefinition.This.value - LBRACE = TokenDefinition.LeftBrace.value - RBRACE = TokenDefinition.RightBrace.value - LPARAN = TokenDefinition.LeftParenthesis.value - RPARAN = TokenDefinition.RightParenthesis.value - LBRACKET = TokenDefinition.LeftBracket.value - RBRACKET = TokenDefinition.RightBracket.value - SEMICOLON = TokenDefinition.Semicolon.value - COLON = TokenDefinition.Colon.value - COMMA = TokenDefinition.Comma.value - POINT = TokenDefinition.Point.value - PLUS = TokenDefinition.Plus.value - MINUS = TokenDefinition.Minus.value - ASTERIK = TokenDefinition.Asterisk.value - SLASH = TokenDefinition.Slash.value - EQUAL = TokenDefinition.Equal.value - CARET = TokenDefinition.Caret.value - TRUE = TokenDefinition.BoolTrue.value - FALSE = TokenDefinition.BoolFalse.value - STRING = TokenDefinition.String.value - NUMBER = TokenDefinition.Number.value - EMPTY = TokenDefinition.Empty.value - - NAME = TokenDefinition.Name.value - - def error(self, t): - print("Illegal character '%s'" % t.value[0]) - # self.index += 1 diff --git a/old/src/CCLang_sly/Parser.py b/old/src/CCLang_sly/Parser.py deleted file mode 100644 index c2f20e8..0000000 --- a/old/src/CCLang_sly/Parser.py +++ /dev/null @@ -1,61 +0,0 @@ -from sly import Parser as SlyParser - -from CCLang_sly.Lexer import Lexer - - -class Parser(SlyParser): - tokens = Lexer.tokens - - #precedence = ( - # ('left', '+', '-'), - # ('left', '*', '/'), - # ('right', 'UMINUS'), - #) - - def __init__(self): - self.env = {} - - @_('') - def statement(self, p): - pass - - def error(self, p): - print(f'ERROR: {p}') - pass - - # lib definition - @_('PUBLIC LIBRARY NAME LBRACE') - def statement(self, p): - return ('lib_def', p.NAME, True) - - @_('LIBRARY NAME LBRACE') - def statement(self, p): - return ('lib_def', p.NAME, False) - - # class definition - @_('PUBLIC CLASS NAME LBRACE') - def statement(self, p): - return ('class_def', p.NAME, True) - - @_('CLASS NAME LBRACE') - def statement(self, p): - return ('class_def', p.NAME, False) - - # func definition - @_('PUBLIC FUNCTION NAME LPARAN statement RPARAN COLON type LBRACE') - def statement(self, p): - return ('func_def', p.NAME, True) - - @_('FUNCTION NAME LPARAN RPARAN COLON type LBRACE') - def statement(self, p): - return ('func_def', p.NAME, False) - - # types - @_('EMPTY') - def type(self, p): - return ('type', p.EMPTY) - - # right brace - @_('RBRACE') - def statement(self, p): - return ('end', p.RBRACE) diff --git a/old/src/cclang.py b/old/src/cclang.py index f31825d..eecfb5b 100644 --- a/old/src/cclang.py +++ b/old/src/cclang.py @@ -12,7 +12,6 @@ class Main: self.__utils = self.__services.utils self.__repo = self.__services.repo self.__interpreter = self.__services.interpreter - # self.__sly_cclang_interpreter = self.__services.sly_cclang_interpreter def console(self) -> None: """ @@ -23,7 +22,6 @@ class Main: while self.__repo.error is None: self.__repo.line_number = i + 1 self.__interpreter.interpret(input('> ')) - # self.__sly_cclang_interpreter.interpret(input('> ')) i += 1 def files(self, file: str) -> None: @@ -44,7 +42,6 @@ class Main: for i in range(0, len(f)): self.__repo.line_number = i + 1 self.__interpreter.interpret(f[i]) - # self.__sly_cclang_interpreter.interpret(f[i]) if __name__ == '__main__': diff --git a/src/cc_lang/model/datatypes.py b/src/cc_lang/model/datatypes.py new file mode 100644 index 0000000..65177be --- /dev/null +++ b/src/cc_lang/model/datatypes.py @@ -0,0 +1,12 @@ +from enum import Enum + + +class Datatypes(Enum): + + Empty = 'empty' + Any = 'any' + Number = 'number' + String = 'string' + Bool = 'bool' + List = 'list' + Dict = 'dict' diff --git a/src/cc_lang/model/language_definition.py b/src/cc_lang/model/language_definition.py new file mode 100644 index 0000000..7fe6ed6 --- /dev/null +++ b/src/cc_lang/model/language_definition.py @@ -0,0 +1,65 @@ +from cc_lang.model.datatypes import Datatypes +from lexer.model.token_value_types import Keywords, FormatCharacters, ExpressionCharacters, Booleans + + +class LanguageDefinition: + # interpreter + keywords = [ + # define keywords + Keywords.Library.value, + Keywords.Class.value, + Keywords.Function.value, + Keywords.Variable.value, + Keywords.Use.value, + Keywords.From.value, + # builtin functions + Keywords.Output.value, + Keywords.Input.value, + Keywords.Length.value, + Keywords.Range.value, + Keywords.Exit.value, + # normal keywords + Keywords.If.value, + Keywords.ElseIf.value, + Keywords.Else.value, + Keywords.Continue.value, + Keywords.If.value, + Keywords.Return.value, + # loops + Keywords.While.value, + Keywords.For.value, + # access + Keywords.Public.value, + Keywords.This.value + ] + datatypes = [ + Datatypes.Empty.value, + Datatypes.Any.value, + Datatypes.Number.value, + Datatypes.String.value, + Datatypes.Bool.value, + Datatypes.List.value, + Datatypes.Dict.value + ] + format_chars = [ + FormatCharacters.Left_Brace.value, + FormatCharacters.Right_Brace.value, + FormatCharacters.Left_Parenthesis.value, + FormatCharacters.Right_Parenthesis.value, + FormatCharacters.Left_Bracket.value, + FormatCharacters.Right_Bracket.value, + FormatCharacters.Semicolon.value, + FormatCharacters.Colon.value, + FormatCharacters.Comma.value, + FormatCharacters.Point.value + ] + expr_chars = [ + ExpressionCharacters.Plus.value, + ExpressionCharacters.Minus.value, + ExpressionCharacters.Asterisk.value, + ExpressionCharacters.Slash.value, + ExpressionCharacters.Equal.value, + ExpressionCharacters.Caret.value + ] + bool_expr_chars = ['<', '>', '!', '!=', '==', '>=', '<=', '&&', '||'] + bool_values = [Booleans.Right.value, Booleans.Wrong.value] diff --git a/src/cc_lang_interpreter/application.py b/src/cc_lang_interpreter/application.py index bddf84d..7fc8be5 100644 --- a/src/cc_lang_interpreter/application.py +++ b/src/cc_lang_interpreter/application.py @@ -1,9 +1,12 @@ +import os + from cpl.application import ApplicationABC from cpl.configuration import ConfigurationABC from cpl.console import Console from cpl.dependency_injection import ServiceProviderABC from lexer.abc.lexer_abc import LexerABC +from runtime.abc.runtime_service_abc import RuntimeServiceABC class Application(ApplicationABC): @@ -12,19 +15,57 @@ class Application(ApplicationABC): ApplicationABC.__init__(self, config, services) self._lexer: LexerABC = services.get_service(LexerABC) + self._runtime: RuntimeServiceABC = services.get_service(RuntimeServiceABC) + self._path = config.get_configuration('p') - def _console(self): pass + def _interpret(self, line: str): + tokens = self._lexer.tokenize(line) - def _files(self): pass + line.replace("\n", "").replace("\t", "") + Console.write_line(f'\nLINE: {line}') + tokens.for_each(lambda t: Console.write_line(t.type, t.value)) + + def _console(self): + i = 0 + while True: + self._runtime.line_count = i + 1 + self._interpret(input('> ')) + 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('.cc'): + self._read_file(os.path.join(r, file)) + + def _read_file(self, file: str): + if not os.path.isfile(file): + raise FileNotFoundError + # self.__utils.runtime_error(Error(ErrorCodes.FileNotFound)) + + if not file.endswith('.cc'): + raise Exception('Wrong file type') + # self.__utils.runtime_error(Error(ErrorCodes.WrongFileType)) + + 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]) def configure(self): pass def main(self): - Console.write_line(self._configuration.additional_arguments, self._path) if self._path is None: self._console() return - self._files() + if os.path.isfile(self._path): + self._read_file(self._path) + else: + self._files() diff --git a/src/cc_lang_interpreter/startup.py b/src/cc_lang_interpreter/startup.py index f84d94a..4aa8673 100644 --- a/src/cc_lang_interpreter/startup.py +++ b/src/cc_lang_interpreter/startup.py @@ -4,6 +4,8 @@ from cpl.dependency_injection import ServiceProviderABC, ServiceCollectionABC from lexer.abc.lexer_abc import LexerABC from lexer.service.lexer_service import LexerService +from runtime.abc.runtime_service_abc import RuntimeServiceABC +from runtime.service.runtime_service import RuntimeService class Startup(StartupABC): @@ -16,12 +18,13 @@ class Startup(StartupABC): self._services = services def configure_configuration(self) -> ConfigurationABC: - self._configuration.add_console_argument(ConsoleArgument('-', 'p', [], ' ')) + self._configuration.add_console_argument(ConsoleArgument('-', 'p', [], ' ', is_value_token_optional=True)) self._configuration.add_console_arguments() return self._configuration def configure_services(self) -> ServiceProviderABC: self._services.add_singleton(LexerABC, LexerService) + self._services.add_singleton(RuntimeServiceABC, RuntimeService) return self._services.build_service_provider() diff --git a/src/lexer/abc/lexer_abc.py b/src/lexer/abc/lexer_abc.py index 4c892e5..0c618a9 100644 --- a/src/lexer/abc/lexer_abc.py +++ b/src/lexer/abc/lexer_abc.py @@ -1,7 +1,14 @@ from abc import ABC, abstractmethod +from cpl_query.extension.list import List + +from lexer.model.token import Token + class LexerABC(ABC): @abstractmethod def __init__(self): pass + + @abstractmethod + def tokenize(self, line: str) -> List[Token]: pass diff --git a/old/src/CCLang_sly/__init__.py b/src/lexer/model/__init__.py similarity index 100% rename from old/src/CCLang_sly/__init__.py rename to src/lexer/model/__init__.py diff --git a/src/lexer/model/token.py b/src/lexer/model/token.py new file mode 100644 index 0000000..64493ab --- /dev/null +++ b/src/lexer/model/token.py @@ -0,0 +1,20 @@ +from lexer.model.token_types import TokenTypes + + +class Token: + + def __init__(self, token_type: TokenTypes, value: str) -> None: + self._type: TokenTypes = token_type + self._value: str = value + + @property + def type(self) -> TokenTypes: + return self._type + + @property + def value(self) -> str: + return self._value + + @value.setter + def value(self, value: str): + self._value = value diff --git a/src/lexer/model/token_types.py b/src/lexer/model/token_types.py new file mode 100644 index 0000000..94580b1 --- /dev/null +++ b/src/lexer/model/token_types.py @@ -0,0 +1,24 @@ +from enum import Enum + + +class TokenTypes(Enum): + Empty = 0 + Keyword = 1 + Type = 2 + Name = 3 + Bool = 4 + String = 5 + Number = 6 + Expression_Character = 7 + Bool_Expression_Character = 8 + Format_Character = 9 + + +class UnresolvedTokenTypes(Enum): + Empty = 0 + Word = 1 + Number = 2 + String = 3 + Expression_Character = 4 + Bool_Expression_Character = 5 + Format_Character = 6 diff --git a/src/lexer/model/token_value_types.py b/src/lexer/model/token_value_types.py new file mode 100644 index 0000000..70dc45b --- /dev/null +++ b/src/lexer/model/token_value_types.py @@ -0,0 +1,62 @@ +from enum import Enum + + +class Keywords(Enum): + # define keywords + Library = 'lib' + Class = 'class' + Function = 'func' + Variable = 'var' + Use = 'use' + From = 'from' + + # builtin functions + Output = 'output' + Input = 'input' + Length = 'length' + Range = 'range' + Exit = 'exit' + + # normal keywords + If = 'if' + ElseIf = 'elseif' + Else = 'else' + Continue = 'continue' + In = 'in' + Return = 'return' + + # loops + While = 'while' + For = 'for' + + # access + Public = 'public' + This = 'this' + + +class Booleans(Enum): + Right = 'true' + Wrong = 'false' + + +class ExpressionCharacters(Enum): + Plus = '+' + Minus = '-' + Asterisk = '*' + Slash = '/' + Equal = '=' + Caret = '^' + + +class FormatCharacters(Enum): + Left_Brace = '{' + Right_Brace = '}' + Left_Parenthesis = '(' + Right_Parenthesis = ')' + Left_Bracket = '[' + Right_Bracket = ']' + Semicolon = ';' + Colon = ':' + Comma = ',' + Point = '.' + diff --git a/src/lexer/service/lexer_service.py b/src/lexer/service/lexer_service.py index 397174e..0e97faf 100644 --- a/src/lexer/service/lexer_service.py +++ b/src/lexer/service/lexer_service.py @@ -1,7 +1,166 @@ +from cpl_query.extension.list import List + +from cc_lang.model.language_definition import LanguageDefinition from lexer.abc.lexer_abc import LexerABC +from lexer.model.token import Token +from lexer.model.token_types import UnresolvedTokenTypes, TokenTypes +from runtime.abc.runtime_service_abc import RuntimeServiceABC class LexerService(LexerABC): - def __init__(self): - pass + def __init__(self, runtime: RuntimeServiceABC): + self._runtime = runtime + + self._is_ml_comment = False + + def _add_tok(self, tokens: List[Token], value: str, input_token_type: UnresolvedTokenTypes) -> None: + """ + Creates token object + :param value: + :param input_token_type: + :return: + """ + token_type: TokenTypes = TokenTypes.Empty + + if value != '': + if input_token_type == UnresolvedTokenTypes.Word: + if value in LanguageDefinition.keywords: + token_type = TokenTypes.Keyword + + elif value in LanguageDefinition.datatypes: + token_type = TokenTypes.Type + + elif value in LanguageDefinition.bool_values: + token_type = TokenTypes.Bool + + elif value == UnresolvedTokenTypes.Empty: + token_type = TokenTypes.Empty + + else: + token_type = TokenTypes.Name + + elif input_token_type == UnresolvedTokenTypes.Number: + token_type = TokenTypes.Number + + elif input_token_type == UnresolvedTokenTypes.String: + token_type = TokenTypes.String + + elif input_token_type == UnresolvedTokenTypes.Expression_Character: + token_type = TokenTypes.Expression_Character + + elif input_token_type == UnresolvedTokenTypes.Bool_Expression_Character: + token_type = TokenTypes.Bool_Expression_Character + + elif input_token_type == UnresolvedTokenTypes.Format_Character: + token_type = TokenTypes.Format_Character + + tokens.append(Token(token_type, value)) + + def tokenize(self, line: str) -> List[Token]: + tokens: List[Token] = List(Token) + word = '' + ol_comment = False + is_string1 = False # 'hello' + is_string2 = False # "hello" + is_number = False + is_expr_char = False + + for i in range(0, len(line)): + c = line[i] + # ignore comments and spaces + if not ol_comment and not self._is_ml_comment: + # comment filtering + if c == '#' and not is_string1 and not is_string2: + ol_comment = True + + elif line[i - 1] == '/' and c == '/': + ol_comment = True + + elif line[i - 1] == '/' and c == '*': + self._is_ml_comment = True + i += 2 + + # end of number + elif not c.isdigit() and c != '.' and is_number: + self._add_tok(tokens, word, UnresolvedTokenTypes.Number) + local_tokens = self.tokenize(c) + for local_token in local_tokens: + tokens.append(local_token) + + word = '' + is_number = False + + # end of expression char + elif c not in LanguageDefinition.expr_chars and is_expr_char: + self._add_tok(tokens, word, UnresolvedTokenTypes.Expression_Character) + word = '' + is_expr_char = False + + # begin of is_string1 + elif c == '\'' and not is_string1: + is_string1 = True + word = '' + + # end of is_string1 + elif c == '\'' and is_string1: + is_string1 = False + self._add_tok(tokens, word, UnresolvedTokenTypes.String) + word = '' + + # begin of is_string2 + elif c == '\"' and not is_string2: + is_string2 = True + word = '' + + # end of is_string2 + elif c == '\"' and is_string2: + is_string2 = False + self._add_tok(tokens, word, UnresolvedTokenTypes.String) + word = '' + + # format char + elif c in LanguageDefinition.format_chars: + self._add_tok(tokens, word, UnresolvedTokenTypes.Word) + self._add_tok(tokens, c, UnresolvedTokenTypes.Format_Character) + word = '' + + # begin of number + elif c.isdigit() and not is_number and word == '': + word += c + is_number = True + + # continue number + elif (c.isdigit() or c == '.') and is_number: + word += c + + # begin expression char + elif c in LanguageDefinition.expr_chars and not is_expr_char: + word += c + is_expr_char = True + + # continue expression char + elif c in LanguageDefinition.expr_chars and is_expr_char: + word += c + + # bool expression char + elif c in LanguageDefinition.bool_expr_chars: + self._add_tok(tokens, word, UnresolvedTokenTypes.Word) + self._add_tok(tokens, c, UnresolvedTokenTypes.Bool_Expression_Character) + word = '' + + # end of word + elif c == ' ' and not is_string1 and not is_string2 or c == '\n': + self._add_tok(tokens, word, UnresolvedTokenTypes.Word) + word = '' + + else: + word += c + + if c == '\n' and ol_comment: + ol_comment = False + + if line[i - 1] == '*' and c == '/': + self._is_ml_comment = False + + return tokens diff --git a/src/runtime/abc/runtime_service_abc.py b/src/runtime/abc/runtime_service_abc.py new file mode 100644 index 0000000..acd6550 --- /dev/null +++ b/src/runtime/abc/runtime_service_abc.py @@ -0,0 +1,15 @@ +from abc import ABC, abstractmethod + + +class RuntimeServiceABC(ABC): + + @abstractmethod + def __init__(self): pass + + @property + @abstractmethod + def line_count(self) -> int: pass + + @line_count.setter + @abstractmethod + def line_count(self, line_count: int): pass diff --git a/src/runtime/service/__init__.py b/src/runtime/service/__init__.py new file mode 100644 index 0000000..425ab6c --- /dev/null +++ b/src/runtime/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/src/runtime/service/runtime_service.py b/src/runtime/service/runtime_service.py new file mode 100644 index 0000000..54d34a0 --- /dev/null +++ b/src/runtime/service/runtime_service.py @@ -0,0 +1,15 @@ +from runtime.abc.runtime_service_abc import RuntimeServiceABC + + +class RuntimeService(RuntimeServiceABC): + + def __init__(self): + self._line_count = 0 + + @property + def line_count(self) -> int: + return self._line_count + + @line_count.setter + def line_count(self, line_count: int): + self._line_count = line_count -- 2.45.2 From 62f8e8640926b88201e6d33c4fe0a7c5abd2c08a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 14 Aug 2021 16:43:48 +0200 Subject: [PATCH 06/11] Improved language design --- src/cc_lang/model/language_definition.py | 2 ++ src/lexer/model/token_value_types.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/cc_lang/model/language_definition.py b/src/cc_lang/model/language_definition.py index 7fe6ed6..ba51750 100644 --- a/src/cc_lang/model/language_definition.py +++ b/src/cc_lang/model/language_definition.py @@ -18,6 +18,7 @@ class LanguageDefinition: Keywords.Length.value, Keywords.Range.value, Keywords.Exit.value, + Keywords.ForEach.value, # normal keywords Keywords.If.value, Keywords.ElseIf.value, @@ -28,6 +29,7 @@ class LanguageDefinition: # loops Keywords.While.value, Keywords.For.value, + Keywords.Foreach.value, # access Keywords.Public.value, Keywords.This.value diff --git a/src/lexer/model/token_value_types.py b/src/lexer/model/token_value_types.py index 70dc45b..8713c28 100644 --- a/src/lexer/model/token_value_types.py +++ b/src/lexer/model/token_value_types.py @@ -16,6 +16,7 @@ class Keywords(Enum): Length = 'length' Range = 'range' Exit = 'exit' + ForEach = 'forEach' # normal keywords If = 'if' @@ -28,6 +29,7 @@ class Keywords(Enum): # loops While = 'while' For = 'for' + Foreach = 'foreach' # access Public = 'public' -- 2.45.2 From 1197863296051286060df87bc73bedd2221c970e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 14 Aug 2021 16:44:51 +0200 Subject: [PATCH 07/11] Moved language design classes --- src/cc_lang/model/language_definition.py | 2 +- .../model/language_definition_classes.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{lexer/model/token_value_types.py => cc_lang/model/language_definition_classes.py} (100%) diff --git a/src/cc_lang/model/language_definition.py b/src/cc_lang/model/language_definition.py index ba51750..71f0263 100644 --- a/src/cc_lang/model/language_definition.py +++ b/src/cc_lang/model/language_definition.py @@ -1,5 +1,5 @@ from cc_lang.model.datatypes import Datatypes -from lexer.model.token_value_types import Keywords, FormatCharacters, ExpressionCharacters, Booleans +from cc_lang.model.language_definition_classes import Keywords, FormatCharacters, ExpressionCharacters, Booleans class LanguageDefinition: diff --git a/src/lexer/model/token_value_types.py b/src/cc_lang/model/language_definition_classes.py similarity index 100% rename from src/lexer/model/token_value_types.py rename to src/cc_lang/model/language_definition_classes.py -- 2.45.2 From 1f6d13551bd95a139d8bb84879e1898fefe21596 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 26 Oct 2021 23:32:08 +0200 Subject: [PATCH 08/11] Added CPL 2021.10.0 support --- src/cc_lang/model/language_definition.py | 2 ++ .../model/language_definition_classes.py | 2 ++ src/cc_lang_interpreter/application.py | 17 ++++++----- .../cc-lang-interpreter.json | 3 +- src/cc_lang_interpreter/main.py | 2 +- src/cc_lang_interpreter/startup.py | 29 +++++++++---------- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/cc_lang/model/language_definition.py b/src/cc_lang/model/language_definition.py index 71f0263..c1417f1 100644 --- a/src/cc_lang/model/language_definition.py +++ b/src/cc_lang/model/language_definition.py @@ -32,6 +32,8 @@ class LanguageDefinition: Keywords.Foreach.value, # access Keywords.Public.value, + Keywords.Private.value, + Keywords.Static.value, Keywords.This.value ] datatypes = [ diff --git a/src/cc_lang/model/language_definition_classes.py b/src/cc_lang/model/language_definition_classes.py index 8713c28..6c0365b 100644 --- a/src/cc_lang/model/language_definition_classes.py +++ b/src/cc_lang/model/language_definition_classes.py @@ -33,6 +33,8 @@ class Keywords(Enum): # access Public = 'public' + Private = 'private' + Static = 'static' This = 'this' diff --git a/src/cc_lang_interpreter/application.py b/src/cc_lang_interpreter/application.py index 7fc8be5..e52b16f 100644 --- a/src/cc_lang_interpreter/application.py +++ b/src/cc_lang_interpreter/application.py @@ -1,9 +1,9 @@ import os -from cpl.application import ApplicationABC -from cpl.configuration import ConfigurationABC -from cpl.console import Console -from cpl.dependency_injection import ServiceProviderABC +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 lexer.abc.lexer_abc import LexerABC from runtime.abc.runtime_service_abc import RuntimeServiceABC @@ -18,19 +18,22 @@ class Application(ApplicationABC): 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) line.replace("\n", "").replace("\t", "") - Console.write_line(f'\nLINE: {line}') - tokens.for_each(lambda t: Console.write_line(t.type, t.value)) + 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 while True: self._runtime.line_count = i + 1 - self._interpret(input('> ')) + self._interpret(Console.read('> ')) i += 1 def _files(self): diff --git a/src/cc_lang_interpreter/cc-lang-interpreter.json b/src/cc_lang_interpreter/cc-lang-interpreter.json index 3f2ea2b..21a7165 100644 --- a/src/cc_lang_interpreter/cc-lang-interpreter.json +++ b/src/cc_lang_interpreter/cc-lang-interpreter.json @@ -16,7 +16,8 @@ "LicenseName": "", "LicenseDescription": "", "Dependencies": [ - "sh_cpl==2021.4.0.post2" + "sh_cpl-core==2021.10.0.post1", + "sh_cpl-query==2021.10.0" ], "PythonVersion": ">=3.9.2", "PythonPath": { diff --git a/src/cc_lang_interpreter/main.py b/src/cc_lang_interpreter/main.py index 808d05d..95ff50b 100644 --- a/src/cc_lang_interpreter/main.py +++ b/src/cc_lang_interpreter/main.py @@ -1,4 +1,4 @@ -from cpl.application import ApplicationBuilder +from cpl_core.application import ApplicationBuilder from cc_lang_interpreter.application import Application from cc_lang_interpreter.startup import Startup diff --git a/src/cc_lang_interpreter/startup.py b/src/cc_lang_interpreter/startup.py index 4aa8673..5e9f1d0 100644 --- a/src/cc_lang_interpreter/startup.py +++ b/src/cc_lang_interpreter/startup.py @@ -1,6 +1,7 @@ -from cpl.application import StartupABC -from cpl.configuration import ConfigurationABC, ConsoleArgument -from cpl.dependency_injection import ServiceProviderABC, ServiceCollectionABC +from cpl_core.application import StartupABC +from cpl_core.configuration import ConfigurationABC, ConsoleArgument +from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironment from lexer.abc.lexer_abc import LexerABC from lexer.service.lexer_service import LexerService @@ -10,21 +11,17 @@ from runtime.service.runtime_service import RuntimeService class Startup(StartupABC): - def __init__(self, config: ConfigurationABC, services: ServiceCollectionABC): + def __init__(self): StartupABC.__init__(self) - self._configuration = config - self._environment = self._configuration.environment - self._services = services + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironment) -> ConfigurationABC: + config.add_console_argument(ConsoleArgument('-', 'p', [], ' ', is_value_token_optional=True)) + config.add_console_arguments() - def configure_configuration(self) -> ConfigurationABC: - self._configuration.add_console_argument(ConsoleArgument('-', 'p', [], ' ', is_value_token_optional=True)) - self._configuration.add_console_arguments() + return config - return self._configuration + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironment) -> ServiceProviderABC: + services.add_singleton(LexerABC, LexerService) + services.add_singleton(RuntimeServiceABC, RuntimeService) - def configure_services(self) -> ServiceProviderABC: - self._services.add_singleton(LexerABC, LexerService) - self._services.add_singleton(RuntimeServiceABC, RuntimeService) - - return self._services.build_service_provider() + return services.build_service_provider() -- 2.45.2 From 4af133ab21762b4efe8f87534ce4a550315b1b7e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 27 Oct 2021 09:18:57 +0200 Subject: [PATCH 09/11] Added Parser --- src/cc_lang_interpreter/application.py | 12 +++++++----- src/cc_lang_interpreter/startup.py | 3 +++ src/lexer/model/token.py | 3 +++ src/parser/__init__.py | 2 +- src/parser/abc/__init__.py | 1 + src/parser/abc/ast_abc.py | 7 +++++++ src/parser/abc/parser_abc.py | 14 ++++++++++++++ src/parser/service/__init__.py | 1 + src/parser/service/parser_service.py | 19 +++++++++++++++++++ 9 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 src/parser/abc/__init__.py create mode 100644 src/parser/abc/ast_abc.py create mode 100644 src/parser/abc/parser_abc.py create mode 100644 src/parser/service/__init__.py create mode 100644 src/parser/service/parser_service.py 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) -- 2.45.2 From ad10bc035704fe23a8a057ba4f6b153184c19429 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 27 Oct 2021 09:19:05 +0200 Subject: [PATCH 10/11] Added .vscode --- .vscode/launch.json | 59 +++++++++++++++++++++++++++++++++++++++++++ .vscode/settings.json | 25 ++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c5dd981 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,59 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Aktuelle Datei", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "env": { + "PYTHONPATH": "${workspaceFolder}/src/:${workspaceFolder}/venv/bin/python" + } + }, + { + "name": "CCL", + "type": "python", + "request": "launch", + "cwd": "/home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cclang/src/", + "program": "cc_lang_interpreter/main.py", + "console": "integratedTerminal", + "args": [ + "First.cc" + ], + "env": { + "PYTHONPATH": "${workspaceFolder}/src/:${workspaceFolder}/venv/bin/python" + } + }, + { + "name": "CCL: File", + "type": "python", + "request": "launch", + "cwd": "/home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cclang/src/", + "program": "cc_lang_interpreter/main.py", + "console": "integratedTerminal", + "args": [ + "-p", + "../cc_code_preview" + ], + "env": { + "PYTHONPATH": "${workspaceFolder}/src/:${workspaceFolder}/venv/bin/python" + } + }, + { + "name": "CCL: Console", + "type": "python", + "request": "launch", + "cwd": "/home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cclang/src/", + "program": "cc_lang_interpreter/main.py", + "console": "integratedTerminal", + "args": [], + "env": { + "PYTHONPATH": "${workspaceFolder}/src/:${workspaceFolder}/venv/bin/python" + } + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..aa0ff85 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,25 @@ +{ + "workbench.colorCustomizations": { + "activityBar.activeBackground": "#fbed80", + "activityBar.activeBorder": "#06b9a5", + "activityBar.background": "#fbed80", + "activityBar.foreground": "#15202b", + "activityBar.inactiveForeground": "#15202b99", + "activityBarBadge.background": "#06b9a5", + "activityBarBadge.foreground": "#15202b", + "editorGroup.border": "#fbed80", + "panel.border": "#fbed80", + "sash.hoverBorder": "#fbed80", + "sideBar.border": "#fbed80", + "statusBar.background": "#f9e64f", + "statusBar.foreground": "#15202b", + "statusBarItem.hoverBackground": "#f7df1e", + "statusBarItem.remoteBackground": "#f9e64f", + "statusBarItem.remoteForeground": "#15202b", + "titleBar.activeBackground": "#f9e64f", + "titleBar.activeForeground": "#15202b", + "titleBar.inactiveBackground": "#f9e64f99", + "titleBar.inactiveForeground": "#15202b99" + }, + "peacock.color": "#f9e64f" +} \ No newline at end of file -- 2.45.2 From f6642f834b3b1412da0139482b2d441071000623 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 27 Oct 2021 09:21:40 +0200 Subject: [PATCH 11/11] Renamed .cc to .ccl --- .vscode/launch.json | 2 +- cc_code_preview/Preview/{classes.cc => classes.ccl} | 0 cc_code_preview/Preview/{functions.cc => functions.ccl} | 0 cc_code_preview/Preview/{variables.cc => variables.ccl} | 0 cc_code_preview/Program/{main.cc => main.ccl} | 0 old/src/cclang.py | 2 +- src/cc_lang_interpreter/application.py | 4 ++-- 7 files changed, 4 insertions(+), 4 deletions(-) rename cc_code_preview/Preview/{classes.cc => classes.ccl} (100%) rename cc_code_preview/Preview/{functions.cc => functions.ccl} (100%) rename cc_code_preview/Preview/{variables.cc => variables.ccl} (100%) rename cc_code_preview/Program/{main.cc => main.ccl} (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json index c5dd981..170549c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -22,7 +22,7 @@ "program": "cc_lang_interpreter/main.py", "console": "integratedTerminal", "args": [ - "First.cc" + "First.ccl" ], "env": { "PYTHONPATH": "${workspaceFolder}/src/:${workspaceFolder}/venv/bin/python" diff --git a/cc_code_preview/Preview/classes.cc b/cc_code_preview/Preview/classes.ccl similarity index 100% rename from cc_code_preview/Preview/classes.cc rename to cc_code_preview/Preview/classes.ccl diff --git a/cc_code_preview/Preview/functions.cc b/cc_code_preview/Preview/functions.ccl similarity index 100% rename from cc_code_preview/Preview/functions.cc rename to cc_code_preview/Preview/functions.ccl diff --git a/cc_code_preview/Preview/variables.cc b/cc_code_preview/Preview/variables.ccl similarity index 100% rename from cc_code_preview/Preview/variables.cc rename to cc_code_preview/Preview/variables.ccl diff --git a/cc_code_preview/Program/main.cc b/cc_code_preview/Program/main.ccl similarity index 100% rename from cc_code_preview/Program/main.cc rename to cc_code_preview/Program/main.ccl diff --git a/old/src/cclang.py b/old/src/cclang.py index eecfb5b..5baf30f 100644 --- a/old/src/cclang.py +++ b/old/src/cclang.py @@ -34,7 +34,7 @@ class Main: self.__utils.runtime_error(Error(ErrorCodes.FileNotFound)) return - if not file.endswith('.cc'): + if not file.endswith('.ccl'): self.__utils.runtime_error(Error(ErrorCodes.WrongFileType)) return diff --git a/src/cc_lang_interpreter/application.py b/src/cc_lang_interpreter/application.py index 252d142..944190d 100644 --- a/src/cc_lang_interpreter/application.py +++ b/src/cc_lang_interpreter/application.py @@ -45,7 +45,7 @@ class Application(ApplicationABC): # r=root, d=directories, f=files for r, d, f in os.walk(self._path): for file in f: - if file.endswith('.cc'): + if file.endswith('.ccl'): self._read_file(os.path.join(r, file)) def _read_file(self, file: str): @@ -53,7 +53,7 @@ class Application(ApplicationABC): raise FileNotFoundError # self.__utils.runtime_error(Error(ErrorCodes.FileNotFound)) - if not file.endswith('.cc'): + if not file.endswith('.ccl'): raise Exception('Wrong file type') # self.__utils.runtime_error(Error(ErrorCodes.WrongFileType)) -- 2.45.2