aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/constant.lux
blob: c2756715ea2fb3e4cf6c7f79b8069303fc5a5678 (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
(.module:
  [library
   [lux #*
    ["." ffi (#+ import:)]
    ["@" target]
    [abstract
     [monad (#+ do)]
     ["." equivalence (#+ Equivalence)]]
    [data
     ["." sum]
     ["." product]
     ["." text]
     [format
      [".F" binary (#+ Writer) ("#\." monoid)]]
     [collection
      ["." row (#+ Row)]]]
    [macro
     ["." template]]
    [math
     [number
      ["." i32 (#+ I32)]
      ["." i64]
      ["." int]
      ["." frac]]]
    [type
     abstract]]]
  ["." / #_
   ["#." tag]
   ["/#" // #_
    ["#." index (#+ Index)]
    [type
     ["#." category]
     ["#." descriptor (#+ Descriptor)]]
    [encoding
     ["#." unsigned]]]])

(type: .public UTF8
  Text)

(def: utf8_writer
  (Writer UTF8)
  binaryF.utf8/16)

(abstract: .public Class
  {}
  
  (Index UTF8)

  (def: .public index
    (-> Class (Index UTF8))
    (|>> :representation))
  
  (def: .public class
    (-> (Index UTF8) Class)
    (|>> :abstraction))

  (def: .public class_equivalence
    (Equivalence Class)
    (\ equivalence.functor each
       ..index
       //index.equivalence))

  (def: class_writer
    (Writer Class)
    (|>> :representation //index.writer))
  )

(import: java/lang/Float
  ["#::."
   (#static floatToRawIntBits #manual [float] int)])

(implementation: .public float_equivalence
  (Equivalence java/lang/Float)

  (def: (= parameter subject)
    (for {@.old
          ("jvm feq" parameter subject)
          
          @.jvm
          ("jvm float ="
           ("jvm object cast" parameter)
           ("jvm object cast" subject))})))

(import: java/lang/Double
  ["#::."
   (#static doubleToRawLongBits [double] long)])

(abstract: .public (Value kind)
  {}
  
  kind

  (def: .public value
    (All [kind] (-> (Value kind) kind))
    (|>> :representation))

  (def: .public (value_equivalence Equivalence<kind>)
    (All [kind]
      (-> (Equivalence kind)
          (Equivalence (Value kind))))
    (\ equivalence.functor each
       (|>> :representation)
       Equivalence<kind>))

  (template [<constructor> <type> <marker>]
    [(type: .public <type>
       (Value <marker>))

     (def: .public <constructor>
       (-> <marker> <type>)
       (|>> :abstraction))]

    [integer Integer I32]
    [float   Float   java/lang/Float]
    [long    Long    .Int]
    [double  Double  Frac]
    [string  String  (Index UTF8)]
    )

  (template [<writer_name> <type> <write> <writer>]
    [(def: <writer_name>
       (Writer <type>)
       (`` (|>> :representation
                (~~ (template.spliced <write>))
                (~~ (template.spliced <writer>)))))]

    [integer_writer Integer [] [binaryF.bits/32]]
    [float_writer Float [java/lang/Float::floatToRawIntBits ffi.int_to_long (:as I64)] [i32.i32 binaryF.bits/32]]
    [long_writer Long [] [binaryF.bits/64]]
    [double_writer Double [java/lang/Double::doubleToRawLongBits] [binaryF.bits/64]]
    [string_writer String [] [//index.writer]]
    )
  )

(type: .public (Name_And_Type of)
  (Record
   {#name (Index UTF8)
    #descriptor (Index (Descriptor of))}))

(type: .public (Reference of)
  (Record
   {#class (Index Class)
    #name_and_type (Index (Name_And_Type of))}))

(template [<type> <equivalence> <writer>]
  [(def: .public <equivalence>
     (Equivalence (<type> Any))
     ($_ product.equivalence
         //index.equivalence
         //index.equivalence))

   (def: <writer>
     (Writer (<type> Any))
     ($_ binaryF.and
         //index.writer
         //index.writer))]

  [Name_And_Type name_and_type_equivalence name_and_type_writer]
  [Reference reference_equivalence reference_writer]
  )

(type: .public Constant
  (Variant
   (#UTF8 UTF8)
   (#Integer Integer)
   (#Float Float)
   (#Long Long)
   (#Double Double)
   (#Class Class)
   (#String String)
   (#Field (Reference //category.Value))
   (#Method (Reference //category.Method))
   (#Interface_Method (Reference //category.Method))
   (#Name_And_Type (Name_And_Type Any))))

(def: .public (size constant)
  (-> Constant Nat)
  (case constant
    (^or (#Long _) (#Double _))
    2

    _
    1))

(def: .public equivalence
  (Equivalence Constant)
  ... TODO: Delete the explicit "implementation" and use the combinator
  ... version below as soon as the new format for variants is implemented.
  (implementation
   (def: (= reference sample)
     (case [reference sample]
       (^template [<tag> <equivalence>]
         [[(<tag> reference) (<tag> sample)]
          (\ <equivalence> = reference sample)])
       ([#UTF8 text.equivalence]
        [#Integer (..value_equivalence i32.equivalence)]
        [#Long (..value_equivalence int.equivalence)]
        [#Float (..value_equivalence float_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)))
  ... ($_ sum.equivalence
  ...     ... #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: .public writer
  (Writer Constant)
  (with_expansions [<constants> (as_is [#UTF8             /tag.utf8             ..utf8_writer]
                                       [#Integer          /tag.integer          ..integer_writer]
                                       [#Float            /tag.float            ..float_writer]
                                       [#Long             /tag.long             ..long_writer]
                                       [#Double           /tag.double           ..double_writer]
                                       [#Class            /tag.class            ..class_writer]
                                       [#String           /tag.string           ..string_writer]
                                       [#Field            /tag.field            ..reference_writer]
                                       [#Method           /tag.method           ..reference_writer]
                                       [#Interface_Method /tag.interface_method ..reference_writer]
                                       [#Name_And_Type    /tag.name_and_type    ..name_and_type_writer]
                                       ... TODO: Method_Handle
                                       ... TODO: Method_Type
                                       ... TODO: Invoke_Dynamic
                                       )]
    (function (_ value)
      (case value
        (^template [<case> <tag> <writer>]
          [(<case> value)
           (binaryF\composite (/tag.writer <tag>)
                              (<writer> value))])
        (<constants>)
        ))))