Generate patterns from ref types
This commit is contained in:
parent
ae04bc1019
commit
f710419ead
|
@ -1,7 +1,7 @@
|
|||
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[macros, tables]
|
||||
import std/[macros, tables, typetraits]
|
||||
|
||||
import preserves
|
||||
import ./protocols/dataspacePatterns
|
||||
|
@ -29,13 +29,11 @@ proc `?`*(d: DCompound): Pattern =
|
|||
proc `?`*(s: string): Pattern =
|
||||
?DLit(value: toPreserve(s, Ref))
|
||||
|
||||
proc arity(T: typedesc): int =
|
||||
var t: T # a hack to iterate the fields of a non-existent instance
|
||||
for _ in fields(t): inc result
|
||||
proc drop*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
||||
proc grab*(): Pattern = ?DBind(pattern: drop())
|
||||
|
||||
proc `?_`*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
||||
|
||||
proc `?*`*(): Pattern = ?DBind(pattern: `?_`())
|
||||
proc `?_`*(): Pattern = drop()
|
||||
proc `?*`*(): Pattern = grab()
|
||||
|
||||
proc `?`*(T: typedesc; bindings: openArray[(int, Pattern)]): Pattern =
|
||||
## Pattern constructor operator.
|
||||
|
@ -44,8 +42,10 @@ proc `?`*(T: typedesc; bindings: openArray[(int, Pattern)]): Pattern =
|
|||
result = ?DCompound(
|
||||
orKind: DCompoundKind.rec,
|
||||
rec: DCompoundRec(
|
||||
ctor: CRec(label: label, arity: T.arity),
|
||||
ctor: CRec(label: label, arity: bindings.len),
|
||||
members: toTable bindings))
|
||||
elif T is ref:
|
||||
`?`(pointerBase(T), bindings)
|
||||
else:
|
||||
{.error: "no custom pragma on " & $T.}
|
||||
|
||||
|
|
Loading…
Reference in New Issue