aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/host/jvm/type.lux
blob: 72a1925b4fb0fa49e4bfb45ed5e6006cda61e8fa (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
(.module:
  [lux (#- int char)
   [data
    ["." text
     format]
    [collection
     ["." list ("#/." functor)]]]]
  ["." //])

## Types
(template [<name> <primitive>]
  [(def: #export <name> //.Type (#//.Primitive <primitive>))]

  [boolean #//.Boolean]
  [byte    #//.Byte]
  [short   #//.Short]
  [int     #//.Int]
  [long    #//.Long]
  [float   #//.Float]
  [double  #//.Double]
  [char    #//.Char]
  )

(def: #export (class name params)
  (-> Text (List //.Generic) //.Type)
  (#//.Generic (#//.Class name params)))

(def: #export (var name)
  (-> Text //.Type)
  (#//.Generic (#//.Var name)))

(def: #export (wildcard bound)
  (-> (Maybe [//.Bound //.Generic]) //.Type)
  (#//.Generic (#//.Wildcard bound)))

(def: #export (array depth elemT)
  (-> Nat //.Type //.Type)
  (case depth
    0 elemT
    _ (#//.Array (array (dec depth) elemT))))

(def: #export binary-name
  (-> Text Text)
  (text.replace-all "." "/"))

(def: #export (descriptor type)
  (-> //.Type Text)
  (case type
    (#//.Primitive prim)
    (case prim
      #//.Boolean "Z"
      #//.Byte    "B"
      #//.Short   "S"
      #//.Int     "I"
      #//.Long    "J"
      #//.Float   "F"
      #//.Double  "D"
      #//.Char    "C")

    (#//.Array sub)
    (format "[" (descriptor sub))

    (#//.Generic generic)
    (case generic
      (#//.Class class params)
      (format "L" (binary-name class) ";")

      (^or (#//.Var name) (#//.Wildcard ?bound))
      (descriptor (#//.Generic (#//.Class "java.lang.Object" (list)))))
    ))

(def: #export (signature type)
  (-> //.Type Text)
  (case type
    (#//.Primitive prim)
    (case prim
      #//.Boolean "Z"
      #//.Byte    "B"
      #//.Short   "S"
      #//.Int     "I"
      #//.Long    "J"
      #//.Float   "F"
      #//.Double  "D"
      #//.Char    "C")

    (#//.Array sub)
    (format "[" (signature sub))

    (#//.Generic generic)
    (case generic
      (#//.Class class params)
      (let [=params (if (list.empty? params)
                      ""
                      (format "<"
                              (|> params
                                  (list/map (|>> #//.Generic signature))
                                  (text.join-with ""))
                              ">"))]
        (format "L" (binary-name class) =params ";"))

      (#//.Var name)
      (format "T" name ";")

      (#//.Wildcard #.None)
      "*"

      (^template [<tag> <prefix>]
        (#//.Wildcard (#.Some [<tag> bound]))
        (format <prefix> (signature (#//.Generic bound))))
      ([#//.Upper "+"]
       [#//.Lower "-"]))
    ))

## Methods
(def: #export (method args return exceptions)
  (-> (List //.Type) (Maybe //.Type) (List //.Generic) //.Method)
  {#//.args args #//.return return #//.exceptions exceptions})

(def: #export (method-descriptor method)
  (-> //.Method Text)
  (format "(" (text.join-with "" (list/map descriptor (get@ #//.args method))) ")"
          (case (get@ #//.return method)
            #.None
            "V"

            (#.Some return)
            (descriptor return))))

(def: #export (method-signature method)
  (-> //.Method Text)
  (format "(" (|> (get@ #//.args method) (list/map signature) (text.join-with "")) ")"
          (case (get@ #//.return method)
            #.None
            "V"

            (#.Some return)
            (signature return))
          (|> (get@ #//.exceptions method)
              (list/map (|>> #//.Generic signature (format "^")))
              (text.join-with ""))))