Skip to content
Snippets Groups Projects
Select Git revision
  • d2e352c6de4228606a5a0e688b6a0904a92ca4fd
  • master default protected
  • develop
  • 0.1.0
  • 0.0.3
  • 0.0.2
  • 0.0.1
  • 0.0.0
8 results

plot_usecase_exampleMVML.py.md5

Blame
  • main.py 6.78 KiB
    from ast import Not
    import logic
    import parsing
    import image_maker
    import app_functions
    import os
    import time
    import pyfiglet
    import keyboard
    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_operations_options = {
        1: 'Addition',
        2: 'Subtraction',
        3: 'Multiplication',
        4: 'Power',
        5: 'Successor',
        6: 'Predecessor',
        7: 'Back',
    }
    
    choice_number_representation= {
        1: 'Integer numbers (church representation)',
        2: 'Relative numbers',
        3: 'Back'
    }
    #vide un repertoire
    def delete_images(path):
        for file in os.listdir(path):
            file_path = os.path.join(path, file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_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:
            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:
            logic.image_counter = 0
        logic.beta_reduction_interactive_totale(terme,path)
    
    def run_show_numbers(path):
        os.makedirs(path, exist_ok=True)
        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)
        elif choice==3:
            run_main_menu()
        
    
    
    #------------------------------------------------------------------------------------------------------------------------------------
    
    def print_menu(options):
        for key, value in options.items():
            print(key,'--',value)
    
    def run_main_menu():
        choice = ''
        print(welcome_banner)
        print_menu(main_menu_options)
        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('Enter your choice: '))
        if choice == 1:
            clear()
            terme1 = input('Enter a term: ')
            run_beta_reduction_totale(terme1)
            clear()
            print('Done!')
            return_main_menu()
        elif choice == 2:
            clear()
            terme2 = input('Enter a term: ')
            run_beta_reduction_interactive_totale(terme2)
            clear()
            print('Done!')
            return_main_menu()
        elif choice == 3:
            clear()
            run_arithmetic_operations_menu()
        elif choice == 4:
            clear()
            run_show_numbers('show_numbers')
            return_main_menu()
        elif choice == 5:
            clear()
            print(good_bye_banner)
    
    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_operations_options:
            clear()
            print_menu(arithmetic_operations_options)
            choice = int(input('Invalid choice. Enter your choice: '))
        if choice == 1:
            clear()
            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("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("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:
            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()
    
    run_main_menu()
    
    input()