tut3.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. =======================
  2. Nim Tutorial (Part III)
  3. =======================
  4. :Author: Arne Döring
  5. :Version: |nimversion|
  6. .. contents::
  7. Introduction
  8. ============
  9. "With Great Power Comes Great Responsibility." -- Spider Man's Uncle
  10. This document is a tutorial about Nim's macro system.
  11. A macro is a function that is executed at compile time and transforms
  12. a Nim syntax tree into a different tree.
  13. Examples of things that can be implemented in macros:
  14. * An assert macro that prints both sides of a comparison operator, if
  15. the assertion fails. ``myAssert(a == b)`` is converted to
  16. ``if a != b: quit($a " != " $b)``
  17. * A debug macro that prints the value and the name of the symbol.
  18. ``myDebugEcho(a)`` is converted to ``echo "a: ", a``
  19. * Symbolic differentiation of an expression.
  20. ``diff(a*pow(x,3) + b*pow(x,2) + c*x + d, x)`` is converted to
  21. ``3*a*pow(x,2) + 2*a*x + c``
  22. Macro Arguments
  23. ---------------
  24. The types of macro arguments have two faces. One face is used for
  25. the overload resolution, and the other face is used within the macro
  26. body. For example, if ``macro foo(arg: int)`` is called in an
  27. expression ``foo(x)``, ``x`` has to be of a type compatible to int, but
  28. *within* the macro's body ``arg`` has the type ``NimNode``, not ``int``!
  29. Why it is done this way will become obvious later, when we have seen
  30. concrete examples.
  31. There are two ways to pass arguments to a macro, an argument can be
  32. either ``typed`` or ``untyped``.
  33. Untyped Arguments
  34. -----------------
  35. Untyped macro arguments are passed to the macro before they are
  36. semantically checked. This means the syntax tree that is passed down
  37. to the macro does not need to make sense for Nim yet, the only
  38. limitation is that it needs to be parseable. Usually the macro does
  39. not check the argument either but uses it in the transformation's
  40. result somehow. The result of a macro expansion is always checked
  41. by the compiler, so apart from weird error messages nothing bad
  42. can happen.
  43. The downside for an ``untyped`` argument is that these do not play
  44. well with Nim's overloading resolution.
  45. The upside for untyped arguments is that the syntax tree is
  46. quite predictable and less complex compared to its ``typed``
  47. counterpart.
  48. Typed Arguments
  49. ---------------
  50. For typed arguments, the semantic checker runs on the argument and
  51. does transformations on it, before it is passed to the macro. Here
  52. identifier nodes are resolved as symbols, implicit type
  53. conversions are visible in the tree as calls, templates are
  54. expanded and probably most importantly, nodes have type information.
  55. Typed arguments can have the type ``typed`` in the arguments list.
  56. But all other types, such as ``int``, ``float`` or ``MyObjectType``
  57. are typed arguments as well, and they are passed to the macro as a
  58. syntax tree.
  59. Static Arguments
  60. ----------------
  61. Static arguments are a way to pass values as values and not as syntax
  62. tree nodes to a macro. For example for ``macro foo(arg: static[int])``
  63. in the expression ``foo(x)``, ``x`` needs to be an integer constant,
  64. but in the macro body ``arg`` is just like a normal parameter of type
  65. ``int``.
  66. .. code-block:: nim
  67. import macros
  68. macro myMacro(arg: static[int]): untyped =
  69. echo arg # just an int (7), not ``NimNode``
  70. myMacro(1 + 2 * 3)
  71. Code blocks as arguments
  72. ------------------------
  73. It is possible to pass the last argument of a call expression in a
  74. separate code block with indentation. For example the following code
  75. example is a valid (but not a recommended) way to call ``echo``:
  76. .. code-block:: nim
  77. echo "Hello ":
  78. let a = "Wor"
  79. let b = "ld!"
  80. a & b
  81. For macros this way of calling is very useful; syntax trees of arbitrary
  82. complexity can be passed to macros with this notation.
  83. The Syntax Tree
  84. ---------------
  85. In order to build a Nim syntax tree one needs to know how Nim source
  86. code is represented as a syntax tree, and how such a tree needs to
  87. look like so that the Nim compiler will understand it. The nodes of the
  88. Nim syntax tree are documented in the `macros <macros.html>`_ module.
  89. But a more interactive way to explore the Nim
  90. syntax tree is with ``macros.treeRepr``, it converts a syntax tree
  91. into a multi line string for printing on the console. It can be used
  92. to explore how the argument expressions are represented in tree form
  93. and for debug printing of generated syntax tree. ``dumpTree`` is a
  94. predefined macro that just prints its argument in tree representation,
  95. but does nothing else. Here is an example of such a tree representation:
  96. .. code-block:: nim
  97. dumpTree:
  98. var mt: MyType = MyType(a:123.456, b:"abcdef")
  99. # output:
  100. # StmtList
  101. # VarSection
  102. # IdentDefs
  103. # Ident "mt"
  104. # Ident "MyType"
  105. # ObjConstr
  106. # Ident "MyType"
  107. # ExprColonExpr
  108. # Ident "a"
  109. # FloatLit 123.456
  110. # ExprColonExpr
  111. # Ident "b"
  112. # StrLit "abcdef"
  113. Custom sematic checking
  114. -----------------------
  115. The first thing that a macro should do with its arguments is to check
  116. if the argument is in the correct form. Not every type of wrong input
  117. needs to be caught here, but anything that could cause a crash during
  118. macro evaluation should be caught and create a nice error message.
  119. ``macros.expectKind`` and ``macros.expectLen`` are a good start. If
  120. the checks need to be more complex, arbitrary error messages can
  121. be created with the ``macros.error`` proc.
  122. .. code-block:: nim
  123. macro myAssert(arg: untyped): untyped =
  124. arg.expectKind nnkInfix
  125. Generating Code
  126. ---------------
  127. There are two ways to generate the code. Either by creating the syntax
  128. tree with expressions that contain a lot of calls to ``newTree`` and
  129. ``newLit``, or with ``quote do:`` expressions. The first option offers
  130. the best low level control for the syntax tree generation, but the
  131. second option is much less verbose. If you choose to create the syntax
  132. tree with calls to ``newTree`` and ``newLit`` the macro
  133. ``marcos.dumpAstGen`` can help you with the verbosity. ``quote do:``
  134. allows you to write the code that you want to generate literally,
  135. backticks are used to insert code from ``NimNode`` symbols into the
  136. generated expression. This means that you can't use backticks within
  137. ``quote do:`` for anything else than injecting symbols. Make sure to
  138. inject only symbols of type ``NimNode`` into the generated syntax
  139. tree. You can use ``newLit`` to convert arbitrary values into
  140. expressions trees of type ``NimNode`` so that it is safe to inject
  141. them into the tree.
  142. .. code-block:: nim
  143. :test: "nim c $1"
  144. import macros
  145. type
  146. MyType = object
  147. a: float
  148. b: string
  149. macro myMacro(arg: untyped): untyped =
  150. var mt: MyType = MyType(a:123.456, b:"abcdef")
  151. # ...
  152. let mtLit = newLit(mt)
  153. result = quote do:
  154. echo `arg`
  155. echo `mtLit`
  156. myMacro("Hallo")
  157. The call to ``myMacro`` will generate the following code:
  158. .. code-block:: nim
  159. echo "Hallo"
  160. echo MyType(a: 123.456'f64, b: "abcdef")
  161. Building your first macro
  162. -------------------------
  163. To give a footstart to writing macros we will show now how to
  164. implement the ``myDebug`` macro mentioned earlier. The first thing to
  165. do is to build a simple example of the macro usage, and then just
  166. print the argument. This way it is possible to get an idea of a
  167. correct argument should be look like.
  168. .. code-block:: nim
  169. :test: "nim c $1"
  170. import macros
  171. macro myAssert(arg: untyped): untyped =
  172. echo arg.treeRepr
  173. let a = 1
  174. let b = 2
  175. myAssert(a != b)
  176. .. code-block::
  177. Infix
  178. Ident "!="
  179. Ident "a"
  180. Ident "b"
  181. From the output it is possible to see that the information that the
  182. argument is an infix operator (node kind is "Infix"), as well as that the two
  183. operands are at index 1 and 2. With this information the actual
  184. macro can be written.
  185. .. code-block:: nim
  186. :test: "nim c $1"
  187. import macros
  188. macro myAssert(arg: untyped): untyped =
  189. # all node kind identifiers are prefixed with "nnk"
  190. arg.expectKind nnkInfix
  191. arg.expectLen 3
  192. # operator as string literal
  193. let op = newLit(" " & arg[0].repr & " ")
  194. let lhs = arg[1]
  195. let rhs = arg[2]
  196. result = quote do:
  197. if not `arg`:
  198. raise newException(AssertionError,$`lhs` & `op` & $`rhs`)
  199. let a = 1
  200. let b = 2
  201. myAssert(a != b)
  202. myAssert(a == b)
  203. This is the code that will be generated. To debug what the macro
  204. actually generated, the statement ``echo result.repr`` can be used, in
  205. the last line of the macro. It is also the statement that has been
  206. used to get this output.
  207. .. code-block:: nim
  208. if not (a != b):
  209. raise newException(AssertionError, $a & " != " & $b)
  210. With Power Comes Responsibility
  211. -------------------------------
  212. Macros are very powerful. A good advice is to use them as little as
  213. possible, but as much as necessary. Macros can change the semantics of
  214. expressions, making the code incomprehensible for anybody who does not
  215. know exactly what the macro does with it. So whenever a macro is not
  216. necessary and the same logic can be implemented using templates or
  217. generics, it is probably better not to use a macro. And when a macro
  218. is used for something, the macro should better have a well written
  219. documentation. For all the people who claim to write only perfectly
  220. self-explanatory code: when it comes to macros, the implementation is
  221. not enough for documentation.
  222. Limitations
  223. -----------
  224. Since macros are evaluated in the compiler in the NimVM, macros share
  225. all the limitations of the NimVM. They have to be implemented in pure Nim
  226. code. Macros can start external processes on the shell, but they
  227. cannot call C functions except from those that are built in the
  228. compiler.
  229. More Examples
  230. =============
  231. This tutorial can only cover the basics of the macro system. There are
  232. macros out there that could be an inspiration for you of what is
  233. possible with it.
  234. Strformat
  235. ---------
  236. In the Nim standard library, the ``strformat`` library provides a
  237. macro that parses a string literal at compile time. Parsing a string
  238. in a macro like here is generally not recommended. The parsed AST
  239. cannot have type information, and parsing implemented on the VM is
  240. generally not very fast. Working on AST nodes is almost always the
  241. recommended way. But still ``strformat`` is a good example for a
  242. practical use case for a macro that is slightly more complex that the
  243. ``assert`` macro.
  244. `Strformat <https://github.com/nim-lang/Nim/blob/5845716df8c96157a047c2bd6bcdd795a7a2b9b1/lib/pure/strformat.nim#L280>`_
  245. Ast Pattern Matching
  246. --------------------
  247. Ast Pattern Matching is a macro library to aid in writing complex
  248. macros. This can be seen as a good example of how to repurpose the
  249. Nim syntax tree with new semantics.
  250. `Ast Pattern Matching <https://github.com/krux02/ast-pattern-matching>`_
  251. OpenGL Sandbox
  252. --------------
  253. This project has a working Nim to GLSL compiler written entirely in
  254. macros. It scans recursively though all used function symbols to
  255. compile them so that cross library functions can be executed on the GPU.
  256. `OpenGL Sandbox <https://github.com/krux02/opengl-sandbox>`_