diff options
author | apense <apense@users.noreply.github.com> | 2015-06-26 01:11:27 -0400 |
---|---|---|
committer | apense <apense@users.noreply.github.com> | 2015-06-26 01:11:27 -0400 |
commit | 0e06a72c64e09af1367f9e0adb4b5ff91a3e0251 (patch) | |
tree | 3d75ec7fdf91f355cb05b73a4396cfde5797f215 /doc | |
parent | afad61c220280ef6ab6362dc5cadbab8ab0b20fc (diff) | |
download | Nim-0e06a72c64e09af1367f9e0adb4b5ff91a3e0251.tar.gz |
Updated nnkRange docs
Addresses #2929
Diffstat (limited to 'doc')
-rw-r--r-- | doc/astspec.txt | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/doc/astspec.txt b/doc/astspec.txt index 9bedb00fc..72cd22cc6 100644 --- a/doc/astspec.txt +++ b/doc/astspec.txt @@ -397,6 +397,8 @@ Ranges ------ Ranges occur in set constructors, case statement branches, or array slices. +Internally, the node kind ``nnkRange`` is used, but when constructing the +AST, construction with ``..`` as an infix operator should be used instead. Concrete syntax: @@ -406,7 +408,33 @@ Concrete syntax: AST: .. code-block:: nim - nnkRange(nnkIntLit(1), nnkIntLit(3)) + nnkInfix( + nnkIdent(!".."), + nnkIntLit(1), + nnkIntLit(3) + ) + +Example code: + +.. code-block:: nim + macro genRepeatEcho(): stmt = + result = newNimNode(nnkStmtList) + + var forStmt = newNimNode(nnkForStmt) # generate a for statement + forStmt.add(ident("i")) # use the variable `i` for iteration + + var rangeDef = newNimNode(nnkInfix).add( + ident("..")).add( + newIntLitNode(3),newIntLitNode(5)) # iterate over the range 3..5 + + forStmt.add(rangeDef) + forStmt.add(newCall(ident("echo"), newIntLitNode(3))) # meat of the loop + result.add(forStmt) + + genRepeatEcho() # gives: + # 3 + # 3 + # 3 If expression |