You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
2 years ago
|
import re
|
||
|
|
||
|
class Token:
|
||
|
OPERAND_ADDITION = "+"
|
||
|
OPERAND_MULTIPLICATION = "*"
|
||
|
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):
|
||
|
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)
|
||
|
else:
|
||
|
return int(this.val)
|
||
|
|
||
|
def __str__():
|
||
|
pass
|
||
|
|
||
|
exp = Expression('*',
|
||
|
Expression(6, None, None),
|
||
|
Expression('+',
|
||
|
Expression(4, None, None),
|
||
|
Expression(3, None, None)
|
||
|
))
|
||
|
print(exp.evalue(exp))
|