hy/eg/tryhy/main.hy

27 lines
888 B
Hy
Raw Normal View History

2013-10-31 22:30:00 +01:00
(require hy.contrib.meth)
(import [hy.cmdline [HyREPL]]
[sys]
[StringIO [StringIO]]
2013-11-01 19:44:05 +01:00
[flask [Flask redirect request]]
[json])
2013-10-31 22:30:00 +01:00
(defclass MyHyREPL [HyREPL]
[[eval (fn [self code]
(setv old-stdout sys.stdout)
2013-11-01 19:44:05 +01:00
(setv old-stderr sys.stderr)
2013-10-31 22:30:00 +01:00
(setv fake-stdout (StringIO))
(setv sys.stdout fake-stdout)
2013-11-01 19:44:05 +01:00
(setv fake-stderr (StringIO))
(setv sys.stderr fake-stderr)
2013-10-31 22:30:00 +01:00
(HyREPL.runsource self code "<input>" "single")
(setv sys.stdout old-stdout)
2013-11-01 19:44:05 +01:00
(setv sys.stderr old-stderr)
{"stdout" (fake-stdout.getvalue) "stderr" (fake-stderr.getvalue)})]])
2013-10-31 22:30:00 +01:00
(def repl (MyHyREPL))
(def app (Flask __name__))
(route hello "/<name>" [name] (.format "(hello \"{0}!\")" name))
2013-11-01 19:44:05 +01:00
(route eval-get "/eval" [] (json.dumps (repl.eval (get request.args "code"))))