Repository navigation
Expand file tree
/
Copy pathcompiler_v2.shl
More file actions
142 lines (130 loc) · 6.68 KB
/
Copy pathcompiler_v2.shl
File metadata and controls
142 lines (130 loc) · 6.68 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
use "subprocess"
SHLCPP_BIN = "shlcpp"
to next_run_id()
give str(std_time_ms()) + "_" + str(randint(0, 999999))
define page compiler_page using
give std_ui_widget("html", {}, [
std_ui_widget("head", {}, [
std_ui_widget("title", {}, ["Interactive Playground - shlcpp"]),
std_ui_widget("link", {"rel": "stylesheet", "href": "/static/style.css?v=0.6.1"}, []),
std_ui_widget("meta", {"charset": "utf-8"}, []),
std_ui_widget("meta", {"name": "viewport", "content": "width=device-width, initial-scale=1.0"}, [])
]),
std_ui_widget("body", {}, [
global_navigation(),
std_ui_widget("div", {"class": "container", "style": "padding-top: 40px;"}, [
std_ui_widget("div", {"class": "compiler-container"}, [
std_ui_widget("div", {"class": "compiler-header"}, [
std_ui_widget("div", {"style": "display: flex; align-items: center; gap: 12px;"}, [
std_ui_widget("span", {"style": "font-size: 0.85rem; font-weight: 600; color: #94a3b8;"}, ["main.shl"])
]),
std_ui_widget("button", {"class": "run-btn", "onclick": "runCode()"}, ["Run"])
]),
std_ui_widget("div", {"class": "compiler-body"}, [
std_ui_widget("div", {"class": "editor-pane"}, [
std_ui_widget("div", {"id": "line-numbers", "class": "line-numbers"}, ["1"]),
std_ui_widget("div", {"class": "editor-wrapper"}, [
std_ui_widget("textarea", {"id": "code-input", "spellcheck": "false"}, ["say 'Welcome to the shlcpp Playground!'"])
])
]),
std_ui_widget("div", {"class": "output-pane"}, [
std_ui_widget("div", {"class": "output-header"}, [
std_ui_widget("span", {}, ["TERMINAL"]),
std_ui_widget("button", {"class": "btn", "style": "background: transparent; color: #64748b; font-size: 0.7rem; padding: 4px 8px;", "onclick": "clearOutput()"}, ["CLEAR"])
]),
std_ui_widget("div", {"id": "output-display"}, ["// Program output will appear here..."])
])
]),
std_ui_widget("div", {"class": "stdin-container"}, [
std_ui_widget("div", {"class": "stdin-header", "onclick": "toggleStdin()"}, [
std_ui_widget("div", {"style": "display: flex; align-items: center; gap: 8px;"}, [
std_ui_widget("span", {"style": "font-size: 0.8rem; font-weight: 600; color: #cbd5e1;"}, ["Standard Input (stdin)"]),
std_ui_widget("span", {"id": "stdin-badge", "class": "stdin-badge", "style": "display: none;"}, ["active"])
]),
std_ui_widget("span", {"id": "stdin-toggle-text", "style": "font-size: 0.75rem; color: #64748b;"}, ["Click to expand ▼"])
]),
std_ui_widget("div", {"id": "stdin-drawer", "class": "stdin-drawer", "style": "display: none;"}, [
std_ui_widget("p", {"style": "font-size: 0.75rem; color: #64748b; margin-bottom: 8px;"}, ["Provide input for ask / input statements (one line per prompt):"]),
std_ui_widget("textarea", {"id": "stdin-input", "placeholder": "e.g.\n50\n75\n62", "spellcheck": "false"}, [])
])
])
]),
std_ui_widget("div", {"style": "margin-top: 20px; color: #64748b; font-size: 0.9rem; text-align: center;"}, [
std_ui_widget("p", {}, ["Tip: press Ctrl+Enter to run your code."])
])
]),
std_ui_widget("script", {"src": "/static/compiler.js"}, []),
universal_footer()
])
])
to compiler_handler(req)
give "<!DOCTYPE html>" + compiler_page()
to api_run_handler(req)
output = "Error: could not run that code"
try
code = ""
stdin_val = ""
if dict_has(req, "body") and req["body"] != ""
try
data = json_parse(req["body"])
if dict_has(data, "code")
code = data["code"]
if dict_has(data, "stdin")
stdin_val = data["stdin"]
catch err_body
code = req["body"]
if code == ""
if dict_has(req, "query") and dict_has(req["query"], "code")
code = req["query"]["code"]
if stdin_val == ""
if dict_has(req, "query") and dict_has(req["query"], "stdin")
stdin_val = req["query"]["stdin"]
if code == ""
code = "say 'Welcome to the shlcpp Playground!'"
tag = next_run_id()
shl_file = "run_" + tag + ".shl"
out_file = "run_" + tag + ".out"
in_file = "run_" + tag + ".in"
has_custom_in = false
try
io_write(shl_file, code)
bin_cmd = SHLCPP_BIN
if std_os_name() == "windows"
if io_exists("../shlcpp/shlcpp.exe")
bin_cmd = "..\\shlcpp\\shlcpp.exe"
elif io_exists("shlcpp.exe")
bin_cmd = "shlcpp.exe"
in_redir = ""
if stdin_val != ""
io_write(in_file, stdin_val)
has_custom_in = true
in_redir = "< " + in_file
else
if std_os_name() == "windows"
in_redir = "< NUL"
else
in_redir = "< /dev/null"
cmd = bin_cmd + " " + shl_file + " " + in_redir + " > " + out_file + " 2>&1"
if std_os_name() == "linux"
cmd = "timeout 5 " + cmd
subprocess.run(cmd)
if io_exists(out_file)
output = io_read(out_file)
else
output = "No output produced"
finally
if io_exists(shl_file)
io_delete(shl_file)
if io_exists(out_file)
io_delete(out_file)
if has_custom_in and io_exists(in_file)
io_delete(in_file)
output = str_trim(output)
if output == ""
output = "Executed successfully (No Output)"
catch err
output = "Error: " + str(err)
give output
std_web_on_request("get", "/compiler", compiler_handler)
std_web_on_request("post", "/api/run", api_run_handler)
std_web_on_request("get", "/api/run", api_run_handler)