-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepl.cljc
50 lines (34 loc) · 1.44 KB
/
prepl.cljc
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
(ns fif.impl.prepl
"Implementation of programmable repl. Useful for implementing fif
outside of a standard input/output environment."
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.evaluators :as evaluators]
#?(:clj [fif.utils.display :refer [PrintWriter-on]])))
(defn prepl-eval
"Programmable Repl Evaluation for clojure(script). Function is useful
for a full repl implementation, since the output can be processed
via `output-fn`.
Keyword Arguments:
sm -- Stack-machine
input-string -- String representation of fif form to be evaluated.
output-fn -- Function of the form (fn [{:keys [tag value]}]).
Output Function Key Arguments:
tag -- either :error from *err* output, :out from *out* output.
value -- string value of the presented tag.
Return Value:
Returns an updated stack-machine after the `input-string` has been evaluated.
Notes:
- Standard Out is flushed after evaluation, however, while
evaluating, output-fn will get called preceding any newline
delimited string within the print writer.
"
[sm input-string output-fn]
(binding
#?(:clj [*out* (PrintWriter-on #(output-fn {:tag :out :value %1}) nil)
*err* (PrintWriter-on #(output-fn {:tag :error :value %1}) nil)]
:cljs [*print-newline* true
*print-fn* #(output-fn {:tag :out :value %1})])
(let [evaled-sm (evaluators/eval-string sm input-string)]
(flush)
evaled-sm)))