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.
23 lines
878 B
23 lines
878 B
from lexer import Token
|
|
|
|
class Expression:
|
|
|
|
def __init__(self, val, gauche=None, 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):
|
|
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(self.val)
|
|
|
|
def __str__(self):
|
|
if Token.NUMBER.match(str(self.val)) != None:
|
|
return str(self.val)
|
|
return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")"
|
|
|