dodanie regex
This commit is contained in:
parent
270e217040
commit
87fba95736
37
program.py
37
program.py
@ -39,6 +39,41 @@ class SimpleInterpreter(AbstractInterpreter):
|
|||||||
print("previous is also a variable");
|
print("previous is also a variable");
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
class RegexInterpreter(AbstractInterpreter):
|
||||||
|
def __init__(self):
|
||||||
|
#check if grep is available
|
||||||
|
import subprocess
|
||||||
|
r=subprocess.run("grep")
|
||||||
|
|
||||||
|
def isValid(self,formula)->bool:
|
||||||
|
|
||||||
|
make_reg = lambda formula,pattern: 'echo "{0}" | grep -P "{1}"'.format(formula,pattern)
|
||||||
|
def check_assertion(formula, pattern):
|
||||||
|
result = subprocess.run([make_reg(formula,pattern)], shell=True, stdout=subprocess.PIPE)
|
||||||
|
return len(result.stdout)>0
|
||||||
|
|
||||||
|
result = subprocess.run([make_reg(formula,'^(\((?:[^()]++|(?1))*\))$')], shell=True, stdout=subprocess.PIPE)
|
||||||
|
isValid = len(result.stdout)>0
|
||||||
|
if not isValid: return False
|
||||||
|
|
||||||
|
#assertions
|
||||||
|
assertions = ['(?:[>|&|\|=][\(\)]*){2,}','\((?:[>|&|\|=][\(\)]*)','[a-z]{2,}','\((?:[>|&|\|=|!])?\)']
|
||||||
|
for assertion in assertions:
|
||||||
|
if not check_assertion(formula,assertion):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
# (?:[>|&|\|=][\(\)]*){2,} - czy powtarzają się jakieś operatory obok siebie?
|
||||||
|
|
||||||
|
|
||||||
|
# echo "formuła" | grep -P "regex" - to jeżeli coś zwróci to znaczy, że mamy dopasowanie
|
||||||
|
#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
|
||||||
|
|
||||||
|
# \((?:[>|&|\|=][\(\)]*) - (>b)
|
||||||
|
# [a-z]{2,} - czy powtarzają się jakieś lierki obok siebie?
|
||||||
|
# \((?:[>|&|\|=|!])?\) - czy istnieją puste nawiasy, bądź nawiasy z samym operatorem?
|
||||||
class Formula:
|
class Formula:
|
||||||
def __init__(self, formula_string:str, interpreter:AbstractInterpreter):
|
def __init__(self, formula_string:str, interpreter:AbstractInterpreter):
|
||||||
self.s = formula_string
|
self.s = formula_string
|
||||||
@ -51,7 +86,7 @@ class Formula:
|
|||||||
|
|
||||||
return interpreter.isValid(self)
|
return interpreter.isValid(self)
|
||||||
|
|
||||||
|
#g = RegexInterpreter()
|
||||||
i = SimpleInterpreter()
|
i = SimpleInterpreter()
|
||||||
print(sys.argv)
|
print(sys.argv)
|
||||||
if sys.argv[1] == '-f':
|
if sys.argv[1] == '-f':
|
||||||
|
Loading…
Reference in New Issue
Block a user