|
@ -3,30 +3,30 @@ import re |
|
|
class Token: |
|
|
class Token: |
|
|
OPERAND_ADDITION = "+" |
|
|
OPERAND_ADDITION = "+" |
|
|
OPERAND_MULTIPLICATION = "*" |
|
|
OPERAND_MULTIPLICATION = "*" |
|
|
NUMBER = re.compile(r"^\\d+$") #CHANGER |
|
|
NUMBER = re.compile(r'\d+') #CHANGER |
|
|
|
|
|
|
|
|
class Expression: |
|
|
class Expression: |
|
|
|
|
|
|
|
|
def __init__(self, val, gauche=None, droite=None): |
|
|
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") |
|
|
raise AttributeError("gauche et droite ne peuvent pas exister si val est un nombre") |
|
|
self.val = val |
|
|
self.val = val |
|
|
self.gauche = gauche |
|
|
self.gauche = gauche |
|
|
self.droite = droite |
|
|
self.droite = droite |
|
|
|
|
|
|
|
|
def evalue(self, this): |
|
|
def evalue(self): |
|
|
#if Token.NUMBER.match(str(this.val)): |
|
|
if self.val == Token.OPERAND_ADDITION: |
|
|
print(this.val) |
|
|
return self.gauche.evalue() + self.droite.evalue() |
|
|
# return int(this.val) |
|
|
elif self.val == Token.OPERAND_MULTIPLICATION: |
|
|
if this.val == Token.OPERAND_ADDITION: |
|
|
return self.gauche.evalue() * self.droite.evalue() |
|
|
return this.evalue(this.gauche) + this.evalue(this.droite) |
|
|
|
|
|
elif this.val == Token.OPERAND_MULTIPLICATION: |
|
|
|
|
|
return this.evalue(this.gauche) * this.evalue(this.droite) |
|
|
|
|
|
else: |
|
|
else: |
|
|
return int(this.val) |
|
|
return int(self.val) |
|
|
|
|
|
|
|
|
|
|
|
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__() + ")" |
|
|
|
|
|
|
|
|
def __str__(): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
exp = Expression('*', |
|
|
exp = Expression('*', |
|
|
Expression(6, None, None), |
|
|
Expression(6, None, None), |
|
@ -34,4 +34,5 @@ Expression('+', |
|
|
Expression(4, None, None), |
|
|
Expression(4, None, None), |
|
|
Expression(3, None, None) |
|
|
Expression(3, None, None) |
|
|
)) |
|
|
)) |
|
|
print(exp.evalue(exp)) |
|
|
print(exp.evalue()) |
|
|
|
|
|
print(str(exp)) |