aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/constant.lux
blob: af6b1b0783a5afbef20da72c055169e4cfa5e84c (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
(.module:
  [lux #*
   [abstract
    [monad (#+ do)]
    ["." equivalence (#+ Equivalence)]]
   [control
    ["." parser]]
   [data
    [number
     ["." int]
     ["." frac]]
    ["." text]
    [format
     ["." binary (#+ Format) ("#;." monoid)]]
    [collection
     ["." row (#+ Row)]]]
   [type
    abstract]]
  ["." / #_
   ["#." tag ("#;." equivalence)]
   ["." // #_
    [descriptor (#+ Descriptor)]
    ["#." index (#+ Index)]
    [encoding
     [unsigned (#+ U4)]]]])

(type: #export UTF8 Text)

(def: utf8-format
  (Format UTF8)
  binary.utf8/16)

(abstract: #export Class
  {}
  
  (Index UTF8)
  
  (def: #export class
    (-> (Index UTF8) Class)
    (|>> :abstraction))

  (def: #export class-equivalence
    (Equivalence Class)
    (:: equivalence.contravariant map-1
        (|>> :representation)
        //index.equivalence))

  (def: class-format
    (Format Class)
    (binary.adapt (|>> :abstraction)
                  (|>> :representation)
                  //index.format))
  )

(abstract: #export (Value kind)

  {}

  kind

  (def: #export value
    (All [kind] (-> (Value kind) kind))
    (|>> :representation))

  (def: #export (value-equivalence Equivalence<kind>)
    (All [kind]
      (-> (Equivalence kind)
          (Equivalence (Value kind))))
    (:: equivalence.contravariant map-1
        (|>> :representation)
        Equivalence<kind>))

  (template [<constructor> <type> <marker>]
    [(type: #export <type> (Value <marker>))

     (def: #export <constructor>
       (-> <marker> <type>)
       (|>> :abstraction))]

    [integer Integer U4]
    [long    Long    .Int]
    [float   Float   Nothing]
    [double  Double  Frac]
    [string  String  (Index UTF8)]
    )

  (template [<name> <type> <read> <write> <base>]
    [(def: <name>
       (Format <type>)
       (binary.adapt (|>> <read> :abstraction)
                     (|>> :representation <write>)
                     <base>))]

    [long-format Long .int (<|) binary.bits/64]
    [double-format Double frac.bits-to-frac frac.frac-to-bits binary.bits/64]
    [string-format String (<|) (<|) //index.format]
    )
  )

(type: #export Name-And-Type
  {#name (Index UTF8)
   #descriptor (Index (Descriptor Any))})

(type: #export Reference
  {#class (Index Class)
   #name-and-type (Index Name-And-Type)})

(template [<type> <equivalence> <format>]
  [(def: #export <equivalence>
     (Equivalence <type>)
     ($_ equivalence.product
         //index.equivalence
         //index.equivalence))

   (def: #export <format>
     (Format <type>)
     ($_ binary.and
         //index.format
         //index.format))]

  [Name-And-Type name-and-type-equivalence name-and-type-format]
  [Reference reference-equivalence reference-format]
  )

(type: #export Constant
  (#UTF8 UTF8)
  (#Long Long)
  (#Double Double)
  (#Class Class)
  (#String String)
  (#Field Reference)
  (#Method Reference)
  (#Interface-Method Reference)
  (#Name-And-Type Name-And-Type))

(def: #export equivalence
  (Equivalence Constant)
  ## TODO: Delete the explicit "structure" and use the combinator
  ## version below as soon as the new format for variants is implemented.
  (structure
   (def: (= reference sample)
     (case [reference sample]
       (^template [<tag> <equivalence>]
         [(<tag> reference) (<tag> sample)]
         (:: <equivalence> = reference sample))
       ([#UTF8 text.equivalence]
        [#Long (..value-equivalence int.equivalence)]
        [#Double (..value-equivalence frac.equivalence)]
        [#Class ..class-equivalence]
        [#String (..value-equivalence //index.equivalence)]
        [#Field ..reference-equivalence]
        [#Method ..reference-equivalence]
        [#Interface-Method ..reference-equivalence]
        [#Name-And-Type ..name-and-type-equivalence])
       
       _
       false)))
  ## ($_ equivalence.sum
  ##     ## #UTF8
  ##     text.equivalence
  ##     ## #Long
  ##     (..value-equivalence int.equivalence)
  ##     ## #Double
  ##     (..value-equivalence frac.equivalence)
  ##     ## #Class
  ##     ..class-equivalence
  ##     ## #String
  ##     (..value-equivalence //index.equivalence)
  ##     ## #Field
  ##     ..reference-equivalence
  ##     ## #Method
  ##     ..reference-equivalence
  ##     ## #Interface-Method
  ##     ..reference-equivalence
  ##     ## #Name-And-Type
  ##     ..name-and-type-equivalence
  ##     )
  )

(def: #export format
  (Format Constant)
  (with-expansions [<constants> (as-is [#UTF8             /tag.utf8             ..utf8-format]
                                       ## TODO: Integer
                                       ## TODO: Float
                                       [#Long             /tag.long             ..long-format]
                                       [#Double           /tag.double           ..double-format]
                                       [#Class            /tag.class            ..class-format]
                                       [#String           /tag.string           ..string-format]
                                       [#Field            /tag.field            ..reference-format]
                                       [#Method           /tag.method           ..reference-format]
                                       [#Interface-Method /tag.interface-method ..reference-format]
                                       [#Name-And-Type    /tag.name-and-type    ..name-and-type-format]
                                       ## TODO: Method-Handle
                                       ## TODO: Method-Type
                                       ## TODO: Invoke-Dynamic
                                       )]
    {#binary.reader (do parser.monad
                      [tag (get@ #binary.reader /tag.format)]
                      (`` (cond (~~ (template [<case> <tag> <format>]
                                      [(/tag;= <tag> tag)
                                       (:: @ map (|>> <case>) (get@ #binary.reader <format>))]

                                      <constants>))
                                
                                ## else
                                (parser.fail "Cannot parse constant."))))
     #binary.writer (function (_ value)
                      (case value
                        (^template [<case> <tag> <format>]
                          (<case> value)
                          (binary;compose ((get@ #binary.writer /tag.format) <tag>)
                                          ((get@ #binary.writer <format>) value)))
                        (<constants>)
                        ))}))