-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_v2.shl
More file actions
90 lines (70 loc) · 3.55 KB
/
compiler_v2.shl
File metadata and controls
90 lines (70 loc) · 3.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
use python "subprocess"
use python "os"
use python "os.path" as path
use python "sys"
use python "hashlib"
use python "time"
define page CompilerPage
html
head
title "Interactive Playground - ShellLite"
link rel="stylesheet" href="/static/style.css?v=0.6.1"
meta charset="utf-8"
meta name="viewport" content="width=device-width, initial-scale=1.0"
body
GlobalNavigation
div class="container" style="padding-top: 40px;"
div class="compiler-container"
div class="compiler-header"
div style="display: flex; align-items: center; gap: 12px;"
span style="font-size: 0.85rem; font-weight: 600; color: #94a3b8;" "main.shl"
button class="run-btn" onclick="runCode()" "Run"
div class="compiler-body"
div class="editor-pane"
div id="line-numbers" class="line-numbers" "1"
div class="editor-wrapper"
textarea id="code-input" spellcheck="false" "say 'Welcome to the ShellLite Playground!'"
div class="output-pane"
div class="output-header"
span "TERMINAL"
button class="btn" style="background: transparent; color: #64748b; font-size: 0.7rem; padding: 4px 8px;" onclick="clearOutput()" "CLEAR"
div id="output-display" "// Program output will appear here..."
div style="margin-top: 20px; color: #64748b; font-size: 0.9rem; text-align: center;"
p "Tip: Your code is cached for 10 minutes. Running the same code twice is instant."
script src="/static/compiler.js"
UniversalFooter
when someone visits "/compiler"
CompilerPage
when someone submits to "/api/run"
received_data = request["json"]
program_code = received_data["code"]
if program_code is ""
program_code = "say 'Hello World'"
current_time = time.time()
for entry in os.listdir(".")
if entry.startswith("run_")
file_stats = os.stat(entry)
file_age = current_time - file_stats.st_mtime
if file_age > 600
os.remove(entry)
content_hash = hashlib.md5(program_code.encode("utf-8")).hexdigest()
script_filename = "run_" + content_hash + ".shl"
if path.exists(script_filename) is false
write program_code to file script_filename
execution_env = os.environ.copy()
shell_command = list of sys.executable, "-m", "shell_lite.main", "run", script_filename
final_output = "Error: Execution failed"
try
execution_result = subprocess.run(shell_command, capture_output=yes, text=yes, timeout=5, env=execution_env)
standard_output = execution_result.stdout
error_output = execution_result.stderr
final_output = standard_output
if final_output is "" or final_output is None
final_output = error_output
if final_output is not None
final_output = final_output.trim()
catch execution_error
final_output = "Execution Error: " + str(execution_error)
if final_output is "" or final_output is None or final_output == "None"
final_output = "Executed successfully (No Output)"
return final_output