Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tcollect test for the new for-loops as expressions feature
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
discard """ | ||
output: '''@[2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4] | ||
@[0, 1, 2, 3]''' | ||
""" | ||
|
||
const data = [1,2,3,4,5,6] | ||
|
||
import macros | ||
|
||
macro collect(body): untyped = | ||
# analyse the body, find the deepest expression 'it' and replace it via | ||
# 'result.add it' | ||
let res = genSym(nskVar, "collectResult") | ||
|
||
when false: | ||
proc detectForLoopVar(n: NimNode): NimNode = | ||
if n.kind == nnkForStmt: | ||
result = n[0] | ||
else: | ||
for x in n: | ||
result = detectForLoopVar(x) | ||
if result != nil: return result | ||
return nil | ||
|
||
proc t(n, res: NimNode): NimNode = | ||
case n.kind | ||
of nnkStmtList, nnkStmtListExpr, nnkBlockStmt, nnkBlockExpr, | ||
nnkWhileStmt, | ||
nnkForStmt, nnkIfExpr, nnkIfStmt, nnkTryStmt, nnkCaseStmt, | ||
nnkElifBranch, nnkElse, nnkElifExpr: | ||
result = copyNimTree(n) | ||
if n.len >= 1: | ||
result[^1] = t(n[^1], res) | ||
else: | ||
if true: #n == it: | ||
template adder(res, it) = | ||
res.add it | ||
result = getAst adder(res, n) | ||
else: | ||
result = n | ||
|
||
when false: | ||
let it = detectForLoopVar(body) | ||
if it == nil: error("no for loop in body", body) | ||
|
||
let v = newTree(nnkVarSection, | ||
newTree(nnkIdentDefs, res, newTree(nnkBracketExpr, bindSym"seq", | ||
newCall(bindSym"type", body)), newEmptyNode())) | ||
|
||
result = newTree(nnkStmtListExpr, v, t(body, res), res) | ||
#echo repr result | ||
|
||
let stuff = collect: | ||
var i = -1 | ||
while i < 4: | ||
inc i | ||
for it in data: | ||
if it < 5 and it > 1: | ||
it | ||
|
||
echo stuff | ||
|
||
echo collect(for i in 0..3: i) |