tmacros1.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. discard """
  2. output: '''Got: 'nnkCall' hi
  3. {a}
  4. {b}
  5. {a, b}'''
  6. """
  7. import macros
  8. macro outterMacro*(n, blck: untyped): untyped =
  9. let n = callsite()
  10. var j : string = "hi"
  11. proc innerProc(i: int): string =
  12. echo "Using arg ! " & n.repr
  13. result = "Got: '" & $n.kind & "' " & $j
  14. var callNode = n[0]
  15. expectKind(n, NimNodeKind.nnkCall)
  16. if n.len != 3 or n[1].kind != NimNodeKind.nnkIdent:
  17. error("Macro " & callNode.repr &
  18. " requires the ident passed as parameter (eg: " & callNode.repr &
  19. "(the_name_you_want)): statements.")
  20. result = newNimNode(NimNodeKind.nnkStmtList)
  21. var ass : NimNode = newNimNode(nnkAsgn)
  22. ass.add(newIdentNode(n[1].ident))
  23. ass.add(newStrLitNode(innerProc(4)))
  24. result.add(ass)
  25. var str: string
  26. outterMacro(str):
  27. "hellow"
  28. echo str
  29. type E = enum a b
  30. macro enumerators1(): set[E] = newLit({a})
  31. macro enumerators2(): set[E] =
  32. return newLit({b})
  33. macro enumerators3(): set[E] =
  34. result = newLit({E.low .. E.high})
  35. var myEnums: set[E]
  36. myEnums = enumerators1()
  37. echo myEnums
  38. myEnums = enumerators2()
  39. echo myEnums
  40. myEnums = enumerators3()
  41. echo myEnums
  42. #10751
  43. type Tuple = tuple
  44. a: string
  45. b: int
  46. macro foo(t: static Tuple): untyped =
  47. doAssert t.a == "foo"
  48. doAssert t.b == 12345
  49. foo((a: "foo", b: 12345))