-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
201 lines (133 loc) · 6.02 KB
/
interface.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from tkinter import *
from tkinter import scrolledtext
from analisador_lexico import AnalisadorLexico
from analisador_sintatico import AnalisadorSintatico
class Application:
analisador_lexico = None
root = None
container_esquerdo = None
btn_analise = None
txt = None
items_result = []
def selectall(self, event):
event.widget.tag_add("sel", "1.0", "end")
def shortcut_run(self, event):
self.click_analisar()
def __init__(self, analisador_lexico: AnalisadorLexico, analisador_sintatico: AnalisadorSintatico):
self.analisador_lexico = analisador_lexico
self.analisador_sintatico = analisador_sintatico
def start(self):
self.root = Tk()
self.root.bind_class("Text", "<Control-a>", self.selectall)
self.root.bind_class("Text", "<Control-r>", self.shortcut_run)
w, h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
self.root.geometry("%dx%d+0+0" % (w, h))
self.root.update_idletasks()
self.root.update()
container_principal = Frame(self.root)
container_principal["padx"] = 50
container_principal.pack()
titulo = Label(container_principal, text="Compilador - Linguagem LMS")
titulo["font"] = ("Arial", "15", "bold")
titulo.pack()
container_direito = Frame(container_principal)
container_direito["pady"] = 10
container_direito.pack(side=LEFT)
container_meio = Frame(container_principal)
container_meio["padx"] = 20
container_meio.pack(side=LEFT)
container_esquerdo = Frame(container_principal)
container_esquerdo["padx"] = 20
container_esquerdo.pack(side=LEFT)
self.container_esquerdo = container_esquerdo
self.txt = self.create_textarea(container_direito)
self.btn_analise = Button(container_meio, text="Run >>>", fg="black", command=self.click_analisar)
self.btn_analise.pack()
#self.create_grid(container_esquerdo, [])
self.refresh()
def refresh(self):
self.root.update_idletasks()
self.root.update()
def create_textarea(self, container):
dica = Label(container, text="Atalho: Ctrl + r -> run")
dica["font"] = ("Arial", "11")
dica.grid()
textarea = scrolledtext.ScrolledText(container, width=80, height=50, undo=True)
textarea.grid(column=0, row=2)
return textarea
def create_grid(self, container, tokens: list):
items = []
label = Label(container, text="Código")
label["font"] = ("Arial", "11")
label.grid(row=0, column=0)
items.append(label)
label = Label(container, text="Token")
label["font"] = ("Arial", "11")
label.grid(row=0, column=1)
items.append(label)
label = Label(container, text="Descrição")
label["font"] = ("Arial", "11")
label.grid(row=0, column=2)
items.append(label)
# Create a frame for the canvas with non-zero row&column weights
frame_canvas = Frame(container)
frame_canvas.grid(row=2, column=0, columnspan=3)
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
# Set grid_propagate to False to allow 5-by-5 buttons resizing later
frame_canvas.grid_propagate(False)
# Add a canvas in that frame
canvas = Canvas(frame_canvas, bg="yellow")
canvas.grid(row=0, column=0, sticky="news")
# Link a scrollbar to the canvas
vsb = Scrollbar(frame_canvas, orient="vertical", command=canvas.yview)
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)
# Create a frame to contain the buttons
frame_buttons = Frame(canvas, bg="blue")
canvas.create_window((0, 0), window=frame_buttons, anchor='nw')
tabela_tokens = [[Entry() for j in range(3)] for i in enumerate(tokens)]
for i, token in enumerate(tokens):
r = i + 1
for j, valor in enumerate(token): # Columns
b = Entry(frame_buttons)
b.insert(0, token[valor])
items.append(b)
b.grid(row=r, column=j, sticky='news')
tabela_tokens[i][j] = b
# Update buttons frames idle tasks to let tkinter calculate buttons sizes
frame_buttons.update_idletasks()
# Resize the canvas frame to show exactly 5-by-5 buttons and the scrollbar
first5columns_width = sum([tabela_tokens[0][j].winfo_width() for j in range(0, 3)])
first5rows_height = sum([tabela_tokens[i][0].winfo_height() for i in range(0, 30)])
frame_canvas.config(width=first5columns_width + vsb.winfo_width(), height=first5rows_height)
# Set the canvas scrolling region
canvas.config(scrollregion=canvas.bbox("all"))
self.items_result.append(frame_canvas)
def create_grid_erro(self, container, erro):
label = Label(container, text=erro, fg="red")
label["font"] = ("Arial", "11")
label.grid(columnspan=20)
self.items_result.append(label)
def create_grid_sucesso(self, container, msg):
label = Label(container, text=msg, fg="green")
label["font"] = ("Arial", "11")
label.grid(columnspan=20)
self.items_result.append(label)
def click_analisar(self):
self.analisador_lexico.clear()
for widget in self.items_result:
widget.destroy()
code = self.txt.get("1.0", "end-1c")
self.analisador_lexico.add_codigo('', code)
tokens = []
try:
tokens = self.analisador_lexico.analise()
if tokens != None:
for i in tokens:
print(i)
print('======================================')
self.analisador_sintatico.analise(tokens)
self.create_grid_sucesso(self.container_esquerdo, 'Compilado com sucesso.')
except Exception as e:
self.create_grid_erro(self.container_esquerdo, e)