added return type to function

This commit is contained in:
Sven Heidemann 2020-05-25 16:36:44 +02:00
parent 38353246b2
commit b9e938f8a1
15 changed files with 74 additions and 48 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
/.idea/

View File

@ -4,11 +4,11 @@ use test2 as test3 from Tests;
lib Main { lib Main {
class Program { class Program {
func Main(args: list): void { func Main(args: list): void {
test_a = test1(); var test_a: test1 = test1();
test_a.dec_vars(); test_a.dec_vars();
test_a.is_error(); test_a.is_error();
if (!error) { if (!error) {
test_b = test3(); var test_b: test3 = test3();
test3.continue(); test3.continue();
} }
} }

View File

@ -3,22 +3,22 @@ lib Tests
/* /*
declaration of some tests declaration of some tests
*/ */
export class test1 public class test1
{ {
export test_string: string = 'Hello'; public var test_string: string = 'Hello';
export test_string_2: string = "Hello World"; public var test_string_2: string = "Hello World";
export test_num: num = 1; public var test_num: num = 1;
export test_num_2: num = 1.0; public var test_num_2: num = 1.0;
export test_num_3: num = this.test_num + this.test_num_2; public var test_num_3: num = this.test_num + this.test_num_2;
export func dec_vars(): void public func dec_vars(): void
{ {
test_bool: bool = true; var test_bool: bool = true;
test_bool_2: bool = false; var test_bool_2: bool = false;
test_bool_3: bool = test_bool != test_bool_2; # true var test_bool_3: bool = test_bool != test_bool_2; # true
} }
export is_error(): bool public is_error(): bool
{ {
if (error != empty) if (error != empty)
{ {

View File

@ -1,12 +1,12 @@
lib Tests { lib Tests {
export class test2 { public class test2 {
string_a = string1(); string_a = string1();
export func continue() { public func continue() {
input(string_a.string1 + ': '); input(string_a.string1 + ': ');
} }
} }
class strings { class strings {
public string1 = "hello world"; var public string1 = "hello world";
} }
} }

View File

@ -6,15 +6,15 @@
lib Main { lib Main {
class Program { class Program {
func Main() { func Main(): void {
testBool: bool; var testBool: bool;
testEmpty: emptyType = empty; var testEmpty: emptyType = empty;
testNum: number = 3.0; var testNum: number = 3.0;
testBool_2: bool = 3 > 1; var testBool_2: bool = 3 > 1;
output('Hello World'); output('Hello World');
output(66); output(66);
output(3 + 3); output(3 + 3);
test: string = input('# '); var test: string = input('# ');
output(test); output(test);
output(false); output(false);
@ -25,7 +25,7 @@ lib Main {
} }
# public func test1234(param: list) { # public func test1234(param: list) {
public func test1234() public func test1234(): void
{ {
/*for i in range(0, length(param)) { /*for i in range(0, length(param)) {
output(i); output(i);

View File

@ -2,9 +2,9 @@ from Interpreter.Repo import Repo
from Interpreter.Utils import Utils from Interpreter.Utils import Utils
from Models.Interpreter.Error import Error from Models.Interpreter.Error import Error
from Models.Interpreter.Token import Token from Models.Interpreter.Token import Token
from Models.Language.AST.Class import Class from Models.Language.Class import Class
from Models.Language.AST.Func import Func from Models.Language.Func import Func
from Models.Language.AST.Lib import Lib from Models.Language.Lib import Lib
class Parser: class Parser:
@ -49,7 +49,7 @@ class Parser:
for a_class in a_lib.ast: for a_class in a_lib.ast:
print(a_class.name, a_class.access) print(a_class.name, a_class.access)
for a_funcs in a_class.ast: 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('___')
# print(self.__repo.ast, '\n') # print(self.__repo.ast, '\n')
@ -85,7 +85,7 @@ class Parser:
if not self.__is_scope_started() and tok.type == 'keyword': if not self.__is_scope_started() and tok.type == 'keyword':
self.__check_keyword(tok) 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 pass
elif not self.__is_scope_started() and tok.type in self.__repo.var_types: 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: def __check_format_char(self, tok: Token) -> None:
if tok.value == '{': if tok.value == '{':
# token in storage must be name!
if len(self.__token_storage) > 0 and self.__token_storage[0].type == '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: 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.__lib = Lib(self.__token_storage[0].value)
self.__token_storage = [] self.__token_storage = []
self.__is_start_lib = False self.__is_start_lib = False
# class
elif not self.__is_start_lib and self.__is_start_class and not self.__is_start_func: elif not self.__is_start_lib and self.__is_start_class and not self.__is_start_func:
access = '' access = ''
if self.__is_public: if self.__is_public:
@ -144,15 +147,20 @@ class Parser:
self.__token_storage = [] self.__token_storage = []
self.__is_start_class = False self.__is_start_class = False
# func
elif not self.__is_start_lib and not self.__is_start_class and self.__is_start_func: elif not self.__is_start_lib and not self.__is_start_class and self.__is_start_func:
access = '' access = ''
if self.__is_public:
access = 'public'
self.__is_public = False
self.__func = Func(self.__token_storage[0].value, access=access) if len(self.__token_storage) > 1 and self.__token_storage[1].type == 'type':
self.__token_storage = [] if self.__is_public:
self.__is_start_func = False 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: else:
self.__utils.error(Error(2.9, f'{tok.type}: {tok.value}')) 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: elif not self.__is_scope_started() and tok.value in self.__repo.format_chars:
pass 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: else:
self.__utils.error(Error(2.9, f'{tok.type}: {tok.value}')) self.__utils.error(Error(2.9, f'{tok.type}: {tok.value}'))

View File

@ -9,6 +9,7 @@ class Repo:
'lib', 'lib',
'class', 'class',
'func', 'func',
'var',
# builtin functions # builtin functions
'output', 'output',
'input', 'input',

View File

@ -25,8 +25,7 @@ class Main:
for line in f: for line in f:
self.__interpreter.interpret(line) self.__interpreter.interpret(line)
else: else:
self.__repo.error = Error(1.1, 'File not found') self.__utils.error(Error(1.1))
self.__utils.error()
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -15,10 +15,6 @@ class Error:
2.3: 'Unknown function', 2.3: 'Unknown function',
2.4: 'Unknown class', 2.4: 'Unknown class',
2.5: 'Unknown library', 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: # Parser:
3.0: 'Lib in lib', 3.0: 'Lib in lib',
@ -28,6 +24,11 @@ class Error:
3.4: 'Class in func', 3.4: 'Class in func',
3.5: 'Func in lib', 3.5: 'Func in lib',
3.6: 'Func in func', 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 = self.__msgs[code].format(msg)

View File

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

View File

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

View 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

View File

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

View 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