blob: 209063dfd96a66eb56a74a9ae5135539cabdfce8 (
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
|
(.module:
[lux #*
[control
[monad (#+ do)]
["ex" exception (#+ exception:)]
[security
["." taint (#+ Dirty taint)]
[capability (#+ Capability)]]]
[data
["." error (#+ Error)]
["." text
format]]
["." io (#+ IO Process io)]
[concurrency
["." promise (#+ Promise)]]
[host (#+ import:)]
[platform
[compiler
["." host]]]])
(do-template [<name>]
[(exception: #export (<name>)
"")]
[cannot-open]
[cannot-close]
)
(type: #export (Can-Read ! o)
(Capability [] (! (Error (Dirty o)))))
(type: #export (Can-Write ! i)
(Capability i (! (Error Any))))
(type: #export (Can-Close !)
(Capability [] (! (Error Any))))
(signature: #export (Console !)
(: (Can-Read ! Nat)
read)
(: (Can-Read ! Text)
read-line)
(: (Can-Write ! Text)
write)
(: (Can-Close !)
close))
(def: #export (async console)
(-> (Console IO) (Console Promise))
(`` (structure (~~ (do-template [<capability>]
[(def: <capability> (|>> (:: console <capability>) promise.future))]
[read] [read-line] [write] [close])))))
(`` (for {(~~ (static host.jvm))
(as-is (import: java/lang/String)
(import: #long java/io/Console
(readLine [] #io #try String))
(import: java/io/InputStream
(read [] #io #try int))
(import: java/io/PrintStream
(print [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 system
(IO (Error (Console IO)))
(do io.Monad<IO>
[?jvm-console (System::console)]
(case ?jvm-console
#.None
(io.fail (ex.construct cannot-open []))
(#.Some jvm-console)
(let [jvm-input (System::in)
jvm-output (System::out)]
(<| io.from-io
wrap
(: (Console IO)) ## TODO: Remove ASAP
(structure
(def: (read _)
(|> jvm-input
InputStream::read
(:: io.Functor<Process> map (|>> .nat taint))))
(def: (read-line _)
(|> jvm-console
java/io/Console::readLine
(:: io.Functor<Process> map taint)))
(def: (write message)
(PrintStream::print message jvm-output))
(def: close
(|>> (ex.construct cannot-close) io.fail)))))))))
}))
(def: #export (write-line message Console<!>)
(All [!] (-> Text (Console !) (! Any)))
(:: Console<!> write (format message text.new-line)))
|