Skip to content
Snippets Groups Projects
Commit 39da3e9a authored by Antonio MATTAR's avatar Antonio MATTAR
Browse files

-Added the main menu // - Fixed save image to save each type of cmd in a specific folder

parent 48855d4c
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,9 @@ from image_maker import* ...@@ -5,8 +5,9 @@ from image_maker import*
f=new_var(freshVar()) f=new_var(freshVar())
x=new_var(freshVar()) x=new_var(freshVar())
y=new_var(freshVar()) y=new_var(freshVar())
# z=new_var(freshVar()) z=new_var(freshVar())
# w=new_var(freshVar()) w=new_var(freshVar())
succs= new_abs(z,new_abs(f,new_abs(x,new_app(new_app(z,f), new_app(f,x)))))
boolean={True: new_abs(x,new_abs(y,x)), False: new_abs(x,new_abs(y,y))} boolean={True: new_abs(x,new_abs(y,x)), False: new_abs(x,new_abs(y,y))}
#definition des entiers de church #definition des entiers de church
...@@ -34,6 +35,17 @@ def dec_to_lambda_relative_integers(number): ...@@ -34,6 +35,17 @@ def dec_to_lambda_relative_integers(number):
return pair(boolean[True],dec_to_church(number)) return pair(boolean[True],dec_to_church(number))
return pair(boolean[False], dec_to_church(number)) return pair(boolean[False], dec_to_church(number))
def succ(n):
return beta_reduction_totale(new_abs(f,new_abs(x,new_app(new_app(n,f), new_app(f,x)))))
#print(to_string(getSecondFromPair(pair(boolean[True],dec_to_church(1))))) def adder(n,m):
print(to_string(pair(x,y))) return beta_reduction_totale(new_app(new_app(m,succs),n))
def power(n,m):# retourne n puiss m
return beta_reduction_totale(new_app(m,n))
def multiplication(n,m):
return beta_reduction_totale(new_abs(f,new_abs(x,new_app(m,new_app(n,f)))))
def is_zero(n):
return beta_reduction_totale(new_app(new_app(n,new_abs(x,boolean[False])),boolean[True]))
...@@ -128,5 +128,8 @@ def createImage(terme): ...@@ -128,5 +128,8 @@ def createImage(terme):
else: else:
raise Exception("Unsupported term type") raise Exception("Unsupported term type")
def saveImage(image,name): def saveImage(image,name,path):
image.image.save( name+".png", "PNG") if path == None:
\ No newline at end of file image.image.save(name+'.png', 'PNG')
else:
image.image.save(path+'/'+name+'.png', 'PNG')
\ No newline at end of file
...@@ -193,15 +193,21 @@ def beta_reduction(t): ...@@ -193,15 +193,21 @@ def beta_reduction(t):
import image_maker import image_maker
image_counter = 0 image_counter = 0
def captureImage(terme): def captureImage(terme, path):
global image_counter global image_counter
image_maker.saveImage(image_maker.createImage(terme),str(image_counter)) if path == None:
image_maker.saveImage(image_maker.createImage(terme),str(image_counter),None)
else:
image_maker.saveImage(image_maker.createImage(terme),str(image_counter),path)
image_counter += 1 image_counter += 1
def beta_reduction_totale(terme): def beta_reduction_totale(terme, path):
captureImage(terme) if path == None:
captureImage(terme, None)
else:
captureImage(terme,path)
if beta_reduction(terme) != None: if beta_reduction(terme) != None:
return beta_reduction_totale(beta_reduction(terme)) return beta_reduction_totale(beta_reduction(terme), path)
return (terme) return (terme)
numbers_to_letters = { numbers_to_letters = {
...@@ -214,8 +220,19 @@ numbers_to_letters = { ...@@ -214,8 +220,19 @@ numbers_to_letters = {
6:'r', 6:'r',
7:'t', 7:'t',
8:'u', 8:'u',
9:'p',
10:'d',
11:'a',
12:'k',
13:'m',
14:'n',
15:'v',
16:'f',
17:'ç'
} }
def to_string_var(terme): def to_string_var(terme):
assert (isVariable(terme)), 'The argument is not a variable' assert (isVariable(terme)), 'The argument is not a variable'
return (numbers_to_letters[terme[1]]) return (numbers_to_letters[terme[1]])
...@@ -420,20 +437,20 @@ def cleanReductions(l): ...@@ -420,20 +437,20 @@ def cleanReductions(l):
else: else:
return l return l
def beta_reduction_interactive_totale(terme): def beta_reduction_interactive_totale(terme,path):
if beta_reduction((terme)) != None: if beta_reduction((terme)) != None:
print(to_string(terme)) print(to_string(terme))
at = (annotate_beta_reduction((terme))) at = (annotate_beta_reduction((terme)))
captureImage(at) captureImage(at,path)
choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non ")) choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non "))
if choix==1: if choix==1:
return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at)) return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at),path)
else: else:
terme = cleanReductions(terme) terme = cleanReductions(terme)
captureImage((terme)) captureImage((terme),path)
print("C'est fini, le terme obtenu est : "+to_string(terme)) print("C'est fini, le terme obtenu est : "+to_string(terme))
else: else:
captureImage(terme) captureImage(terme,path)
return (terme) return (terme)
......
import logic import logic
import parsing import parsing
import image_maker import image_maker
import app_functions
import os
def run(): def delete_images(path):
# x = image_maker.createImage(parsing.parseTerm(input("Enter a term: "))) for file in os.listdir(path):
# image_maker.saveImage(x, "terme") file_path = os.path.join(path, file)
# x.show() try:
logic.beta_reduction_interactive_totale(parsing.parseTerm(input("Enter a term: "))) if os.path.isfile(file_path):
# x = logic.new_var('x') os.unlink(file_path)
# y = logic.new_var('y') except Exception as e:
# print(logic.to_string(logic.new_app(y,logic.new_app(x,logic.new_app(x,x))))) print(e)
def run_beta_reduction_totale(terme,path):
terme = parsing.parseTerm(terme)
os.makedirs(path, exist_ok=True)
if len(os.listdir(path)) > 0:
delete_images(path)
logic.beta_reduction_totale(terme,path)
def run_beta_reduction_interactive_totale(terme,path):
terme = parsing.parseTerm(terme)
os.makedirs(path, exist_ok=True)
if len(os.listdir(path)) > 0:
delete_images(path)
logic.beta_reduction_interactive_totale(terme,path)
def run_show_numbers(path):
os.makedirs(path, exist_ok=True)
terme = int(input('Enter a number: '))
t = app_functions.dec_to_lambda_relative_integers(terme)
print('Voici le terme:',logic.to_string(t))
logic.captureImage(t,path)
run()
\ No newline at end of file
menu.py 0 → 100644
import main
import logic
import parsing
import image_maker
import app_functions
import os
import time
import pyfiglet
clear = lambda: os.system('cls')
welcome_banner = pyfiglet.figlet_format('LambdaCalculus Interpreter')
good_bye_banner = pyfiglet.figlet_format('Goodbye!')
main_menu_options = {
1: 'Beta-Reduction',
2: 'Interactive Beta-Reduction',
3: 'Arithmetic Operations',
4: 'Show Numbers',
5: 'Exit',
}
arithmetic_operatins_options = {
1: 'Addition',
2: 'Subtraction',
3: 'Multiplication',
4: 'Back',
}
def print_menu(options):
for key, value in options.items():
print(key,'--',value)
def run_main_menu():
print(welcome_banner)
print_menu(main_menu_options)
choice = int(input('Enter your choice: '))
while choice not in main_menu_options:
choice = int(input('Invalid choice. Enter your choice: '))
if choice == 1:
clear()
terme1 = input('Enter a term: ')
main.run_beta_reduction_totale(terme1,'beta_reduction_totale')
clear()
print('Done!')
time.sleep(1)
logic.image_counter = 0
clear()
run_main_menu()
elif choice == 2:
clear()
terme2 = input('Enter a term: ')
main.run_beta_reduction_interactive_totale(terme2,'beta_reduction_interactive_totale')
clear()
print('Done!')
logic.image_counter = 0
time.sleep(1)
clear()
run_main_menu()
elif choice == 3:
clear()
run_arithmetic_operations_menu()
elif choice == 4:
clear()
main.run_show_numbers('show_numbers')
logic.image_counter = 0
time.sleep(3)
clear()
run_main_menu()
elif choice == 5:
clear()
print(good_bye_banner)
else:
print('Invalid choice')
run_main_menu()
def run_arithmetic_operations_menu():
print_menu(arithmetic_operatins_options)
choice = int(input('Enter your choice: '))
while choice not in arithmetic_operatins_options:
choice = int(input('Invalid choice. Enter your choice: '))
if choice == 1:
clear()
print('addition COMING SOON...')
time.sleep(1)
clear()
run_arithmetic_operations_menu()
elif choice == 2:
clear()
print('subtraction COMING SOON...')
time.sleep(1)
clear()
run_arithmetic_operations_menu()
elif choice == 3:
clear()
print('multiplication COMING SOON...')
time.sleep(1)
clear()
run_arithmetic_operations_menu()
elif choice == 4:
clear()
run_main_menu()
run_main_menu()
...@@ -196,7 +196,6 @@ def extract_terms(text): ...@@ -196,7 +196,6 @@ def extract_terms(text):
def buildApp(terme): def buildApp(terme):
assert (checkType(terme) == APP) assert (checkType(terme) == APP)
liste_de_termes = extract_terms(terme) liste_de_termes = extract_terms(terme)
print(liste_de_termes)
if terme[0]!='(': if terme[0]!='(':
n = len(liste_de_termes) n = len(liste_de_termes)
if n == 0: if n == 0:
......
os
time
pyfiglet
\ 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