Browse Source

ahhh

master
alexandre.aboulin 2 years ago
commit
07d68718e2
  1. 37
      expression.py
  2. 0
      main.py

37
expression.py

@ -0,0 +1,37 @@
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))

0
main.py

Loading…
Cancel
Save