tmacros1.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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))
  50. # bug #16307
  51. macro bug(x: untyped): string =
  52. newLit repr(x)
  53. let res = bug:
  54. block:
  55. ## one
  56. ## two
  57. ## three
  58. doAssert res == """
  59. block:
  60. ## one
  61. ## two
  62. ## three"""