diff options
author | hlaaftana <10591326+hlaaftana@users.noreply.github.com> | 2021-02-08 21:35:06 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-08 19:35:06 +0100 |
commit | 6a7baff97dd0800712b60b7b685ff23f66b9d127 (patch) | |
tree | 127dabe830ba5498471e90f3bdc241960e0122f8 /tests/macros/tcasestmtmacro.nim | |
parent | abac35e7437dd1ac2b3687dfa51de7f9d4b6e853 (diff) | |
download | Nim-6a7baff97dd0800712b60b7b685ff23f66b9d127.tar.gz |
rename case statement macro from match to `case` (#16923)
* rename case statement macro from match to `case` * fix test
Diffstat (limited to 'tests/macros/tcasestmtmacro.nim')
-rw-r--r-- | tests/macros/tcasestmtmacro.nim | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/macros/tcasestmtmacro.nim b/tests/macros/tcasestmtmacro.nim new file mode 100644 index 000000000..26519f637 --- /dev/null +++ b/tests/macros/tcasestmtmacro.nim @@ -0,0 +1,35 @@ +discard """ + output: ''' +yes +''' +""" + +{.experimental: "caseStmtMacros".} + +import macros + +macro `case`(n: tuple): untyped = + result = newTree(nnkIfStmt) + let selector = n[0] + for i in 1 ..< n.len: + let it = n[i] + case it.kind + of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr: + result.add it + of nnkOfBranch: + for j in 0..it.len-2: + let cond = newCall("==", selector, it[j]) + result.add newTree(nnkElifBranch, cond, it[^1]) + else: + error "custom 'case' for tuple cannot handle this node", it + +var correct = false + +case ("foo", 78) +of ("foo", 78): + correct = true + echo "yes" +of ("bar", 88): echo "no" +else: discard + +doAssert correct |