aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/console.lux
blob: 00346ef03136d220136b3bae224625272a384c5e (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
(.using
 [library
  [lux "*"
   ["@" target]
   ["[0]" ffi {"+" import:}]
   [abstract
    [monad {"+" do}]]
   [control
    ["[0]" maybe]
    ["[0]" try {"+" Try} ("[1]#[0]" functor)]
    ["[0]" exception {"+" exception:}]
    ["[0]" io {"+" IO io} ("[1]#[0]" functor)]
    [concurrency
     ["[0]" async {"+" Async} ("[1]#[0]" monad)]
     ["[0]" atom]]]
   [data
    ["[0]" text {"+" Char}
     ["%" format {"+" format}]]]]])

(type: .public (Console !)
  (Interface
   (is (-> [] (! (Try Char)))
       read)
   (is (-> [] (! (Try Text)))
       read_line)
   (is (-> Text (! (Try Any)))
       write)
   (is (-> [] (! (Try Any)))
       close)))

(def: .public (async console)
  (-> (Console IO) (Console Async))
  (`` (implementation
       (~~ (template [<capability>]
             [(def: <capability>
                (|>> (# console <capability>) async.future))]

             [read]
             [read_line]
             [write]
             [close])))))

(exception: .public cannot_close)

(with_expansions [<jvm> (these (import: java/lang/String
                                 "[1]::[0]")

                               (import: java/io/Console
                                 "[1]::[0]"
                                 (readLine [] "io" "try" java/lang/String))

                               (import: java/io/InputStream
                                 "[1]::[0]"
                                 (read [] "io" "try" int))

                               (import: java/io/PrintStream
                                 "[1]::[0]"
                                 (print [java/lang/String] "io" "try" void))

                               (import: java/lang/System
                                 "[1]::[0]"
                                 ("static" console [] "io" "?" java/io/Console)
                                 ("static" in java/io/InputStream)
                                 ("static" out java/io/PrintStream))

                               (exception: .public cannot_open)

                               (def: .public default
                                 (IO (Try (Console IO)))
                                 (do io.monad
                                   [?jvm_console (java/lang/System::console)]
                                   (case ?jvm_console
                                     {.#None}
                                     (in (exception.except ..cannot_open []))

                                     {.#Some jvm_console}
                                     (let [jvm_input (java/lang/System::in)
                                           jvm_output (java/lang/System::out)]
                                       (<| in
                                           {try.#Success}
                                           (is (Console IO)) ... TODO: Remove ASAP
                                           (implementation
                                            (def: (read _)
                                              (|> jvm_input
                                                  java/io/InputStream::read
                                                  (# (try.with io.monad) each (|>> ffi.of_int .nat))))
                                            
                                            (def: (read_line _)
                                              (io#each (try#each (|>> ffi.of_string))
                                                       (java/io/Console::readLine jvm_console)))
                                            
                                            (def: (write message)
                                              (java/io/PrintStream::print (ffi.as_string message) jvm_output))
                                            
                                            (def: close
                                              (|>> (exception.except ..cannot_close) in)))))))))]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)
       @.js (these (ffi.import: Buffer
                     "[1]::[0]"
                     (toString [] ffi.String))

                   (ffi.import: Readable_Stream
                     "[1]::[0]"
                     (read [] "?" Buffer)
                     (unshift "as" unshift|String [ffi.String] ffi.Boolean)
                     (unshift "as" unshift|Buffer [Buffer] ffi.Boolean))

                   (ffi.import: Writable_Stream
                     "[1]::[0]"
                     (write [ffi.String ffi.Function] ffi.Boolean)
                     (once [ffi.String ffi.Function] Any))

                   (ffi.import: process
                     "[1]::[0]"
                     ("static" stdout Writable_Stream)
                     ("static" stdin Readable_Stream))

                   (exception: .public cannot_read)

                   (template: (!read <type> <query>)
                     [(let [it (process::stdin)]
                        (case (Readable_Stream::read it)
                          {.#Some buffer}
                          (let [input (Buffer::toString buffer)]
                            (case (is (Maybe [<type> Text])
                                      <query>)
                              {.#Some [head tail]}
                              (exec
                                (Readable_Stream::unshift|String tail it)
                                (async#in {try.#Success head}))
                              
                              {.#None}
                              (exec
                                (Readable_Stream::unshift|Buffer buffer it)
                                (async#in (exception.except ..cannot_read [])))))

                          {.#None}
                          (async#in (exception.except ..cannot_read []))))])

                   (def: .public default
                     (Maybe (Console Async))
                     (if ffi.on_node_js?
                       {.#Some (implementation
                                (def: (read _)
                                  (!read Char (do maybe.monad
                                                [head (text.char 0 input)
                                                 [_ tail] (text.split_at 1 input)]
                                                (in [head tail]))))
                                
                                (def: (read_line _)
                                  (!read Text (text.split_by text.\n input)))
                                
                                (def: (write it)
                                  (let [[read! write!] (is [(async.Async (Try [])) (async.Resolver (Try []))]
                                                           (async.async []))]
                                    (exec
                                      (Writable_Stream::write it (ffi.function (_ []) Any (io.run! (write! {try.#Success []})))
                                                              (process::stdout))
                                      read!)))
                                
                                (def: close
                                  (|>> (exception.except ..cannot_close) async#in)))}
                       {.#None})))
       (these)))

(def: .public (write_line message console)
  (All (_ !) (-> Text (Console !) (! (Try Any))))
  (# console write (format message text.new_line)))

(type: .public (Mock s)
  (Interface
   (is (-> s (Try [s Char]))
       on_read)
   (is (-> s (Try [s Text]))
       on_read_line)
   (is (-> Text s (Try s))
       on_write)
   (is (-> s (Try s))
       on_close)))

(def: .public (mock mock init)
  (All (_ s) (-> (Mock s) s (Console IO)))
  (let [state (atom.atom init)]
    (`` (implementation
         (~~ (template [<method> <mock>]
               [(def: (<method> _)
                  (do [! io.monad]
                    [|state| (atom.read! state)]
                    (case (# mock <mock> |state|)
                      {try.#Success [|state| output]}
                      (do !
                        [_ (atom.write! |state| state)]
                        (in {try.#Success output}))
                      
                      {try.#Failure error}
                      (in {try.#Failure error}))))]

               [read on_read]
               [read_line on_read_line]
               ))

         (def: (write input)
           (do [! io.monad]
             [|state| (atom.read! state)]
             (case (# mock on_write input |state|)
               {try.#Success |state|}
               (do !
                 [_ (atom.write! |state| state)]
                 (in {try.#Success []}))
               
               {try.#Failure error}
               (in {try.#Failure error}))))

         (def: (close _)
           (do [! io.monad]
             [|state| (atom.read! state)]
             (case (# mock on_close |state|)
               {try.#Success |state|}
               (do !
                 [_ (atom.write! |state| state)]
                 (in {try.#Success []}))
               
               {try.#Failure error}
               (in {try.#Failure error}))))
         ))))