Skip to content

Commit 2517768

Browse files
committed
added custom console for JS
1 parent d08ad23 commit 2517768

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

238 Bytes
Binary file not shown.

bscript-gui.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,34 @@ def compile_js_code(js_code):
4747
script_path = os.path.join(folder, "script.js")
4848
index_path = os.path.join(folder, "index.html")
4949

50+
51+
5052
# Write script.js
5153
with open(script_path, "w") as f:
5254
f.write(js_code)
5355

5456
# Write index.html that links script.js
5557
html_content = """<!DOCTYPE html>
56-
<html lang="en">
58+
<html>
5759
<head>
58-
<meta charset="UTF-8" />
59-
<title>BScript JS Output</title>
60+
<title>BScript Output</title>
61+
<style>
62+
#console {
63+
background: #000;
64+
color: #0f0;
65+
font-family: monospace;
66+
padding: 10px;
67+
white-space: pre-wrap;
68+
}
69+
</style>
6070
</head>
6171
<body>
62-
<script src="script.js"></script>
72+
<div id="console"></div>
73+
<script src="script.js"></script>
6374
</body>
6475
</html>
6576
"""
77+
6678
with open(index_path, "w") as f:
6779
f.write(html_content)
6880

compiler.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,18 @@ def transpile_to_js(self, code: str) -> str:
3737

3838
js_lines = [
3939
'// Generated JavaScript code from BScript',
40+
''
41+
'function print(text) {',
42+
' const consoleEl = document.getElementById("console");',
43+
' if (consoleEl) {',
44+
' consoleEl.textContent += text + "\\n";',
45+
' } else {',
46+
' // fallback if no custom console',
47+
' console.log(text);',
48+
' }',
49+
'}',
4050
'let str = "";',
41-
'',
51+
''
4252
]
4353
global_vars = set()
4454
local_vars = set()
@@ -207,11 +217,11 @@ def indent():
207217
if m:
208218
expr = m.group(1).strip()
209219
if expr == 'str':
210-
js_lines.append(indent() + 'console.log(str);')
220+
js_lines.append(indent() + 'print(str);')
211221
elif expr.isdigit():
212-
js_lines.append(indent() + f'console.log({expr});')
222+
js_lines.append(indent() + f'print({expr});')
213223
else:
214-
js_lines.append(indent() + f'console.log({expr});')
224+
js_lines.append(indent() + f'print({expr});')
215225
i += 1
216226
continue
217227

@@ -224,7 +234,7 @@ def indent():
224234
continue
225235

226236
# Input (simplified): input
227-
if line == 'input':
237+
if line == 'input;':
228238
js_lines.append(indent() + 'str = prompt("Input:") || "";')
229239
i += 1
230240
continue
@@ -491,7 +501,7 @@ def transpile(self, code: str, lang: str = "c") -> str:
491501
continue
492502

493503
# Input
494-
if line == 'input':
504+
if line == 'input;':
495505
if self.in_function:
496506
self.c_lines.append(f'{self.indent()}fgets(str, sizeof(str), stdin);')
497507
else:

0 commit comments

Comments
 (0)