forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·199 lines (170 loc) · 6.42 KB
/
Copy pathserver.py
File metadata and controls
executable file
·199 lines (170 loc) · 6.42 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
"""Nulang Playground Server — serves index.html and runs Nulang code via POST /run, /compile, /check.
Usage:
python3 playground/server.py [--port 8080] [--nulang ./target/debug/nulang]
"""
import http.server
import json
import os
import subprocess
import sys
import urllib.parse
from pathlib import Path
PLAYGROUND_DIR = Path(__file__).resolve().parent
INDEX_HTML = (PLAYGROUND_DIR / "index.html").read_text()
NULANG_BIN = os.environ.get("NULANG_PATH", None)
# Try to find the nulang binary
if not NULANG_BIN:
candidates = [
Path(__file__).resolve().parent.parent / "target" / "debug" / "nulang",
Path(__file__).resolve().parent.parent / "target" / "release" / "nulang",
]
for c in candidates:
if c.exists():
NULANG_BIN = str(c)
break
if not NULANG_BIN:
# Try to find in PATH
import shutil
NULANG_BIN = shutil.which("nulang") or "nulang"
class Handler(http.server.BaseHTTPRequestHandler):
def send_json(self, data):
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode())
def send_error_response(self, data):
self.send_response(400)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data).encode())
def do_GET(self):
if self.path == "/" or self.path == "/index.html":
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(INDEX_HTML.encode())
else:
self.send_response(404)
self.end_headers()
def do_POST(self):
if self.path == "/run":
self.handle_run()
elif self.path == "/compile":
self.handle_compile()
elif self.path == "/check":
self.handle_check()
else:
self.send_response(404)
self.end_headers()
def handle_run(self):
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length).decode('utf-8')
try:
data = json.loads(body)
code = data.get('code', '')
except:
self.send_error_response({'ok': False, 'stderr': 'Invalid JSON'})
return
try:
# Write code to temp file
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.nula', delete=False) as f:
f.write(code)
temp_file = f.name
result = subprocess.run(
[NULANG_BIN, '--backend', 'native', temp_file],
capture_output=True,
text=True,
timeout=30
)
os.unlink(temp_file)
self.send_json({
'ok': result.returncode == 0,
'stdout': result.stdout,
'stderr': result.stderr
})
except subprocess.TimeoutExpired:
self.send_json({'ok': False, 'stderr': 'Execution timed out (30s)'})
except Exception as e:
self.send_json({'ok': False, 'stderr': str(e)})
def handle_compile(self):
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length).decode('utf-8')
try:
data = json.loads(body)
code = data.get('code', '')
target = data.get('target', 'native')
except:
self.send_error_response({'ok': False, 'stderr': 'Invalid JSON'})
return
try:
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.nula', delete=False) as f:
f.write(code)
temp_file = f.name
result = subprocess.run(
[NULANG_BIN, '--backend', 'native', '--target', target, '--emit-nbc', temp_file],
capture_output=True,
text=True,
timeout=30
)
os.unlink(temp_file)
self.send_json({
'ok': result.returncode == 0,
'stdout': result.stdout,
'stderr': result.stderr
})
except subprocess.TimeoutExpired:
self.send_json({'ok': False, 'stderr': 'Compilation timed out (30s)'})
except Exception as e:
self.send_json({'ok': False, 'stderr': str(e)})
def handle_check(self):
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length).decode('utf-8')
try:
data = json.loads(body)
code = data.get('code', '')
except:
self.send_error_response({'ok': False, 'stderr': 'Invalid JSON'})
return
try:
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.nula', delete=False) as f:
f.write(code)
temp_file = f.name
result = subprocess.run(
[NULANG_BIN, '--check', temp_file],
capture_output=True,
text=True,
timeout=30
)
os.unlink(temp_file)
self.send_json({
'ok': result.returncode == 0,
'stdout': result.stdout,
'stderr': result.stderr
})
except subprocess.TimeoutExpired:
self.send_json({'ok': False, 'stderr': 'Type check timed out (30s)'})
except Exception as e:
self.send_json({'ok': False, 'stderr': str(e)})
def log_message(self, format, *args):
sys.stderr.write("[playground] %s\n" % (format % args))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=8080)
parser.add_argument('--nulang', type=str, default=None)
args = parser.parse_args()
if args.nulang:
NULANG_BIN = args.nulang
port = args.port
server = http.server.HTTPServer(('', port), Handler)
print(f"Nulang Playground running on http://localhost:{port}")
print(f"Using nulang binary: {NULANG_BIN}")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nShutting down...")
server.server_close()