aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/compiler/target/jvm/type/descriptor.lux
blob: 2327f07b2a6f71c585eb281346d40d7d12b28e49 (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
(.require
 [library
  [lux (.except Declaration int char)
   [abstract
    [equivalence (.only Equivalence)]]
   [control
    ["[0]" maybe]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" list (.use "[1]#[0]" functor)]]]
   [math
    [number
     ["n" nat]]]
   [meta
    [type
     ["[0]" nominal (.except def)]]]]]
 ["[0]" //
  [category (.only Void Value Return Method Primitive Object Class Array Var Parameter Declaration)]
  ["/[1]" //
   [encoding
    ["[1][0]" name (.only Internal External)]]]])

(nominal.def .public (Descriptor category)
  Text

  (def .public descriptor
    (-> (Descriptor Any) Text)
    (|>> representation))

  (with_template [<sigil> <category> <name>]
    [(def .public <name>
       (Descriptor <category>)
       (abstraction <sigil>))]

    ["V" Void void]
    ["Z" Primitive boolean]
    ["B" Primitive byte]
    ["S" Primitive short]
    ["I" Primitive int]
    ["J" Primitive long]
    ["F" Primitive float]
    ["D" Primitive double]
    ["C" Primitive char]
    )

  (def .public class_prefix "L")
  (def .public class_suffix ";")

  (def .public class
    (-> External (Descriptor Class))
    (|>> ///name.internal
         ///name.read
         (text.enclosed [..class_prefix ..class_suffix])
         abstraction))

  (def .public (declaration name)
    (-> External (Descriptor Declaration))
    (transmutation (..class name)))

  (def .public as_class
    (-> (Descriptor Declaration) (Descriptor Class))
    (|>> transmutation))

  (with_template [<name> <category>]
    [(def .public <name>
       (Descriptor <category>)
       (transmutation
        (..class "java.lang.Object")))]

    [var Var]
    [wildcard Parameter]
    )

  (def .public (lower descriptor)
    (-> (Descriptor Parameter) (Descriptor Parameter))
    ..wildcard)

  (def .public upper
    (-> (Descriptor Parameter) (Descriptor Parameter))
    (|>> transmutation))
  
  (def .public array_prefix "[")

  (def .public array
    (-> (Descriptor Value)
        (Descriptor Array))
    (|>> representation
         (format ..array_prefix)
         abstraction))

  (def .public (method [inputs output])
    (-> [(List (Descriptor Value))
         (Descriptor Return)]
        (Descriptor Method))
    (abstraction
     (format (|> inputs
                 (list#each ..descriptor)
                 text.together
                 (text.enclosed ["(" ")"]))
             (representation output))))

  (def .public equivalence
    (All (_ category) (Equivalence (Descriptor category)))
    (implementation
     (def (= parameter subject)
       (text#= (representation parameter) (representation subject)))))

  (def .public class_name
    (-> (Descriptor Object) Internal)
    (let [prefix_size (text.size ..class_prefix)
          suffix_size (text.size ..class_suffix)]
      (function (_ descriptor)
        (let [repr (representation descriptor)]
          (if (text.starts_with? ..array_prefix repr)
            (///name.internal repr)
            (|> repr
                (text.clip prefix_size
                           (|> (text.size repr)
                               (n.- prefix_size)
                               (n.- suffix_size)))
                (of maybe.monad each ///name.internal)
                maybe.trusted))))))
  )