hy/eg/tryhy/main.hy

30 lines
953 B
Hy
Raw Normal View History

2013-10-31 22:30:00 +01:00
(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
2013-11-02 00:21:54 +01:00
(def app (Flask __name__))
2013-10-31 22:30:00 +01:00
2013-11-02 00:21:54 +01:00
(with-decorator (kwapply (app.route "/eval") {"methods" ["POST"]})
(fn []
(let [[repl (MyHyREPL)] [input (request.get_json)]]
(foreach [expr (get input "env")]
(repl.eval expr))
(json.dumps (repl.eval (get input "code")))
)))
2013-10-31 22:30:00 +01:00