From 08c105c4e58718c4e7f9bca64f75604deb451ca3 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 27 Aug 2018 11:26:31 +0200 Subject: [PATCH] added missing file to make tests green --- tests/pragmas/mpushexperimental.nim | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/pragmas/mpushexperimental.nim diff --git a/tests/pragmas/mpushexperimental.nim b/tests/pragmas/mpushexperimental.nim new file mode 100644 index 0000000000..569861c1d9 --- /dev/null +++ b/tests/pragmas/mpushexperimental.nim @@ -0,0 +1,30 @@ + +import macros + +macro enumerate(x: ForLoopStmt): untyped = + expectKind x, nnkForStmt + # we strip off the first for loop variable and use + # it as an integer counter: + result = newStmtList() + result.add newVarStmt(x[0], newLit(0)) + var body = x[^1] + if body.kind != nnkStmtList: + body = newTree(nnkStmtList, body) + body.add newCall(bindSym"inc", x[0]) + var newFor = newTree(nnkForStmt) + for i in 1..x.len-3: + newFor.add x[i] + # transform enumerate(X) to 'X' + newFor.add x[^2][1] + newFor.add body + result.add newFor + +proc main*[T](x: T) = + {.push experimental: "forLoopMacros".} + + for a, b in enumerate(items([1, 2, 3])): + echo a, " ", b + + for a2, b2 in enumerate([1, 2, 3, 5]): + echo a2, " ", b2 + {.pop.}