From 4ed90b0e0297451da0545088d06f4c146a32ac88 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 18 Jan 2024 19:22:56 +0200 Subject: [PATCH] patterns: do not match inner types at dropType --- shell.nix | 7 ++-- src/syndicate/patterns.nim | 82 +++++++++++++++++++++++--------------- syndicate.nimble | 2 +- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/shell.nix b/shell.nix index 3a6cdd4..17e5d58 100644 --- a/shell.nix +++ b/shell.nix @@ -1,5 +1,6 @@ -{ pkgs ? import { } }: -pkgs.buildNimPackage { - name = "dummy"; +let pkgs = import { }; +in pkgs.buildNimPackage { + name = "noiseprotocol"; + buildInputs = [ pkgs.noise-c ]; lockFile = ./lock.json; } diff --git a/src/syndicate/patterns.nim b/src/syndicate/patterns.nim index 84cba41..5feb66f 100644 --- a/src/syndicate/patterns.nim +++ b/src/syndicate/patterns.nim @@ -114,33 +114,6 @@ proc grab*[T](x: T): Pattern = $grab([0, 1, 2, 3]) == " ]>" grab(x.toPreserves) -proc patternOfType(typ: static typedesc; `bind`: static bool): Pattern = - when typ is ref: - patternOfType(pointerBase(typ), `bind`) - elif typ.hasPreservesRecordPragma: - var rec = DCompoundRec(label: typ.recordLabel.toSymbol) - for _, f in fieldPairs(default typ): - add(rec.fields, patternOfType(typeof f, `bind`)) - result = rec.toPattern - elif typ.hasPreservesDictionaryPragma: - var dict = DCompoundDict() - for key, val in fieldPairs(default typ): - dict.entries[key.toSymbol] = patternOfType(typeof val, `bind`) - dict.toPattern - elif typ is tuple: - var arr = DCompoundArr() - for _, f in fieldPairs(default typ): - add(arr.items, patternOfType(typeof f, `bind`)) - arr.toPattern - elif typ is array: - var arr = DCompoundArr() - arr.items.setLen(len(typ)) - for e in arr.items.mitems: e = grab() - arr.toPattern - else: - if `bind`: grab() - else: drop() - proc grabType*(typ: static typedesc): Pattern = ## Derive a `Pattern` from type `typ`. ## This works for `tuple` and `object` types but in the @@ -162,16 +135,61 @@ proc grabType*(typ: static typedesc): Pattern = "> >]> > >]>]>" $(grabType ColoredRect) == "> rect: > >]> > >]>]>}>" - patternOfType(typ, true) - -proc dropType*(typ: static typedesc): Pattern = - ## Derive a `Pattern` from type `typ` without any bindings. - patternOfType(typ, false) + when typ is ref: + grabType(pointerBase(typ)) + elif typ.hasPreservesRecordPragma: + var rec = DCompoundRec(label: typ.recordLabel.toSymbol) + for _, f in fieldPairs(default typ): + add(rec.fields, grabType(typeof f)) + result = rec.toPattern + elif typ.hasPreservesDictionaryPragma: + var dict = DCompoundDict() + for key, val in fieldPairs(default typ): + dict.entries[key.toSymbol] = grabType(typeof val) + dict.toPattern + elif typ is tuple: + var arr = DCompoundArr() + for _, f in fieldPairs(default typ): + add(arr.items, grabType(typeof f)) + arr.toPattern + elif typ is array: + var arr = DCompoundArr() + arr.items.setLen(len(typ)) + for e in arr.items.mitems: e = grab() + arr.toPattern + else: + grab() proc fieldCount(T: typedesc): int = for _, _ in fieldPairs(default T): inc result +proc dropType*(typ: static typedesc): Pattern = + ## Derive a `Pattern` from type `typ` without any bindings. + when typ is ref: + dropType(pointerBase(typ)) + elif typ.hasPreservesRecordPragma: + var rec = DCompoundRec(label: typ.recordLabel.toSymbol) + rec.fields.setLen(fieldCount typ) + for i, _ in rec.fields: + rec.fields[i] = drop() + result = rec.toPattern + elif typ.hasPreservesDictionaryPragma: + DCompoundDict().toPattern + elif typ is tuple: + var arr = DCompoundArr() + arr.items.setLen(len typ) + for i, _ in arr.items: + arr.items[i] = drop() + arr.toPattern + elif typ is array: + var arr = DCompoundArr() + arr.items.setLen(len(typ)) + for e in arr.items.mitems: e = drop() + arr.toPattern + else: + drop() + proc lookup(bindings: openArray[(int, Pattern)]; i: int): Pattern = for (j, b) in bindings: if i == j: return b diff --git a/syndicate.nimble b/syndicate.nimble index 62c0a63..8aab614 100644 --- a/syndicate.nimble +++ b/syndicate.nimble @@ -1,6 +1,6 @@ # Package -version = "20240114" +version = "20240116" author = "Emery Hemingway" description = "Syndicated actors for conversational concurrency" license = "Unlicense"