Browse Source

ça avance

master
Kalyax 2 years ago
parent
commit
756608deae
  1. 1
      .gitignore
  2. 22
      expression.py
  3. 118
      infixe.py
  4. 6
      lexer.py

1
.gitignore

@ -0,0 +1 @@
__pycache__/

22
expression.py

@ -1,23 +1 @@
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__()) + ")"

118
infixe.py

@ -0,0 +1,118 @@
import re
class Expression:
def __init__(self, val=None, gauche=None, droite=None):
if Token.isNumber(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):
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.isNumber(self.val):
return str(self.val)
return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")"
class Token:
ADDITION = "+"
MULTIPLICATION = "*"
PARENTHESE_OUVERTE = "("
PARENTHESE_FERME = ")"
NUMBER = re.compile(r'\d+')
@staticmethod
def isNumber(n):
return Token.NUMBER.match(str(n)) is not None
def tokenize(text):
buffer = ""
tokens = []
for char in text:
if Token.isNumber(char):
buffer += char
continue
elif char == " ":
continue
if buffer != "":
tokens.append(buffer)
buffer = ""
tokens.append(char)
return tokens
def arbre(tokens): #-> Expression
print(tokens)
current = None
waiting_before_exp = None
waiting_for_after_exp = None
##Brackets loop
i = 0
while i != len(tokens):
if tokens[i] == "(":
j = i + 1
innerBrackets = 1
innerTokens = []
while tokens[j] != ")" and innerBrackets != 0:
if tokens[j] == "(":
innerBrackets += 1
elif tokens[j] == ")":
innerBrackets -= 1
innerTokens.append(tokens[j])
j += 1
exp = arbre(innerTokens)
if waiting_for_after_exp != None:
waiting_for_after_exp.droite == exp
else:
waiting_before_exp = exp
elif tokens[i] == "*":
exp = Expression("*")
if Token.isNumber(tokens[i-1]):
exp.gauche == tokens[i-1]
if Token.isNumber(tokens[i+1]):
exp.droite == tokens[i+1]
if Token.isNumber(tokens[i-1]) and Token.isNumber(tokens[i+1]):
return exp
if tokens[i-1] == ")":
exp.gauche == waiting_before_exp
waiting_before_exp = None
elif tokens[i+1] == "(":
waiting_for_after_exp = exp
else:
raise Exception()
i += 1
##Multiplication loop
#i = 0
#while i != len(tokens):
# if tokens[i] == "*":
# print("at")
# return Expression("*", tokens[i-1], tokens[i+1])
# i += 1
##Addition loop
#i = 0
#while i != len(tokens):
# if tokens[i] == "+":
# return Expression("+", tokens[i-1], tokens[i+1])
# i += 1
return waiting_for_after_exp
if __name__ == "__main__":
print(arbre(tokenize("4*(7*4)")))

6
lexer.py

@ -1,6 +0,0 @@
import re
class Token:
OPERAND_ADDITION = "+"
OPERAND_MULTIPLICATION = "*"
NUMBER = re.compile(r'\d+') #CHANGER
Loading…
Cancel
Save