33import compiler as c
44import os
55import tempfile
6+ import subprocess
67
78def transpile_code ():
89 code = code_input .get ("1.0" , tk .END )
10+ lang = lang_var .get ()
911 compiler = c .BScriptCompiler ()
1012 try :
11- c_code = compiler .transpile (code )
13+ output_code = compiler .transpile (code , lang = lang )
1214 output .delete ("1.0" , tk .END )
13- output .insert (tk .END , c_code )
15+ output .insert (tk .END , output_code )
16+ except NotImplementedError as nie :
17+ messagebox .showerror ("Not Supported" , str (nie ))
1418 except Exception as e :
1519 messagebox .showerror ("Error" , str (e ))
1620
17- def save_c_code ():
18- c_code = output .get ("1.0" , tk .END )
19- if not c_code .strip ():
20- messagebox .showwarning ("Warning" , "No C code to save." )
21+ def save_code ():
22+ code = output .get ("1.0" , tk .END )
23+ if not code .strip ():
24+ messagebox .showwarning ("Warning" , "No code to save." )
2125 return
22- file_path = filedialog .asksaveasfilename (defaultextension = ".c" , filetypes = [("C Files" , "*.c" )])
26+ ext = ".c" if lang_var .get () == "c" else ".js"
27+ file_path = filedialog .asksaveasfilename (defaultextension = ext , filetypes = [(f"{ lang_var .get ().upper ()} Files" , f"*{ ext } " )])
2328 if file_path :
2429 with open (file_path , "w" ) as f :
25- f .write (c_code )
26- messagebox .showinfo ("Saved" , f"C code saved to { file_path } " )
30+ f .write (code )
31+ messagebox .showinfo ("Saved" , f"Code saved to { file_path } " )
2732
2833def load_bs_file ():
2934 file_path = filedialog .askopenfilename (filetypes = [("BScript Files" , "*.bs" )])
@@ -33,31 +38,70 @@ def load_bs_file():
3338 code_input .delete ("1.0" , tk .END )
3439 code_input .insert (tk .END , code )
3540
36- def compile_c_code ( ):
37- c_code = output . get ( "1.0" , tk . END )
38- if not c_code . strip ():
39- messagebox . showwarning ( "Warning" , "No C code to compile." )
41+ def compile_js_code ( js_code ):
42+ # Ask user to select folder to save index.html and script.js
43+ folder = filedialog . askdirectory ( title = "Select folder to save JS app" )
44+ if not folder :
4045 return
4146
42- # Save C code to a temporary file
43- with tempfile .NamedTemporaryFile (delete = False , suffix = ".c" ) as tmp_c :
44- tmp_c .write (c_code .encode ())
45- c_file = tmp_c .name
47+ script_path = os .path .join (folder , "script.js" )
48+ index_path = os .path .join (folder , "index.html" )
4649
47- # Ask user where to save the compiled binary
48- output_path = filedialog .asksaveasfilename (defaultextension = "" , filetypes = [("Executable" , "" )])
49- if not output_path :
50- os .remove (c_file )
50+ # Write script.js
51+ with open (script_path , "w" ) as f :
52+ f .write (js_code )
53+
54+ # Write index.html that links script.js
55+ html_content = """<!DOCTYPE html>
56+ <html lang="en">
57+ <head>
58+ <meta charset="UTF-8" />
59+ <title>BScript JS Output</title>
60+ </head>
61+ <body>
62+ <script src="script.js"></script>
63+ </body>
64+ </html>
65+ """
66+ with open (index_path , "w" ) as f :
67+ f .write (html_content )
68+
69+ messagebox .showinfo ("Success" , f"JS app saved:\n { index_path } \n { script_path } " )
70+
71+
72+ def compile_code ():
73+ lang = lang_var .get ()
74+ code = output .get ("1.0" , tk .END ).strip ()
75+ if not code :
76+ messagebox .showwarning ("Warning" , "No code to compile." )
5177 return
5278
53- compiler = c .BScriptCompiler ()
54- try :
55- compiler .compile (c_file , output_path )
56- messagebox .showinfo ("Success" , f"Compiled successfully to { output_path } " )
57- except Exception as e :
58- messagebox .showerror ("Compile Error" , str (e ))
59- finally :
60- os .remove (c_file )
79+ if lang == "c" :
80+ # Your existing C compile code here...
81+ with tempfile .NamedTemporaryFile (delete = False , suffix = ".c" ) as tmp_c :
82+ tmp_c .write (code .encode ())
83+ c_file = tmp_c .name
84+
85+ output_path = filedialog .asksaveasfilename (defaultextension = "" , filetypes = [("Executable" , "" )])
86+ if not output_path :
87+ os .remove (c_file )
88+ return
89+
90+ compiler = c .BScriptCompiler ()
91+ try :
92+ compiler .compile (c_file , output_path )
93+ messagebox .showinfo ("Success" , f"Compiled successfully to { output_path } " )
94+ except Exception as e :
95+ messagebox .showerror ("Compile Error" , str (e ))
96+ finally :
97+ os .remove (c_file )
98+
99+ elif lang == "js" :
100+ compile_js_code (code )
101+
102+ else :
103+ messagebox .showwarning ("Compile Not Supported" , f"Compile not supported for language: { lang } " )
104+
61105
62106root = tk .Tk ()
63107root .title ("BScript Compiler GUI" )
@@ -68,14 +112,19 @@ def compile_c_code():
68112code_input = scrolledtext .ScrolledText (root , width = 60 , height = 15 )
69113code_input .pack (padx = 10 , pady = 5 )
70114
71- tk .Button (root , text = "Transpile to C" , command = transpile_code ).pack (pady = 5 )
115+ tk .Label (root , text = "Select Output Language:" ).pack (anchor = "w" , padx = 10 )
116+ lang_var = tk .StringVar (value = "c" )
117+ lang_dropdown = tk .OptionMenu (root , lang_var , "c" , "js" )
118+ lang_dropdown .pack (padx = 10 , pady = 5 )
119+
120+ tk .Button (root , text = "Transpile to Selected Language" , command = transpile_code ).pack (pady = 5 )
72121
73- tk .Label (root , text = "Generated C Code:" ).pack (anchor = "w" )
122+ tk .Label (root , text = "Generated Code:" ).pack (anchor = "w" )
74123output = scrolledtext .ScrolledText (root , width = 60 , height = 15 )
75124output .pack (padx = 10 , pady = 5 )
76125
77- tk .Button (root , text = "Save C Code" , command = save_c_code ).pack (pady = 5 )
126+ tk .Button (root , text = "Save Code" , command = save_code ).pack (pady = 5 )
78127
79- tk .Button (root , text = "Compile C Code" , command = compile_c_code ).pack (pady = 5 )
128+ tk .Button (root , text = "Compile C Code" , command = compile_code ).pack (pady = 5 )
80129
81- root .mainloop ()
130+ root .mainloop ()
0 commit comments