tnewproc.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import macros
  2. macro test(a: untyped): untyped =
  3. # proc hello*(x: int = 3, y: float32): int {.inline.} = discard
  4. let
  5. nameNode = nnkPostfix.newTree(
  6. newIdentNode("*"),
  7. newIdentNode("hello")
  8. )
  9. params = @[
  10. newIdentNode("int"),
  11. nnkIdentDefs.newTree(
  12. newIdentNode("x"),
  13. newIdentNode("int"),
  14. newLit(3)
  15. ),
  16. nnkIdentDefs.newTree(
  17. newIdentNode("y"),
  18. newIdentNode("float32"),
  19. newEmptyNode()
  20. )
  21. ]
  22. paramsNode = nnkFormalParams.newTree(params)
  23. pragmasNode = nnkPragma.newTree(
  24. newIdentNode("inline")
  25. )
  26. bodyNode = nnkStmtList.newTree(
  27. nnkDiscardStmt.newTree(
  28. newEmptyNode()
  29. )
  30. )
  31. var
  32. expected = nnkProcDef.newTree(
  33. nameNode,
  34. newEmptyNode(),
  35. newEmptyNode(),
  36. paramsNode,
  37. pragmasNode,
  38. newEmptyNode(),
  39. bodyNode
  40. )
  41. doAssert expected == newProc(name=nameNode, params=params,
  42. body = bodyNode, pragmas=pragmasNode)
  43. expected.pragma = newEmptyNode()
  44. doAssert expected == newProc(name=nameNode, params=params,
  45. body = bodyNode)
  46. test:
  47. 42