cclang/old/src/Models/AbstractSyntaxTree/AbstractSyntaxTree.py

93 lines
2.5 KiB
Python

from typing import List, Union
from Models.Interpreter.Datatypes import Datatypes
from Models.Token.TokenValueTypes import ExpressionCharacters
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(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.Plus.value,
ExpressionCharacters.Minus.value,
ExpressionCharacters.Asterisk.value,
ExpressionCharacters.Slash.value,
ExpressionCharacters.Caret.value
]
def eval(self):
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