aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/type/signature.lux
blob: 84a6a59825743e5810f511189e95dd8e8df994f4 (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
(.module:
  [library
   [lux (#- int char)
    [abstract
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]]
    [data
     ["." text ("#\." hash)
      ["%" format (#+ format)]]
     [collection
      ["." list ("#\." functor)]]]
    [type
     abstract]]]
  ["." // #_
   [category (#+ Void Value Return Method Primitive Object Class Array Var Parameter Declaration)]
   ["#." descriptor]
   ["/#" // #_
    [encoding
     ["#." name (#+ External)]]]])

(abstract: #export (Signature category)
  Text

  (def: #export signature
    (-> (Signature Any) Text)
    (|>> :representation))

  (template [<category> <name> <descriptor>]
    [(def: #export <name>
       (Signature <category>)
       (:abstraction (//descriptor.descriptor <descriptor>)))]

    [Void void //descriptor.void]
    [Primitive boolean //descriptor.boolean]
    [Primitive byte //descriptor.byte]
    [Primitive short //descriptor.short]
    [Primitive int //descriptor.int]
    [Primitive long //descriptor.long]
    [Primitive float //descriptor.float]
    [Primitive double //descriptor.double]
    [Primitive char //descriptor.char]
    )

  (def: #export array
    (-> (Signature Value) (Signature Array))
    (|>> :representation
         (format //descriptor.array_prefix)
         :abstraction))

  (def: #export wildcard
    (Signature Parameter)
    (:abstraction "*"))

  (def: #export var_prefix "T")
  
  (def: #export var
    (-> Text (Signature Var))
    (|>> (text.enclosed [..var_prefix //descriptor.class_suffix])
         :abstraction))

  (def: #export var_name
    (-> (Signature Var) Text)
    (|>> :representation
         (text.replace_all ..var_prefix "")
         (text.replace_all //descriptor.class_suffix "")))

  (def: #export lower_prefix "-")
  (def: #export upper_prefix "+")
  
  (template [<name> <prefix>]
    [(def: #export <name>
       (-> (Signature Class) (Signature Parameter))
       (|>> :representation (format <prefix>) :abstraction))]

    [lower ..lower_prefix]
    [upper ..upper_prefix]
    )

  (def: #export parameters_start "<")
  (def: #export parameters_end ">")

  (def: #export (class name parameters)
    (-> External (List (Signature Parameter)) (Signature Class))
    (:abstraction
     (format //descriptor.class_prefix
             (|> name ///name.internal ///name.read)
             (case parameters
               #.End
               ""

               _
               (format ..parameters_start
                       (|> parameters
                           (list\map ..signature)
                           (text.join_with ""))
                       ..parameters_end))
             //descriptor.class_suffix)))

  (def: #export (declaration name variables)
    (-> External (List (Signature Var)) (Signature Declaration))
    (:transmutation (..class name variables)))

  (def: #export as_class
    (-> (Signature Declaration) (Signature Class))
    (|>> :transmutation))

  (def: #export arguments_start "(")
  (def: #export arguments_end ")")

  (def: #export exception_prefix "^")

  (def: class_bound
    (|> (..class "java.lang.Object" (list))
        ..signature
        (format ":")))

  (def: #export (method [type_variables inputs output exceptions])
    (-> [(List (Signature Var))
         (List (Signature Value))
         (Signature Return)
         (List (Signature Class))]
        (Signature Method))
    (:abstraction
     (format (case type_variables
               #.End
               ""
               _
               (|> type_variables
                   (list\map (|>> ..var_name
                                  (text.suffix ..class_bound)))
                   (text.join_with "")
                   (text.enclosed [..parameters_start
                                   ..parameters_end])))
             (|> inputs
                 (list\map ..signature)
                 (text.join_with "")
                 (text.enclosed [..arguments_start
                                 ..arguments_end]))
             (:representation output)
             (|> exceptions
                 (list\map (|>> :representation (format ..exception_prefix)))
                 (text.join_with "")))))

  (implementation: #export equivalence
    (All [category] (Equivalence (Signature category)))
    
    (def: (= parameter subject)
      (text\= (:representation parameter)
              (:representation subject))))

  (implementation: #export hash
    (All [category] (Hash (Signature category)))

    (def: &equivalence ..equivalence)
    (def: hash (|>> :representation text\hash)))
  )