summary refs log tree commit diff stats
path: root/doc/manual.rst
diff options
context:
space:
mode:
authorArne Döring <arne.doering@gmx.net>2018-04-21 08:15:41 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-21 08:15:41 +0200
commit516ce7306660a183b3e81d6c2622a84b9396f3e1 (patch)
treef434283fa2825c1a2d7e20de81aa9620872b529d /doc/manual.rst
parent33b69f0ed0272a4792322d9a0fbaffd5bef2f6e9 (diff)
downloadNim-516ce7306660a183b3e81d6c2622a84b9396f3e1.tar.gz
macro manual improvements (#7666)
* macro manual improvements

* fixes a typo

* Small fixes
Diffstat (limited to 'doc/manual.rst')
-rw-r--r--doc/manual.rst32
1 files changed, 19 insertions, 13 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index b114f0a8a..9f9206e5c 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -5241,15 +5241,21 @@ chance to convert it into a sequence.
 Macros
 ======
 
-A macro is a special kind of low level template. Macros can be used
-to implement `domain specific languages`:idx:.
+A macro is a special function that is executed at compile-time.
+Normally the input for a macro is an abstract syntax
+tree (AST) of the code that is passed to it. The macro can then do
+transformations on it and return the transformed AST. The
+transformed AST is then passed to the compiler as if the macro
+invocation would have been replaced by its result in the source
+code. This can be used to implement `domain specific
+languages`:idx:.
 
 While macros enable advanced compile-time code transformations, they
 cannot change Nim's syntax. However, this is no real restriction because
 Nim's syntax is flexible enough anyway.
 
 To write macros, one needs to know how the Nim concrete syntax is converted
-to an abstract syntax tree.
+to an AST.
 
 There are two ways to invoke a macro:
 (1) invoking a macro like a procedure call (`expression macros`)
@@ -5269,19 +5275,21 @@ variable number of arguments:
   # ``macros`` module:
   import macros
 
-  macro debug(n: varargs[untyped]): untyped =
-    # `n` is a Nim AST that contains the whole macro invocation
-    # this macro returns a list of statements:
-    result = newNimNode(nnkStmtList, n)
+  macro debug(args: varargs[untyped]): untyped =
+    # `args` is a collection of `NimNode` values that each contain the
+    # AST for an argument of the macro. A macro always has to
+    # return a `NimNode`. A node of kind `nnkStmtList` is suitable for
+    # this use case.
+    result = nnkStmtList.newTree()
     # iterate over any argument that is passed to this macro:
-    for i in 0..n.len-1:
+    for n in args:
       # add a call to the statement list that writes the expression;
       # `toStrLit` converts an AST to its string representation:
-      add(result, newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
+      result.add newCall("write", newIdentNode("stdout"), newLit(n.repr))
       # add a call to the statement list that writes ": "
-      add(result, newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
+      result.add newCall("write", newIdentNode("stdout"), newLit(": "))
       # add a call to the statement list that writes the expressions value:
-      add(result, newCall("writeLine", newIdentNode("stdout"), n[i]))
+      result.add newCall("writeLine", newIdentNode("stdout"), n)
 
   var
     a: array[0..10, int]
@@ -8187,5 +8195,3 @@ validation errors:
 
 If the taint mode is turned off, ``TaintedString`` is simply an alias for
 ``string``.
-
-
re>6e1eeeeb ^
a56d969e ^
6e1eeeeb ^


4c131b38 ^
c2d60fbe ^

2844327a ^





a5fa9785 ^
2844327a ^
4c131b38 ^
2844327a ^

2844327a ^


a5fa9785 ^
2844327a ^
4c131b38 ^
2844327a ^








6e1eeeeb ^






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112