From d1248d271c49678fb5f82e6fb396f70c734b9b76 Mon Sep 17 00:00:00 2001 From: Kalyax Date: Wed, 25 Jan 2023 22:25:35 +0100 Subject: [PATCH] ???????? --- infixe.py | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/infixe.py b/infixe.py index 2739d86..4a06ed6 100644 --- a/infixe.py +++ b/infixe.py @@ -49,6 +49,8 @@ def tokenize(text): tokens.append(buffer) buffer = "" tokens.append(char) + if buffer != "": + tokens.append(buffer) return tokens def arbre(tokens): #-> Expression @@ -59,7 +61,7 @@ def arbre(tokens): #-> Expression ##Brackets loop i = 0 - while i != len(tokens): + while i < len(tokens): if tokens[i] == "(": j = i + 1 innerBrackets = 1 @@ -72,24 +74,54 @@ def arbre(tokens): #-> Expression innerTokens.append(tokens[j]) j += 1 exp = arbre(innerTokens) + tokens = tokens[:i] + [" "] + tokens[j+1:] + #i -= j+1 if waiting_for_after_exp != None: - waiting_for_after_exp.droite == exp + waiting_for_after_exp.droite = exp + waiting_before_exp = waiting_for_after_exp + waiting_for_after_exp = None else: waiting_before_exp = exp + elif tokens[i] == "*": exp = Expression("*") if Token.isNumber(tokens[i-1]): - exp.gauche == tokens[i-1] + exp.gauche = tokens[i-1] if Token.isNumber(tokens[i+1]): - exp.droite == tokens[i+1] + exp.droite = tokens[i+1] if Token.isNumber(tokens[i-1]) and Token.isNumber(tokens[i+1]): return exp if tokens[i-1] == ")": - exp.gauche == waiting_before_exp + exp.gauche = waiting_before_exp waiting_before_exp = None elif tokens[i+1] == "(": waiting_for_after_exp = exp + print(exp) + elif tokens[i-1] == " ": + exp.gauche = waiting_before_exp + waiting_for_after_exp = exp + else: + raise Exception() + + elif tokens[i] == "+": + exp = Expression("+") + if Token.isNumber(tokens[i-1]): + exp.gauche = tokens[i-1] + if Token.isNumber(tokens[i+1]): + exp.droite = tokens[i+1] + if Token.isNumber(tokens[i-1]) and Token.isNumber(tokens[i+1]): + return exp + + if tokens[i-1] == ")": + exp.gauche = waiting_before_exp + waiting_before_exp = None + elif tokens[i+1] == "(": + waiting_for_after_exp = exp + print(exp) + elif tokens[i-1] == " ": + exp.gauche = waiting_before_exp + waiting_for_after_exp = exp else: raise Exception() @@ -115,4 +147,4 @@ def arbre(tokens): #-> Expression if __name__ == "__main__": - print(arbre(tokenize("4*(7*4)"))) + print(arbre(tokenize("(2*8)*4+(7*4)*7")))