29 lines
820 B
Nim
29 lines
820 B
Nim
import std/macros
|
|
|
|
proc replaceNodes(ast: NimNode, what: NimNode, by: NimNode): NimNode =
|
|
# Replace "what" ident node by "by"
|
|
proc inspect(node: NimNode): NimNode =
|
|
case node.kind:
|
|
of {nnkIdent, nnkSym}:
|
|
if node.eqIdent(what):
|
|
return by
|
|
return node
|
|
of nnkEmpty:
|
|
return node
|
|
of nnkLiterals:
|
|
return node
|
|
else:
|
|
var rTree = node.kind.newTree()
|
|
for child in node:
|
|
rTree.add inspect(child)
|
|
return rTree
|
|
result = inspect(ast)
|
|
|
|
macro staticFor*(idx: untyped{nkIdent}, start, stopEx: static int, body: untyped): untyped =
|
|
## staticFor [min inclusive, max exclusive)
|
|
result = newStmtList()
|
|
for i in start ..< stopEx:
|
|
result.add nnkBlockStmt.newTree(
|
|
ident("unrolledIter_" & $idx & $i),
|
|
body.replaceNodes(idx, newLit i)
|
|
) |