alexandre.aboulin
2 years ago
commit
07d68718e2
2 changed files with 37 additions and 0 deletions
@ -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)) |
Loading…
Reference in new issue