added return type to function
This commit is contained in:
@@ -2,9 +2,9 @@ from Interpreter.Repo import Repo
|
||||
from Interpreter.Utils import Utils
|
||||
from Models.Interpreter.Error import Error
|
||||
from Models.Interpreter.Token import Token
|
||||
from Models.Language.AST.Class import Class
|
||||
from Models.Language.AST.Func import Func
|
||||
from Models.Language.AST.Lib import Lib
|
||||
from Models.Language.Class import Class
|
||||
from Models.Language.Func import Func
|
||||
from Models.Language.Lib import Lib
|
||||
|
||||
|
||||
class Parser:
|
||||
@@ -49,7 +49,7 @@ class Parser:
|
||||
for a_class in a_lib.ast:
|
||||
print(a_class.name, a_class.access)
|
||||
for a_funcs in a_class.ast:
|
||||
print(a_funcs.name, a_funcs.access)
|
||||
print(a_funcs.name, a_funcs.return_type, a_funcs.access)
|
||||
|
||||
print('___')
|
||||
# print(self.__repo.ast, '\n')
|
||||
@@ -85,7 +85,7 @@ class Parser:
|
||||
if not self.__is_scope_started() and tok.type == 'keyword':
|
||||
self.__check_keyword(tok)
|
||||
|
||||
elif not self.__is_scope_started() and tok.type == 'type' or tok.type in self.__repo.types:
|
||||
elif tok.type == 'type' or tok.type in self.__repo.types:
|
||||
pass
|
||||
|
||||
elif not self.__is_scope_started() and tok.type in self.__repo.var_types:
|
||||
@@ -128,12 +128,15 @@ class Parser:
|
||||
|
||||
def __check_format_char(self, tok: Token) -> None:
|
||||
if tok.value == '{':
|
||||
# token in storage must be name!
|
||||
if len(self.__token_storage) > 0 and self.__token_storage[0].type == 'name':
|
||||
# lib
|
||||
if self.__is_start_lib and not self.__is_start_class and not self.__is_start_func:
|
||||
self.__lib = Lib(self.__token_storage[0].value)
|
||||
self.__token_storage = []
|
||||
self.__is_start_lib = False
|
||||
|
||||
# class
|
||||
elif not self.__is_start_lib and self.__is_start_class and not self.__is_start_func:
|
||||
access = ''
|
||||
if self.__is_public:
|
||||
@@ -144,15 +147,20 @@ class Parser:
|
||||
self.__token_storage = []
|
||||
self.__is_start_class = False
|
||||
|
||||
# func
|
||||
elif not self.__is_start_lib and not self.__is_start_class and self.__is_start_func:
|
||||
access = ''
|
||||
if self.__is_public:
|
||||
access = 'public'
|
||||
self.__is_public = False
|
||||
|
||||
self.__func = Func(self.__token_storage[0].value, access=access)
|
||||
self.__token_storage = []
|
||||
self.__is_start_func = False
|
||||
if len(self.__token_storage) > 1 and self.__token_storage[1].type == 'type':
|
||||
if self.__is_public:
|
||||
access = 'public'
|
||||
self.__is_public = False
|
||||
|
||||
self.__func = Func(self.__token_storage[0].value, self.__token_storage[1].value, access=access)
|
||||
self.__token_storage = []
|
||||
self.__is_start_func = False
|
||||
else:
|
||||
self.__utils.error(Error(3.7))
|
||||
|
||||
else:
|
||||
self.__utils.error(Error(2.9, f'{tok.type}: {tok.value}'))
|
||||
@@ -179,6 +187,14 @@ class Parser:
|
||||
elif not self.__is_scope_started() and tok.value in self.__repo.format_chars:
|
||||
pass
|
||||
|
||||
elif tok.value == ':':
|
||||
if self.__is_only_func_started():
|
||||
next = self.__get_next_token()
|
||||
if next.type == 'type':
|
||||
self.__token_storage.append(next)
|
||||
else:
|
||||
self.__utils.error(Error(3.7))
|
||||
|
||||
else:
|
||||
self.__utils.error(Error(2.9, f'{tok.type}: {tok.value}'))
|
||||
|
||||
|
@@ -9,6 +9,7 @@ class Repo:
|
||||
'lib',
|
||||
'class',
|
||||
'func',
|
||||
'var',
|
||||
# builtin functions
|
||||
'output',
|
||||
'input',
|
||||
|
@@ -25,8 +25,7 @@ class Main:
|
||||
for line in f:
|
||||
self.__interpreter.interpret(line)
|
||||
else:
|
||||
self.__repo.error = Error(1.1, 'File not found')
|
||||
self.__utils.error()
|
||||
self.__utils.error(Error(1.1))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@@ -15,10 +15,6 @@ class Error:
|
||||
2.3: 'Unknown function',
|
||||
2.4: 'Unknown class',
|
||||
2.5: 'Unknown library',
|
||||
2.6: 'Access error: no export',
|
||||
2.7: 'Expression error',
|
||||
2.8: 'Unexpected end of line',
|
||||
2.9: 'Unexpected {}', # other types
|
||||
|
||||
# Parser:
|
||||
3.0: 'Lib in lib',
|
||||
@@ -28,6 +24,11 @@ class Error:
|
||||
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)
|
||||
|
@@ -1,6 +0,0 @@
|
||||
class Func:
|
||||
|
||||
def __init__(self, name: str, access: str = '') -> None:
|
||||
self.name = name
|
||||
self.ast = []
|
||||
self.access = access
|
@@ -2,5 +2,5 @@ class Class:
|
||||
|
||||
def __init__(self, name: str, access: str = '') -> None:
|
||||
self.name = name
|
||||
self.ast = []
|
||||
self.ast = [] # filled by parser
|
||||
self.access = access
|
7
src/Models/Language/Func.py
Normal file
7
src/Models/Language/Func.py
Normal file
@@ -0,0 +1,7 @@
|
||||
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
|
@@ -2,4 +2,4 @@ class Lib:
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
self.name = name
|
||||
self.ast = []
|
||||
self.ast = [] # filled by parser
|
6
src/Models/Language/Var.py
Normal file
6
src/Models/Language/Var.py
Normal file
@@ -0,0 +1,6 @@
|
||||
class Var:
|
||||
|
||||
def __init__(self, name: str, type: str, access: str = ''):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.access = access
|
Reference in New Issue
Block a user