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

-Added a feature to get an infinty of colors!

parent 9a324cb7
Branches
No related tags found
No related merge requests found
...@@ -7,8 +7,21 @@ x=new_var(freshVar()) ...@@ -7,8 +7,21 @@ 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))))) u=new_var(freshVar())
boolean={True: new_abs(x,new_abs(y,x)), False: new_abs(x,new_abs(y,y))} 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 #definition des entiers de church
def dec_to_church(entier): def dec_to_church(entier):
...@@ -22,36 +35,33 @@ def dec_to_church(entier): ...@@ -22,36 +35,33 @@ def dec_to_church(entier):
return new_abs(f,new_abs(x,term)) return new_abs(f,new_abs(x,term))
def pair(A,B): 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): 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): 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): def dec_to_lambda_relative_integers(number):
if number >= 0: if number >= 0:
return pair(boolean[True],dec_to_church(number)) return pair(TRUE,dec_to_church(number))
return pair(boolean[False], dec_to_church(number)) return pair(FALSE[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)
def power(n,m):# retourne n puiss m # def succ(n):#done
return beta_reduction_totale(new_app(m,n),None,False) # return beta_reduction_totale(new_app(SUCCS,n),None,False)
def multiplication(n,m): # def add(n,m):#done
return beta_reduction_totale(new_abs(f,new_abs(x,new_app(m,new_app(n,f)))),None,False) # return beta_reduction_totale(new_app(new_app(ADD,n),m),None,False)
def is_zero(n): # def power(n,m):# done
return beta_reduction_totale(new_app(new_app(n,new_abs(x,boolean[False])),boolean[True]),None,False) # 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): #def predec(n):#done
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) # return beta_reduction_totale(new_app(PRED, n),None,True)
\ No newline at end of file
...@@ -5,18 +5,32 @@ from PIL import ImageDraw ...@@ -5,18 +5,32 @@ from PIL import ImageDraw
from PIL import ImageFont from PIL import ImageFont
import logic import logic
import parsing 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 = {} variables_colors_couple = {}
def associateVariableWithColor(variable): def associateVariableWithColor(variable):
assert (logic.isVariable(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: if str(variable) not in variables_colors_couple:
variables_colors_couple[str(variable)] = colors[-1] variables_colors_couple[str(variable)] = new_color()
colors.pop()
#------------------------------------------------------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------------------------------------------------------
class My_image: class My_image:
def __init__(self,image,b): def __init__(self,image,b):
...@@ -32,14 +46,18 @@ class My_image: ...@@ -32,14 +46,18 @@ class My_image:
def createVarImage(variable): def createVarImage(variable):
assert (logic.isVariable(variable)) assert (logic.isVariable(variable))
associateVariableWithColor(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) image=My_image(egg_img,True)
return egg_img return egg_img
def createAlligator(terme): def createAlligator(terme):
assert (logic.isVariable(terme)) assert (logic.isVariable(terme))
associateVariableWithColor(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) image=My_image(alligator_img,False)
return alligator_img return alligator_img
......
...@@ -3,7 +3,28 @@ import parsing ...@@ -3,7 +3,28 @@ import parsing
import image_maker import image_maker
import app_functions import app_functions
import os 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): def delete_images(path):
for file in os.listdir(path): for file in os.listdir(path):
file_path = os.path.join(path, file) file_path = os.path.join(path, file)
...@@ -15,7 +36,7 @@ def delete_images(path): ...@@ -15,7 +36,7 @@ def delete_images(path):
def run_beta_reduction_totale(terme,path='beta_reduction_totale'): def run_beta_reduction_totale(terme,path='beta_reduction_totale'):
terme = parsing.parseTerm(terme) 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: if len(os.listdir(path)) > 0:
delete_images(path) delete_images(path)
logic.beta_reduction_totale(terme,path) logic.beta_reduction_totale(terme,path)
...@@ -34,3 +55,81 @@ def run_show_numbers(path): ...@@ -34,3 +55,81 @@ def run_show_numbers(path):
print('Voici le terme:',logic.to_string(t)) print('Voici le terme:',logic.to_string(t))
logic.captureImage(t,path) 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
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment