mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-09 01:03:13 +00:00
support tuple binding in nim < 1.6
This commit is contained in:
parent
7a3d3430a4
commit
e7afaeac49
@ -48,13 +48,17 @@ macro bindTuple(name, expression): bool =
|
|||||||
bindFailed(evaluated)
|
bindFailed(evaluated)
|
||||||
placeholder(`T`)
|
placeholder(`T`)
|
||||||
|
|
||||||
|
# build tuple unpacking statement, eg:
|
||||||
|
# let (a, b) = `valueNode`
|
||||||
|
let tplNode = nnkVarTuple.newTree()
|
||||||
|
for i in 0..<name.len:
|
||||||
|
tplNode.add name[i]
|
||||||
|
tplNode.add newEmptyNode()
|
||||||
|
tplNode.add valueNode
|
||||||
|
|
||||||
stmtList.add nnkStmtList.newTree(
|
stmtList.add nnkStmtList.newTree(
|
||||||
nnkLetSection.newTree(
|
nnkLetSection.newTree(
|
||||||
nnkVarTuple.newTree(
|
tplNode
|
||||||
name.children.toSeq.concat(
|
|
||||||
@[newEmptyNode(), valueNode]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
stmtList.add quote do: `opt`.isSome
|
stmtList.add quote do: `opt`.isSome
|
||||||
@ -65,10 +69,14 @@ macro `=?`*(name, expression): bool =
|
|||||||
## new variable. It can be used inside of a conditional expression, for
|
## new variable. It can be used inside of a conditional expression, for
|
||||||
## instance in an `if` statement.
|
## instance in an `if` statement.
|
||||||
|
|
||||||
name.expectKind({nnkIdent, nnkVarTy, nnkTupleConstr})
|
when (NimMajor, NimMinor) < (1, 6):
|
||||||
|
name.expectKind({nnkIdent, nnkVarTy, nnkTupleConstr, nnkPar})
|
||||||
|
else:
|
||||||
|
name.expectKind({nnkIdent, nnkVarTy, nnkTupleConstr})
|
||||||
|
|
||||||
if name.kind == nnkIdent:
|
if name.kind == nnkIdent:
|
||||||
quote do: bindLet(`name`, `expression`)
|
quote do: bindLet(`name`, `expression`)
|
||||||
elif name.kind == nnkTupleConstr:
|
elif name.kind == nnkTupleConstr or name.kind == nnkPar:
|
||||||
quote do: bindTuple(`name`, `expression`)
|
quote do: bindTuple(`name`, `expression`)
|
||||||
else:
|
else:
|
||||||
let name = name[0]
|
let name = name[0]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user