From 07d68718e2b61c6ed0dfc78295147dce360a6111 Mon Sep 17 00:00:00 2001 From: "alexandre.aboulin" Date: Wed, 25 Jan 2023 11:55:37 +0100 Subject: [PATCH] ahhh --- expression.py | 37 +++++++++++++++++++++++++++++++++++++ main.py | 0 2 files changed, 37 insertions(+) create mode 100644 expression.py create mode 100644 main.py diff --git a/expression.py b/expression.py new file mode 100644 index 0000000..0c330e3 --- /dev/null +++ b/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)) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..e69de29