repr_v2.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. include system/inclrtl
  2. when defined(nimPreviewSlimSystem):
  3. import std/formatfloat
  4. proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
  5. ## imported from typetraits
  6. proc distinctBase(T: typedesc, recursive: static bool = true): typedesc {.magic: "TypeTrait".}
  7. ## imported from typetraits
  8. proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
  9. proc repr*(x: int): string =
  10. ## Same as $x
  11. $x
  12. proc repr*(x: int64): string =
  13. ## Same as $x
  14. $x
  15. proc repr*(x: uint64): string {.noSideEffect.} =
  16. ## Same as $x
  17. $x
  18. proc repr*(x: float): string =
  19. ## Same as $x
  20. $x
  21. proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
  22. ## repr for a boolean argument. Returns `x`
  23. ## converted to the string "false" or "true".
  24. proc repr*(x: char): string {.noSideEffect.} =
  25. ## repr for a character argument. Returns `x`
  26. ## converted to an escaped string.
  27. ##
  28. ## .. code-block:: Nim
  29. ## assert repr('c') == "'c'"
  30. result.add '\''
  31. # Elides string creations if not needed
  32. if x in {'\\', '\0'..'\31', '\127'..'\255'}:
  33. result.add '\\'
  34. if x in {'\0'..'\31', '\127'..'\255'}:
  35. result.add $x.uint8
  36. else:
  37. result.add x
  38. result.add '\''
  39. proc repr*(x: string | cstring): string {.noSideEffect.} =
  40. ## repr for a string argument. Returns `x`
  41. ## converted to a quoted and escaped string.
  42. result.add '\"'
  43. for i in 0..<x.len:
  44. if x[i] in {'"', '\\', '\0'..'\31', '\127'..'\255'}:
  45. result.add '\\'
  46. case x[i]:
  47. of '\n':
  48. result.add "n\n"
  49. of '\0'..'\9', '\11'..'\31', '\127'..'\255':
  50. result.add $x[i].uint8
  51. else:
  52. result.add x[i]
  53. result.add '\"'
  54. proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
  55. ## repr for an enumeration argument. This works for
  56. ## any enumeration type thanks to compiler magic.
  57. ##
  58. ## If a `repr` operator for a concrete enumeration is provided, this is
  59. ## used instead. (In other words: *Overwriting* is possible.)
  60. proc reprDiscriminant*(e: int): string {.compilerproc.} =
  61. # repr and reprjs can use `PNimType` to symbolize `e`; making this work here
  62. # would require a way to pass the set of enum stringified values to cgen.
  63. $e
  64. proc repr*(p: pointer): string =
  65. ## repr of pointer as its hexadecimal value
  66. if p == nil:
  67. result = "nil"
  68. else:
  69. when nimvm:
  70. result = "ptr"
  71. else:
  72. const HexChars = "0123456789ABCDEF"
  73. const len = sizeof(pointer) * 2
  74. var n = cast[uint](p)
  75. result = newString(len)
  76. for j in countdown(len-1, 0):
  77. result[j] = HexChars[n and 0xF]
  78. n = n shr 4
  79. proc repr*(p: proc): string =
  80. ## repr of a proc as its address
  81. repr(cast[ptr pointer](unsafeAddr p)[])
  82. template repr*(x: distinct): string =
  83. repr(distinctBase(typeof(x))(x))
  84. template repr*(t: typedesc): string = $t
  85. proc reprObject[T: tuple|object](res: var string, x: T) =
  86. res.add '('
  87. var firstElement = true
  88. const isNamed = T is object or isNamedTuple(T)
  89. when not isNamed:
  90. var count = 0
  91. for name, value in fieldPairs(x):
  92. if not firstElement: res.add(", ")
  93. when isNamed:
  94. res.add(name)
  95. res.add(": ")
  96. else:
  97. count.inc
  98. res.add repr(value)
  99. firstElement = false
  100. when not isNamed:
  101. if count == 1:
  102. res.add(',') # $(1,) should print as the semantically legal (1,)
  103. res.add(')')
  104. proc repr*[T: tuple|object](x: T): string =
  105. ## Generic `repr` operator for tuples that is lifted from the components
  106. ## of `x`. Example:
  107. ##
  108. ## .. code-block:: Nim
  109. ## $(23, 45) == "(23, 45)"
  110. ## $(a: 23, b: 45) == "(a: 23, b: 45)"
  111. ## $() == "()"
  112. when T is object:
  113. result = $typeof(x)
  114. reprObject(result, x)
  115. proc repr*[T](x: ref T | ptr T): string =
  116. if isNil(x): return "nil"
  117. when T is object:
  118. result = $typeof(x)
  119. reprObject(result, x[])
  120. else:
  121. result = when typeof(x) is ref: "ref " else: "ptr "
  122. result.add repr(x[])
  123. proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
  124. result = prefix
  125. var firstElement = true
  126. for value in items(x):
  127. if firstElement:
  128. firstElement = false
  129. else:
  130. result.add(separator)
  131. result.add repr(value)
  132. result.add(suffix)
  133. proc repr*[T](x: set[T]): string =
  134. ## Generic `repr` operator for sets that is lifted from the components
  135. ## of `x`. Example:
  136. ##
  137. ## .. code-block:: Nim
  138. ## ${23, 45} == "{23, 45}"
  139. collectionToRepr(x, "{", ", ", "}")
  140. proc repr*[T](x: seq[T]): string =
  141. ## Generic `repr` operator for seqs that is lifted from the components
  142. ## of `x`. Example:
  143. ##
  144. ## .. code-block:: Nim
  145. ## $(@[23, 45]) == "@[23, 45]"
  146. collectionToRepr(x, "@[", ", ", "]")
  147. proc repr*[T, U](x: HSlice[T, U]): string =
  148. ## Generic `repr` operator for slices that is lifted from the components
  149. ## of `x`. Example:
  150. ##
  151. ## .. code-block:: Nim
  152. ## $(1 .. 5) == "1 .. 5"
  153. result = repr(x.a)
  154. result.add(" .. ")
  155. result.add(repr(x.b))
  156. proc repr*[T, IDX](x: array[IDX, T]): string =
  157. ## Generic `repr` operator for arrays that is lifted from the components.
  158. collectionToRepr(x, "[", ", ", "]")
  159. proc repr*[T](x: openArray[T]): string =
  160. ## Generic `repr` operator for openarrays that is lifted from the components
  161. ## of `x`. Example:
  162. ##
  163. ## .. code-block:: Nim
  164. ## $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  165. collectionToRepr(x, "[", ", ", "]")