tmacro2.nim 735 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. output: "ta-da Your value sir: 'HE!!!!o Wor!!d'"
  3. """
  4. import macros, strutils
  5. proc testBlock(): string {.compileTime.} =
  6. block myBlock:
  7. while true:
  8. echo "inner block"
  9. break myBlock
  10. echo "outer block"
  11. result = "ta-da"
  12. macro mac(n: typed): string =
  13. let n = callsite()
  14. expectKind(n, nnkCall)
  15. expectLen(n, 2)
  16. expectKind(n[1], nnkStrLit)
  17. var s: string = n[1].strVal
  18. s = s.replace("l", "!!")
  19. result = newStrLitNode("Your value sir: '$#'" % [s])
  20. const s = testBlock()
  21. const t = mac("HEllo World")
  22. echo s, " ", t
  23. #-----------------------------------------------------------------------------
  24. # issue #15326
  25. macro m(n:typed):auto =
  26. result = n
  27. proc f[T](x:T): T {.m.} = x
  28. discard f(3)