aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/source/lux/debug.lux75
1 files changed, 75 insertions, 0 deletions
diff --git a/stdlib/source/lux/debug.lux b/stdlib/source/lux/debug.lux
new file mode 100644
index 000000000..63a46aff4
--- /dev/null
+++ b/stdlib/source/lux/debug.lux
@@ -0,0 +1,75 @@
+(.module:
+ [lux #*
+ [data
+ ["." text
+ format]
+ [collection
+ ["." array (#+ Array)]
+ ["." list ("#@." functor)]]]
+ [macro
+ ["." template]]
+ ["." host (#+ import:)]])
+
+(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)))
+ ))