-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_code.py
More file actions
87 lines (73 loc) · 3.72 KB
/
Copy pathexecute_code.py
File metadata and controls
87 lines (73 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# execute_code.py
#
# LAROCHE Tristan
# 31-01-2023
#==================================
from ExecuteInstructions import ExecuteInstructions
from Tape import Tape
import tkinter as tk
import tkinter.filedialog as fd
def import_code(self, file=""):
"""Imports file if specified otherwise asks to opens one then starts the program
Args:
file (str, optional): The path of the file you want to open, used to load templates. Defaults to "" for nothing.
"""
if file == "":
open_file = fd.askopenfilename(filetypes=[('Python Turing Machine (.ptm)', '*.ptm')])
else:
open_file = file
if open_file == "":
return
self.preview_tab()
self.instructions.clear()
self.execution = ExecuteInstructions()
self.instructions = self.execution.process(open_file)
if type(self.instructions) is str:
self.error_window = tk.Toplevel(self.root)
self.error_window.title("Error")
tk.Label(self.error_window, text= self.instructions).pack()
tk.Button(self.error_window, text= "Quit!", command= self.error_window.destroy).pack()
for i in self.instructions.keys():
self.new_state(self.instructions[i])
def execute_code(self, ui, instructions: dict, delay):
"""Starts the programs with the properly formatted file. Called by 'import_code'.
Args:
instructions (dict): Contains every instructions of the program loaded.
"""
if instructions == {}:
return
self.execution.start(ui, instructions, self.current_value, self.next_value, self.color_highlight, self.tape_memory, delay)
def move(self, ui, direction: str):
"""Moves left or right on the 'tape' (tape_memory.tape) based on the parameter.
Args:
direction (str): '<' or '>' for the direction.
"""
self.tape_memory.move(ui, direction)
tape_index = self.tape_memory.position
tape_index_value = self.tape_memory.read(tape_index)
self.current_value.config(text=tape_index_value)
def write(self, ui, value: str):
"""Writes a value on the 'tape' (tape_memory.tape) based on the parameter.
Args:
value (str): 'b', '0' or '1' for the values to write.
"""
self.tape_memory.write(ui, self.tape_memory.position, value)
tape_index = self.tape_memory.position
tape_index_value = self.tape_memory.read(tape_index)
self.current_value.config(text=tape_index_value)
def clear_tape(self, ui):
"""Clears the 'tape' (tape_memory) by instantiating the class again. The previous tape will be left unused and eventually discarded by the garbage collector.
"""
self.tape_memory = Tape()
self.circles = {'14': [40, 20, 'white'], '13': [42, 40, 'white'], '12': [45, 60, 'white'], '11': [49, 80, 'white'], '10': [54, 100, 'white'],
'9': [60, 120, 'white'], '8': [68, 140, 'white'], '7': [78, 160, 'white'], '6': [90, 180, 'white'], '5': [104, 200, 'white'],
'4': [120, 220, 'white'], '3': [138, 240, 'white'], '2': [158, 260, 'white'], '1': [180, 270, 'white'], '0': [204, 275, 'white'],
'-1': [228, 270, 'white'], '-2': [250, 260, 'white'], '-3': [270, 240, 'white'], '-4':[288, 220, 'white'], '-5': [304, 200, 'white'],
'-6': [318, 180, 'white'], '-7': [330, 160, 'white'], '-8': [340, 140, 'white'], '-9': [348, 120, 'white'], '-10': [354, 100, 'white'],
'-11': [359, 80, 'white'], '-12': [363, 60, 'white'], '-13': [366, 40, 'white'], '-14': [368, 20, 'white']}
ui.canvas2.destroy()
ui.canvas2 = tk.Canvas(ui.right_tab, height=310, bg='AntiqueWhite2')
ui.canvas2.grid(row=2, column=0, columnspan=10, sticky='nesw')
for value in ui.circles.values() :
ui.drawcircle(ui.canvas2, value[0], value[1], 6, value[2])