This repository was archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtryhy.hy
More file actions
63 lines (58 loc) · 2.5 KB
/
tryhy.hy
File metadata and controls
63 lines (58 loc) · 2.5 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
(import os
json
sys
[io [StringIO]]
[hy.cmdline [HyREPL]]
hy
[flask [Flask redirect request render-template-string]])
(setv index-template #[[<!DOCTYPE html>
<html>
<head>
<title>try-hylang</title>
<meta name="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="{{ url_for('static', filename='jquery-1.4.2.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='jquery.console.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='repl.js') }}"></script>
<script type="text/javascript">
var hy_version = '{{hy_version}}';
var server_software = '{{server_software}}';
</script>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<meta property="og:title" content="try-hylang" />
<meta property="og:image" content="http://docs.hylang.org/en/latest/_images/hy_logo-smaller.png" />
<meta property="og:description" content="hylang repl">
<body>
<div id="terminal">
<img src="{{ url_for('static', filename='symbolics.jpg') }}">
<div id="hy-console" class="console"></div>
<div id="footer"><a href='https://github.com/hylang/hy" target="_new">hylang/hy</a></div>
</div>
</body>
</html>]])
(defclass MyHyREPL [HyREPL]
(defn eval [self code]
(setv old-stdout sys.stdout)
(setv old-stderr sys.stderr)
(setv fake-stdout (StringIO))
(setv sys.stdout fake-stdout)
(setv fake-stderr (StringIO))
(setv sys.stderr fake-stderr)
(HyREPL.runsource self code "<input>" "single")
(setv sys.stdout old-stdout)
(setv sys.stderr old-stderr)
{"stdout" (fake-stdout.getvalue) "stderr" (fake-stderr.getvalue)}))
(setv app (Flask __name__))
(with-decorator (app.route "/")
(defn index-page []
(render-template-string index-template
:hy_version hy.__version__
:server_software (get os.environ "SERVER_SOFTWARE"))
))
(with-decorator (app.route "/eval" :methods ["POST"])
(defn eval-page []
(setv my-hy-repl (MyHyREPL)
eval-input (request.get_json))
(for [expr (get eval-input "env")]
(my-hy-repl.eval expr))
(json.dumps (my-hy-repl.eval (get eval-input "code")))
))