Improved parsing

This commit is contained in:
Sven Heidemann
2020-09-17 19:33:52 +02:00
parent cb54261118
commit dfc4b44b25
31 changed files with 717 additions and 189 deletions

View File

@@ -0,0 +1,9 @@
from typing import List
from Models.Interpreter.Token import Token
class AbstractSyntaxTree:
def __init__(self):
self.tokens: List[Token] = []

View File

@@ -0,0 +1,13 @@
from typing import List
from Models.AbstractSyntaxTree.Function import Function
from Models.AbstractSyntaxTree.Variable import Variable
class Class:
def __init__(self):
self.name: str = ''
self.variables: List[Variable] = []
self.functions: List[Function] = []
self.is_public = False

View File

@@ -0,0 +1,14 @@
from typing import List
from Models.AbstractSyntaxTree.Variable import Variable
from Models.Interpreter.Types import Types
class Function:
def __init__(self) -> None:
self.name: str = ''
self.variables: List[Variable] = []
self.instructions: [] = []
self.return_type: Types = Types.Any
self.is_public = False

View File

@@ -0,0 +1,9 @@
from Models.AbstractSyntaxTree.AbstractSyntaxTree import AbstractSyntaxTree
from Models.AbstractSyntaxTree.InstructionTypes import InstructionTypes
class Instruction:
def __init__(self):
self.type: InstructionTypes = InstructionTypes.Unknown
self.ast: AbstractSyntaxTree = AbstractSyntaxTree()

View File

@@ -0,0 +1,10 @@
from enum import Enum
class InstructionTypes(Enum):
Unknown = 0
Expression = 1
Bool_Expression = 2
Assignment = 3
Call = 4

View File

@@ -0,0 +1,11 @@
from typing import List
from Models.AbstractSyntaxTree.Class import Class
class Library:
def __init__(self):
self.name: str = ''
self.classes: List[Class] = []
self.is_public = False

View File

@@ -0,0 +1,9 @@
from typing import List
from Models.AbstractSyntaxTree.Library import Library
class RuntimeAbstractSyntaxTree:
def __init__(self):
self.libs: List[Library] = []

View File

@@ -0,0 +1,12 @@
from typing import List
from Models.Interpreter.Token import Token
class Variable:
def __init__(self):
self.name: str = ''
self.type: str = ''
self.value: List[Token] = []
self.is_public = False

View File

@@ -0,0 +1,4 @@
class Booleans:
Right = 'true'
Wrong = 'false'

View File

@@ -1,34 +1,27 @@
from enum import Enum
class ErrorCode(Enum):
StartFailed = 'Start failed'
FileNotFound = 'File not found'
Unknown = 'Unknown {}'
Inaccessible = '{} inaccessible'
Unexpected = 'Unexpected {}'
Expected = 'Expected {}'
LibInLib = 'Lib in lib'
LibInClass = 'Lib in class'
LibInFunc = 'Lib in func'
ClassInClass = 'Class in class'
ClassInFunc = 'Class in func'
FuncInLib = 'Func in lib'
FuncInFunc = 'Func in func'
class Error:
def __init__(self, code: float, msg: str = ''):
def __init__(self, code: ErrorCode, msg: str = ''):
self.code = code
self.__msgs = {
# Interpreter:
1.0: 'Start failed',
1.1: 'File not found',
# Runtime:
2.0: 'Unknown keyword',
2.1: 'Unknown type',
2.2: 'Unknown variable',
2.3: 'Unknown function',
2.4: 'Unknown class',
2.5: 'Unknown library',
# Parser:
3.0: 'Lib in lib',
3.1: 'Lib in class',
3.2: 'Lib in func',
3.3: 'Class in class',
3.4: 'Class in func',
3.5: 'Func in lib',
3.6: 'Func in func',
3.7: 'Expected: type',
3.8: 'Access error: no export',
3.9: 'Expression error',
3.10: 'Unexpected end of line',
3.11: 'Unexpected {}', # other types
}
self.msg = self.__msgs[code].format(msg)
self.msg = code.value.format(msg)

View File

@@ -0,0 +1,19 @@
from enum import Enum
class ExpressionCharacters(Enum):
Plus = 0
Minus = 1
Asterisk = 2
Slash = 3
Equal = 4
Caret = 5
chars = [
'+',
'-',
'*',
'/',
'=',
'^'
]

View File

@@ -0,0 +1,27 @@
from enum import Enum
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
chars = [
'{',
'}',
'(',
')',
'[',
']',
';',
':',
',',
'.'
]

View File

@@ -0,0 +1,30 @@
class Keywords:
# define keywords
Library: str = 'lib'
Class: str = 'class'
Function: str = 'func'
Variable: str = 'var'
Use: str = 'use'
From: str = 'from'
# builtin functions
Output: str = 'output'
Input: str = 'input'
Length: str = 'length'
Range: str = 'range'
Exit: str = 'exit'
# normal keywords
If: str = 'if'
ElseIf: str = 'elseif'
Else: str = 'else'
Pass: str = 'pass'
In: str = 'in'
Return: str = 'return'
# loops
While: str = 'while'
For: str = 'for'
# access
Public: str = 'public'

View File

@@ -1,5 +1,8 @@
from Models.Interpreter import TokenTypes
class Token:
def __init__(self, type: str, value: str) -> None:
self.type = type
def __init__(self, token_type: TokenTypes, value: str) -> None:
self.type: TokenTypes = token_type
self.value = value

View File

@@ -0,0 +1,14 @@
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

View File

@@ -0,0 +1,24 @@
from enum import Enum
class Types(Enum):
Any = 0
Number = 1
String = 2
Bool = 3
List = 4
Dict = 5
Empty = 6
Void = 7
strings = [
'any',
'number',
'string',
'bool',
'list',
'dict',
'empty',
'void'
]

View File

@@ -0,0 +1,11 @@
from enum import Enum
class UnresolvedTokenTypes(Enum):
Empty = 0
Word = 1
Number = 2
String = 3
Expression_Character = 4
Bool_Expression_Character = 5
Format_Character = 6

View File

@@ -1,6 +0,0 @@
class Class:
def __init__(self, name: str, access: str = '') -> None:
self.name = name
self.ast = [] # filled by parser
self.access = access

View File

@@ -1,7 +0,0 @@
class Func:
def __init__(self, name: str, return_type: str, access: str = '') -> None:
self.name = name
self.return_type = return_type
self.ast = [] # filled by parser
self.access = access

View File

@@ -1,5 +0,0 @@
class Lib:
def __init__(self, name: str) -> None:
self.name = name
self.ast = [] # filled by parser

View File

@@ -1,6 +0,0 @@
class Var:
def __init__(self, name: str, type: str, access: str = ''):
self.name = name
self.type = type
self.access = access