From d66ea29d8fa208e5f14d1bd35d80f44cff732405 Mon Sep 17 00:00:00 2001 From: edraft Date: Sat, 23 May 2020 13:40:51 +0200 Subject: [PATCH] bugfix in lexer --- doc/test.bl | 9 ++++---- src/Interpreter/Lexer.py | 31 +++++++++++++-------------- src/Interpreter/Parser.py | 4 +++- src/Interpreter/Repo.py | 21 ++++++++++++------ src/Main.py | 2 +- src/Models/Class.py | 6 ------ src/Models/Func.py | 6 ------ src/Models/{ => Interpreter}/Token.py | 0 src/Models/Interpreter/__init__.py | 0 src/Models/{ => Language}/Error.py | 0 src/Models/Language/__init__.py | 0 src/Models/Lib.py | 5 ----- src/Models/Var.py | 6 ------ src/test.py | 3 +++ 14 files changed, 42 insertions(+), 51 deletions(-) delete mode 100644 src/Models/Class.py delete mode 100644 src/Models/Func.py rename src/Models/{ => Interpreter}/Token.py (100%) create mode 100644 src/Models/Interpreter/__init__.py rename src/Models/{ => Language}/Error.py (100%) create mode 100644 src/Models/Language/__init__.py delete mode 100644 src/Models/Lib.py delete mode 100644 src/Models/Var.py create mode 100644 src/test.py diff --git a/doc/test.bl b/doc/test.bl index d69fca2..fcb0d50 100644 --- a/doc/test.bl +++ b/doc/test.bl @@ -16,16 +16,17 @@ lib Main { output(test); output(false); - if (testBool != empty) { + /*if (testBool != empty) { output(testEmpty); - } + }*/ test1234(range(0, 10)); } public func test1234(param: list) { - for i in range(0, length(param)) { + /*for i in range(0, length(param)) { output(i); - } + }*/ + pass; } } } \ No newline at end of file diff --git a/src/Interpreter/Lexer.py b/src/Interpreter/Lexer.py index afe7db2..d4255db 100644 --- a/src/Interpreter/Lexer.py +++ b/src/Interpreter/Lexer.py @@ -1,6 +1,6 @@ from Interpreter.Repo import Repo from Interpreter.Utils import Utils -from Models.Token import Token +from Models.Interpreter.Token import Token class Lexer: @@ -45,30 +45,29 @@ class Lexer: c = line[i] # ignore comments and spaces if not ol_comment and not self.__ml_comment: + # comment filtering + if c == '#' and not is_string1 and not is_string2: + ol_comment = True + + elif line[i-1] == '/' and c == '/': + ol_comment = True + + elif line[i-1] == '/' and c == '*': + self.__ml_comment = True + i += 2 # end of number - if not c.isdigit() and is_number: + elif not c.isdigit() and is_number: self.__add_tok(word, 'number') word = '' is_number = False # end of expression char - if c not in self.__repo.expr_chars and is_expr_char: + elif c not in self.__repo.expr_chars and is_expr_char: self.__add_tok(word, 'expr_char') word = '' is_expr_char = False - # comment filtering - if c == '#' and not is_string1 and not is_string2: - ol_comment = True - - elif c == '/' and line[+1] == '/': - ol_comment = True - - elif c == '/' and line[+1] == '*': - self.__ml_comment = True - i += 2 - # begin of is_string1 elif c == '\'' and not is_string1: is_string1 = True @@ -132,8 +131,8 @@ class Lexer: if c == '\n' and ol_comment: ol_comment = False - if c == '*' and line[i + 1] == '/': + if line[i - 1] == '*' and c == '/': self.__ml_comment = False - # self.__repo.output_tokens(self.__toks) + self.__repo.output_tokens(self.__toks) return self.__toks diff --git a/src/Interpreter/Parser.py b/src/Interpreter/Parser.py index 8e10243..f765773 100644 --- a/src/Interpreter/Parser.py +++ b/src/Interpreter/Parser.py @@ -1,5 +1,6 @@ from Interpreter.Repo import Repo from Interpreter.Utils import Utils +from Models.Interpreter.Token import Token class Parser: @@ -9,4 +10,5 @@ class Parser: self.__utils = utils def parse(self, toks: list) -> None: - self.__repo.output_tokens(toks) + # self.__repo.output_tokens(toks) + pass diff --git a/src/Interpreter/Repo.py b/src/Interpreter/Repo.py index 2975356..87a3c46 100644 --- a/src/Interpreter/Repo.py +++ b/src/Interpreter/Repo.py @@ -27,6 +27,7 @@ class Repo: 'public' ] self.types = [ + 'any', 'number', 'string', 'bool', @@ -35,7 +36,15 @@ class Repo: 'emptyType', 'void' ] - self.expr_chars = ['+', '-', '*', '/', '='] + self.var_types = [ + 'any', + 'number', + 'string', + 'bool', + 'list', + 'dict' + ] + self.expr_chars = ['+', '-', '*', '/', '=', '^'] self.bool_expr_chars = ['<', '>', '!', '!=', '==', '>=', '<='] self.bool_values = ['true', 'false'] self.format_chars = ['{', '}', '(', ')', ';', ':', ','] @@ -45,10 +54,10 @@ class Repo: def output_tokens(self, toks: list) -> None: if self.debug and len(toks) > 0: - # outp_toks = [] + outp_toks = [] for tok in toks: - # outp_toks.append({tok.value: tok.type}) - print({tok.value: tok.type}) + outp_toks.append({tok.value: tok.type}) + # print({tok.value: tok.type}) - # print(outp_toks) - print('\n') + print(outp_toks) + # print('\n') diff --git a/src/Main.py b/src/Main.py index c578749..4743c1b 100644 --- a/src/Main.py +++ b/src/Main.py @@ -1,7 +1,7 @@ import os import sys -from Models.Error import Error +from Models.Language.Error import Error from ServiceInitializer import ServiceInitializer diff --git a/src/Models/Class.py b/src/Models/Class.py deleted file mode 100644 index 95eb18f..0000000 --- a/src/Models/Class.py +++ /dev/null @@ -1,6 +0,0 @@ -class Class: - - def __init__(self, name: str, ast: list, access: ''): - self.name = name - self.ast = ast - self.access = access diff --git a/src/Models/Func.py b/src/Models/Func.py deleted file mode 100644 index 38388d6..0000000 --- a/src/Models/Func.py +++ /dev/null @@ -1,6 +0,0 @@ -class Func: - - def __init__(self, name: str, ast: list, access='') -> None: - self.name = name - self.ast = ast - self.access = access diff --git a/src/Models/Token.py b/src/Models/Interpreter/Token.py similarity index 100% rename from src/Models/Token.py rename to src/Models/Interpreter/Token.py diff --git a/src/Models/Interpreter/__init__.py b/src/Models/Interpreter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/Models/Error.py b/src/Models/Language/Error.py similarity index 100% rename from src/Models/Error.py rename to src/Models/Language/Error.py diff --git a/src/Models/Language/__init__.py b/src/Models/Language/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/Models/Lib.py b/src/Models/Lib.py deleted file mode 100644 index 776338a..0000000 --- a/src/Models/Lib.py +++ /dev/null @@ -1,5 +0,0 @@ -class Lib: - - def __init__(self, name: str, ast: list): - self.name = name - self.ast = ast diff --git a/src/Models/Var.py b/src/Models/Var.py deleted file mode 100644 index 9eab3ee..0000000 --- a/src/Models/Var.py +++ /dev/null @@ -1,6 +0,0 @@ -class Var: - - def __init__(self, name: str, value: str, type: str) -> None: - self.name = name - self.value = value - self.type = type diff --git a/src/test.py b/src/test.py new file mode 100644 index 0000000..3f3e514 --- /dev/null +++ b/src/test.py @@ -0,0 +1,3 @@ +test = 3 ** 2 +print(test) +print(eval('3**2')) \ No newline at end of file