aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/debug.lux
blob: 43d3f4762e6bf18d5bc5a76a5bc8a38ed9617c63 (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
(.module:
  [lux #*
   ["." type]
   ["." host (#+ import:)]
   [abstract
    [monad (#+ do)]]
   [control
    ["." function]
    ["." exception (#+ exception:)]
    ["<>" parser
     ["<.>" type (#+ Parser)]]
    pipe]
   [data
    ["." error (#+ Error)]
    ["." text
     format]
    [format
     [xml (#+ XML)]
     [json (#+ JSON)]]
    [collection
     ["." array (#+ Array)]
     ["." list ("#@." functor)]]]
   [time
    [instant (#+ Instant)]
    [duration (#+ Duration)]
    [date (#+ Date)]]
   [macro
    ["." template]]])

(import: #long java/lang/String)

(import: #long (java/lang/Class a)
  (getCanonicalName [] java/lang/String))

(import: #long java/lang/Object
  (new [])
  (toString [] java/lang/String)
  (getClass [] (java/lang/Class java/lang/Object)))

(import: #long java/lang/Integer
  (longValue [] java/lang/Long))

(import: #long java/lang/Long
  (intValue [] java/lang/Integer))

(import: #long java/lang/Number
  (intValue [] java/lang/Integer)
  (longValue [] long)
  (doubleValue [] double))

(def: #export (inspect value)
  (-> Any Text)
  (let [object (:coerce java/lang/Object value)]
    (`` (<| (~~ (template [<class> <processing>]
                  [(case (host.check <class> object)
                     (#.Some value)
                     (`` (|> value (~~ (template.splice <processing>))))
                     #.None)]

                  [java/lang/Boolean [%b]]
                  [java/lang/String [%t]]
                  [java/lang/Long [.int %i]]
                  [java/lang/Number [java/lang/Number::doubleValue %f]]
                  ))
            (case (host.check (Array java/lang/Object) object)
              (#.Some value)
              (let [value (:coerce (Array java/lang/Object) value)]
                (case (array.read 0 value)
                  (^multi (#.Some tag)
                          [(host.check java/lang/Integer tag)
                           (#.Some tag)]
                          [[(array.read 1 value)
                            (array.read 2 value)]
                           [last?
                            (#.Some choice)]])
                  (let [last? (case last?
                                (#.Some _) #1
                                #.None #0)]
                    (|> (format (%n (.nat (java/lang/Integer::longValue tag)))
                                " " (%b last?)
                                " " (inspect choice))
                        (text.enclose ["(" ")"])))

                  _
                  (|> value
                      array.to-list
                      (list@map inspect)
                      (text.join-with " ")
                      (text.enclose ["[" "]"]))))
              #.None)
            (java/lang/Object::toString object)))
    ))

(exception: #export (cannot-represent-value {type Type})
  (exception.report
   ["Type" (%type type)]))

(type: Representation (-> Any Text))

(def: primitive-representation
  (Parser Representation)
  (`` ($_ <>.either
          (do <>.monad
            [_ (<type>.exactly Any)]
            (wrap (function.constant "[]")))
          
          (~~ (template [<type> <formatter>]
                [(do <>.monad
                   [_ (<type>.sub <type>)]
                   (wrap (|>> (:coerce <type>) <formatter>)))]

                [Bit %b]
                [Nat %n]
                [Int %i]
                [Rev %r]
                [Frac %f]
                [Text %t])))))

(def: (special-representation representation)
  (-> (Parser Representation) (Parser Representation))
  (`` ($_ <>.either
          (~~ (template [<type> <formatter>]
                [(do <>.monad
                   [_ (<type>.sub <type>)]
                   (wrap (|>> (:coerce <type>) <formatter>)))]

                [Type %type]
                [Code %code]
                [Instant %instant]
                [Duration %duration]
                [Date %date]
                [JSON %json]
                [XML %xml]))

          (do <>.monad
            [[_ elemT] (<type>.apply (<>.and (<type>.exactly List) <type>.any))
             elemR (<type>.local (list elemT) representation)]
            (wrap (|>> (:coerce (List Any)) (%list elemR))))

          (do <>.monad
            [[_ elemT] (<type>.apply (<>.and (<type>.exactly Maybe) <type>.any))
             elemR (<type>.local (list elemT) representation)]
            (wrap (|>> (:coerce (Maybe Any))
                       (case> #.None
                              "#.None"

                              (#.Some elemV)
                              (format "(#.Some " (elemR elemV) ")"))))))))

(def: (variant-representation representation)
  (-> (Parser Representation) (Parser Representation))
  (do <>.monad
    [membersR+ (<type>.variant (<>.many representation))]
    (wrap (function (_ variantV)
            (let [[lefts right? sub-repr] (loop [lefts 0
                                                 representations membersR+
                                                 variantV variantV]
                                            (case representations
                                              (#.Cons leftR (#.Cons rightR extraR+))
                                              (case (:coerce (| Any Any) variantV)
                                                (#.Left left)
                                                [lefts #0 (leftR left)]

                                                (#.Right right)
                                                (case extraR+
                                                  #.Nil
                                                  [lefts #1 (rightR right)]

                                                  extraR+
                                                  (recur (inc lefts) (#.Cons rightR extraR+) right)))

                                              _
                                              (undefined)))]
              (format "(" (%n lefts) " " (%b right?) " " sub-repr ")"))))))

(def: (tuple-representation representation)
  (-> (Parser Representation) (Parser Representation))
  (do <>.monad
    [membersR+ (<type>.tuple (<>.many representation))]
    (wrap (function (_ tupleV)
            (let [tuple-body (loop [representations membersR+
                                    tupleV tupleV]
                               (case representations
                                 #.Nil
                                 ""
                                 
                                 (#.Cons lastR #.Nil)
                                 (lastR tupleV)
                                 
                                 (#.Cons headR tailR)
                                 (let [[leftV rightV] (:coerce [Any Any] tupleV)]
                                   (format (headR leftV) " " (recur tailR rightV)))))]
              (format "[" tuple-body "]"))))))

(def: representation
  (Parser Representation)
  (<>.rec
   (function (_ representation)
     ($_ <>.either
         primitive-representation
         (special-representation representation)
         (variant-representation representation)
         (tuple-representation representation)

         (do <>.monad
           [[funcT inputsT+] (<type>.apply (<>.and <type>.any (<>.many <type>.any)))]
           (case (type.apply inputsT+ funcT)
             (#.Some outputT)
             (<type>.local (list outputT) representation)

             #.None
             (<>.fail "")))

         (do <>.monad
           [[name anonymous] <type>.named]
           (<type>.local (list anonymous) representation))

         (<>.fail "")
         ))))

(def: #export (represent type value)
  (-> Type Any (Error Text))
  (case (<type>.run type ..representation)
    (#error.Success representation)
    (#error.Success (representation value))

    (#error.Failure error)
    (exception.throw cannot-represent-value type)))