add progmas to params of macros.newProc (#11025)

Merging
This commit is contained in:
Lolo Iccl 2019-04-27 20:22:02 +09:00 • committed by cooldome
commit 69755542f4
2 changed files with 58 additions and 3 deletions

View file

@ -1076,20 +1076,24 @@ proc expectKind*(n: NimNode; k: set[NimNodeKind]) {.compileTime.} =
## macros that check the AST that is passed to them. ## macros that check the AST that is passed to them.
if n.kind notin k: error("Expected one of " & $k & ", got " & $n.kind, n) if n.kind notin k: error("Expected one of " & $k & ", got " & $n.kind, n)
proc newProc*(name = newEmptyNode(); params: openArray[NimNode] = [newEmptyNode()]; proc newProc*(name = newEmptyNode();
body: NimNode = newStmtList(), procType = nnkProcDef): NimNode {.compileTime.} = params: openArray[NimNode] = [newEmptyNode()];
body: NimNode = newStmtList();
procType = nnkProcDef;
pragmas: NimNode = newEmptyNode()): NimNode {.compileTime.} =
## shortcut for creating a new proc ## shortcut for creating a new proc
## ##
## The ``params`` array must start with the return type of the proc, ## The ``params`` array must start with the return type of the proc,
## followed by a list of IdentDefs which specify the params. ## followed by a list of IdentDefs which specify the params.
if procType notin RoutineNodes: if procType notin RoutineNodes:
error("Expected one of " & $RoutineNodes & ", got " & $procType) error("Expected one of " & $RoutineNodes & ", got " & $procType)
pragmas.expectKind({nnkEmpty, nnkPragma})
result = newNimNode(procType).add( result = newNimNode(procType).add(
name, name,
newEmptyNode(), newEmptyNode(),
newEmptyNode(), newEmptyNode(),
newNimNode(nnkFormalParams).add(params), newNimNode(nnkFormalParams).add(params),
newEmptyNode(), # pragmas pragmas,
newEmptyNode(), newEmptyNode(),
body) body)

51
tests/macros/tnewproc.nim Normal file
View file

@ -0,0 +1,51 @@
import macros
macro test(a: untyped): untyped =
# proc hello*(x: int = 3, y: float32): int {.inline.} = discard
let
nameNode = nnkPostfix.newTree(
newIdentNode("*"),
newIdentNode("hello")
)
params = @[
newIdentNode("int"),
nnkIdentDefs.newTree(
newIdentNode("x"),
newIdentNode("int"),
newLit(3)
),
nnkIdentDefs.newTree(
newIdentNode("y"),
newIdentNode("float32"),
newEmptyNode()
)
]
paramsNode = nnkFormalParams.newTree(params)
pragmasNode = nnkPragma.newTree(
newIdentNode("inline")
)
bodyNode = nnkStmtList.newTree(
nnkDiscardStmt.newTree(
newEmptyNode()
)
)
var
expected = nnkProcDef.newTree(
nameNode,
newEmptyNode(),
newEmptyNode(),
paramsNode,
pragmasNode,
newEmptyNode(),
bodyNode
)
doAssert expected == newProc(name=nameNode, params=params,
body = bodyNode, pragmas=pragmasNode)
expected.pragma = newEmptyNode()
doAssert expected == newProc(name=nameNode, params=params,
body = bodyNode)
test:
42