cleaned up macros.nim

This commit is contained in:
Andreas Rumpf 2017-11-19 13:15:03 +01:00
commit 5ccf69ffa3
3 changed files with 14 additions and 19 deletions

View file

@ -80,7 +80,6 @@ This now needs to be written as:
trace in a structured manner without string parsing. trace in a structured manner without string parsing.
- Added ``sequtils.mapLiterals`` for easier construction of array and tuple - Added ``sequtils.mapLiterals`` for easier construction of array and tuple
literals. literals.
- Added ``macros.isAtomicLit`` predicate.
- Added ``parseutils.parseSaturatedNatural``. - Added ``parseutils.parseSaturatedNatural``.
- Moved from stdlib into Nimble packages: - Moved from stdlib into Nimble packages:
- [``basic2d``](https://github.com/nim-lang/basic2d) - [``basic2d``](https://github.com/nim-lang/basic2d)

View file

@ -296,7 +296,7 @@ proc newIdentNode*(i: NimIdent): NimNode {.compileTime.} =
proc newIdentNode*(i: string): NimNode {.compileTime.} = proc newIdentNode*(i: string): NimNode {.compileTime.} =
## creates an identifier node from `i` ## creates an identifier node from `i`
result = newNimNode(nnkIdent) result = newNimNode(nnkIdent)
result.ident = !i result.ident = toNimIdent i
type type
@ -586,11 +586,6 @@ proc newLit*(s: string): NimNode {.compileTime.} =
result = newNimNode(nnkStrLit) result = newNimNode(nnkStrLit)
result.strVal = s result.strVal = s
proc isAtomicLit*(n: NimNode): bool =
## returns true if ``n`` is some kind literal like ``0.3`` (a ``float``
## literal) or ``"abc"`` (a ``string`` literal).
result = n.kind in {nnkCharLit..nnkNilLit}
proc nestList*(theProc: NimIdent, proc nestList*(theProc: NimIdent,
x: NimNode): NimNode {.compileTime.} = x: NimNode): NimNode {.compileTime.} =
## nests the list `x` into a tree of call expressions: ## nests the list `x` into a tree of call expressions:
@ -618,7 +613,7 @@ proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
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: res.add(" " & $n.strVal) of nnkStrLit..nnkTripleStrLit: res.add(" " & $n.strVal)
of nnkIdent: res.add(" !\"" & $n.ident & '"') of nnkIdent: res.add(" ident\"" & $n.ident & '"')
of nnkSym: res.add(" \"" & $n.symbol & '"') of nnkSym: res.add(" \"" & $n.symbol & '"')
of nnkNone: assert false of nnkNone: assert false
else: else:
@ -643,7 +638,7 @@ proc lispRepr*(n: NimNode): string {.compileTime, benign.} =
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: add(result, $n.strVal) of nnkStrLit..nnkTripleStrLit: add(result, $n.strVal)
of nnkIdent: add(result, "!\"" & $n.ident & '"') of nnkIdent: add(result, "ident\"" & $n.ident & '"')
of nnkSym: add(result, $n.symbol) of nnkSym: add(result, $n.symbol)
of nnkNone: assert false of nnkNone: assert false
else: else:
@ -667,7 +662,7 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
## .. code-block:: nim ## .. code-block:: nim
## nnkStmtList.newTree( ## nnkStmtList.newTree(
## nnkCommand.newTree( ## nnkCommand.newTree(
## newIdentNode(!"echo"), ## newIdentNode("echo"),
## newLit("Hello world") ## newLit("Hello world")
## ) ## )
## ) ## )
@ -721,7 +716,7 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
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)
of nnkStrLit..nnkTripleStrLit: res.add($n.strVal.escape()) of nnkStrLit..nnkTripleStrLit: res.add($n.strVal.escape())
of nnkIdent: res.add("!" & ($n.ident).escape()) of nnkIdent: res.add(($n.ident).escape())
of nnkSym: res.add(($n.symbol).escape()) of nnkSym: res.add(($n.symbol).escape())
of nnkNone: assert false of nnkNone: assert false
else: else:
@ -934,7 +929,7 @@ proc newEnum*(name: NimNode, fields: openArray[NimNode],
if pure: if pure:
let pragmaNode = newNimNode(nnkPragmaExpr).add( let pragmaNode = newNimNode(nnkPragmaExpr).add(
typeDefArgs[0], typeDefArgs[0],
add(newNimNode(nnkPragma), newIdentNode(!"pure"))) add(newNimNode(nnkPragma), newIdentNode("pure")))
typeDefArgs[0] = pragmaNode typeDefArgs[0] = pragmaNode
@ -1060,7 +1055,7 @@ template findChild*(n: NimNode; cond: untyped): NimNode {.dirty.} =
## ##
## .. code-block:: nim ## .. code-block:: nim
## var res = findChild(n, it.kind == nnkPostfix and ## var res = findChild(n, it.kind == nnkPostfix and
## it.basename.ident == !"foo") ## it.basename.ident == toNimIdent"foo")
block: block:
var res: NimNode var res: NimNode
for it in n.children: for it in n.children:
@ -1094,7 +1089,7 @@ proc basename*(a: NimNode): NimNode =
proc `basename=`*(a: NimNode; val: string) {.compileTime.}= proc `basename=`*(a: NimNode; val: string) {.compileTime.}=
case a.kind case a.kind
of nnkIdent: macros.`ident=`(a, !val) of nnkIdent: macros.`ident=`(a, toNimIdent val)
of nnkPostfix, nnkPrefix: a[1] = ident(val) of nnkPostfix, nnkPrefix: a[1] = ident(val)
else: else:
quit "Do not know how to get basename of (" & treeRepr(a) & ")\n" & repr(a) quit "Do not know how to get basename of (" & treeRepr(a) & ")\n" & repr(a)
@ -1155,7 +1150,7 @@ proc eqIdent*(node: NimNode; s: string): bool {.compileTime.} =
## other ways like ``node.ident`` are much more error-prone, unfortunately. ## other ways like ``node.ident`` are much more error-prone, unfortunately.
case node.kind case node.kind
of nnkIdent: of nnkIdent:
result = node.ident == !s result = node.ident == toNimIdent s
of nnkSym: of nnkSym:
result = eqIdent($node.symbol, s) result = eqIdent($node.symbol, s)
of nnkOpenSymChoice, nnkClosedSymChoice: of nnkOpenSymChoice, nnkClosedSymChoice:

View file

@ -706,16 +706,17 @@ template newSeqWith*(len: int, init: untyped): untyped =
result[i] = init result[i] = init
result result
proc mapLitsImpl(constructor: NimNode; op: NimNode; nested: bool): NimNode = proc mapLitsImpl(constructor: NimNode; op: NimNode; nested: bool;
if isAtomicLit(constructor): filter = nnkLiterals): NimNode =
if constructor.kind in filter:
result = newNimNode(nnkCall, lineInfoFrom=constructor) result = newNimNode(nnkCall, lineInfoFrom=constructor)
result.add op result.add op
result.add constructor result.add constructor
else: else:
result = newNimNode(constructor.kind, lineInfoFrom=constructor) result = newNimNode(constructor.kind, lineInfoFrom=constructor)
for v in constructor: for v in constructor:
if nested or isAtomicLit(v): if nested or v.kind in filter:
result.add mapLitsImpl(v, op, nested) result.add mapLitsImpl(v, op, nested, filter)
else: else:
result.add v result.add v