dodanie kilku edge caseów
This commit is contained in:
parent
ded73fd70a
commit
427d769aa6
25
program.py
25
program.py
@ -19,7 +19,7 @@ class SimpleInterpreter(AbstractInterpreter):
|
|||||||
logic_signs = ['=', '>', '&', '|', '!', '(', ')']
|
logic_signs = ['=', '>', '&', '|', '!', '(', ')']
|
||||||
return char in logic_signs
|
return char in logic_signs
|
||||||
def isParentice(char):
|
def isParentice(char):
|
||||||
return char in ('(', ')')
|
return char in ['(', ')']
|
||||||
def isVariable(char):
|
def isVariable(char):
|
||||||
return not isSign(char)
|
return not isSign(char)
|
||||||
def isNegation(char):
|
def isNegation(char):
|
||||||
@ -31,12 +31,20 @@ class SimpleInterpreter(AbstractInterpreter):
|
|||||||
prev = formula.s[i-1] if i>0 else None #None, gdy jesteśmy na pierwszym znaku
|
prev = formula.s[i-1] if i>0 else None #None, gdy jesteśmy na pierwszym znaku
|
||||||
if (char=='(' and isVariable(prev) and prev is not None): #a(b|c)
|
if (char=='(' and isVariable(prev) and prev is not None): #a(b|c)
|
||||||
return False
|
return False
|
||||||
|
if (prev is None):
|
||||||
|
if not (isVariable(char) or isParentice(char) or isNegation(char)):
|
||||||
|
return False
|
||||||
if isSign(char):
|
if isSign(char):
|
||||||
|
|
||||||
if (not isNegation(char)):
|
if (not isNegation(char)):
|
||||||
if isSign(prev) and not isNegation(prev): # tylko negacje mogą się powtarzać po innym znaku
|
if isSign(prev) and not isNegation(prev): # tylko negacje mogą się powtarzać po innym znaku
|
||||||
return False
|
return False
|
||||||
else:
|
if isNegation(prev):
|
||||||
if isVariable(prev) and prev is not None: # obrona przed a!>b
|
|
||||||
|
if not (isNegation(char) or isParentice(char)):
|
||||||
|
print("xd")
|
||||||
|
return False
|
||||||
|
if isVariable(prev): #a!
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
#if current char is a variable
|
#if current char is a variable
|
||||||
@ -113,6 +121,7 @@ class ProperInterpreter(AbstractInterpreter):
|
|||||||
if len(formula)==0:
|
if len(formula)==0:
|
||||||
# print("Empty subformula")
|
# print("Empty subformula")
|
||||||
return None
|
return None
|
||||||
|
if (formula=='!'): raise Exception("Negacja źle postawiona")
|
||||||
x=[0 for u in formula]
|
x=[0 for u in formula]
|
||||||
current_depth=0
|
current_depth=0
|
||||||
for i,char in enumerate(formula):
|
for i,char in enumerate(formula):
|
||||||
@ -131,7 +140,7 @@ class ProperInterpreter(AbstractInterpreter):
|
|||||||
if current_depth!=0:
|
if current_depth!=0:
|
||||||
raise Exception("Nawiasy źle umieszczone w podformule: {0}".format(formula))
|
raise Exception("Nawiasy źle umieszczone w podformule: {0}".format(formula))
|
||||||
|
|
||||||
#print(formula)
|
print(formula)
|
||||||
|
|
||||||
min_depth = min(x)
|
min_depth = min(x)
|
||||||
|
|
||||||
@ -148,7 +157,7 @@ class ProperInterpreter(AbstractInterpreter):
|
|||||||
slice_point = i
|
slice_point = i
|
||||||
break
|
break
|
||||||
if is_simple:
|
if is_simple:
|
||||||
# print(formula, " is simple")
|
#print(formula, " is simple")
|
||||||
if not self.check_simple_formula(formula):
|
if not self.check_simple_formula(formula):
|
||||||
raise Exception("Błąd na poziomie formuły bez zagnieżdzeń: {0}".format(formula))
|
raise Exception("Błąd na poziomie formuły bez zagnieżdzeń: {0}".format(formula))
|
||||||
return False
|
return False
|
||||||
@ -177,7 +186,7 @@ class Formula:
|
|||||||
def isValid(self, interpreter=None)->bool:
|
def isValid(self, interpreter=None)->bool:
|
||||||
i = self.__i if interpreter is None else interpreter
|
i = self.__i if interpreter is None else interpreter
|
||||||
return i.isValid(self)
|
return i.isValid(self)
|
||||||
|
sim = SimpleInterpreter()
|
||||||
if len(sys.argv) > 1 :
|
if len(sys.argv) > 1 :
|
||||||
i = ProperInterpreter()
|
i = ProperInterpreter()
|
||||||
if '-g' in sys.argv:
|
if '-g' in sys.argv:
|
||||||
@ -192,7 +201,9 @@ if len(sys.argv) > 1 :
|
|||||||
for l in u:
|
for l in u:
|
||||||
print(Formula(l,i).isValid())
|
print(Formula(l,i).isValid())
|
||||||
else:
|
else:
|
||||||
print(Formula( sys.argv[1] if sys.argv[1] != '-g' else sys.argv[2],i).isValid())
|
print(
|
||||||
|
Formula(sys.argv[1] if sys.argv[1] != '-g' else sys.argv[2],i).isValid()
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("help?")
|
print("help?")
|
||||||
|
Loading…
Reference in New Issue
Block a user