forked from thobidogs/Boompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
194 lines (159 loc) · 8.55 KB
/
Copy pathgui.py
File metadata and controls
194 lines (159 loc) · 8.55 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
import tkinter as tk
from tkinter import filedialog, messagebox
from lexical_analyzer import LexicalAnalyzer
from syntax_analyzer import SyntaxAnalyzer
from semantic_analyzer import SemanticAnalyzer
class MiniCompilerGUI:
def __init__(self, root):
self.root = root
self.root.title("Boompiler")
self.root.geometry("1050x750")
# Initialize analyzers
self.lexical_analyzer = LexicalAnalyzer()
self.syntax_analyzer = SyntaxAnalyzer()
self.semantic_analyzer = SemanticAnalyzer()
# Default mode is Light
self.is_dark_mode = False
# Setup UI (☞゚ヮ゚)☞
self.initialize_ui()
def initialize_ui(self):
# Header setup
self.header = tk.Frame(self.root, width=150)
self.header.pack(side="top", fill="y", padx=10, pady=10)
head_style = {"bg": "#f5f5f5", "fg": "#191919", "bd": 0, "font": ("Arial", 28, "bold")}
self.headLabel = tk.Label(self.header, text="ִ ⸜(。˃ ᵕ ˂ )⸝♡ BOOMPILER ദ്ദി(˵ •̀ ᴗ - ˵ )", **head_style)
self.headLabel.pack(pady=10)
desc_style = {"bg": "#f5f5f5", "fg": "#191919", "bd": 0, "font": ("Arial", 12, "italic")}
self.descLabel = tk.Label(self.header, text="---------- By Pia Macalanda, Juliana Mancera, & Thoby Ralleta ----------", **desc_style)
self.descLabel.pack(pady=5)
# Sidebar setup with buttons
self.sidebar = tk.Frame(self.root, width=150)
self.sidebar.pack(side="top", fill="y", padx=10, pady=10)
button_style = {"bg": "#f5f5f5", "fg": "#191919", "bd": 0, "font": ("Arial", 12, "bold")}
self.open_file_button = tk.Button(self.sidebar, text="Open File", command=self.open_file, **button_style, width=15)
self.lexical_analysis_button = tk.Button(self.sidebar, text="Lexical Analysis", command=self.lexical_analysis, state="disabled", **button_style, width=15)
self.syntax_analysis_button = tk.Button(self.sidebar, text="Syntax Analysis", command=self.syntax_analysis, state="disabled", **button_style, width=15)
self.semantic_analysis_button = tk.Button(self.sidebar, text="Semantic Analysis", command=self.semantic_analysis, state="disabled", **button_style, width=15)
self.clear_button = tk.Button(self.sidebar, text="Clear", command=self.clear, **button_style, width=15)
self.toggle_mode_button = tk.Button(self.sidebar, text="࣪ ִֶָ☾.", command=self.toggle_mode, **button_style, width=5)
# Pack button
buttons = [
self.open_file_button, self.lexical_analysis_button, self.syntax_analysis_button,
self.semantic_analysis_button, self.clear_button, self.toggle_mode_button
]
for button in buttons:
button.pack(side=tk.LEFT, fill="y", pady=10, padx=10)
self.add_hover_effect(button)
# Main Content Area displaying code and results
self.main_content = tk.Frame(self.root, padx=10, pady=10)
self.main_content.pack(side="right", fill="both", expand=True, padx=10, pady=5)
self.code_label = tk.Label(self.main_content, text="CODE:", font=("Arial", 12, "bold"))
self.code_label.pack(anchor="w")
self.code_text = tk.Text(self.main_content, height=10, wrap="word", borderwidth=2, relief="solid", font=("Courier", 12, "normal"), state="disabled")
self.code_text.pack(fill="both", expand=True, pady=5)
self.result_label = tk.Label(self.main_content, text="RESULT:", font=("Arial", 12, "bold"))
self.result_label.pack(anchor="w")
self.result_text = tk.Text(self.main_content, height=10, wrap="word", borderwidth=2, relief="solid", font=("Courier", 12, "normal"), state="disabled")
self.result_text.pack(fill="both", expand=True, pady=5)
self.apply_styles()
def add_hover_effect(self, button):
def on_enter(e):
button['bg'] = "#cccccc" if not self.is_dark_mode else "#555555"
def on_leave(e):
button['bg'] = "#f5f5f5" if not self.is_dark_mode else "#333333"
button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
def apply_styles(self):
if self.is_dark_mode:
bg_color = "#191919"
fg_color = "#ffffff"
self.root.configure(bg=bg_color)
self.header.configure(bg=bg_color)
self.sidebar.configure(bg=bg_color)
self.main_content.configure(bg=bg_color)
else:
bg_color = "#f5f5f5"
fg_color = "#191919"
self.root.configure(bg=bg_color)
self.header.configure(bg=bg_color)
self.sidebar.configure(bg=bg_color)
self.main_content.configure(bg=bg_color)
self.update_ui_colors(bg_color, fg_color)
def update_ui_colors(self, bg_color, fg_color):
self.headLabel.config(bg=bg_color, fg=fg_color)
self.descLabel.config(bg=bg_color, fg=fg_color)
button_style = {"bg": "#f5f5f5" if not self.is_dark_mode else "#333333", "fg": fg_color, "bd": 0, "font": ("Arial", 12, "bold")}
for button in [self.open_file_button, self.lexical_analysis_button, self.syntax_analysis_button, self.semantic_analysis_button, self.clear_button, self.toggle_mode_button]:
button.config(**button_style)
self.code_text.config(bg="#ffffff" if not self.is_dark_mode else "#333333", fg=fg_color)
self.result_text.config(bg="#ffffff" if not self.is_dark_mode else "#333333", fg=fg_color)
self.code_label.config(bg=bg_color, fg=fg_color)
self.result_label.config(bg=bg_color, fg=fg_color)
def toggle_mode(self):
self.is_dark_mode = not self.is_dark_mode
self.apply_styles()
def open_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
if file_path:
with open(file_path, 'r') as file:
code = file.read()
self.display_code(code)
self.enable_button(self.lexical_analysis_button)
else:
messagebox.showinfo("No file selected", "Please select a valid file.")
def display_code(self, code):
self.code_text.config(state="normal")
self.code_text.delete("1.0", tk.END)
self.code_text.insert("1.0", code)
self.code_text.config(state="disabled")
def display_result(self, result):
self.result_text.config(state="normal")
self.result_text.insert(tk.END, result + "\n")
self.result_text.config(state="disabled")
def clear(self):
self.code_text.config(state="normal")
self.code_text.delete("1.0", tk.END)
self.result_text.config(state="normal")
self.result_text.delete("1.0", tk.END)
self.result_text.config(state="disabled")
self.disable_all_buttons()
self.enable_button(self.open_file_button)
def disable_all_buttons(self):
self.lexical_analysis_button.config(state="disabled")
self.syntax_analysis_button.config(state="disabled")
self.semantic_analysis_button.config(state="disabled")
def enable_button(self, button):
button.config(state="normal")
def lexical_analysis(self):
code = self.code_text.get("1.0", tk.END).strip()
tokens, error = self.lexical_analyzer.tokenize(code)
if error:
self.display_result(error)
return
self.display_result("Lexical Analysis Success! BOOM! \n--- Tokens ---")
for token in tokens:
self.display_result(f"{token['type']}: {token['value']}")
self.enable_button(self.syntax_analysis_button)
self.lexical_analysis_button.config(state="disabled")
def syntax_analysis(self):
tokens = self.lexical_analyzer.tokenize(self.code_text.get("1.0", tk.END).strip())[0]
result, error = self.syntax_analyzer.parse_syntax(tokens)
if not result:
self.display_result(error)
return
self.display_result("Syntax Analysis Success! BOOM! Syntax Free.")
self.enable_button(self.semantic_analysis_button)
self.syntax_analysis_button.config(state="disabled")
def semantic_analysis(self):
tokens = self.lexical_analyzer.tokenize(self.code_text.get("1.0", tk.END).strip())[0]
result, error = self.semantic_analyzer.check_semantics(tokens)
if not result:
self.display_result(error)
return
self.display_result("Semantic Analysis Success! BOOM! Semantics Valid.")
self.semantic_analysis_button.config(state="disabled")
# ╰(*°▽°*)╯
if __name__ == "__main__":
root = tk.Tk()
app = MiniCompilerGUI(root)
root.mainloop()