Repr v2 progress (#13268)

* progress on repr_v2

* repr progress

* add ref objects with distrinct

* fix failing tests
This commit is contained in:
cooldome 2020-01-28 19:05:57 +00:00 • committed by Andreas Rumpf
commit 76ede7c198
4 changed files with 97 additions and 78 deletions

View file

@ -1988,10 +1988,6 @@ template unlikely*(val: bool): bool =
import system/dollars import system/dollars
export dollars export dollars
when defined(nimV2):
import system/repr_v2
export repr_v2
const const
NimMajor* {.intdefine.}: int = 1 NimMajor* {.intdefine.}: int = 1
## is the major number of Nim's version. ## is the major number of Nim's version.
@ -2620,7 +2616,8 @@ type
## Represents a Nim AST node. Macros operate on this type. ## Represents a Nim AST node. Macros operate on this type.
when defined(nimV2): when defined(nimV2):
proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.} import system/repr_v2
export repr_v2
macro lenVarargs*(x: varargs[untyped]): int {.since: (1, 1).} = macro lenVarargs*(x: varargs[untyped]): int {.since: (1, 1).} =
## returns number of variadic arguments in `x` ## returns number of variadic arguments in `x`

View file

@ -1,3 +1,11 @@
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
## imported from typetraits
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
## imported from typetraits
proc repr*(x: NimNode): string {.magic: "Repr", noSideEffect.}
proc repr*(x: int): string {.magic: "IntToStr", noSideEffect.} proc repr*(x: int): string {.magic: "IntToStr", noSideEffect.}
## repr for an integer argument. Returns `x` ## repr for an integer argument. Returns `x`
## converted to a decimal string. ## converted to a decimal string.
@ -37,19 +45,47 @@ proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.}
## If a `repr` operator for a concrete enumeration is provided, this is ## If a `repr` operator for a concrete enumeration is provided, this is
## used instead. (In other words: *Overwriting* is possible.) ## used instead. (In other words: *Overwriting* is possible.)
template repr(t: typedesc): string = $t proc repr*(p: pointer): string =
## repr of pointer as its hexadecimal value
proc isNamedTuple(T: typedesc): bool = if p == nil:
# Taken from typetraits. result = "nil"
when T isnot tuple: result = false
else: else:
var t: T when nimvm:
for name, _ in t.fieldPairs: result = "ptr"
when name == "Field0": else:
return compiles(t.Field0) const HexChars = "0123456789ABCDEF"
else: const len = sizeof(pointer) * 2
return true var n = cast[uint](p)
return false result = newString(len)
for j in countdown(len-1, 0):
result[j] = HexChars[n and 0xF]
n = n shr 4
template repr*(x: distinct): string =
repr(distinctBase(typeof(x))(x))
template repr*(t: typedesc): string = $t
proc reprObject[T: tuple|object](res: var string, x: T) =
res.add '('
var firstElement = true
const isNamed = T is object or isNamedTuple(T)
when not isNamed:
var count = 0
for name, value in fieldPairs(x):
if not firstElement: res.add(", ")
when isNamed:
res.add(name)
res.add(": ")
else:
count.inc
res.add repr(value)
firstElement = false
when not isNamed:
if count == 1:
res.add(',') # $(1,) should print as the semantically legal (1,)
res.add(')')
proc repr*[T: tuple|object](x: T): string = proc repr*[T: tuple|object](x: T): string =
## Generic `repr` operator for tuples that is lifted from the components ## Generic `repr` operator for tuples that is lifted from the components
@ -61,56 +97,16 @@ proc repr*[T: tuple|object](x: T): string =
## $() == "()" ## $() == "()"
when T is object: when T is object:
result = $typeof(x) result = $typeof(x)
else: reprObject(result, x)
result = ""
result.add '(' proc repr*[T](x: ptr T): string =
var firstElement = true result.add repr(pointer(x)) & " "
const isNamed = T is object or isNamedTuple(T) result.add repr(x[])
when not isNamed:
var count = 0
for name, value in fieldPairs(x):
if not firstElement: result.add(", ")
when isNamed:
result.add(name)
result.add(": ")
else:
count.inc
when compiles($value):
when value isnot string and value isnot seq and compiles(value.isNil):
if value.isNil: result.add "nil"
else: result.addQuoted(value)
else:
result.addQuoted(value)
firstElement = false
else:
result.add("...")
firstElement = false
when not isNamed:
if count == 1:
result.add(',') # $(1,) should print as the semantically legal (1,)
result.add(')')
proc repr*[T: (ref object)](x: T): string = proc repr*[T](x: ref T | ptr T): string =
## Generic `repr` operator for tuples that is lifted from the components if isNil(x): return "nil"
## of `x`. result = $typeof(x)
if x == nil: return "nil" reprObject(result, x[])
result = $typeof(x) & "("
var firstElement = true
for name, value in fieldPairs(x[]):
if not firstElement: result.add(", ")
result.add(name)
result.add(": ")
when compiles($value):
when value isnot string and value isnot seq and compiles(value.isNil):
if value.isNil: result.add "nil"
else: result.addQuoted(value)
else:
result.addQuoted(value)
firstElement = false
else:
result.add("...")
firstElement = false
result.add(')')
proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string = proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
result = prefix result = prefix
@ -120,15 +116,7 @@ proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
firstElement = false firstElement = false
else: else:
result.add(separator) result.add(separator)
result.add repr(value)
when value isnot string and value isnot seq and compiles(value.isNil):
# this branch should not be necessary
if value.isNil:
result.add "nil"
else:
result.addQuoted(value)
else:
result.addQuoted(value)
result.add(suffix) result.add(suffix)
proc repr*[T](x: set[T]): string = proc repr*[T](x: set[T]): string =
@ -153,9 +141,9 @@ proc repr*[T, U](x: HSlice[T, U]): string =
## ##
## .. code-block:: Nim ## .. code-block:: Nim
## $(1 .. 5) == "1 .. 5" ## $(1 .. 5) == "1 .. 5"
result = $x.a result = repr(x.a)
result.add(" .. ") result.add(" .. ")
result.add($x.b) result.add(repr(x.b))
proc repr*[T, IDX](x: array[IDX, T]): string = proc repr*[T, IDX](x: array[IDX, T]): string =
## Generic `repr` operator for arrays that is lifted from the components. ## Generic `repr` operator for arrays that is lifted from the components.

34
tests/arc/trepr.nim Normal file
View file

@ -0,0 +1,34 @@
discard """
cmd: "nim c --gc:arc $file"
nimout: '''(a: true, n: doAssert)
Table[system.string, trepr.MyType](data: @[], counter: 0)
nil
'''
"""
import tables
type
NimSym = distinct NimNode
MyType = tuple
a: bool
n: NimSym
proc myproc(t: MyType) =
echo repr(t)
proc myproc2(t: MyType) =
var x = Table[string, t]()
echo repr(x)
proc myproc3(t: MyType) =
var x: TableRef[string, t]
echo repr(x)
macro dumpSym(a: typed) =
myproc((a: true, n: NimSym(a)))
myproc2((a: true, n: NimSym(a)))
myproc3((a: true, n: NimSym(a)))
dumpSym(doAssert)

View file

@ -1,6 +1,6 @@
discard """ discard """
cmd: "nim c --gc:arc $file" cmd: "nim c --gc:arc $file"
output: '''Foo(field: "Dick Laurent", k: ka, x: 0.0) output: '''Foo(field: Dick Laurent, k: ka, x: 0.0)
Nobody is dead Nobody is dead
Dick Laurent is dead''' Dick Laurent is dead'''
""" """