-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbscript-gui.py
More file actions
149 lines (119 loc) · 4.45 KB
/
bscript-gui.py
File metadata and controls
149 lines (119 loc) · 4.45 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
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
import compiler as c
import os
import tempfile
import subprocess
import pixeleditor
def transpile_code():
code = code_input.get("1.0", tk.END)
lang = lang_var.get()
compiler = c.BScriptCompiler()
try:
output_code = compiler.transpile(code, lang=lang)
output.delete("1.0", tk.END)
output.insert(tk.END, output_code)
except NotImplementedError as nie:
messagebox.showerror("Not Supported", str(nie))
except Exception as e:
messagebox.showerror("Error", str(e))
def save_code():
code = output.get("1.0", tk.END)
if not code.strip():
messagebox.showwarning("Warning", "No code to save.")
return
ext = ".c" if lang_var.get() == "c" else ".js"
file_path = filedialog.asksaveasfilename(defaultextension=ext, filetypes=[(f"{lang_var.get().upper()} Files", f"*{ext}")])
if file_path:
with open(file_path, "w") as f:
f.write(code)
messagebox.showinfo("Saved", f"Code saved to {file_path}")
def load_bs_file():
file_path = filedialog.askopenfilename(filetypes=[("BScript Files", "*.bs")])
if file_path:
with open(file_path, "r") as f:
code = f.read()
code_input.delete("1.0", tk.END)
code_input.insert(tk.END, code)
def compile_js_code(js_code):
# Ask user to select folder to save index.html and script.js
folder = filedialog.askdirectory(title="Select folder to save JS app")
if not folder:
return
script_path = os.path.join(folder, "script.js")
index_path = os.path.join(folder, "index.html")
# Write script.js
with open(script_path, "w") as f:
f.write(js_code)
# Write index.html that links script.js
html_content = """<!DOCTYPE html>
<html>
<head>
<title>BScript Output</title>
<style>
#console {
background: #000;
color: #0f0;
font-family: monospace;
padding: 10px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<div id="console"></div>
<script src="script.js"></script>
</body>
</html>
"""
with open(index_path, "w") as f:
f.write(html_content)
messagebox.showinfo("Success", f"JS app saved:\n{index_path}\n{script_path}")
def compile_code():
lang = lang_var.get()
code = output.get("1.0", tk.END).strip()
if not code:
messagebox.showwarning("Warning", "No code to compile.")
return
if lang == "c":
# Your existing C compile code here...
with tempfile.NamedTemporaryFile(delete=False, suffix=".c") as tmp_c:
tmp_c.write(code.encode())
c_file = tmp_c.name
output_path = filedialog.asksaveasfilename(defaultextension="", filetypes=[("Executable", "")])
if not output_path:
os.remove(c_file)
return
compiler = c.BScriptCompiler()
try:
compiler.compile(c_file, output_path)
messagebox.showinfo("Success", f"Compiled successfully to {output_path}")
except Exception as e:
messagebox.showerror("Compile Error", str(e))
finally:
os.remove(c_file)
elif lang == "js":
compile_js_code(code)
else:
messagebox.showwarning("Compile Not Supported", f"Compile not supported for language: {lang}")
root = tk.Tk()
root.title("BScript Compiler GUI")
icon = tk.PhotoImage(file="assets/icon.png")
root.iconphoto(True, icon)
tk.Button(root, text="Pixel Editor (Beta)", command=pixeleditor.pixelEditor).pack(pady=5)
tk.Label(root, text="It is not recommended to write code using this editor,\nuse an actual IDE for that!").pack(pady=10)
tk.Button(root, text="Load BScript File", command=load_bs_file).pack(pady=5)
tk.Label(root, text="BScript Code:").pack(anchor="w")
code_input = scrolledtext.ScrolledText(root, width=60, height=15)
code_input.pack(padx=10, pady=5)
tk.Label(root, text="Select Output Language:").pack(anchor="w", padx=10)
lang_var = tk.StringVar(value="c")
lang_dropdown = tk.OptionMenu(root, lang_var, "c", "js")
lang_dropdown.pack(padx=10, pady=5)
tk.Button(root, text="Transpile to Selected Language", command=transpile_code).pack(pady=5)
tk.Label(root, text="Generated Code:").pack(anchor="w")
output = scrolledtext.ScrolledText(root, width=60, height=15)
output.pack(padx=10, pady=5)
tk.Button(root, text="Save Code", command=save_code).pack(pady=5)
tk.Button(root, text="Compile Code", command=compile_code).pack(pady=5)
root.mainloop()