Skip to content

Commit 07d4fd5

Browse files
committed
Add PMunch's example
1 parent 9315f0f commit 07d4fd5

1 file changed

Lines changed: 44 additions & 7 deletions

File tree

blogPosts/macroTutorial.nim

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,58 @@ do{
138138
i += 1;
139139
}while(i < 10);
140140
```
141+
142+
We present below two templates that attempt to replicate this construct in Nim. The first one is not guaranteed to work if the loop contains some `break` or `continue` keyword.
141143
"""
142144

143145
nbCode:
144-
145-
template doWhile(conditional, loop: untyped) =
146+
template doWhileIncorrect(conditional, loop: untyped) =
146147
loop
147148
while conditional:
148149
loop
149150

150-
var i = 10
151-
doWhile i < 10:
151+
var iter1 = 0
152+
doWhileIncorrect iter1 < 0:
153+
echo "Hello World"
154+
iter1.inc
155+
156+
nbCode:
157+
template doWhile(conditional, loop: untyped) =
158+
var c = true
159+
while c:
160+
defer: c = conditional
161+
loop
162+
163+
var iter2 = 0
164+
doWhile iter2 < 0:
152165
echo "Hello World"
153-
i.inc
166+
iter2.inc
167+
168+
nbText:"""
169+
The following code only works with the second template version.
170+
"""
171+
nbCodeSkip:
172+
for msg in ["Hello even world", "Goodbye even world"]:
173+
var i = 1
174+
doWhile i < 10:
175+
if i mod 2 != 0:
176+
inc i
177+
continue
178+
echo i, ": ", msg
179+
i += 2
180+
181+
nbText:"""
182+
With this code we attempt to print messages with only even numbers. The `if` block ensures
183+
that we do not enter the main part of the `while` loop body
184+
without an even value in the variable `i`.
185+
With `continue`, we go back to the beginning of the loop to ensure that our variable is not greater than 10. With the first template version, the `loop` is first executed outside of the `while` loop and `continue` skip to the next iteration of the `for` loop instead, effectively skipping all `echo` statements.
186+
187+
With the keyword `defer` all the statements are kept inside the `while` loop.
188+
"""
189+
154190

155191
nbText:hlMd"""
156-
Notice though that _syntaxically_ the resulting source code is fairly different than the C++ code.
192+
Notice also that _syntaxically_ the resulting source code is fairly different than the C/C++ code.
157193
158194
In the C source code, appear in this order:
159195
1. the `do` keyword
@@ -698,7 +734,8 @@ There are plentiful of posts in the forum that are good references:
698734
4. [Fast array assignment](https://forum.nim-lang.org/t/10037)
699735
5. [Variable injection](https://forum.nim-lang.org/t/10513)
700736
6. [Proc inspection](https://forum.nim-lang.org/t/9127)
701-
7. etc … Please use the forum search bar with specific keywords like `macro`, `metaprogramming`, `generics`, `template`, …
737+
7. [doWhile improvement](https://forum.nim-lang.org/t/13511#82034)
738+
8. etc … Please use the forum search bar with specific keywords like `macro`, `metaprogramming`, `generics`, `template`, …
702739
703740
Last but no least, there are three Nim books:
704741
1. [Nim In Action, ed. Manning](https://book.picheta.me) and [github repo](https://github.com/dom96/nim-in-action-code)

0 commit comments

Comments
 (0)