bug fixy i interfejs
This commit is contained in:
parent
cd8d88dd98
commit
7e03374867
75
program.py
75
program.py
@ -25,6 +25,8 @@ class SimpleInterpreter(AbstractInterpreter):
|
||||
def isNegation(char):
|
||||
return char == '!'
|
||||
#print(vars(formula))
|
||||
if isSign(formula.s[-1]) and not isParentice(formula.s[-1]): #obrona przed a> na końcu
|
||||
return False
|
||||
for i,char in enumerate(formula.s):
|
||||
if isBlacklisted(char): return False
|
||||
|
||||
@ -53,7 +55,6 @@ class SimpleInterpreter(AbstractInterpreter):
|
||||
elif not isSign(char) and prev is not None:
|
||||
#if current char is a variable
|
||||
if isVariable(prev) and prev is not None: #Obrona abc>d
|
||||
print("previous is also a variable");
|
||||
return False
|
||||
return True
|
||||
class RegexInterpreter(AbstractInterpreter):
|
||||
@ -82,9 +83,10 @@ class RegexInterpreter(AbstractInterpreter):
|
||||
if not isValid: return False
|
||||
|
||||
#assertions
|
||||
assertions = ['(?:[>|&|\|=][\(\)]*){2,}','\((?:[>|&|\|=][\(\)]*)','[a-z]{2,}','\((?:[>|&|\|=|!])?\)']
|
||||
assertions = ['(?:[>|&|\|=][\(\)]*){2,}','\((?:[>|&|\|=][\(\)]*)','[a-z]{2,}','\((?:[>|&|\|=|!])?\)', '[>\|&=](?:[^a-z\(]|$)','[^>&\|=](\()' ]
|
||||
for assertion in assertions:
|
||||
if not check_assertion(formula,assertion):
|
||||
#print(assertion)
|
||||
return False
|
||||
|
||||
return True
|
||||
@ -95,7 +97,8 @@ class RegexInterpreter(AbstractInterpreter):
|
||||
#najpierw sprawdzamy, czy parentices są ok
|
||||
#^(\((?:[^()]++|(?1))*\))$ - powinno zwrócić cały nasz string
|
||||
#potem sprawdzamy czy dopasuje cokolwiek co może być nie tak
|
||||
|
||||
# [>\|&=](?:[^a-z\(]|$) - operator przed czymś innym niż nawias otwierający lub zmienna
|
||||
# [^>&\|=](\() - zmienna przed nawiasem bez operatora pomiędzy
|
||||
# \((?:[>|&|\|=][\(\)]*) - (>b)
|
||||
# [a-z]{2,} - czy powtarzają się jakieś lierki obok siebie?
|
||||
# \((?:[>|&|\|=|!])?\) - czy istnieją puste nawiasy, bądź nawiasy z samym operatorem?
|
||||
@ -190,26 +193,60 @@ class Formula:
|
||||
def isValid(self, interpreter=None)->bool:
|
||||
i = self.__i if interpreter is None else interpreter
|
||||
return i.isValid(self)
|
||||
def print_help():
|
||||
print("""
|
||||
Użycie:
|
||||
python program.py -f plik.txt
|
||||
python program.py a&b>c|(a&!g
|
||||
python program.py
|
||||
python program.py -h
|
||||
""")
|
||||
|
||||
sim = SimpleInterpreter()
|
||||
if len(sys.argv) > 1 :
|
||||
i = ProperInterpreter()
|
||||
if '-g' in sys.argv:
|
||||
i = RegexInterpreter()
|
||||
#print(sys.argv)
|
||||
if '-h' in sys.argv:
|
||||
print_help()
|
||||
else:
|
||||
if '-g' in sys.argv:
|
||||
i = RegexInterpreter()
|
||||
#print(sys.argv)
|
||||
|
||||
if '-f' in sys.argv:
|
||||
#open from file
|
||||
print("opening from file")
|
||||
g = open(sys.argv[sys.argv.index('-f')+1], 'r')
|
||||
u = g.readlines()
|
||||
for l in u:
|
||||
print(Formula(l.replace("\n",'').replace("\r",''),i).isValid())
|
||||
else:
|
||||
print(
|
||||
Formula(sys.argv[1] if sys.argv[1] != '-g' else sys.argv[2],i).isValid()
|
||||
)
|
||||
|
||||
if '-f' in sys.argv:
|
||||
#open from file
|
||||
print("opening from file")
|
||||
g = open(sys.argv[sys.argv.index('-f')+1], 'r')
|
||||
u = g.readlines()
|
||||
for l in u:
|
||||
print(Formula(l.replace("\n",'').replace("\r",''),i).isValid())
|
||||
else:
|
||||
o = sys.argv[1] if sys.argv[1] != '-g' else sys.argv[2]
|
||||
if len(o)>0:
|
||||
print(Formula(o,i).isValid())
|
||||
|
||||
else:
|
||||
print("help?")
|
||||
print_help()
|
||||
print("Tryb interaktywny:")
|
||||
print("Wprowadź formułę lub wpisz \n\n 1. Regex \n 2. Regular \n \n aby zmienić rodzaj używanego walidatora. Domyślnym jest Regular.\n Wpisz exit żeby wyjść")
|
||||
i = ProperInterpreter()
|
||||
|
||||
while True:
|
||||
try:
|
||||
x = input('> ')
|
||||
except:
|
||||
break
|
||||
if x=="Regex":
|
||||
i=RegexInterpreter()
|
||||
print("Zmieniono walidator")
|
||||
|
||||
elif x=="Regular":
|
||||
i=ProperInterpreter()
|
||||
print("Zmieniono walidator")
|
||||
elif x=="exit" or x =="Exit":
|
||||
break
|
||||
else:
|
||||
|
||||
print(Formula(x,i).isValid())
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user