Add objprint

Add ability to print (ref) objects/tuples
This commit is contained in:
Joey 2020-09-16 10:06:58 -06:00 • committed by GitHub
commit e96cd5abe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,40 @@
import strutils
import typetraits
proc typeToStr[T](some: typedesc[T]): string = name(T)
template tupleObjToStr(obj): string =
mixin typeToStr
var res = typeToStr(type(obj))
template helper(n) {.gensym.} =
res.add("(")
var firstElement = true
for name, value in n.fieldPairs():
when compiles(value):
if not firstElement:
res.add(", ")
res.add(name)
res.add(": ")
when (value is object or value is tuple):
when (value is tuple):
res.add("tuple " & typeToStr(type(value)))
else:
res.add(typeToStr(type(value)))
helper(value)
elif (value is string):
res.add("\"" & $value & "\"")
else:
res.add($value)
firstElement = false
res.add(")")
helper(obj)
res
proc `$`*(s: object): string =
result = tupleObjToStr(s).replace(":ObjectType", "")
proc `$`*(s: ref object): string =
result = "ref " & $s[]
proc `$`*(s: tuple): string =
result = tupleObjToStr(s)