From 0e0f4b5c6a6c05c83155bcd7fc35daf34734a5c8 Mon Sep 17 00:00:00 2001 From: "alexandre.aboulin" Date: Wed, 22 Feb 2023 13:43:31 +0100 Subject: [PATCH] changement organisation --- misc/expression.py | 37 +++---------------------------------- misc/parser.py | 6 ++++-- misc/tokenizer.py | 21 +++++++++++++++++---- misc/util.py | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 40 deletions(-) create mode 100644 misc/util.py diff --git a/misc/expression.py b/misc/expression.py index 9ce2e8f..6050eb2 100644 --- a/misc/expression.py +++ b/misc/expression.py @@ -1,10 +1,10 @@ -import re from math import cos, sin, exp, sqrt +from misc.util import isNumber class Expression: def __init__(self, val=None, gauche=None, droite=None): - if Token.isNumber(val) and (gauche != None or droite != None): + if isNumber(val) and (gauche != None or droite != None): raise AttributeError("gauche et droite ne peuvent pas exister si val est un nombre") self.val = val self.gauche = gauche @@ -34,37 +34,6 @@ class Expression: return float(self.val) def __str__(self): - if Token.isNumber(self.val): + if isNumber(self.val): return str(self.val) return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")" - - -class Token: - NUMBER = 0 - OPERATOR = 1 - PARENTHESE = 2 - FUNCTION = 3 - - @staticmethod - def isChar(n): - return re.match(r'[a-z]', str(n)) is not None - - @staticmethod - def isNumber(n): - return re.match(r'\d+', str(n)) is not None - - @staticmethod - def getPrecedence(tok): - if tok == "(" or tok == "" or tok == "^": - return 4 - elif tok == "*" or tok == "/": - return 3 - elif tok == "+" or tok == "-": - return 2 - - def __init__(self, type, val): - self.type = type - self.val = val - - def __str__(self): - return self.val \ No newline at end of file diff --git a/misc/parser.py b/misc/parser.py index 4365274..638e094 100644 --- a/misc/parser.py +++ b/misc/parser.py @@ -1,6 +1,8 @@ -from misc.expression import Token, Expression +from misc.expression import Expression +from misc.tokenizer import Token from lib.Pile import Pile_chaine as Pile from lib.File import File_chaine as File +from misc.util import getPrecedence def shutting_yard(tokens: list) -> File: output = File() @@ -11,7 +13,7 @@ def shutting_yard(tokens: list) -> File: elif token.type == Token.FUNCTION: opertator.empiler(token) elif token.type == Token.OPERATOR: - while (not opertator.est_vide() and opertator.sommet().val != "(") and (Token.getPrecedence(opertator.sommet().val) > Token.getPrecedence(token.val) or (Token.getPrecedence(token.val) == Token.getPrecedence(opertator.sommet().val) and token.val != "^")): + while (not opertator.est_vide() and opertator.sommet().val != "(") and (getPrecedence(opertator.sommet().val) > getPrecedence(token.val) or (getPrecedence(token.val) == getPrecedence(opertator.sommet().val) and token.val != "^")): output.enfiler(opertator.depiler()) opertator.empiler(token) elif token.val == "(": diff --git a/misc/tokenizer.py b/misc/tokenizer.py index 818dc48..f412348 100644 --- a/misc/tokenizer.py +++ b/misc/tokenizer.py @@ -1,13 +1,13 @@ -from misc.expression import Token +from misc.util import isChar, isNumber def tokenize(text: str) -> list: buffer_num = "" buffer_func = "" tokens = [] for char in text: - if Token.isChar(char): + if isChar(char): buffer_func += char - elif Token.isNumber(char) or char == ".": + elif isNumber(char) or char == ".": buffer_num += char else: if buffer_num != "": @@ -24,4 +24,17 @@ def tokenize(text: str) -> list: tokens.append(Token(Token.NUMBER, buffer_num)) if buffer_func != "": tokens.append(Token(Token.FUNCTION, buffer_func)) - return tokens \ No newline at end of file + return tokens + +class Token: + NUMBER = 0 + OPERATOR = 1 + PARENTHESE = 2 + FUNCTION = 3 + + def __init__(self, type, val): + self.type = type + self.val = val + + def __str__(self): + return self.val \ No newline at end of file diff --git a/misc/util.py b/misc/util.py new file mode 100644 index 0000000..9d8601c --- /dev/null +++ b/misc/util.py @@ -0,0 +1,15 @@ +import re + +def isChar(n): + return re.match(r'[a-z]', str(n)) is not None + +def isNumber(n): + return re.match(r'\d+', str(n)) is not None + +def getPrecedence(tok): + if tok == "(" or tok == "" or tok == "^": + return 4 + elif tok == "*" or tok == "/": + return 3 + elif tok == "+" or tok == "-": + return 2 \ No newline at end of file