blob: 7d68ced07734eb4289fca1a717d4ee3515fe1b0e (
plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
(.module:
[lux #*
[host (#+ import:)]
["@" target]
[abstract
[monad (#+ do)]]
[control
["." try (#+ Try)]
["." exception (#+ exception:)]
["." io (#+ IO io)]
[concurrency
["." promise (#+ Promise)]
["." stm]]
[security
["!" capability (#+ capability:)]]]
[data
["." text (#+ Char)
["%" format (#+ format)]]]])
(template [<name>]
[(exception: #export (<name>)
"")]
[cannot-open]
[cannot-close]
)
(capability: #export (Can-Read ! o)
(can-read [] (! (Try o))))
(capability: #export (Can-Write ! i)
(can-write i (! (Try Any))))
(capability: #export (Can-Close !)
(can-close [] (! (Try Any))))
(signature: #export (Console !)
(: (Can-Read ! Char)
read)
(: (Can-Read ! Text)
read-line)
(: (Can-Write ! Text)
write)
(: (Can-Close !)
close))
(def: #export (async console)
(-> (Console IO) (Console Promise))
(`` (structure (~~ (template [<capability> <forge>]
[(def: <capability>
(<forge>
(|>> (!.use (\ console <capability>)) promise.future)))]
[read ..can-read]
[read-line ..can-read]
[write ..can-write]
[close ..can-close])))))
(with-expansions [<jvm> (as-is (import: java/lang/String)
(import: java/io/Console
["#::."
(readLine [] #io #try java/lang/String)])
(import: java/io/InputStream
["#::."
(read [] #io #try int)])
(import: java/io/PrintStream
["#::."
(print [java/lang/String] #io #try void)])
(import: java/lang/System
["#::."
(#static console [] #io #? java/io/Console)
(#static in java/io/InputStream)
(#static out java/io/PrintStream)])
(def: #export default
(IO (Try (Console IO)))
(do io.monad
[?jvm-console (java/lang/System::console)]
(case ?jvm-console
#.None
(wrap (exception.throw ..cannot-open []))
(#.Some jvm-console)
(let [jvm-input (java/lang/System::in)
jvm-output (java/lang/System::out)]
(<| wrap
exception.return
(: (Console IO)) ## TODO: Remove ASAP
(structure
(def: read
(..can-read
(function (_ _)
(|> jvm-input
java/io/InputStream::read
(\ (try.with io.monad) map .nat)))))
(def: read-line
(..can-read
(function (_ _)
(java/io/Console::readLine jvm-console))))
(def: write
(..can-write
(function (_ message)
(java/io/PrintStream::print message jvm-output))))
(def: close
(..can-close
(|>> (exception.throw ..cannot-close) wrap))))))))))]
(for {@.old (as-is <jvm>)
@.jvm (as-is <jvm>)}))
(def: #export (write-line message console)
(All [!] (-> Text (Console !) (! Any)))
(!.use (\ console write) (format message text.new-line)))
(signature: #export (Simulation s)
(: (-> s (Try [s Char]))
on-read)
(: (-> s (Try [s Text]))
on-read-line)
(: (-> Text s (Try s))
on-write)
(: (-> s (Try s))
on-close))
(def: #export (mock simulation init)
(All [s] (-> (Simulation s) s (Console Promise)))
(let [state (stm.var init)]
(`` (structure
(~~ (template [<method> <simulation>]
[(def: <method>
(..can-read
(function (_ _)
(stm.commit
(do {! stm.monad}
[|state| (stm.read state)]
(case (\ simulation <simulation> |state|)
(#try.Success [|state| output])
(do !
[_ (stm.write |state| state)]
(wrap (#try.Success output)))
(#try.Failure error)
(wrap (#try.Failure error))))))))]
[read on-read]
[read-line on-read-line]
))
(def: write
(..can-write
(function (_ input)
(stm.commit
(do {! stm.monad}
[|state| (stm.read state)]
(case (\ simulation on-write input |state|)
(#try.Success |state|)
(do !
[_ (stm.write |state| state)]
(wrap (#try.Success [])))
(#try.Failure error)
(wrap (#try.Failure error))))))))
(def: close
(..can-close
(function (_ _)
(stm.commit
(do {! stm.monad}
[|state| (stm.read state)]
(case (\ simulation on-close |state|)
(#try.Success |state|)
(do !
[_ (stm.write |state| state)]
(wrap (#try.Success [])))
(#try.Failure error)
(wrap (#try.Failure error))))))))
))))
|