rename case statement macro from match to case (#16923)
* rename case statement macro from match to `case` * fix test
This commit is contained in:
parent
abac35e743
commit
6a7baff97d
4 changed files with 51 additions and 16 deletions
35
tests/macros/tcasestmtmacro.nim
Normal file
35
tests/macros/tcasestmtmacro.nim
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue