summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-08-28 22:15:29 +0200
committerAraq <rumpf_a@web.de>2012-08-28 22:15:29 +0200
commitb4844a189d88655394080f765076a7d85fe6185d (patch)
tree466be4d35d50e7928cbef25a416e2c97f50c2490 /doc
parent6bcdb9c8f429b48e534c50d3dadf7b31783aa0bf (diff)
downloadNim-b4844a189d88655394080f765076a7d85fe6185d.tar.gz
parameter passing works the same for macros and templates; use callsite magic to access the invokation AST
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/manual.txt15
-rwxr-xr-xdoc/tut2.txt12
2 files changed, 15 insertions, 12 deletions
diff --git a/doc/manual.txt b/doc/manual.txt
index e10c934f1..df64649ed 100755
--- a/doc/manual.txt
+++ b/doc/manual.txt
@@ -3102,7 +3102,7 @@ a template. ``inject`` and ``gensym`` have no effect in ``dirty`` templates.
 Macros

 ------

 

-A `macro`:idx: is a special kind of low level template. They can be used

+A `macro`:idx: is a special kind of low level template. Macros can be used

 to implement `domain specific languages`:idx:. Like templates, macros come in
 the 2 flavors *immediate* and *ordinary*.

 

@@ -3129,12 +3129,12 @@ variable number of arguments:
   # ``macros`` module:

   import macros

 

-  macro debug(n: expr): stmt =

+  macro debug(n: varargs[expr]): stmt =

     # `n` is a Nimrod AST that contains the whole macro invocation

     # this macro returns a list of statements:

     result = newNimNode(nnkStmtList, n)

     # iterate over any argument that is passed to this macro:

-    for i in 1..n.len-1:

+    for i in 0..n.len-1:

       # 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])))

@@ -3167,6 +3167,11 @@ The macro call expands to:
   writeln(stdout, x)

 
 
+Arguments that are passed to a ``varargs`` parameter are wrapped in an array
+constructor expression. This is why ``debug`` iterates over all of ``n``'s
+children.
+
+
 BindSym
 ~~~~~~~

 
@@ -3179,9 +3184,9 @@ builtin can be used for that:
 .. code-block:: nimrod

   import macros

 

-  macro debug(n: expr): stmt =

+  macro debug(n: varargs[expr]): stmt =

     result = newNimNode(nnkStmtList, n)

-    for i in 1..n.len-1:

+    for i in 0..n.len-1:

       # we can bind symbols in scope via 'bindSym':

       add(result, newCall(bindSym"write", bindSym"stdout", toStrLit(n[i])))

       add(result, newCall(bindSym"write", bindSym"stdout", newStrLitNode(": ")))

diff --git a/doc/tut2.txt b/doc/tut2.txt
index 96507bcb1..85a68c983 100755
--- a/doc/tut2.txt
+++ b/doc/tut2.txt
@@ -67,7 +67,7 @@ Objects have access to their type at runtime. There is an
     person: TPerson
   assert(student of TStudent) # is true
 
-Object fields that should be visible from outside the defining module, have to
+Object fields that should be visible from outside the defining module have to
 be marked by ``*``. In contrast to tuples, different object types are
 never *equivalent*. New object types can only be defined within a type
 section.
@@ -631,19 +631,19 @@ Expression Macros
 -----------------
 
 The following example implements a powerful ``debug`` command that accepts a
-variable number of arguments (this cannot be done with templates):
+variable number of arguments:
 
 .. code-block:: nimrod
   # to work with Nimrod syntax trees, we need an API that is defined in the
   # ``macros`` module:
   import macros
 
-  macro debug(n: expr): stmt =
-    # `n` is a Nimrod AST that contains the whole macro expression
+  macro debug(n: varargs[expr]): stmt =
+    # `n` is a Nimrod AST that contains a list of expressions;
     # this macro returns a list of statements:
     result = newNimNode(nnkStmtList, n)
     # iterate over any argument that is passed to this macro:
-    for i in 1..n.len-1:
+    for i in 0..n.len-1:
       # add a call to the statement list that writes the expression;
       # `toStrLit` converts an AST to its string representation:
       result.add(newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
@@ -702,5 +702,3 @@ regular expressions:
     return tkOperator
   else:
     return tkUnknown
-
-
+0100 version 0.7.4' href='/ahoang/Nim/commit/nim/wordrecg.pas?h=devel&id=439aa2d04d5528b5aed288f70895515d1da2dc3d'>439aa2d04 ^
405b86068



4d4b3b1c0 ^
8b2a9401a ^
405b86068
66a7e3d37 ^

405b86068
e792940f5 ^
07d5a8085 ^
439aa2d04 ^
66a7e3d37 ^
405b86068







07d5a8085 ^
405b86068









66a7e3d37 ^
405b86068



66a7e3d37 ^



300430fbb ^
4d4b3b1c0 ^








405b86068






8b2a9401a ^
07d5a8085 ^
300430fbb ^
053309e60 ^
405b86068
053309e60 ^
e792940f5 ^
405b86068
8b2a9401a ^
405b86068



439aa2d04 ^
405b86068



4d4b3b1c0 ^
8b2a9401a ^
405b86068
66a7e3d37 ^

405b86068
e792940f5 ^
07d5a8085 ^
66a7e3d37 ^
405b86068








07d5a8085 ^
405b86068















































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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220