parent
afad61c220
commit
0e06a72c64
1 changed files with 29 additions and 1 deletions
|
|
@ -397,6 +397,8 @@ Ranges
|
||||||
------
|
------
|
||||||
|
|
||||||
Ranges occur in set constructors, case statement branches, or array slices.
|
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:
|
Concrete syntax:
|
||||||
|
|
||||||
|
|
@ -406,7 +408,33 @@ Concrete syntax:
|
||||||
AST:
|
AST:
|
||||||
|
|
||||||
.. code-block:: nim
|
.. 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
|
If expression
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue