Skip to content
Snippets Groups Projects
Commit e40dd222 authored by Tamazouzt AIT ELDJOUDI's avatar Tamazouzt AIT ELDJOUDI
Browse files

newVar

parent 643f51b9
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -57,12 +57,14 @@ def findClosingParanthesesIndex(terme, openParentheseIndex): ...@@ -57,12 +57,14 @@ def findClosingParanthesesIndex(terme, openParentheseIndex):
return closeParantheseIndex return closeParantheseIndex
def checkType(terme): def checkType(terme):
if terme[0] == '(' and balancedParantheses(terme): if terme[0]== '(':
return APP return checkType(getTermFromParantheses(terme,0))
elif "#" == terme[0]: if "#" == terme[0]:
return ABS return ABS
else: elif not (' ' in terme ) and not ('(' in terme):
return VAR return VAR
else:
return APP
def isVariable(terme): def isVariable(terme):
return checkType(terme) == VAR return checkType(terme) == VAR
...@@ -92,7 +94,10 @@ def parseOutputFromAbs(terme): ...@@ -92,7 +94,10 @@ def parseOutputFromAbs(terme):
def buildVar(terme): def buildVar(terme):
assert (checkType(terme) == VAR) assert (checkType(terme) == VAR)
if terme[0]!='(':
return new_var(terme) return new_var(terme)
else :
return buildVar(getTermFromParantheses(terme,0))
def buildAbs(terme): def buildAbs(terme):
assert (checkType(terme) == ABS) assert (checkType(terme) == ABS)
...@@ -102,7 +107,7 @@ def buildAbs(terme): ...@@ -102,7 +107,7 @@ def buildAbs(terme):
def buildApp(terme): def buildApp(terme):
assert (checkType(terme) == APP) assert (checkType(terme) == APP)
liste_de_termes = getTermsFromParantheses(terme) liste_de_termes = terme.split(' ')
n = len(liste_de_termes) n = len(liste_de_termes)
if n == 0: if n == 0:
raise Exception('Empty List of Terms') raise Exception('Empty List of Terms')
...@@ -113,58 +118,49 @@ def buildApp(terme): ...@@ -113,58 +118,49 @@ def buildApp(terme):
return t return t
A = "#x.(x)(y)"
def buildTerm(terme): def buildTerm(terme):
TermType = checkType(terme) TermType = checkType(terme)
if isVariable(terme): return buildVar(terme) if isVariable(terme): return buildVar(terme)
elif isAbstraction(terme): return buildAbs(terme) elif isAbstraction(terme): return buildAbs(terme)
elif isApplication(terme): return buildApp(terme) elif isApplication(terme): return buildApp(terme)
# renvoie les espaces laissés au debut et a la fin du terme ( ils servent a rien)
def remove_first_and_last_spaces(terme):
new_terme = list(terme)
i = 0
j = len(terme) -1
while i < j and new_terme[i] == " ":
new_terme[i] = ""
i +=1
while j > 0 and new_terme[j] == " ":
new_terme[j] = ""
j -=1
return "".join([str(letter) for letter in new_terme])
# enleve les parentheses quand y a un seul coupme
def remove_inutile_parentheses(terme):
if parantheses_couples(terme) == 1 :
return terme[1:-1] # tout sauf le premier et dernier terme
else:
return terme
# renvoie ce qu'il y a entre la parenthese ouverte a l indice i et sa parethese fermante # renvoie ce qu'il y a entre la parenthese ouverte a l indice i et sa parethese fermante
def getTermFromParantheses(terme,i): def getTermFromParantheses(terme,i):
return terme[i+1:findClosingParanthesesIndex(terme,i)] return terme[i+1:findClosingParanthesesIndex(terme,i)]
# renvoie renvoie tous les termes entre parentheses dans terme #fonction qui enleve les espaces successifs et laisse un seul espace
def getTermsFromParantheses(terme): def remove_multiple_spaces(terme):
terms = [] new_term=''
for i in (open_parantheses_counter(terme)[1]): i=0
terms.append(getTermFromParantheses(terme,i)) while i<len(terme):
return terms new_term+=terme[i]
if terme[i]==' ':
while terme[i]==' ':
print(getTermsFromParantheses("(AAAAAA)(BBBBBBBBB)(CCCCCCCCC)")) i+=1
print(buildTerm(A)) else:
print(remove_first_and_last_spaces('ll')) i+=1
print(isAbstraction(A))
print(isApplication("(x)(y)(z)")) return new_term
print(checkType("(#x.x)"))
print(buildTerm('(#x.x)(#y.y)'))
print(remove_inutile_parentheses('(#x.x)(#y.y)'))
# print(getTermsFromParantheses("(AAAAAA)(BBBBBBBBB)(CCCCCCCCC)"))
# print(buildTerm(A))
# print(remove_first_and_last_spaces('ll'))
# print(isAbstraction(A))
# print(isApplication("(x)(y)(z)"))
# print(checkType("(#x.x)"))
# print(buildTerm('(#x.x)(#y.y)'))
# print(remove_inutile_parentheses('(#x.x)(#y.y)'))
# print(open_parantheses_counter('(((((((((((')) # print(open_parantheses_counter('((((((((((('))
# print(close_parantheses_counter('((((((((((()')) # print(close_parantheses_counter('((((((((((()'))
# print(buildAbs("#x.ABC")) # print(buildAbs("#x.ABC"))
# print(checkType("(#x.xx)(fds)")) # print(checkType("(#x.xx)(fds)"))
# print(findClosingParanthesesIndex('()',0)) # print(findClosingParanthesesIndex('()',0))
#print(remove_multiple_spaces("x y z (#x.z g)"))
print(buildTerm("x "))
print(checkType("(((#x.x)))"))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment