Created
August 15, 2018 18:43
-
-
Save Araq/169d1e24b2b996d024a780ef6a4e6c09 to your computer and use it in GitHub Desktop.
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
{.experimental: "caseStmtMacros".} | |
import macros | |
macro match(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 "'match' cannot handle this node", it | |
echo repr result | |
case ("foo", 78) | |
of ("foo", 78): echo "yes" | |
of ("bar", 88): echo "no" | |
else: discard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
++++1