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