tmemit.nim 695 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. discard """
  2. output: '''
  3. HELLO WORLD
  4. c_func
  5. 12
  6. '''
  7. """
  8. import macros, strutils
  9. emit("echo " & '"' & "hello world".toUpperAscii & '"')
  10. # bug #1025
  11. macro foo(icname): untyped =
  12. let ic = newStrLitNode($icname)
  13. result = quote do:
  14. proc x* =
  15. proc private {.exportc: `ic`.} = discard
  16. echo `ic`
  17. private()
  18. foo(c_func)
  19. x()
  20. template volatileLoad[T](x: ptr T): T =
  21. var res: T
  22. {.emit: [res, " = (*(", type(x[]), " volatile*)", x, ");"].}
  23. res
  24. template volatileStore[T](x: ptr T; y: T) =
  25. {.emit: ["*((", type(x[]), " volatile*)(", x, ")) = ", y, ";"].}
  26. proc main =
  27. var st: int
  28. var foo: ptr int = addr st
  29. volatileStore(foo, 12)
  30. echo volatileLoad(foo)
  31. main()