[DevState] Improved Parsing for AST Lib, Class, Func, Var, Call
This commit is contained in:
@@ -1,30 +1,92 @@
|
||||
from typing import List, Union
|
||||
|
||||
from Models.Interpreter.Datatypes import Datatypes
|
||||
from Models.Token.Token import Token
|
||||
from Models.Token.TokenTypes import TokenTypes
|
||||
from Models.Token.TokenValueTypes import ExpressionCharacters
|
||||
|
||||
|
||||
class ValueNode:
|
||||
def __init__(self, value: Token = 'empty', datatype: Token = Token(TokenTypes.Type, Datatypes.strings.value[Datatypes.Empty.value])):
|
||||
class AbstractSyntaxTree:
|
||||
|
||||
def __init__(self):
|
||||
self.libraries: List[LibraryDefinitionNode] = []
|
||||
|
||||
|
||||
class ASTElement:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
class ValueNode(ASTElement):
|
||||
def __init__(self, value: str, datatype: Datatypes):
|
||||
super().__init__()
|
||||
self.value = value
|
||||
self.type = datatype
|
||||
|
||||
|
||||
class BinaryOperationNode:
|
||||
def __init__(self, left: Token, op_token: Token, right: Token):
|
||||
class BinaryOperationNode(ASTElement):
|
||||
def __init__(self, left: str, op_token: str, right: str):
|
||||
super().__init__()
|
||||
self.left = left
|
||||
self.op_token = op_token
|
||||
self.right = right
|
||||
|
||||
self.operation_chars = [
|
||||
ExpressionCharacters.chars.value[ExpressionCharacters.Plus.value],
|
||||
ExpressionCharacters.chars.value[ExpressionCharacters.Minus.value],
|
||||
ExpressionCharacters.chars.value[ExpressionCharacters.Asterisk.value],
|
||||
ExpressionCharacters.chars.value[ExpressionCharacters.Slash.value],
|
||||
ExpressionCharacters.chars.value[ExpressionCharacters.Caret.value]
|
||||
ExpressionCharacters.Plus.value,
|
||||
ExpressionCharacters.Minus.value,
|
||||
ExpressionCharacters.Asterisk.value,
|
||||
ExpressionCharacters.Slash.value,
|
||||
ExpressionCharacters.Caret.value
|
||||
]
|
||||
|
||||
def eval(self):
|
||||
if self.op_token.value not in self.operation_chars:
|
||||
return eval(f'{self.left.value} {self.op_token.value} {self.right.value}')
|
||||
if self.op_token in self.operation_chars:
|
||||
return eval(f'{self.left} {self.op_token} {self.right}')
|
||||
|
||||
|
||||
class LibraryDefinitionNode(ASTElement):
|
||||
|
||||
def __init__(self, is_public: bool, name: str):
|
||||
super().__init__()
|
||||
self.is_public = is_public
|
||||
self.name = name
|
||||
self.classes: List[ClassDefinitionNode] = []
|
||||
|
||||
|
||||
class ClassDefinitionNode(ASTElement):
|
||||
|
||||
def __init__(self, is_public: bool, name: str):
|
||||
super().__init__()
|
||||
self.is_public = is_public
|
||||
self.name = name
|
||||
self.variables: [VariableDefinitionNode] = []
|
||||
self.functions: List[FunctionDefinitionNode] = []
|
||||
|
||||
|
||||
class CallDefinitionNode(ASTElement):
|
||||
|
||||
def __init__(self, name: str):
|
||||
super().__init__()
|
||||
self.name = name
|
||||
self.args: List[ValueNode] = []
|
||||
|
||||
|
||||
class FunctionDefinitionNode(ASTElement):
|
||||
|
||||
def __init__(self, is_public: bool, name: str, return_type: Datatypes):
|
||||
super().__init__()
|
||||
self.is_public = is_public
|
||||
self.name = name
|
||||
self.args: List[VariableDefinitionNode] = []
|
||||
self.return_type = return_type
|
||||
self.variables: [VariableDefinitionNode] = []
|
||||
self.instructions: List[ASTElement] = []
|
||||
|
||||
|
||||
class VariableDefinitionNode(ASTElement):
|
||||
|
||||
def __init__(self, is_public: bool, name: str, datatype: Union[str, Datatypes], value: Union[str, CallDefinitionNode]):
|
||||
super().__init__()
|
||||
self.is_public = is_public
|
||||
self.name = name
|
||||
self.datatype = datatype
|
||||
self.value = value
|
||||
|
@@ -3,20 +3,10 @@ from enum import Enum
|
||||
|
||||
class Datatypes(Enum):
|
||||
|
||||
Empty = 0
|
||||
Any = 1
|
||||
Number = 2
|
||||
String = 3
|
||||
Bool = 4
|
||||
List = 5
|
||||
Dict = 6
|
||||
|
||||
strings = [
|
||||
'empty',
|
||||
'any',
|
||||
'number',
|
||||
'string',
|
||||
'bool',
|
||||
'list',
|
||||
'dict'
|
||||
]
|
||||
Empty = 'empty'
|
||||
Any = 'any'
|
||||
Number = 'number'
|
||||
String = 'string'
|
||||
Bool = 'bool'
|
||||
List = 'list'
|
||||
Dict = 'dict'
|
||||
|
@@ -1,82 +1,62 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Keywords:
|
||||
class Keywords(Enum):
|
||||
# define keywords
|
||||
Library: str = 'lib'
|
||||
Class: str = 'class'
|
||||
Function: str = 'func'
|
||||
Variable: str = 'var'
|
||||
Use: str = 'use'
|
||||
From: str = 'from'
|
||||
Library = 'lib'
|
||||
Class = 'class'
|
||||
Function = 'func'
|
||||
Variable = 'var'
|
||||
Use = 'use'
|
||||
From = 'from'
|
||||
|
||||
# builtin functions
|
||||
Output: str = 'output'
|
||||
Input: str = 'input'
|
||||
Length: str = 'length'
|
||||
Range: str = 'range'
|
||||
Exit: str = 'exit'
|
||||
Output = 'output'
|
||||
Input = 'input'
|
||||
Length = 'length'
|
||||
Range = 'range'
|
||||
Exit = 'exit'
|
||||
|
||||
# normal keywords
|
||||
If: str = 'if'
|
||||
ElseIf: str = 'elseif'
|
||||
Else: str = 'else'
|
||||
Continue: str = 'continue'
|
||||
In: str = 'in'
|
||||
Return: str = 'return'
|
||||
If = 'if'
|
||||
ElseIf = 'elseif'
|
||||
Else = 'else'
|
||||
Continue = 'continue'
|
||||
In = 'in'
|
||||
Return = 'return'
|
||||
|
||||
# loops
|
||||
While: str = 'while'
|
||||
For: str = 'for'
|
||||
While = 'while'
|
||||
For = 'for'
|
||||
|
||||
# access
|
||||
Public: str = 'public'
|
||||
Public = 'public'
|
||||
This = 'this'
|
||||
|
||||
|
||||
class Booleans:
|
||||
class Booleans(Enum):
|
||||
Right = 'true'
|
||||
Wrong = 'false'
|
||||
|
||||
|
||||
class ExpressionCharacters(Enum):
|
||||
Plus = 0
|
||||
Minus = 1
|
||||
Asterisk = 2
|
||||
Slash = 3
|
||||
Equal = 4
|
||||
Caret = 5
|
||||
|
||||
chars = [
|
||||
'+',
|
||||
'-',
|
||||
'*',
|
||||
'/',
|
||||
'=',
|
||||
'^'
|
||||
]
|
||||
Plus = '+'
|
||||
Minus = '-'
|
||||
Asterisk = '*'
|
||||
Slash = '/'
|
||||
Equal = '='
|
||||
Caret = '^'
|
||||
|
||||
|
||||
class FormatCharacters(Enum):
|
||||
Left_Brace = 0
|
||||
Right_Brace = 1
|
||||
Left_Parenthesis = 2
|
||||
Right_Parenthesis = 3
|
||||
Left_Bracket = 4
|
||||
Right_Bracket = 5
|
||||
Semicolon = 6
|
||||
Colon = 7
|
||||
Comma = 8
|
||||
Point = 9
|
||||
Left_Brace = '{'
|
||||
Right_Brace = '}'
|
||||
Left_Parenthesis = '('
|
||||
Right_Parenthesis = ')'
|
||||
Left_Bracket = '['
|
||||
Right_Bracket = ']'
|
||||
Semicolon = ';'
|
||||
Colon = ':'
|
||||
Comma = ','
|
||||
Point = '.'
|
||||
|
||||
chars = [
|
||||
'{',
|
||||
'}',
|
||||
'(',
|
||||
')',
|
||||
'[',
|
||||
']',
|
||||
';',
|
||||
':',
|
||||
',',
|
||||
'.'
|
||||
]
|
||||
|
Reference in New Issue
Block a user