From 257a190ffe8599093bf1f62fe7752c9c6f384fb7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 29 Oct 2021 12:18:26 +0200 Subject: [PATCH] Added logic to handle constructor for classes --- cc_code_preview/Preview/classes.ccl | 2 +- cc_code_preview/Program/main.ccl | 2 +- src/cc_lang/model/language_definition.py | 1 + .../model/language_definition_classes.py | 1 + src/parser/model/ast_types_enum.py | 1 + src/parser/service/parser_service.py | 49 +++++++++++++++++++ 6 files changed, 54 insertions(+), 2 deletions(-) diff --git a/cc_code_preview/Preview/classes.ccl b/cc_code_preview/Preview/classes.ccl index 26c1321..d808077 100644 --- a/cc_code_preview/Preview/classes.ccl +++ b/cc_code_preview/Preview/classes.ccl @@ -2,7 +2,7 @@ public lib Preview.Classes { public class Test { private var _name: string; - public constructor(name: string) { + constructor(name: string) { this._name = name; } diff --git a/cc_code_preview/Program/main.ccl b/cc_code_preview/Program/main.ccl index 7c17321..c2bbe63 100644 --- a/cc_code_preview/Program/main.ccl +++ b/cc_code_preview/Program/main.ccl @@ -6,7 +6,7 @@ public lib Main { public class Program { private var test: Test; - public constructor() { + constructor() { this.test = Test(str); } diff --git a/src/cc_lang/model/language_definition.py b/src/cc_lang/model/language_definition.py index 9879705..f7b3fa8 100644 --- a/src/cc_lang/model/language_definition.py +++ b/src/cc_lang/model/language_definition.py @@ -12,6 +12,7 @@ class LanguageDefinition: Keywords.Variable.value, Keywords.Use.value, Keywords.From.value, + Keywords.Constructor.value, # builtin functions Keywords.Output.value, Keywords.Input.value, diff --git a/src/cc_lang/model/language_definition_classes.py b/src/cc_lang/model/language_definition_classes.py index 26ed8a2..ab1ce4e 100644 --- a/src/cc_lang/model/language_definition_classes.py +++ b/src/cc_lang/model/language_definition_classes.py @@ -9,6 +9,7 @@ class Keywords(Enum): Variable = 'var' Use = 'use' From = 'from' + Constructor = 'constructor' # builtin functions Output = 'output' diff --git a/src/parser/model/ast_types_enum.py b/src/parser/model/ast_types_enum.py index 4fb78b6..ea9e85c 100644 --- a/src/parser/model/ast_types_enum.py +++ b/src/parser/model/ast_types_enum.py @@ -14,3 +14,4 @@ class ASTTypesEnum(Enum): VariableValue = 'variable_value' UseDirective = 'use_directive' Arguments = 'arguments' + Constructor = 'constructor' diff --git a/src/parser/service/parser_service.py b/src/parser/service/parser_service.py index e3f1742..e94f139 100644 --- a/src/parser/service/parser_service.py +++ b/src/parser/service/parser_service.py @@ -363,6 +363,52 @@ class ParserService(ParserABC): return AST(ASTTypesEnum.UseDirective, ast, self._runtime.line_count, self._runtime.line_count) + def _parse_constructor(self, tokens: List[Token]) -> AST: + """ Parses constructor + + Args: + tokens (List[Token]): Tokens from lexer + + AST: + constructor() { + <(> <)> <{> + constructor(i: number) { + <(> <:> <)> <{> + constructor(i: number, name: string) { + <(> <:> <,> <:> <)> <{> + + Returns: + AST: Library or class AST + """ + ast = List(AST) + end = None + i = 0 + while i < tokens.count(): + token: Token = tokens[i] + + if token.type == TokenTypes.Format_Character and token.value == FormatCharacters.Left_Brace.value: + break + + elif token.type == TokenTypes.Format_Character and token.value == FormatCharacters.Right_Brace.value: + end = self._runtime.line_count + break + + elif i == 0 and token.type == TokenTypes.Keyword and token.value == Keywords.Constructor.value: + ast.append(AST(ASTTypesEnum.Keyword, token.value, self._runtime.line_count, self._runtime.line_count)) + + elif i == 1 and token.type == TokenTypes.Format_Character and token.value == FormatCharacters.Left_Parenthesis.value: + args, last_token = self._parse_args(tokens.skip(i)) + if last_token is not None: + i = tokens.index(last_token) + ast.append(args) + + else: + self._runtime.error(Error(ErrorCodesEnum.Unexpected, token.value)) + + i += 1 + + return AST(ASTTypesEnum.Constructor, ast, self._runtime.line_count, end) + def create_ast(self, tokens: List[Token]) -> List[AST]: self._ast = List(AST) @@ -381,4 +427,7 @@ class ParserService(ParserABC): elif tokens.where(lambda t: t.type == TokenTypes.Keyword and t.value == Keywords.Variable.value).count() > 0: self._ast.append(self._parse_variable(tokens)) + elif tokens.where(lambda t: t.type == TokenTypes.Keyword and t.value == Keywords.Constructor.value).count() > 0: + self._ast.append(self._parse_constructor(tokens)) + return self._ast