Browse Source

changement organisation

master
alexandre.aboulin 2 years ago
parent
commit
0e0f4b5c6a
  1. 37
      misc/expression.py
  2. 6
      misc/parser.py
  3. 19
      misc/tokenizer.py
  4. 15
      misc/util.py

37
misc/expression.py

@ -1,10 +1,10 @@
import re
from math import cos, sin, exp, sqrt from math import cos, sin, exp, sqrt
from misc.util import isNumber
class Expression: class Expression:
def __init__(self, val=None, gauche=None, droite=None): def __init__(self, val=None, gauche=None, droite=None):
if Token.isNumber(val) and (gauche != None or droite != None): if isNumber(val) and (gauche != None or droite != None):
raise AttributeError("gauche et droite ne peuvent pas exister si val est un nombre") raise AttributeError("gauche et droite ne peuvent pas exister si val est un nombre")
self.val = val self.val = val
self.gauche = gauche self.gauche = gauche
@ -34,37 +34,6 @@ class Expression:
return float(self.val) return float(self.val)
def __str__(self): def __str__(self):
if Token.isNumber(self.val): if isNumber(self.val):
return str(self.val) return str(self.val)
return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")" return "(" + str(self.gauche.__str__()) + str(self.val) + str(self.droite.__str__()) + ")"
class Token:
NUMBER = 0
OPERATOR = 1
PARENTHESE = 2
FUNCTION = 3
@staticmethod
def isChar(n):
return re.match(r'[a-z]', str(n)) is not None
@staticmethod
def isNumber(n):
return re.match(r'\d+', str(n)) is not None
@staticmethod
def getPrecedence(tok):
if tok == "(" or tok == "" or tok == "^":
return 4
elif tok == "*" or tok == "/":
return 3
elif tok == "+" or tok == "-":
return 2
def __init__(self, type, val):
self.type = type
self.val = val
def __str__(self):
return self.val

6
misc/parser.py

@ -1,6 +1,8 @@
from misc.expression import Token, Expression from misc.expression import Expression
from misc.tokenizer import Token
from lib.Pile import Pile_chaine as Pile from lib.Pile import Pile_chaine as Pile
from lib.File import File_chaine as File from lib.File import File_chaine as File
from misc.util import getPrecedence
def shutting_yard(tokens: list) -> File: def shutting_yard(tokens: list) -> File:
output = File() output = File()
@ -11,7 +13,7 @@ def shutting_yard(tokens: list) -> File:
elif token.type == Token.FUNCTION: elif token.type == Token.FUNCTION:
opertator.empiler(token) opertator.empiler(token)
elif token.type == Token.OPERATOR: elif token.type == Token.OPERATOR:
while (not opertator.est_vide() and opertator.sommet().val != "(") and (Token.getPrecedence(opertator.sommet().val) > Token.getPrecedence(token.val) or (Token.getPrecedence(token.val) == Token.getPrecedence(opertator.sommet().val) and token.val != "^")): while (not opertator.est_vide() and opertator.sommet().val != "(") and (getPrecedence(opertator.sommet().val) > getPrecedence(token.val) or (getPrecedence(token.val) == getPrecedence(opertator.sommet().val) and token.val != "^")):
output.enfiler(opertator.depiler()) output.enfiler(opertator.depiler())
opertator.empiler(token) opertator.empiler(token)
elif token.val == "(": elif token.val == "(":

19
misc/tokenizer.py

@ -1,13 +1,13 @@
from misc.expression import Token from misc.util import isChar, isNumber
def tokenize(text: str) -> list: def tokenize(text: str) -> list:
buffer_num = "" buffer_num = ""
buffer_func = "" buffer_func = ""
tokens = [] tokens = []
for char in text: for char in text:
if Token.isChar(char): if isChar(char):
buffer_func += char buffer_func += char
elif Token.isNumber(char) or char == ".": elif isNumber(char) or char == ".":
buffer_num += char buffer_num += char
else: else:
if buffer_num != "": if buffer_num != "":
@ -25,3 +25,16 @@ def tokenize(text: str) -> list:
if buffer_func != "": if buffer_func != "":
tokens.append(Token(Token.FUNCTION, buffer_func)) tokens.append(Token(Token.FUNCTION, buffer_func))
return tokens return tokens
class Token:
NUMBER = 0
OPERATOR = 1
PARENTHESE = 2
FUNCTION = 3
def __init__(self, type, val):
self.type = type
self.val = val
def __str__(self):
return self.val

15
misc/util.py

@ -0,0 +1,15 @@
import re
def isChar(n):
return re.match(r'[a-z]', str(n)) is not None
def isNumber(n):
return re.match(r'\d+', str(n)) is not None
def getPrecedence(tok):
if tok == "(" or tok == "" or tok == "^":
return 4
elif tok == "*" or tok == "/":
return 3
elif tok == "+" or tok == "-":
return 2
Loading…
Cancel
Save