tmacros1.nim 793 B

123456789101112131415161718192021222324252627282930
  1. discard """
  2. output: "Got: 'nnkCall' hi"
  3. """
  4. import
  5. macros, strutils
  6. macro outterMacro*(n, blck: untyped): untyped =
  7. let n = callsite()
  8. var j : string = "hi"
  9. proc innerProc(i: int): string =
  10. echo "Using arg ! " & n.repr
  11. result = "Got: '" & $n.kind & "' " & $j
  12. var callNode = n[0]
  13. expectKind(n, TNimrodNodeKind.nnkCall)
  14. if n.len != 3 or n[1].kind != TNimrodNodeKind.nnkIdent:
  15. error("Macro " & callNode.repr &
  16. " requires the ident passed as parameter (eg: " & callNode.repr &
  17. "(the_name_you_want)): statements.")
  18. result = newNimNode(TNimrodNodeKind.nnkStmtList)
  19. var ass : NimNode = newNimNode(nnkAsgn)
  20. ass.add(newIdentNode(n[1].ident))
  21. ass.add(newStrLitNode(innerProc(4)))
  22. result.add(ass)
  23. var str: string
  24. outterMacro(str):
  25. "hellow"
  26. echo str