From 29b43eacedb8de33888b50a556bca0e0f3a781ab Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 28 Oct 2021 19:43:20 +0200 Subject: [PATCH] Pattern constructors --- src/syndicate/patterns.nim | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/syndicate/patterns.nim diff --git a/src/syndicate/patterns.nim b/src/syndicate/patterns.nim new file mode 100644 index 0000000..3cb8d4d --- /dev/null +++ b/src/syndicate/patterns.nim @@ -0,0 +1,38 @@ +# 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)))