2021-10-28 17:43:20 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
|
2021-12-11 18:36:31 +00:00
|
|
|
import std/[macros, tables, typetraits]
|
2021-10-28 17:43:20 +00:00
|
|
|
|
|
|
|
import preserves
|
2021-11-02 11:58:51 +00:00
|
|
|
import ./protocols/dataspacePatterns
|
2021-10-28 17:43:20 +00:00
|
|
|
from ./actors import Ref
|
|
|
|
|
2021-10-29 12:06:00 +00:00
|
|
|
export dataspacePatterns.`$`
|
|
|
|
|
2021-10-28 17:43:20 +00:00
|
|
|
type
|
2021-10-29 12:06:00 +00:00
|
|
|
CRec = dataspacePatterns.CRec[Ref]
|
|
|
|
DCompoundRec = dataspacePatterns.DCompoundRec[Ref]
|
|
|
|
DBind* = dataspacePatterns.DBind[Ref]
|
|
|
|
DLit* = dataspacePatterns.DLit[Ref]
|
|
|
|
DCompound* = dataspacePatterns.DCompound[Ref]
|
2021-10-28 17:43:20 +00:00
|
|
|
Pattern* = dataspacePatterns.Pattern[Ref]
|
|
|
|
|
2021-10-29 12:06:00 +00:00
|
|
|
proc `?`*(d: DBind): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DBind, dbind: d)
|
|
|
|
|
|
|
|
proc `?`*(d: DLit): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DLit, dlit: d)
|
|
|
|
|
|
|
|
proc `?`*(d: DCompound): Pattern =
|
|
|
|
Pattern(orKind: PatternKind.DCompound, dcompound: d)
|
|
|
|
|
2021-11-02 11:58:51 +00:00
|
|
|
proc `?`*(s: string): Pattern =
|
|
|
|
?DLit(value: toPreserve(s, Ref))
|
|
|
|
|
2021-12-11 18:36:31 +00:00
|
|
|
proc drop*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
|
|
|
proc grab*(): Pattern = ?DBind(pattern: drop())
|
2021-11-02 11:58:51 +00:00
|
|
|
|
2021-12-11 18:36:31 +00:00
|
|
|
proc `?_`*(): Pattern = drop()
|
|
|
|
proc `?*`*(): Pattern = grab()
|
2021-10-29 12:06:00 +00:00
|
|
|
|
|
|
|
proc `?`*(T: typedesc; bindings: openArray[(int, Pattern)]): Pattern =
|
|
|
|
## Pattern constructor operator.
|
|
|
|
when T.hasCustomPragma(preservesRecord):
|
2021-11-02 11:58:51 +00:00
|
|
|
var label = tosymbol(T.getCustomPragmaVal(preservesRecord), Ref)
|
2021-10-29 12:06:00 +00:00
|
|
|
result = ?DCompound(
|
|
|
|
orKind: DCompoundKind.rec,
|
|
|
|
rec: DCompoundRec(
|
2021-12-11 18:36:31 +00:00
|
|
|
ctor: CRec(label: label, arity: bindings.len),
|
2021-11-02 11:58:51 +00:00
|
|
|
members: toTable bindings))
|
2021-12-11 18:36:31 +00:00
|
|
|
elif T is ref:
|
|
|
|
`?`(pointerBase(T), bindings)
|
2021-11-02 11:58:51 +00:00
|
|
|
else:
|
|
|
|
{.error: "no custom pragma on " & $T.}
|
|
|
|
|
|
|
|
proc observe*(pat: Pattern): Pattern =
|
|
|
|
# TODO: why doesn't Observe from ./protocols/dataspace.nim work?
|
|
|
|
?DCompound(
|
|
|
|
orKind: DCompoundKind.rec,
|
|
|
|
rec: DCompoundRec(
|
|
|
|
ctor: CRec(label: toSymbol("Observe", Ref), arity: 2),
|
2021-11-03 18:22:33 +00:00
|
|
|
members: toTable {0 : pat, 1: `?_`()}))
|