From 5c7f08426e62ed92e0c419e7f9e283cb68f5c0d0 Mon Sep 17 00:00:00 2001 From: Antonio MATTAR <antonio.mattar@etu.univ-amu.fr> Date: Fri, 24 Jun 2022 16:29:48 +0200 Subject: [PATCH] -Added the SUB-AND-OR-NOT operators // -Added 2 parameters for date and counter in captureImage --- app_functions.py | 6 ++- image_maker.py | 22 ++++++-- logic.py | 58 ++++++++++++++------ main.py | 136 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 167 insertions(+), 55 deletions(-) diff --git a/app_functions.py b/app_functions.py index 32a4557..d531f84 100644 --- a/app_functions.py +++ b/app_functions.py @@ -17,10 +17,14 @@ 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))))) +POW = new_abs(x,(new_abs(y,new_app(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))) +SUB= new_abs(x,new_abs(y,new_app(new_app(y,PRED),x))) +AND= new_abs(x,new_abs(y,new_app(new_app(x,y),FALSE))) +OR= new_abs(x,new_abs(y,new_app(new_app(x,TRUE),y))) +NOT = new_abs(x,new_abs(y,new_abs(z, new_app(new_app(x,z),y)))) #definition des entiers de church diff --git a/image_maker.py b/image_maker.py index 86c1fbd..e3be561 100644 --- a/image_maker.py +++ b/image_maker.py @@ -6,6 +6,7 @@ from PIL import ImageFont import logic import parsing import random +import datetime def color_image(img,color): width = img.size[0] @@ -29,8 +30,11 @@ variables_colors_couple = {} def associateVariableWithColor(variable): assert (logic.isVariable(variable)) + c = new_color() + if c in list(variables_colors_couple.values()): + associateVariableWithColor(variable) # recursive call to not get the same color if str(variable) not in variables_colors_couple: - variables_colors_couple[str(variable)] = new_color() + variables_colors_couple[str(variable)] = c #------------------------------------------------------------------------------------------------------------------------------------------ class My_image: def __init__(self,image,b): @@ -146,8 +150,16 @@ def createImage(terme): else: raise Exception("Unsupported term type") -def saveImage(image,name,path): - if path == None: - image.image.save(name+'.png', 'PNG') +def saveImage(image,name,path,date=True): + if date: + now = datetime.datetime.now() + now_string = now.strftime("%Y-%m-%d__%H-%M-%S") + if path == None: + image.image.save(now_string+'---'+name+'.png', 'PNG') + else: + image.image.save(path+'/'+now_string+'---'+name+'.png', 'PNG') else: - image.image.save(path+'/'+name+'.png', 'PNG') \ No newline at end of file + if path == None: + image.image.save(name+'.png', 'PNG') + else: + image.image.save(path+'/'+name+'.png', 'PNG') diff --git a/logic.py b/logic.py index bdcea21..36ef3b8 100644 --- a/logic.py +++ b/logic.py @@ -193,13 +193,22 @@ def beta_reduction(t): import image_maker image_counter = 0 -def captureImage(terme, path): +def captureImage(terme, path, counter=True, date= True): global image_counter - if path == None: - image_maker.saveImage(image_maker.createImage(terme),str(image_counter),None) + print(counter) + if type(counter) == bool and counter: + if path == None: + image_maker.saveImage(image_maker.createImage(terme),str(image_counter),None,date) + else: + image_maker.saveImage(image_maker.createImage(terme),str(image_counter),path,date) + image_counter += 1 else: - image_maker.saveImage(image_maker.createImage(terme),str(image_counter),path) - image_counter += 1 + if path == None: + image_maker.saveImage(image_maker.createImage(terme),str(counter),None,date) + else: + image_maker.saveImage(image_maker.createImage(terme),str(counter),path,date) + image_counter += 1 + def beta_reduction_totale(terme, path, saveImages=True): if saveImages == False: @@ -445,20 +454,35 @@ def cleanReductions(l): return l def beta_reduction_interactive_totale(terme,path): - if beta_reduction((terme)) != None: - print(to_string(terme)) - at = (annotate_beta_reduction((terme))) - captureImage(at,path) - choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non ")) - if choix==1: - return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at),path) + save_image_choice = int(input("Save image? (1 for yes, 0 for no): ")) + if save_image_choice == 1: + if beta_reduction((terme)) != None: + print(to_string(terme)) + at = (annotate_beta_reduction((terme))) + captureImage(at,path) + choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non ")) + if choix==1: + return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at),path) + else: + terme = cleanReductions(terme) + captureImage((terme),path) + print("C'est fini, le terme obtenu est : "+to_string(terme)) else: - terme = cleanReductions(terme) - captureImage((terme),path) - print("C'est fini, le terme obtenu est : "+to_string(terme)) + captureImage(terme,path) + return (terme) else: - captureImage(terme,path) - return (terme) + if beta_reduction((terme)) != None: + print(to_string(terme)) + at = (annotate_beta_reduction((terme))) + choix=int(input("voulez-vous faire la reduction tapez sur 1 pour oui tapez sur 2 pour non ")) + if choix==1: + return beta_reduction_interactive_totale(beta_reduction_interactive(terme,at),path) + else: + terme = cleanReductions(terme) + print("C'est fini, le terme obtenu est : "+to_string(terme)) + else: + return (terme) + diff --git a/main.py b/main.py index 93ad965..c6e7f07 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from ast import Not import logic import parsing import image_maker @@ -5,6 +6,7 @@ import app_functions import os import time import pyfiglet +import keyboard clear = lambda: os.system('cls') welcome_banner = pyfiglet.figlet_format('LambdaCalculus Interpreter') @@ -18,11 +20,19 @@ main_menu_options = { 5: 'Exit', } -arithmetic_operatins_options = { +arithmetic_operations_options = { 1: 'Addition', 2: 'Subtraction', 3: 'Multiplication', - 4: 'Back', + 4: 'power', + 5: 'successor', + 6: 'predecessor', + 7: 'Back', +} + +choice_number_representation= { + 1: 'integer numbers (church representation)', + 2: 'relative numbers' } #vide un repertoire def delete_images(path): @@ -34,26 +44,68 @@ def delete_images(path): except Exception as e: print(e) +def return_main_menu(): + not_pressed = True + print('Press ENTER to return to the main menu') + while not_pressed: # making a loop + try: # used try so that if user pressed other than the given key error will not be shown + if keyboard.is_pressed('ENTER'): # if key 'ENTER' is pressed + not_pressed = False # finishing the loop + clear() + time.sleep(1) + logic.image_counter = 0 + logic.var_counter = 0 + image_maker.variables_colors_couple = {} + clear() + break + except: + break # if user pressed a key other than the given key the loop will break + run_main_menu() + def run_beta_reduction_totale(terme,path='beta_reduction_totale'): terme = parsing.parseTerm(terme) 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.image_counter = 0 logic.beta_reduction_totale(terme,path) def run_beta_reduction_interactive_totale(terme,path='beta_reduction_interactive_totale'): terme = parsing.parseTerm(terme) os.makedirs(path, exist_ok=True) if len(os.listdir(path)) > 0: - delete_images(path) + logic.image_counter = 0 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) + print_menu(choice_number_representation) + choice = int(input('Enter your choice: ')) + while choice not in main_menu_options: + clear() + print_menu(choice_number_representation) + choice = int(input('Invalid choice. Enter your choice: ')) + if choice ==1: + clear() + terme = int(input('Enter a number: ')) + t = app_functions.dec_to_church(terme) + print('Voici le terme:',logic.to_string(t)) + save_image_choice = input('Do you want to save the image? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + if save_image_choice == 'y': + logic.captureImage(t,path,'ENTIER'+str(terme),False) + + elif choice==2: + clear() + terme = int(input('Enter a number: ')) + t = app_functions.dec_to_lambda_relative_integers(terme) + print('Voici le terme:',logic.to_string(t)) + save_image_choice = input('Do you want to save the image? (y/n): ') + while save_image_choice not in ['y','n']: + save_image_choice = input('Invalid choice. Do you want to save the image? (y/n): ') + if save_image_choice == 'y': + logic.captureImage(t,path,'ENTIER-RELATIF-'+str(terme),False) + #------------------------------------------------------------------------------------------------------------------------------------ @@ -63,72 +115,92 @@ def print_menu(options): print(key,'--',value) def run_main_menu(): + choice = '' print(welcome_banner) print_menu(main_menu_options) - choice = int(input('Enter your choice: ')) + try: + choice = int(input('Enter your choice: ')) + except ValueError: + clear() + print_menu(main_menu_options) while choice not in main_menu_options: - choice = int(input('Invalid choice. Enter your choice: ')) + choice = int(input('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() + return_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() + return_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() + return_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) +def run_arithmetic_operations_menu(path='arithmetic expressions'): + os.makedirs(path, exist_ok=True) + print_menu(arithmetic_operations_options) choice = int(input('Enter your choice: ')) - while choice not in arithmetic_operatins_options: + while choice not in arithmetic_operations_options: + clear() + print_menu(arithmetic_operations_options) choice = int(input('Invalid choice. Enter your choice: ')) if choice == 1: clear() - print('addition COMING SOON...') + print("Voici le terme: "+ logic.to_string(app_functions.ADD)) + logic.captureImage(app_functions.ADD,path,'ADD', False) time.sleep(1) clear() run_arithmetic_operations_menu() elif choice == 2: clear() - print('subtraction COMING SOON...') + print("Voici le terme: "+ logic.to_string(app_functions.SUB)) + logic.captureImage(app_functions.SUB,path,'SUB', False) time.sleep(1) clear() run_arithmetic_operations_menu() elif choice == 3: clear() - print('multiplication COMING SOON...') + print("Voici le terme: "+ logic.to_string(app_functions.MUL)) + logic.captureImage(app_functions.MUL,path,'MUL',False) time.sleep(1) clear() run_arithmetic_operations_menu() - elif choice == 4: + elif choice==4: + clear() + print("Voici le terme: "+ logic.to_string(app_functions.POW)) + logic.captureImage(app_functions.POW,path,'POWER',False) + time.sleep(1) + clear() + run_arithmetic_operations_menu() + elif choice==5: + clear() + print("Voici le terme: "+ logic.to_string(app_functions.SUCCS)) + logic.captureImage(app_functions.SUCCS,path,'SUCCS',False) + time.sleep(1) + clear() + run_arithmetic_operations_menu() + elif choice==6: + clear() + print("Voici le terme: "+ logic.to_string(app_functions.PRED)) + logic.captureImage(app_functions.PRED,path,'PRED',False) + time.sleep(1) + clear() + run_arithmetic_operations_menu() + elif choice == 7: clear() run_main_menu() -- GitLab