From 7e03374867708d15096aa737859703c1eea14e47 Mon Sep 17 00:00:00 2001 From: emilekm2142 Date: Sat, 1 Jun 2019 14:33:01 +0200 Subject: [PATCH] bug fixy i interfejs --- b | 1 + program.py | 75 ++++++++++++++++++++++++++++++++++++++++-------------- testy.txt | 4 +-- 3 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 b diff --git a/b b/b new file mode 100644 index 0000000..0ca9514 --- /dev/null +++ b/b @@ -0,0 +1 @@ +True diff --git a/program.py b/program.py index c210493..abb4817 100644 --- a/program.py +++ b/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()) + diff --git a/testy.txt b/testy.txt index 7532d63..230a87d 100644 --- a/testy.txt +++ b/testy.txt @@ -1,7 +1,7 @@ (a>) (>) ()(a) -a a(b&c)>d (b&d)d -(a>b)>k +a> +b&d&