syndicate-nim/src/syndicate/patterns.nim

39 lines
1.1 KiB
Nim
Raw Normal View History

2021-10-28 17:43:20 +00:00
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/tables
from std/macros import hasCustomPragma, getCustomPragmaVal
import preserves
import ../syndicate/protocols/[dataspacePatterns]
from ./actors import Ref
type
Pattern* = dataspacePatterns.Pattern[Ref]
proc bindDiscard(): Pattern =
Pattern(
orKind: PatternKind.DBind,
dbind: DBind[Ref](
pattern: Pattern(
orKind: PatternKind.DDiscard)))
proc toPattern*(T: typedesc): Pattern =
when T.hasCustomPragma(preservesRecord):
let label = tosymbol(T.getCustomPragmaVal(preservesRecord), Ref)
var
arity: BiggestInt
members: Table[BiggestInt, Pattern]
t: ptr T # a hack to iterate the fields of a non-existent instance
for _ in fields(t[]):
members[arity] = bindDiscard()
inc arity
result = Pattern(
orKind: PatternKind.DCompound,
dcompound: DCompound[Ref](
orKind: DCompoundKind.rec,
rec: DCompoundRec[Ref](
ctor: CRec[Ref](label: label, arity: arity),
members: members)))