mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-10 09:43:07 +00:00
Add comments to clarify chaining code
This commit is contained in:
parent
e266870dfa
commit
b8e52b6acf
@ -1,33 +1,39 @@
|
|||||||
import std/macros
|
import std/macros
|
||||||
|
|
||||||
template `?.`*(option: typed, identifier: untyped{nkIdent}): untyped =
|
template `?.`*(option: typed, identifier: untyped{nkIdent}): untyped =
|
||||||
|
# chain is of shape: option?.identifier
|
||||||
option ->? option.unsafeGet.identifier
|
option ->? option.unsafeGet.identifier
|
||||||
|
|
||||||
macro `?.`*(option: typed, infix: untyped{nkInfix}): untyped =
|
macro `?.`*(option: typed, infix: untyped{nkInfix}): untyped =
|
||||||
|
# chain is of shape: option?.left `operator` right
|
||||||
let left = infix[1]
|
let left = infix[1]
|
||||||
infix[1] = quote do: `option`?.`left`
|
infix[1] = quote do: `option`?.`left`
|
||||||
infix
|
infix
|
||||||
|
|
||||||
macro `?.`*(option: typed, bracket: untyped{nkBracketExpr}): untyped =
|
macro `?.`*(option: typed, bracket: untyped{nkBracketExpr}): untyped =
|
||||||
|
# chain is of shape: option?.left[right]
|
||||||
let left = bracket[0]
|
let left = bracket[0]
|
||||||
bracket[0] = quote do: `option`?.`left`
|
bracket[0] = quote do: `option`?.`left`
|
||||||
bracket
|
bracket
|
||||||
|
|
||||||
macro `?.`*(option: typed, dot: untyped{nkDotExpr}): untyped =
|
macro `?.`*(option: typed, dot: untyped{nkDotExpr}): untyped =
|
||||||
|
# chain is of shape: option?.left.right
|
||||||
let left = dot[0]
|
let left = dot[0]
|
||||||
dot[0] = quote do: `option`?.`left`
|
dot[0] = quote do: `option`?.`left`
|
||||||
dot
|
dot
|
||||||
|
|
||||||
macro `?.`*(option: typed, call: untyped{nkCall}): untyped =
|
macro `?.`*(option: typed, call: untyped{nkCall}): untyped =
|
||||||
let procedure = call[0]
|
let procedure = call[0]
|
||||||
if call.len > 1:
|
if call.len == 1:
|
||||||
if procedure.kind == nnkDotExpr:
|
# chain is of shape: option?.procedure()
|
||||||
let (inner, outer) = (procedure[0], procedure[1])
|
quote do: `option`?.`procedure`
|
||||||
call[0] = outer
|
elif procedure.kind == nnkDotExpr:
|
||||||
call.insert(1, quote do: `option`?.`inner`)
|
# chain is of shape: option?.left.right(arguments)
|
||||||
call
|
let (left, right) = (procedure[0], procedure[1])
|
||||||
else:
|
call[0] = right
|
||||||
call.insert(1, quote do: `option`.unsafeGet)
|
call.insert(1, quote do: `option`?.`left`)
|
||||||
quote do: `option` ->? `call`
|
call
|
||||||
else:
|
else:
|
||||||
quote do: `option`?.`procedure`
|
# chain is of shape: option?.procedure(arguments)
|
||||||
|
call.insert(1, quote do: `option`.unsafeGet)
|
||||||
|
quote do: `option` ->? `call`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user