blob: cc52587243781cd1c7f7b9eb4bdf24edd371986b (
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
|
(.module:
[lux #*
[host (#+ import:)]
["@" target]
[abstract
[monad (#+ do)]]
[control
["ex" exception (#+ exception:)]
["." io (#+ IO io)]
[concurrency
["." promise (#+ Promise)]]
[security
["!" capability (#+ capability:)]]]
[data
["." error (#+ Error)]
["." text
format]]])
(template [<name>]
[(exception: #export (<name>)
"")]
[cannot-open]
[cannot-close]
)
(capability: #export (Can-Read ! o)
(can-read [] (! (Error o))))
(capability: #export (Can-Write ! i)
(can-write i (! (Error Any))))
(capability: #export (Can-Close !)
(can-close [] (! (Error Any))))
(signature: #export (Console !)
(: (Can-Read ! Nat)
can-read)
(: (Can-Read ! Text)
can-read-line)
(: (Can-Write ! Text)
can-write)
(: (Can-Close !)
can-close))
(def: #export (async console)
(-> (Console IO) (Console Promise))
(`` (structure (~~ (template [<capability> <forge>]
[(def: <capability>
(<forge>
(|>> (!.use (:: console <capability>)) promise.future)))]
[can-read ..can-read]
[can-read-line ..can-read]
[can-write ..can-write]
[can-close ..can-close])))))
(with-expansions [<form-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
[?jvm-console (System::console)]
(case ?jvm-console
#.None
(wrap (ex.throw cannot-open []))
(#.Some jvm-console)
(let [jvm-input (System::in)
jvm-output (System::out)]
(<| wrap
ex.return
(: (Console IO)) ## TODO: Remove ASAP
(structure
(def: can-read
(..can-read
(function (_ _)
(|> jvm-input
InputStream::read
(:: (error.with io.monad) map .nat)))))
(def: can-read-line
(..can-read
(function (_ _)
(java/io/Console::readLine jvm-console))))
(def: can-write
(..can-write
(function (_ message)
(PrintStream::print message jvm-output))))
(def: can-close
(..can-close
(|>> (ex.throw cannot-close) wrap))))))))))]
(`` (for {(~~ (static @.old))
(as-is <form-jvm>)
(~~ (static @.jvm))
(as-is <form-jvm>)
})))
(def: #export (write-line message console)
(All [!] (-> Text (Console !) (! Any)))
(!.use (:: console can-write) (format message text.new-line)))
|