diff --git a/expression.py b/expression.py index 0c330e3..186c43e 100644 --- a/expression.py +++ b/expression.py @@ -3,30 +3,30 @@ import re class Token: OPERAND_ADDITION = "+" OPERAND_MULTIPLICATION = "*" - NUMBER = re.compile(r"^\\d+$") #CHANGER + NUMBER = re.compile(r'\d+') #CHANGER class Expression: def __init__(self, val, gauche=None, droite=None): - if Token.NUMBER.match(str(val)) and (gauche != None or droite != None): + if Token.NUMBER.search(str(val)) is not None 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 self.droite = droite - def evalue(self, this): - #if Token.NUMBER.match(str(this.val)): - print(this.val) - # return int(this.val) - if this.val == Token.OPERAND_ADDITION: - return this.evalue(this.gauche) + this.evalue(this.droite) - elif this.val == Token.OPERAND_MULTIPLICATION: - return this.evalue(this.gauche) * this.evalue(this.droite) + def evalue(self): + if self.val == Token.OPERAND_ADDITION: + return self.gauche.evalue() + self.droite.evalue() + elif self.val == Token.OPERAND_MULTIPLICATION: + return self.gauche.evalue() * self.droite.evalue() else: - return int(this.val) + return int(self.val) - def __str__(): - pass + def __str__(self): + if Token.NUMBER.match(string=self.val) is not None: + return self.val + return "(" + self.gauche.__str__() + self.val + self.droite.__str__() + ")" + exp = Expression('*', Expression(6, None, None), @@ -34,4 +34,5 @@ Expression('+', Expression(4, None, None), Expression(3, None, None) )) -print(exp.evalue(exp)) \ No newline at end of file +print(exp.evalue()) +print(str(exp)) \ No newline at end of file