From f710419ead4c756261fdb67526936721a1c70a26 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 11 Dec 2021 18:36:31 +0000 Subject: [PATCH] Generate patterns from ref types --- src/syndicate/patterns.nim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/syndicate/patterns.nim b/src/syndicate/patterns.nim index 0d52557..14891e9 100644 --- a/src/syndicate/patterns.nim +++ b/src/syndicate/patterns.nim @@ -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.}