aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/binary.lux
blob: 109b1c19d3a752511b8aca95961f93c25189c33e (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
(.module:
  [library
   [lux (#- and or nat int rev list type)
    [abstract
     [monoid (#+ Monoid)]
     [monad (#+ Monad do)]
     [equivalence (#+ Equivalence)]]
    [control
     [pipe (#+ case>)]
     ["." function]
     ["." try (#+ Try)]
     ["<>" parser ("#\." monad)
      ["/" binary (#+ Offset Size Parser)]]]
    [data
     ["." product]
     ["." binary (#+ Binary)]
     [text
      ["%" format (#+ format)]
      [encoding
       ["." utf8]]]
     [collection
      ["." list]
      ["." row (#+ Row) ("#\." functor)]
      ["." set (#+ Set)]]]
    [math
     [number
      ["." i64]
      ["n" nat]
      ["." frac]]]]])

(def: mask
  (-> Size (I64 Any))
  (|>> (n.* i64.bits_per_byte) i64.mask))

(type: .public Mutation
  (-> [Offset Binary] [Offset Binary]))

(type: .public Specification
  [Size Mutation])

(def: .public no_op
  Specification
  [0 function.identity])

(def: .public (instance [size mutation])
  (-> Specification Binary)
  (|> size binary.empty [0] mutation product.right))

(implementation: .public monoid
  (Monoid Specification)
  
  (def: identity
    ..no_op)
  
  (def: (composite [sizeL mutL] [sizeR mutR])
    [(n.+ sizeL sizeR)
     (|>> mutL mutR)]))

(type: .public (Writer a)
  (-> a Specification))

(def: .public (result writer value)
  (All (_ a) (-> (Writer a) a Binary))
  (..instance (writer value)))

(template [<name> <size> <write>]
  [(def: .public <name>
     (Writer (I64 Any))
     (function (_ value)
       [<size>
        (function (_ [offset binary])
          [(n.+ <size> offset)
           (|> binary
               (<write> offset value)
               try.trusted)])]))]

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

(def: .public (or left right)
  (All (_ l r) (-> (Writer l) (Writer r) (Writer (Or l r))))
  (function (_ altV)
    (case altV
      (^template [<number> <tag> <writer>]
        [(<tag> caseV)
         (let [[caseS caseT] (<writer> caseV)]
           [(.++ caseS)
            (function (_ [offset binary])
              (|> binary
                  (binary.write/8! offset <number>)
                  try.trusted
                  [(.++ offset)]
                  caseT))])])
      ([0 #.Left left]
       [1 #.Right right])
      )))

(def: .public (and pre post)
  (All (_ a b) (-> (Writer a) (Writer b) (Writer [a b])))
  (function (_ [preV postV])
    (\ ..monoid composite (pre preV) (post postV))))

(def: .public (rec body)
  (All (_ a) (-> (-> (Writer a) (Writer a)) (Writer a)))
  (function (recur value)
    (body recur value)))

(def: .public any
  (Writer Any)
  (function.constant ..no_op))

(def: .public bit
  (Writer Bit)
  (|>> (case> #0 0 #1 1) ..bits/8))

(template [<name> <type>]
  [(def: .public <name> (Writer <type>) ..bits/64)]

  [nat Nat]
  [int Int]
  [rev Rev]
  )

(def: .public frac
  (Writer Frac)
  (|>> frac.bits ..bits/64))

(def: .public (segment size)
  (-> Nat (Writer Binary))
  (function (_ value)
    [size
     (function (_ [offset binary])
       [(n.+ size offset)
        (try.trusted
         (binary.copy (n.min size (binary.size value))
                      0
                      value
                      offset
                      binary))])]))

(template [<name> <bits> <size> <write>]
  [(def: .public <name>
     (Writer Binary)
     (let [mask (..mask <size>)]
       (function (_ value)
         (let [size (|> value binary.size (i64.and mask))
               size' (n.+ <size> size)]
           [size'
            (function (_ [offset binary])
              [(n.+ size' offset)
               (try.trusted
                (do try.monad
                  [_ (<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!]
  )

(template [<name> <binary>]
  [(def: .public <name>
     (Writer Text)
     (|>> (\ utf8.codec encoded) <binary>))]

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

(def: .public text ..utf8/64)

(template [<name> <size> <write>]
  [(def: .public (<name> valueW)
     (All (_ v) (-> (Writer v) (Writer (Row v))))
     (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.list (list.first capped_count) row.of_list))
             (^open "specification\.") ..monoid
             [size mutation] (|> value
                                 (row\each valueW)
                                 (\ row.mix mix
                                    (function (_ post pre)
                                      (specification\composite pre post))
                                    specification\identity))]
         [(n.+ <size> size)
          (function (_ [offset binary])
            (try.trusted
             (do try.monad
               [_ (<write> offset capped_count binary)]
               (in (mutation [(n.+ <size> offset) binary])))))])))]

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

(def: .public maybe
  (All (_ a) (-> (Writer a) (Writer (Maybe a))))
  (..or ..any))

(def: .public (list value)
  (All (_ a) (-> (Writer a) (Writer (List a))))
  (..rec
   (|>> (..and value)
        (..or ..any))))

(def: .public (set value)
  (All (_ a) (-> (Writer a) (Writer (Set a))))
  (|>> set.list (..list value)))

(def: .public name
  (Writer Name)
  (..and ..text ..text))

(def: .public type
  (Writer Type)
  (..rec
   (function (_ recur)
     (let [pair (..and recur recur)
           indexed ..nat
           quantified (..and (..list recur) recur)]
       (function (_ altV)
         (case altV
           (^template [<number> <tag> <writer>]
             [(<tag> caseV)
              (let [[caseS caseT] (<writer> caseV)]
                [(.++ caseS)
                 (function (_ [offset binary])
                   (|> binary
                       (binary.write/8! offset <number>)
                       try.trusted
                       [(.++ offset)]
                       caseT))])])
           ([0 #.Primitive (..and ..text (..list recur))]
            [1 #.Sum pair]
            [2 #.Product pair]
            [3 #.Function pair]
            [4 #.Parameter indexed]
            [5 #.Var indexed]
            [6 #.Ex indexed]
            [7 #.UnivQ quantified]
            [8 #.ExQ quantified]
            [9 #.Apply pair]
            [10 #.Named (..and ..name recur)])
           ))))))

(def: .public location
  (Writer Location)
  ($_ ..and ..text ..nat ..nat))

(def: .public code
  (Writer Code)
  (..rec
   (function (_ recur)
     (let [sequence (..list recur)]
       (..and ..location
              (function (_ altV)
                (case altV
                  (^template [<number> <tag> <writer>]
                    [(<tag> caseV)
                     (let [[caseS caseT] (<writer> caseV)]
                       [(.++ caseS)
                        (function (_ [offset binary])
                          (|> binary
                              (binary.write/8! offset <number>)
                              try.trusted
                              [(.++ offset)]
                              caseT))])])
                  ([0 #.Bit ..bit]
                   [1 #.Nat ..nat]
                   [2 #.Int ..int]
                   [3 #.Rev ..rev]
                   [4 #.Frac ..frac]
                   [5 #.Text ..text]
                   [6 #.Identifier ..name]
                   [7 #.Tag ..name]
                   [8 #.Form sequence]
                   [9 #.Tuple sequence]
                   [10 #.Record (..list (..and recur recur))])
                  )))))))