aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/type/signature.lux
blob: f669899728164180c981305bb1116e02f9ac0200 (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
(.using
 [library
  [lux (.except Primitive int char)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text (.open: "[1]#[0]" hash)
     ["%" format (.only format)]]
    [collection
     ["[0]" list (.open: "[1]#[0]" functor)]]]
   [type
    [primitive (.except)]]]]
 ["[0]" //
  [category (.only Void Value Return Method Primitive Object Class Array Var Parameter Declaration Inheritance)]
  ["[1][0]" descriptor]
  ["/[1]" //
   [encoding
    ["[1][0]" name (.only External)]]]])

(primitive: .public (Signature category)
  Text

  (def: .public signature
    (-> (Signature Any) Text)
    (|>> representation))

  (template [<category> <name> <descriptor>]
    [(def: .public <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: .public array
    (-> (Signature Value) (Signature Array))
    (|>> representation
         (format //descriptor.array_prefix)
         abstraction))

  (def: .public wildcard
    (Signature Parameter)
    (abstraction "*"))

  (template [<char> <name>]
    [(def: .public <name> <char>)]

    ["T" var_prefix]
    ["-" lower_prefix]
    ["+" upper_prefix]

    ["<" parameters_start]
    [">" parameters_end]
    [":" format_type_parameter_infix]

    ["(" arguments_start]
    [")" arguments_end]
    ["^" exception_prefix]
    )
  
  (template [<name> <prefix>]
    [(def: .public <name>
       (-> (Signature Parameter) (Signature Parameter))
       (|>> representation (format <prefix>) abstraction))]

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

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

  (def: .public var_name
    (-> (Signature Var) Text)
    (|>> representation
         (text.replaced ..var_prefix "")
         (text.replaced //descriptor.class_suffix "")))

  (def: .public (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#each ..signature)
                           text.together)
                       ..parameters_end))
             //descriptor.class_suffix)))

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

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

  (def: var_declaration/1
    (-> (Signature Var) Text)
    (|>> ..var_name
         (text.suffix ..class_bound)))

  (def: var_declaration/+
    (-> (List (Signature Var)) Text)
    (|>> (list#each ..var_declaration/1)
         text.together
         (text.enclosed [..parameters_start
                         ..parameters_end])))

  (def: var_declaration/*
    (-> (List (Signature Var)) Text)
    (|>> (pipe.case
           {.#End}
           ""
           
           it
           (..var_declaration/+ it))))

  (def: .public (inheritance variables super interfaces)
    (-> (List (Signature Var)) (Signature Class) (List (Signature Class)) (Signature Inheritance))
    (abstraction
     (format (var_declaration/* variables)
             (representation super)
             (|> interfaces
                 (list#each ..signature)
                 text.together))))

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

  (def: .public (method [type_variables inputs output exceptions])
    (-> [(List (Signature Var))
         (List (Signature Value))
         (Signature Return)
         (List (Signature Class))]
        (Signature Method))
    (abstraction
     (format (var_declaration/* type_variables)
             (|> inputs
                 (list#each ..signature)
                 text.together
                 (text.enclosed [..arguments_start
                                 ..arguments_end]))
             (representation output)
             (|> exceptions
                 (list#each (|>> representation (format ..exception_prefix)))
                 text.together))))

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

  (implementation: .public hash
    (All (_ category) (Hash (Signature category)))

    (def: equivalence ..equivalence)
    (def: hash (|>> representation text#hash)))
  )