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 {
class Program {
func Main(args: list): void {
test_a = test1();
var test_a: test1 = test1();
test_a.dec_vars();
test_a.is_error();
if (!error) {
test_b = test3();
var test_b: test3 = test3();
test3.continue();
}
}

View File

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

View File

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

View File

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

View File

@ -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 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, access=access)
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}'))

View File

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

View File

@ -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__':

View File

@ -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)

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:
self.name = name
self.ast = []
self.ast = [] # filled by parser
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:
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