diff --git a/app_functions.py b/app_functions.py index 9ded314f46763bdf82b7ab3a2d09e26df980b227..9e88513863c80a40bf988013bea2aa15b9b8ae9f 100644 --- a/app_functions.py +++ b/app_functions.py @@ -7,8 +7,21 @@ x=new_var(freshVar()) y=new_var(freshVar()) z=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))} +u=new_var(freshVar()) +h=new_var(freshVar()) + +FALSE = new_abs(x,new_abs(y,y)) +TRUE = new_abs(x,new_abs(y,x)) +SUCCS= new_abs(z,new_abs(f,new_abs(x,new_app(new_app(z,f), new_app(f,x))))) +MUL= new_abs(y,new_abs(z,new_abs(f,new_abs(x,new_app(new_app(y,new_app(z,f)),x))))) +ADD= new_abs(x,new_abs(y,new_app(new_app(y,SUCCS),x))) +IS_ZERO= new_abs(z,new_app(new_app(z,new_abs(x,FALSE)),TRUE)) +PRED = new_abs(z,(new_abs(f,new_abs(x,new_app((new_app((new_app(z,new_abs(w,new_abs(h,new_app(h, new_app(w,f)))))),new_abs(u,x))), new_abs(u,u)))))) +POW = new_abs(y,new_abs(h,(new_abs(x,new_app(new_app(h,y),x))))) +PAIR = new_abs(x,new_abs(y,(new_abs(f,new_app(new_app(f,x),y))))) +FIRST = new_abs(x,(new_app(x,TRUE))) +SECOND = new_abs(x,(new_app(x,FALSE))) + #definition des entiers de church def dec_to_church(entier): @@ -22,36 +35,33 @@ def dec_to_church(entier): return new_abs(f,new_abs(x,term)) def pair(A,B): - return new_abs(f,new_app(new_app(f,A),B)) + return beta_reduction_totale(new_app((new_app(PAIR,A)),B),None,False) def getFirstFromPair(p): - return beta_reduction_totale(new_app(p,boolean[True]),None) + return beta_reduction_totale(new_app(FIRST, p),None,False) def getSecondFromPair(p): - return beta_reduction_totale(new_app(p,boolean[False]),None) + return beta_reduction_totale(new_app(SECOND, p),None,False) def dec_to_lambda_relative_integers(number): if number >= 0: - return pair(boolean[True],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)))),None,False) - -def adder(n,m): - return beta_reduction_totale(new_app(new_app(m,succs),n),None,False) + return pair(TRUE,dec_to_church(number)) + return pair(FALSE[False], dec_to_church(number)) -def power(n,m):# retourne n puiss m - return beta_reduction_totale(new_app(m,n),None,False) +# def succ(n):#done +# return beta_reduction_totale(new_app(SUCCS,n),None,False) -def multiplication(n,m): - return beta_reduction_totale(new_abs(f,new_abs(x,new_app(m,new_app(n,f)))),None,False) +# def add(n,m):#done +# return beta_reduction_totale(new_app(new_app(ADD,n),m),None,False) -def is_zero(n): - return beta_reduction_totale(new_app(new_app(n,new_abs(x,boolean[False])),boolean[True]),None,False) +# def power(n,m):# done +# return beta_reduction_totale(new_app((new_app(POW, n)),m),None,False) +# def multiplication(n,m):#done +# return beta_reduction_totale(new_app(new_app(MUL,n),m),None,False) -j= new_abs(x,new_abs(y,new_app(new_app(is_zero(new_app(x,dec_to_church(1))),y),succ(new_app(x,y))))) +# def is_zero(n):#done +# return beta_reduction_totale(new_app(IS_ZERO,n),None,False) -def predec(n): - return beta_reduction_totale(new_app(new_app(new_app(n,j),new_abs(z,dec_to_church(0))),dec_to_church(0)),None,False) +#def predec(n):#done +# return beta_reduction_totale(new_app(PRED, n),None,True) \ No newline at end of file diff --git a/image_maker.py b/image_maker.py index c57e5807c916986cd5960910dc76192767eb23b8..86c1fbd09c1f254e02b8f93cac6dadfa1fa36252 100644 --- a/image_maker.py +++ b/image_maker.py @@ -5,18 +5,32 @@ from PIL import ImageDraw from PIL import ImageFont import logic import parsing +import random + +def color_image(img,color): + width = img.size[0] + height = img.size[1] + for i in range(0,width):# process all pixels + for j in range(0,height): + data = img.getpixel((i,j)) + #print(data) #(255, 255, 255) + if (data[0]==66 and data[1]==66 and data[2]==66): + img.putpixel((i,j),(color[0], color[1], color[2])) + return img + +def new_color(): + for i in range(3): + r = random.randint(0,255) + g = random.randint(0,255) + b = random.randint(0,255) + return (r,g,b) - -colors = ['black','blue','green','orange','pink','purple','red','yellow'] variables_colors_couple = {} def associateVariableWithColor(variable): assert (logic.isVariable(variable)) - if len(colors) == 0: - raise Exception ("Not enough colors for variables! (Maximum of 9 different Variables)") if str(variable) not in variables_colors_couple: - variables_colors_couple[str(variable)] = colors[-1] - colors.pop() + variables_colors_couple[str(variable)] = new_color() #------------------------------------------------------------------------------------------------------------------------------------------ class My_image: def __init__(self,image,b): @@ -32,14 +46,18 @@ class My_image: def createVarImage(variable): assert (logic.isVariable(variable)) associateVariableWithColor(variable) - egg_img = Image.open("figures/egg_"+variables_colors_couple[str(variable)]+".png") + egg_img = Image.open("figures/egg_black.png") + egg_img = egg_img.convert("RGB") + egg_img = color_image(egg_img,variables_colors_couple[str(variable)]) image=My_image(egg_img,True) return egg_img def createAlligator(terme): assert (logic.isVariable(terme)) associateVariableWithColor(terme) - alligator_img = Image.open("figures/alligator_"+variables_colors_couple[str(terme)]+".png") + alligator_img = Image.open("figures/alligator_black.png") + alligator_img = alligator_img.convert("RGB") + alligator_img = color_image(alligator_img,variables_colors_couple[str(terme)]) image=My_image(alligator_img,False) return alligator_img diff --git a/main.py b/main.py index 374fa578d7ec32c6f2f95463cf72295298045705..93ad9654d1e2a9a0543115abe3020ef827a12670 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,28 @@ 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', +} +#vide un repertoire def delete_images(path): for file in os.listdir(path): file_path = os.path.join(path, file) @@ -15,7 +36,7 @@ def delete_images(path): def run_beta_reduction_totale(terme,path='beta_reduction_totale'): terme = parsing.parseTerm(terme) - os.makedirs(path, exist_ok=True) + os.makedirs(path, exist_ok=True)# cree le rep si il existe pas, le vide si non if len(os.listdir(path)) > 0: delete_images(path) logic.beta_reduction_totale(terme,path) @@ -34,3 +55,81 @@ def run_show_numbers(path): print('Voici le terme:',logic.to_string(t)) logic.captureImage(t,path) + +#------------------------------------------------------------------------------------------------------------------------------------ + +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: ') + run_beta_reduction_totale(terme1) + clear() + print('Done!') + time.sleep(1) + logic.image_counter = 0 + clear() + run_main_menu() + elif choice == 2: + clear() + terme2 = input('Enter a term: ') + run_beta_reduction_interactive_totale(terme2) + 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() + 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() \ No newline at end of file diff --git a/menu.py b/menu.py deleted file mode 100644 index 4bc9553c61c0d84bd83d29beee6841679fc248a3..0000000000000000000000000000000000000000 --- a/menu.py +++ /dev/null @@ -1,106 +0,0 @@ -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) - 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) - 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() - - -