Added logic to handle constructor for classes
This commit is contained in:
parent
4aaffca9cf
commit
257a190ffe
@ -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;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ public lib Main {
|
||||
public class Program {
|
||||
private var test: Test;
|
||||
|
||||
public constructor() {
|
||||
constructor() {
|
||||
this.test = Test(str);
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -9,6 +9,7 @@ class Keywords(Enum):
|
||||
Variable = 'var'
|
||||
Use = 'use'
|
||||
From = 'from'
|
||||
Constructor = 'constructor'
|
||||
|
||||
# builtin functions
|
||||
Output = 'output'
|
||||
|
@ -14,3 +14,4 @@ class ASTTypesEnum(Enum):
|
||||
VariableValue = 'variable_value'
|
||||
UseDirective = 'use_directive'
|
||||
Arguments = 'arguments'
|
||||
Constructor = 'constructor'
|
||||
|
@ -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> <(> <)> <{>
|
||||
constructor(i: number) {
|
||||
<constructor> <(> <name> <:> <type> <)> <{>
|
||||
constructor(i: number, name: string) {
|
||||
<constructor> <(> <name> <:> <type> <,> <name> <:> <)> <{>
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user