aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/type/lux.lux
blob: 60ca730d2c7bedf85b9df7f37246733e36e04b32 (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
(.using
 [library
  [lux (.except Primitive int char type parameter)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try]
    ["[0]" exception (.only exception:)]
    ["<>" parser ("[1]#[0]" monad)
     ["<[0]>" text (.only Parser)]]]
   [data
    ["[0]" product]
    ["[0]" text ("[1]#[0]" equivalence)
     ["%" format (.only format)]]
    [collection
     ["[0]" array]
     ["[0]" dictionary (.only Dictionary)]]]
   [type
    [primitive (.except)]
    ["[0]" check (.only Check) ("[1]#[0]" monad)]]]]
 ["[0]" // (.only)
  [category (.only Void Value Return Method Primitive Object Class Array Var Parameter)]
  ["[1][0]" descriptor]
  ["[1][0]" signature]
  ["[1][0]" reflection]
  ["[1][0]" parser]
  ["[1][0]" box]
  ["/[1]" //
   [encoding
    ["[1][0]" name]]]])

(template [<name>]
  [(primitive: .public (<name> class) Any)]

  [Lower] [Upper]
  )

(type: .public Mapping
  (Dictionary Text Type))

(def: .public fresh
  Mapping
  (dictionary.empty text.hash))

(exception: .public (unknown_var [var Text])
  (exception.report
   "Var" (%.text var)))

(def: void
  (Parser (Check Type))
  (<>.after //parser.void
            (<>#in (check#in .Any))))

(template [<name> <parser> <reflection>]
  [(def: <name>
     (Parser (Check Type))
     (<>.after <parser>
               (<>#in (check#in {.#Primitive (//reflection.reflection <reflection>) {.#End}}))))]

  [boolean //parser.boolean //reflection.boolean]
  [byte //parser.byte //reflection.byte]
  [short //parser.short //reflection.short]
  [int //parser.int //reflection.int]
  [long //parser.long //reflection.long]
  [float //parser.float //reflection.float]
  [double //parser.double //reflection.double]
  [char //parser.char //reflection.char]
  )

(template [<name> <parser> <box>]
  [(def: <name>
     (Parser (Check Type))
     (<>.after <parser>
               (<>#in (check#in {.#Primitive <box> {.#End}}))))]

  [boxed_boolean //parser.boolean //box.boolean]
  [boxed_byte //parser.byte //box.byte]
  [boxed_short //parser.short //box.short]
  [boxed_int //parser.int //box.int]
  [boxed_long //parser.long //box.long]
  [boxed_float //parser.float //box.float]
  [boxed_double //parser.double //box.double]
  [boxed_char //parser.char //box.char]
  )

(def: primitive
  (Parser (Check Type))
  (all <>.either
       ..boolean
       ..byte
       ..short
       ..int
       ..long
       ..float
       ..double
       ..char
       ))

(def: boxed_primitive
  (Parser (Check Type))
  (all <>.either
       ..boxed_boolean
       ..boxed_byte
       ..boxed_short
       ..boxed_int
       ..boxed_long
       ..boxed_float
       ..boxed_double
       ..boxed_char
       ))

(def: wildcard
  (Parser (Check Type))
  (<>.after //parser.wildcard
            (<>#in (check#each product.right
                               check.existential))))

(def: (var mapping)
  (-> Mapping (Parser (Check Type)))
  (do <>.monad
    [var //parser.var']
    (in (case (dictionary.value var mapping)
          {.#None}
          (check.except ..unknown_var [var])
          
          {.#Some type}
          (check#in type)))))

(def: (class' parameter)
  (-> (Parser (Check Type)) (Parser (Check Type)))
  (|> (do <>.monad
        [name //parser.class_name
         parameters (|> (<>.some parameter)
                        (<>.after (<text>.this //signature.parameters_start))
                        (<>.before (<text>.this //signature.parameters_end))
                        (<>.else (list)))]
        (in (do [! check.monad]
              [parameters (monad.all ! parameters)]
              (in {.#Primitive name parameters}))))
      (<>.after (<text>.this //descriptor.class_prefix))
      (<>.before (<text>.this //descriptor.class_suffix))))

(template [<name> <prefix> <constructor>]
  [(def: <name>
     (-> (Parser (Check Type)) (Parser (Check Type)))
     (|> (<>.after (<text>.this <prefix>))
         ... TODO: Re-enable Lower and Upper, instead of using the simplified limit.
         ... (<>#each (check#each (|>> <ctor> .type)))
         ))]

  [lower //signature.lower_prefix ..Lower]
  [upper //signature.upper_prefix ..Upper]
  )

(def: (parameter mapping)
  (-> Mapping (Parser (Check Type)))
  (<>.rec
   (function (_ parameter)
     (let [class (..class' parameter)]
       (all <>.either
            (..var mapping)
            ..wildcard
            (..lower class)
            (..upper class)
            class
            )))))

(def: .public class
  (-> Mapping (Parser (Check Type)))
  (|>> ..parameter ..class'))

(def: array
  (-> (Parser (Check Type)) (Parser (Check Type)))
  (|>> (<>#each (check#each (function (_ elementT)
                              (case elementT
                                {.#Primitive name {.#End}}
                                (if (`` (or (~~ (template [<reflection>]
                                                  [(text#= (//reflection.reflection <reflection>) name)]

                                                  [//reflection.boolean]
                                                  [//reflection.byte]
                                                  [//reflection.short]
                                                  [//reflection.int]
                                                  [//reflection.long]
                                                  [//reflection.float]
                                                  [//reflection.double]
                                                  [//reflection.char]))))
                                  {.#Primitive (|> name //reflection.class //reflection.array //reflection.reflection) {.#End}}
                                  (|> elementT array.Array .type))

                                _
                                (|> elementT array.Array .type)))))
       (<>.after (<text>.this //descriptor.array_prefix))))

(def: .public (type mapping)
  (-> Mapping (Parser (Check Type)))
  (<>.rec
   (function (_ type)
     (all <>.either
          ..primitive
          (parameter mapping)
          (..array type)
          ))))

(def: .public (boxed_type mapping)
  (-> Mapping (Parser (Check Type)))
  (<>.rec
   (function (_ type)
     (all <>.either
          ..boxed_primitive
          (parameter mapping)
          (..array type)
          ))))

(def: .public (return mapping)
  (-> Mapping (Parser (Check Type)))
  (all <>.either
       ..void
       (..type mapping)
       ))

(def: .public (boxed_return mapping)
  (-> Mapping (Parser (Check Type)))
  (all <>.either
       ..void
       (..boxed_type mapping)
       ))

(def: .public (check operation input)
  (All (_ a) (-> (Parser (Check a)) Text (Check a)))
  (case (<text>.result operation input)
    {try.#Success check}
    check
    
    {try.#Failure error}
    (check.failure error)))