29 from __future__
import division
32 VERSION =
'v.00.07 --- 2010 April 12'
34 import string, sys, time
36 if sys.version_info[0] >= 3:
38 import tkinter.font
as tkFont
39 import tkinter.messagebox
as tkMessageBox
54 if not 'profile' in dir():
96 tk_Font_monospace = tkFont.Font(tk_Win, size=10, family=
'courier')
99 tk_Font_monospace8 = tkFont.Font(tk_Win, size=8, family=
'courier')
103 tk_Var_numeral_Church = tk.BooleanVar()
107 tk_Var_show_allparent = tk.BooleanVar()
110 tk_Var_show_space = tk.BooleanVar()
114 tk_Var_sleep = tk.BooleanVar()
115 tk_Var_sleep.set(
True)
124 """Renvoie le combinateur c sous forme de string
126 Pre: c: None ou Combinator"""
127 assert (c ==
None)
or isinstance(c, combinator.Combinator), type(c)
129 return (c.__str__(allparent=tk_Var_show_allparent.get(),
130 space=(
' ' if tk_Var_show_space.get()
131 else ''))
if c !=
None
141 tkMessageBox.showinfo(
'About...',
145 (c) Olivier Pirson --- DragonSoft
151 Python {3}""".format(VERSION, DSPython.DS_web, DSPython.VERSION, sys.version))
155 def cmd_change_delay(event=None):
158 s = tk_Entry_delay.get()
166 tk_Entry_delay.delete(0, tk.END)
170 def cmd_change_step(event=None):
173 s = tk_Entry_step.get()
175 nb_eval_step = int(s)
179 if nb_eval_step <= 0:
181 tk_Entry_step.delete(0, tk.END)
182 if not tkMessageBox.askyesno(
'No step?',
183 """Do you want no limitation in evaluation?
184 (Some combinators run for ever!)"""):
186 tk_Entry_step.delete(0, tk.END)
187 tk_Entry_step.insert(tk.END, str(nb_eval_step))
191 def cmd_change_numeral(event=None):
194 s = tk_Entry_numeral.get()
202 tk_Entry_numeral.delete(0, tk.END)
204 cmd_insert_comb(combinator.Combinator.n_to_Church(numeral)
if tk_Var_numeral_Church.get()
205 else combinator.Combinator.n_to_Barendregt(numeral))
210 def cmd_clear(event=None):
215 tk_Entry_comb.delete(0, tk.END)
216 tk_Label_comb.config(text=
'')
217 tk_Listbox_combeval.delete(0, tk.END)
219 tk_Label_nb_eval.config(text=str(nb_eval))
220 tk_Button_eval.config(state=tk.DISABLED)
224 def cmd_comb_update(event=None):
230 for c
in tk_Entry_comb.get().translate(cmd_comb_update.TRANS)])
234 comb = combinator.Combinator(s)
235 tk_Label_comb.config(text=comb_to_str(comb))
239 while (comb ==
None)
and (s !=
''):
241 comb = combinator.Combinator(s)
245 tk_Label_comb.config(text=comb_to_str(comb) +
' ..?')
248 tk_Button_eval.config(state=(tk.DISABLED
if (comb ==
None)
or comb.stable_is()
252 if sys.version_info[0] >= 3:
253 cmd_comb_update.TRANS = str.maketrans(
'[]{}',
'()()')
255 cmd_comb_update.TRANS = string.maketrans(
'[]{}',
'()()')
260 global nb_eval, pause, running
265 tk_Button_eval.config(relief=tk.SUNKEN)
266 tk_Button_stop.config(state=tk.NORMAL)
267 tk_Listbox_combeval.delete(0, tk.END)
271 ds[comb.__str__(space=
'')] = 0
275 while pause
and running:
279 if tk_Var_sleep.get():
280 time.sleep(sleep_delay/1000)
283 c, nb_eval_last =
c(nb=(nb_eval_step
if nb_eval_step > 0
286 nb_eval += nb_eval_last
287 tk_Label_nb_eval.config(text=str(nb_eval))
289 tk_Listbox_combeval.insert(tk.END, comb_to_str(c))
290 s = c.__str__(space=
'')
293 tk_Listbox_combeval.insert(tk.END,
'... {0}'.format(ds[s]))
297 tk_Listbox_combeval.see(tk.END)
301 tk_Button_eval.config(relief=tk.RAISED)
302 tk_Button_stop.config(state=tk.DISABLED)
305 tk_Button_eval.config(relief=(tk.RAISED
if pause
310 def cmd_insert_comb(c):
313 if tk_Entry_comb.get() !=
'':
314 s = (
' {0}' if s.find(
' ') < 0
315 else ' [{0}]').format(s)
316 tk_Entry_comb.insert(tk.INSERT, s)
322 if tkMessageBox.askyesno(
'Quit?',
'Quit Combinator Tk?'):
337 combinator.var_x = comb
338 tk_Label_var_x.config(text=(
'' if comb ==
None
339 else str(comb.__str__(space=
''))))
342 tk_Button_eval.config(state=(tk.DISABLED
if (comb ==
None)
or comb.stable_is()
348 combinator.var_y = comb
349 tk_Label_var_y.config(text=(
'' if comb ==
None
353 tk_Button_eval.config(state=(tk.DISABLED
if (comb ==
None)
or comb.stable_is()
359 combinator.var_z = comb
360 tk_Label_var_z.config(text=(
'' if comb ==
None
364 tk_Button_eval.config(state=(tk.DISABLED
if (comb ==
None)
or comb.stable_is()
372 tk_Win.title(
'Combinator Tk')
373 tk_Win.resizable(0,0)
374 tk_Win.protocol(
'WM_DELETE_WINDOW', cmd_quit)
378 tk_Frame = tk.Frame(tk_Win)
380 for c
in (
'B',
'C',
'I',
'K',
'KI',
'L',
'M',
'O',
'R', 'S', 'T', '
U', 'V', 'W', 'Y'):
381 exec(
'tk.Button(tk_Frame,'
382 +
' text="{0}", command=lambda : cmd_insert_comb(combinator.{1})).pack(side=tk.LEFT)'
385 tk.Frame(tk_Frame, width=5).pack(side=tk.LEFT)
387 for c
in ((unichr(953),
'iota'),
388 (unichr(937),
'Omega')):
389 exec(
'tk.Button(tk_Frame,'
390 +
' text="{0}", command=lambda : cmd_insert_comb(combinator.{1})).pack(side=tk.LEFT)'
393 tk.Frame(tk_Frame, width=10).pack(side=tk.LEFT)
395 for c
in (
'not',
'and',
'or',
'imp'):
396 exec(
'tk.Button(tk_Frame,'
397 +
' text="{0}", command=lambda : cmd_insert_comb(combinator.B{1})).pack(side=tk.LEFT)'
400 tk.Frame(tk_Frame, width=10).pack(side=tk.LEFT)
403 tk_Entry_numeral = tk.Entry(tk_Frame, width=5)
404 tk_Entry_numeral.insert(tk.END, str(numeral))
405 tk_Entry_numeral.bind(
'<Return>', cmd_change_numeral)
406 tk_Entry_numeral.pack(side=tk.LEFT)
408 tk.Checkbutton(tk_Frame, text=
'Church', variable=tk_Var_numeral_Church,
409 command=cmd_change_numeral).pack(side=tk.LEFT)
411 tk.Frame(tk_Frame, width=5).pack(side=tk.LEFT)
413 for c
in (
'x',
'y',
'z'):
414 exec(
'tk.Button(tk_Frame,'
415 +
' text="{0}", command=lambda : cmd_insert_comb(combinator.V{1})).pack(side=tk.LEFT)'
418 exec(
'tk.Button(tk_Frame, text=unichr(8593), command=cmd_to_var_{0}).pack(side=tk.LEFT)'
420 exec(
'tk.Button(tk_Frame, text=unichr(8595),'
421 +
' command=lambda : cmd_insert_comb(combinator.var_{0})).pack(side=tk.LEFT)'
424 exec(
'tk_Label_var_{0} = tk.Label(tk_Frame, font=tk_Font_monospace8, width=5)'.format(c))
425 exec(
'tk_Label_var_{0}.pack(side=tk.LEFT)'.format(c))
427 tk.Frame(tk_Frame, width=5).pack(side=tk.LEFT)
429 tk.Frame(tk_Frame, width=10).pack(side=tk.LEFT)
432 tk_Button_eval = tk.Button(tk_Frame, text=
'Eval', command=cmd_eval, state=tk.DISABLED)
433 tk_Button_eval.pack(side=tk.LEFT)
436 tk_Button_stop = tk.Button(tk_Frame, text=
'Stop', command=cmd_stop, foreground=
'red',
438 tk_Button_stop.pack(side=tk.LEFT)
440 tk.Frame(tk_Frame, width=10).pack(side=tk.LEFT)
441 tk.Button(tk_Frame, text=
'Clear', command=cmd_clear).pack(side=tk.LEFT)
443 tk.Frame(tk_Frame, width=10).pack(side=tk.LEFT)
444 tk.Button(tk_Frame, text=
'About', command=cmd_about).pack(side=tk.LEFT)
445 tk.Button(tk_Frame, text=
'Quit', command=cmd_quit).pack(side=tk.LEFT)
447 tk_Frame.pack(side=tk.TOP, fill=tk.X)
453 tk_Entry_comb = tk.Entry(tk_Win, width=100, font=tk_Font_monospace)
454 tk_Entry_comb.bind(
'<Return>', cmd_comb_update)
455 tk_Entry_comb.pack(side=tk.TOP, fill=tk.X)
457 tk_Frame = tk.Frame(tk_Win)
459 tk_Label_comb = tk.Label(tk_Frame, font=tk_Font_monospace)
460 tk_Label_comb.pack(side=tk.LEFT, fill=tk.X)
462 tk_Frame.pack(side=tk.TOP, fill=tk.X)
465 tk_Frame = tk.Frame(tk_Win)
467 tk_Listbox_combeval = tk.Listbox(tk_Frame, width=120, height=20, font=tk_Font_monospace)
468 tk_Listbox_combeval.grid(sticky=tk.N + tk.S)
471 tk_Scrollbar = tk.Scrollbar(tk_Frame)
472 tk_Listbox_combeval.config(yscrollcommand=tk_Scrollbar.set)
473 tk_Scrollbar.config(command=tk_Listbox_combeval.yview)
474 tk_Scrollbar.grid(row=0, column=1, sticky=tk.N + tk.S)
476 tk_Scrollbar = tk.Scrollbar(tk_Frame, orient=tk.HORIZONTAL)
477 tk_Listbox_combeval.config(xscrollcommand=tk_Scrollbar.set)
478 tk_Scrollbar.config(command=tk_Listbox_combeval.xview)
479 tk_Scrollbar.grid(row=1, sticky=tk.E + tk.W)
481 tk_Frame.pack(side=tk.TOP, fill=tk.X)
485 tk_Frame = tk.Frame(tk_Win)
488 tk.Checkbutton(tk_Frame, text=
'All parenthesis ', variable=tk_Var_show_allparent,
489 command=cmd_comb_update).pack(side=tk.LEFT)
490 tk.Checkbutton(tk_Frame, text=
'Space ', variable=tk_Var_show_space,
491 command=cmd_comb_update).pack(side=tk.LEFT)
493 tk.Checkbutton(tk_Frame, text=
'Delay:', variable=tk_Var_sleep,
494 command=cmd_change_delay).pack(side=tk.LEFT)
496 tk_Entry_delay = tk.Entry(tk_Frame, width=5)
497 tk_Entry_delay.insert(tk.END, str(sleep_delay))
498 tk_Entry_delay.bind(
'<Return>', cmd_change_delay)
499 tk_Entry_delay.pack(side=tk.LEFT)
500 tk.Label(tk_Frame, text=
'ms').pack(side=tk.LEFT)
502 tk.Label(tk_Frame, text=
' Step:').pack(side=tk.LEFT)
504 tk_Entry_step = tk.Entry(tk_Frame, width=5)
505 tk_Entry_step.insert(tk.END, str(nb_eval_step))
506 tk_Entry_step.bind(
'<Return>', cmd_change_step)
507 tk_Entry_step.pack(side=tk.LEFT)
509 tk.Label(tk_Frame, text=
' Nb triggers: ').pack(side=tk.LEFT)
511 tk_Label_nb_eval = tk.Label(tk_Frame, text=
'0')
512 tk_Label_nb_eval.pack(side=tk.LEFT)
514 tk_Frame.pack(side=tk.TOP, fill=tk.X)
520 tk_Win.bind(
'<Escape>', cmd_clear)