30 lines
672 B
Nim
30 lines
672 B
Nim
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/options
|
|
import preserves
|
|
|
|
type
|
|
Discard* {.record: "discard", pure.} = object
|
|
discard
|
|
|
|
Capture* {.record: "capture", pure.} = object
|
|
_: Discard
|
|
|
|
Observe* {.record: "observe", pure.} = object
|
|
pattern: Preserve
|
|
|
|
proc observe*[T](x: T): Preserve =
|
|
Observe(pattern: x.toPreserve).toPreserve
|
|
|
|
proc captureCount*(pattern: Preserve): int =
|
|
if pattern.preserveTo(Capture).isSome:
|
|
result = 1
|
|
else:
|
|
for e in pattern.items:
|
|
result.inc captureCount(e)
|
|
|
|
when isMainModule:
|
|
let a = observe(`?*`)
|
|
assert($toPreserve(a) == "<capture <discard>>")
|