* Fixes #7595

* Add brackets

* Fix for treeRepr and lispRepr too
This commit is contained in:
Dmitry Atamanov 2018-04-16 20:37:08 +03:00 • committed by Andreas Rumpf
commit 412cd61dab

View file

@ -706,8 +706,7 @@ proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
res.add(($n.kind).substr(3)) res.add(($n.kind).substr(3))
case n.kind case n.kind
of nnkEmpty: discard # same as nil node in this representation of nnkEmpty, nnkNilLit: discard # same as nil node in this representation
of nnkNilLit: res.add(" nil")
of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal) of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal) of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal)
of nnkStrLit..nnkTripleStrLit, nnkIdent, nnkSym: of nnkStrLit..nnkTripleStrLit, nnkIdent, nnkSym:
@ -730,8 +729,7 @@ proc lispRepr*(n: NimNode): string {.compileTime, benign.} =
add(result, "(") add(result, "(")
case n.kind case n.kind
of nnkEmpty: discard # same as nil node in this representation of nnkEmpty, nnkNilLit: discard # same as nil node in this representation
of nnkNilLit: add(result, "nil")
of nnkCharLit..nnkInt64Lit: add(result, $n.intVal) of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal) of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkident, nnkSym: of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkident, nnkSym:
@ -766,7 +764,7 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
## See also `repr`, `treeRepr`, and `lispRepr`. ## See also `repr`, `treeRepr`, and `lispRepr`.
const const
NodeKinds = {nnkEmpty, nnkNilLit, nnkIdent, nnkSym, nnkNone, nnkCommentStmt} NodeKinds = {nnkEmpty, nnkIdent, nnkSym, nnkNone, nnkCommentStmt}
LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit} LitKinds = {nnkCharLit..nnkInt64Lit, nnkFloatLit..nnkFloat64Lit, nnkStrLit..nnkTripleStrLit}
proc traverse(res: var string, level: int, n: NimNode) {.benign.} = proc traverse(res: var string, level: int, n: NimNode) {.benign.} =
@ -775,12 +773,13 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
res.add("new" & ($n.kind).substr(3) & "Node(") res.add("new" & ($n.kind).substr(3) & "Node(")
elif n.kind in LitKinds: elif n.kind in LitKinds:
res.add("newLit(") res.add("newLit(")
elif n.kind == nnkNilLit:
res.add("newNilLit()")
else: else:
res.add($n.kind) res.add($n.kind)
case n.kind case n.kind
of nnkEmpty: discard of nnkEmpty, nnkNilLit: discard
of nnkNilLit: res.add("nil")
of nnkCharLit: res.add("'" & $chr(n.intVal) & "'") of nnkCharLit: res.add("'" & $chr(n.intVal) & "'")
of nnkIntLit..nnkInt64Lit: res.add($n.intVal) of nnkIntLit..nnkInt64Lit: res.add($n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal) of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal)