aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/binary.lux
blob: 1be3e7a52c97f89bea3f3c419ffdc1f5393f0478 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
(.module:
  [lux (#- and or nat int rev list type)
   [control
    [monoid (#+ Monoid)]
    ["." fold]
    [monad (#+ do Monad)]
    ["." parser (#+ Parser) ("parser/." Functor<Parser>)]
    ["ex" exception (#+ exception:)]
    [equivalence (#+ Equivalence)]]
   [data
    ["." error (#+ Error)]
    ["." number
     ["." i64]]
    [text
     ["." encoding]
     [format (#+ %n)]]
    [collection
     ["." list]
     ["." row (#+ Row) ("row/." Functor<Row>)]]]
   [type (#+ :share)]
   [world
    ["." binary (#+ Binary)]]])

(exception: #export (binary-was-not-fully-read {length Nat} {read Nat})
  (ex.report ["Binary length" (%n length)]
             ["Read bytes" (%n read)]))

(exception: #export (invalid-tag {range Nat} {byte Nat})
  (ex.report ["Range" (%n range)]
             ["Byte" (%n byte)]))

(type: #export Offset Nat)

(type: #export Size Nat)

(def: #export size/8 Size 1)
(def: #export size/16 Size 2)
(def: #export size/32 Size 4)
(def: #export size/64 Size 8)

(def: mask
  (-> Size (I64 Any))
  (|>> (n/* i64.bits-per-byte) i64.mask))

(type: #export Reader
  (Parser [Offset Binary]))

(type: #export Mutation
  [Size (-> Offset Binary Binary)])

(def: #export no-op
  Mutation
  [0 (function (_ offset data) data)])

(structure: #export _ (Monoid Mutation)
  
  (def: identity
    ..no-op)
  
  (def: (compose [sizeL mutL] [sizeR mutR])
    [(n/+ sizeL sizeR)
     (function (_ offset data)
       (|> data
           (mutL offset)
           (mutR (n/+ sizeL offset))))]))

(type: #export (Writer a)
  (-> a Mutation))

(type: #export (Format a)
  {#reader (Reader a)
   #writer (Writer a)})

(def: #export (adapt post-read pre-write format)
  (All [a a']
    (-> (-> a a')
        (-> a' a)
        (Format a)
        (Format a')))
  (let [(^open "_/.") format]
    {#reader (|> _/reader (parser/map post-read))
     #writer (|>> pre-write _/writer)}))

(def: #export (read format input)
  (All [a] (-> (Format a) Binary (Error a)))
  (case ((get@ #reader format) [0 input])
    (#error.Failure msg)
    (#error.Failure msg)
    
    (#error.Success [[end _] output])
    (let [length (binary.size input)]
      (if (n/= end length)
        (#error.Success output)
        (ex.throw binary-was-not-fully-read [length end])))))

(def: #export (write format value)
  (All [a] (-> (Format a) a Binary))
  (let [[valueS valueT] ((get@ #writer format) value)]
    (|> valueS binary.create (valueT 0))))

## Primitives
(do-template [<name> <size> <read> <write>]
  [(def: #export <name>
     (Format (I64 Any))
     {#reader (function (_ [offset binary])
                (case (<read> offset binary)
                  (#error.Success data)
                  (#error.Success [(n/+ <size> offset) binary] data)
                  
                  (#error.Failure error)
                  (#error.Failure error)))
      #writer (function (_ value)
                [<size>
                 (function (_ offset binary)
                   (|> binary
                       (<write> offset value)
                       error.assume))])})]

  [bits/8  size/8  binary.read/8  binary.write/8]
  [bits/16 size/16 binary.read/16 binary.write/16]
  [bits/32 size/32 binary.read/32 binary.write/32]
  [bits/64 size/64 binary.read/64 binary.write/64]
  )

## Combinators
(def: #export (or leftB rightB)
  (All [l r] (-> (Format l) (Format r) (Format (| l r))))
  {#reader (do parser.Monad<Parser>
             [flag (get@ #reader bits/8)]
             (case flag
               0 (:: @ map (|>> #.Left) (get@ #reader leftB))
               1 (:: @ map (|>> #.Right) (get@ #reader rightB))
               _ (parser.lift (ex.throw invalid-tag [2 (.nat flag)]))))
   #writer (function (_ altV)
             (case altV
               (#.Left leftV)
               (let [[leftS leftT] ((get@ #writer leftB) leftV)]
                 [(.inc leftS)
                  (function (_ offset binary)
                    (|> binary
                        (binary.write/8 offset 0)
                        error.assume
                        (leftT (.inc offset))))])
               
               (#.Right rightV)
               (let [[rightS rightT] ((get@ #writer rightB) rightV)]
                 [(.inc rightS)
                  (function (_ offset binary)
                    (|> binary
                        (binary.write/8 offset 1)
                        error.assume
                        (rightT (.inc offset))))])
               ))})

(def: #export (and preB postB)
  (All [a b] (-> (Format a) (Format b) (Format [a b])))
  {#reader (parser.and (get@ #reader preB) (get@ #reader postB))
   #writer (function (_ [preV postV])
             (let [[preS preT] ((get@ #writer preB) preV)
                   [postS postT] ((get@ #writer postB) postV)]
               [(n/+ preS postS)
                (function (_ offset)
                  (|>> (preT offset)
                       (postT (n/+ preS offset))))]))})

(def: #export (rec body)
  (All [a] (-> (-> (Format a) (Format a)) (Format a)))
  {#reader (function (_ input)
             (let [reader (get@ #reader (body (rec body)))]
               (reader input)))
   #writer (function (_ value)
             (let [writer (get@ #writer (body (rec body)))]
               (writer value)))})

(def: #export (ignore default)
  (All [a] (-> a (Format a)))
  {#reader (function (_ input)
             (#error.Success [input default]))
   #writer (function (_ value)
             ..no-op)})

(def: #export any
  (Format Any)
  (ignore []))

(def: #export bit
  (Format Bit)
  {#reader (function (_ [offset binary])
             (case (binary.read/8 offset binary)
               (#error.Success data)
               (case (: Nat data)
                 (^template [<nat> <bit>]
                   <nat> (#error.Success [(inc offset) binary] <bit>))
                 ([0 #0]
                  [1 #1])
                 
                 _
                 (ex.throw invalid-tag [2 data]))
               
               (#error.Failure error)
               (#error.Failure error)))
   #writer (function (_ value)
             [1
              (function (_ offset binary)
                (|> binary
                    (binary.write/8 offset (if value 1 0))
                    error.assume))])})

(def: #export nat (Format Nat) (:assume ..bits/64))
(def: #export int (Format Int) (:assume ..bits/64))
(def: #export rev (Format Rev) (:assume ..bits/64))

(def: #export frac
  (Format Frac)
  (let [(^slots [#reader #writer]) ..bits/64]
    {#reader (:: parser.Monad<Parser> map number.bits-to-frac reader)
     #writer (|>> number.frac-to-bits writer)}))

(do-template [<name> <bits> <size> <write>]
  [(def: #export <name>
     (Format Binary)
     (let [mask (..mask <size>)]
       {#reader (do parser.Monad<Parser>
                  [size (:coerce (Reader Nat)
                                 ## TODO: Remove coercion.
                                 (get@ #reader <bits>))]
                  (function (_ [offset binary])
                    (do error.Monad<Error>
                      [#let [end (n/+ size offset)]
                       output (binary.slice offset end binary)]
                      (wrap [[end binary] output]))))
        #writer (function (_ value)
                  (let [size (|> value binary.size (i64.and mask))]
                    [(n/+ <size> size)
                     (function (_ offset binary)
                       (error.assume
                        (do error.Monad<Error>
                          [_ (<write> offset size binary)]
                          (binary.copy size 0 value (n/+ <size> offset) binary))))]))}))]

  [binary/8  ..bits/8  ..size/8  binary.write/8]
  [binary/16 ..bits/16 ..size/16 binary.write/16]
  [binary/32 ..bits/32 ..size/32 binary.write/32]
  [binary/64 ..bits/64 ..size/64 binary.write/64]
  )

(do-template [<name> <binary>]
  [(def: #export <name>
     (Format Text)
     (let [(^open "binary/.") <binary>]
       {#reader (do parser.Monad<Parser>
                  [utf8 binary/reader]
                  (parser.lift (encoding.from-utf8 utf8)))
        #writer (|>> encoding.to-utf8 binary/writer)}))]

  [utf8/8  ..binary/8]
  [utf8/16 ..binary/16]
  [utf8/32 ..binary/32]
  [utf8/64 ..binary/64]
  )

(def: #export text ..utf8/64)

(do-template [<name> <with-offset> <bits> <size> <write>]
  [(def: #export (<with-offset> extra-count valueF)
     (All [v] (-> Nat (Format v) (Format (Row v))))
     {#reader (do parser.Monad<Parser>
                [count (|> (get@ #reader <bits>)
                           ## TODO: Remove coercion.
                           (:coerce (Reader Nat))
                           (:: @ map (n/- extra-count)))]
                (loop [index 0
                       output (:share [v]
                                      {(Format v)
                                       valueF}
                                      {(Row v)
                                       row.empty})]
                  (if (n/< count index)
                    (do parser.Monad<Parser>
                      [value (get@ #reader valueF)]
                      (recur (.inc index)
                             (row.add value output)))
                    (:: parser.Monad<Parser> wrap output))))
      #writer (function (_ value)
                (let [original-count (row.size value)
                      capped-count (i64.and (..mask <size>)
                                            original-count)
                      value (if (n/= original-count capped-count)
                              value
                              (|> value row.to-list (list.take capped-count) row.from-list))
                      (^open "mutation/.") ..Monoid<Mutation>
                      [size mutation] (|> value
                                          (row/map (get@ #writer valueF))
                                          (:: row.Fold<Row> fold
                                              (function (_ post pre)
                                                (mutation/compose pre post))
                                              mutation/identity))]
                  [(n/+ <size> size)
                   (function (_ offset binary)
                     (error.assume
                      (do error.Monad<Error>
                        [_ (<write> offset (n/+ extra-count capped-count) binary)]
                        (wrap (mutation (n/+ <size> offset) binary)))))]))})

   (def: #export <name>
     (All [v] (-> (Format v) (Format (Row v))))
     (<with-offset> 0))]

  [row/8  row/8'  ..bits/8  ..size/8  binary.write/8]
  [row/16 row/16' ..bits/16 ..size/16 binary.write/16]
  [row/32 row/32' ..bits/32 ..size/32 binary.write/32]
  [row/64 row/64' ..bits/64 ..size/64 binary.write/64]
  )

(def: #export maybe
  (All [a] (-> (Format a) (Format (Maybe a))))
  (..or ..any))

(def: #export (list value)
  (All [a] (-> (Format a) (Format (List a))))
  (..rec
   (function (_ recur)
     (..or ..any
           (..and value recur)))))

(def: #export name
  (Format Name)
  (..and ..text ..text))

(def: #export type
  (Format Type)
  (..rec
   (function (_ type)
     (let [pair (..and type type)
           indexed ..nat
           quantified (..and (..list type) type)]
       ($_ ..or
           ## #Primitive
           (..and ..text (..list type))
           ## #Sum
           pair
           ## #Product
           pair
           ## #Function
           pair
           ## #Parameter
           indexed
           ## #Var
           indexed
           ## #Ex
           indexed
           ## #UnivQ
           quantified
           ## #ExQ
           quantified
           ## #Apply
           pair
           ## #Named
           (..and ..name type)
           )))))

(def: #export cursor
  (Format Cursor)
  ($_ ..and ..text ..nat ..nat))

(def: #export code
  (Format Code)
  (..rec
   (function (_ code)
     (let [sequence (..list code)
           code' ($_ ..or
                     ## #Bit
                     ..bit
                     ## #Nat
                     ..nat
                     ## #Int
                     ..int
                     ## #Rev
                     ..rev
                     ## #Frac
                     ..frac
                     ## #Text
                     ..text
                     ## #Identifier
                     ..name
                     ## #Tag
                     ..name
                     ## #Form
                     sequence
                     ## #Tuple
                     sequence
                     ## #Record
                     (..list (..and code code)))]
       (..and ..cursor code')))))