-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_manager.py
More file actions
245 lines (216 loc) · 9.4 KB
/
table_manager.py
File metadata and controls
245 lines (216 loc) · 9.4 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from typing import Optional
class TableManager:
def __init__(self):
self.keywords_table = {}
self.operators_table = {}
self.identifiers_table = {}
# {name: {'index': int, 'described': bool, 'type': str}}
self.constants_table = {} # {value: index} - простая структура для констант
self.addresses_table = {} # {name: index} - таблица адресов для ПОЛИЗа
self.address_to_name = (
{}
) # {index: name} - обратное отображение для поиска по индексу
self.keywords_tree = None
self.operators_tree = None
self.identifiers_tree = None
self.constants_tree = None
self.initialize_tables()
def set_treeviews(
self, keywords_tree, operators_tree, identifiers_tree, constants_tree
):
self.keywords_tree = keywords_tree
self.operators_tree = operators_tree
self.identifiers_tree = identifiers_tree
self.constants_tree = constants_tree
self.update_tables_display()
def initialize_tables(self):
keywords = [
"dim",
"integer",
"real",
"boolean",
"as",
"if",
"then",
"else",
"for",
"to",
"do",
"while",
"read",
"write",
"or",
"and",
"not",
"true",
"false",
]
for i, keyword in enumerate(keywords, 1):
self.keywords_table[keyword] = i
operators = [
">",
"=",
"<",
"<=",
">=",
"<>",
"+",
"-",
"or",
"*",
"/",
"and",
"(",
")",
"[",
"]",
"{",
"}",
",",
";",
":",
":=",
".",
"not",
]
for i, operator in enumerate(operators, 1):
self.operators_table[operator] = i
def add_identifier(self, name):
"""Добавить идентификатор в таблицу"""
if name not in self.identifiers_table:
index = len(self.identifiers_table) + 1
self.identifiers_table[name] = {
"index": index,
"described": False,
"type": None,
}
return index
return self.identifiers_table[name]["index"]
def add_constant(self, value):
"""Добавить константу в таблицу"""
if value not in self.constants_table:
index = len(self.constants_table) + 1
self.constants_table[value] = index
return self.constants_table[value]
def set_identifier_info(self, name, described=None, type=None):
"""Установить информацию об идентификаторе"""
if name in self.identifiers_table:
if described is not None:
self.identifiers_table[name]["described"] = described
if type is not None:
self.identifiers_table[name]["type"] = type
def get_identifier_info(self, name):
"""Получить информацию об идентификаторе"""
return self.identifiers_table.get(name)
def update_identifier_table_display(self):
"""Обновляет отображение таблицы идентификаторов с семантической информацией"""
if self.identifiers_tree:
self.identifiers_tree.delete(*self.identifiers_tree.get_children())
# Обновляем колонки для таблицы идентификаторов
columns = ("№", "Лексема", "Описан", "Тип", "Код")
self.identifiers_tree.configure(columns=columns)
# Устанавливаем заголовки
self.identifiers_tree.heading("№", text="№")
self.identifiers_tree.heading("Лексема", text="Лексема")
self.identifiers_tree.heading("Описан", text="Описан")
self.identifiers_tree.heading("Тип", text="Тип")
self.identifiers_tree.heading("Код", text="Код")
# Устанавливаем ширины колонок
self.identifiers_tree.column("№", width=50)
self.identifiers_tree.column("Лексема", width=150)
self.identifiers_tree.column("Описан", width=80)
self.identifiers_tree.column("Тип", width=100)
self.identifiers_tree.column("Код", width=80)
# Заполняем данные
for name, info in self.identifiers_table.items():
described = "Да" if info["described"] else "Нет"
type_str = info["type"] if info["type"] else "—"
self.identifiers_tree.insert(
"",
"end",
values=(
info["index"],
name,
described,
type_str,
f"(4,{info['index']})",
),
)
def update_tables_display(self):
if self.keywords_tree:
self.keywords_tree.delete(*self.keywords_tree.get_children())
for keyword, code in self.keywords_table.items():
self.keywords_tree.insert(
"", "end", values=(code, keyword, f"(1,{code})")
)
if self.operators_tree:
self.operators_tree.delete(*self.operators_tree.get_children())
for operator, code in self.operators_table.items():
self.operators_tree.insert(
"", "end", values=(code, operator, f"(2,{code})")
)
if self.constants_tree:
self.constants_tree.delete(*self.constants_tree.get_children())
for constant, index in self.constants_table.items():
self.constants_tree.insert(
"", "end", values=(index, constant, f"(3,{index})")
)
# Используем новый метод для идентификаторов
self.update_identifier_table_display()
def clear_dynamic_tables(self):
self.identifiers_table.clear()
self.constants_table.clear()
self.addresses_table.clear()
self.address_to_name.clear()
self.update_tables_display()
def add_address(self, identifier_name: str) -> int:
"""Добавить адрес переменной в таблицу адресов"""
if identifier_name not in self.addresses_table:
info = self.get_identifier_info(identifier_name)
if info:
index = info["index"]
self.addresses_table[identifier_name] = index
self.address_to_name[index] = identifier_name
return index
else:
# Если идентификатора нет, создаем временную запись
index = len(self.addresses_table) + 1
self.addresses_table[identifier_name] = index
self.address_to_name[index] = identifier_name
return index
return self.addresses_table[identifier_name]
def get_address_index(self, identifier_name: str) -> int:
"""Получить индекс адреса переменной"""
return self.addresses_table.get(identifier_name, 0)
def get_address_name(self, index: int) -> Optional[str]:
"""Получить имя переменной по индексу адреса"""
return self.address_to_name.get(index)
def get_address_info(self, address_index: int) -> Optional[dict]:
"""Получает информацию о переменной по индексу адреса"""
# Ищем имя переменной по адресу
var_name = self.get_address_name(address_index)
if var_name and var_name in self.identifiers_table:
info = self.identifiers_table[var_name].copy()
info["name"] = var_name
info["index"] = address_index
return info
# Если не нашли по имени, ищем в таблице идентификаторов по индексу
for name, info in self.identifiers_table.items():
if info["index"] == address_index:
result = info.copy()
result["name"] = name
result["index"] = address_index
return result
return None
def get_identifier_by_index(self, index: int) -> Optional[str]:
"""Получить имя идентификатора по его индексу"""
for name, info in self.identifiers_table.items():
if info["index"] == index:
return name
return None
def get_constant_by_index(self, index: int) -> Optional[str]:
"""Получить значение константы по её индексу"""
for const, idx in self.constants_table.items():
if idx == index:
return const
return None