-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_r.clj
executable file
·118 lines (105 loc) · 3.45 KB
/
process_r.clj
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
(ns rt.basic.impl.process-r
(:require [std.lang.base.runtime :as default]
[rt.basic.type-common :as common]
[rt.basic.type-oneshot :as oneshot]
[rt.basic.type-basic :as basic]
[std.lang.model.spec-r :as spec]
[std.lang.base.impl :as impl]
[std.lang.base.runtime :as rt]
[std.json :as json]
[std.lib :as h]
[std.string :as str]
[xt.lang.base-repl :as k]))
(def +program-init+
(common/put-program-options
:r {:default {:oneshot :rlang
:basic :rlang
:interactive :rlang}
:env {:rlang {:exec "r"
:output {}
:flags {:oneshot ["-s" "-e"]
:basic ["-s" "-e"]
:interactive ["-i"]
:json ["rjsonlite" :instal]}}}}))
(def ^{:arglists '([body])}
default-oneshot-wrap
(let [bootstrap (->> ["library(jsonlite)"
(impl/emit-entry-deps
k/return-eval
{:lang :r
:layout :flat})]
(str/join "\n\n"))]
(fn [body]
(str bootstrap
"\n\n"
(impl/emit-as
:r [(list 'toString (list 'return-eval body))])))))
(defn default-oneshot-trim
"trim for oneshot
(default-oneshot-trim \"[1] \\\"1\\\"\")
=> \"1\""
{:added "4.0"}
[s]
(json/read (subs s 4)))
(def +r-oneshot-config+
(common/set-context-options
[:r :oneshot :default]
{:main {:in #'default-oneshot-wrap
:out #'default-oneshot-trim}
:emit {:body {:transform #'default/return-transform}}
:json :full}))
(def +r-oneshot+
[(rt/install-type!
:r :oneshot
{:type :hara/rt.oneshot
:instance {:create oneshot/rt-oneshot:create}})])
;;
;;
;;
(def +client-basic+
'[(defn client-basic
[host port opts]
(while true
(while true
(var conn := (socketConnection :port port :blocking true))
(tryCatch
(block
(while true
(var line := (readLines conn :n 1))
(cond (== line "<PING>") (next)
:else
(writeLines (return-eval (fromJSON line)) conn :sep "\n"))))
:error (fn [err])))))])
(def ^{:arglists '([port & [{:keys [host]}]])}
default-basic-client
(let [bootstrap (->> ["library(jsonlite)"
(impl/emit-entry-deps
k/return-eval
{:lang :r
:layout :flat})
(impl/emit-as
:r +client-basic+)]
(str/join "\n\n"))]
(fn [port & [{:keys [host]}]]
(str bootstrap
"\n\n"
(impl/emit-as
:r [(list 'client-basic
(or host "127.0.0.1")
port
{})])))))
(def +r-basic-config+
(common/set-context-options
[:r :basic :default]
{:bootstrap #'default-basic-client
:main {}
:emit {:body {:transform #'default/return-transform}}
:json :full
:encode :json
:timeout 2000}))
(def +r-basic+
[(rt/install-type!
:r :basic
{:type :hara/rt.basic
:instance {:create #'basic/rt-basic:create}
:config {:layout :full}})])