patterns: do not match inner types at dropType
This commit is contained in:
parent
a4ba81a481
commit
2fa2276dfe
|
@ -1,2 +1,3 @@
|
||||||
include depends.tup
|
include depends.tup
|
||||||
|
NIM = $(DIRENV) nim
|
||||||
NIM_GROUPS += $(TUP_CWD)/<lock>
|
NIM_GROUPS += $(TUP_CWD)/<lock>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
{ pkgs ? import <nixpkgs> { } }:
|
let pkgs = import <nixpkgs> { };
|
||||||
pkgs.buildNimPackage {
|
in pkgs.buildNimPackage {
|
||||||
name = "dummy";
|
name = "noiseprotocol";
|
||||||
|
buildInputs = [ pkgs.noise-c ];
|
||||||
lockFile = ./lock.json;
|
lockFile = ./lock.json;
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,33 +114,6 @@ proc grab*[T](x: T): Pattern =
|
||||||
$grab([0, 1, 2, 3]) == "<arr [<lit 0> <lit 1> <lit 2> <lit 3>]>"
|
$grab([0, 1, 2, 3]) == "<arr [<lit 0> <lit 1> <lit 2> <lit 3>]>"
|
||||||
grab(x.toPreserves)
|
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 =
|
proc grabType*(typ: static typedesc): Pattern =
|
||||||
## Derive a `Pattern` from type `typ`.
|
## Derive a `Pattern` from type `typ`.
|
||||||
## This works for `tuple` and `object` types but in the
|
## This works for `tuple` and `object` types but in the
|
||||||
|
@ -162,16 +135,61 @@ proc grabType*(typ: static typedesc): Pattern =
|
||||||
"<rec rect [<arr [<bind <_>> <bind <_>>]> <arr [<bind <_>> <bind <_>>]>]>"
|
"<rec rect [<arr [<bind <_>> <bind <_>>]> <arr [<bind <_>> <bind <_>>]>]>"
|
||||||
$(grabType ColoredRect) ==
|
$(grabType ColoredRect) ==
|
||||||
"<dict {color: <bind <_>> rect: <rec rect [<arr [<bind <_>> <bind <_>>]> <arr [<bind <_>> <bind <_>>]>]>}>"
|
"<dict {color: <bind <_>> rect: <rec rect [<arr [<bind <_>> <bind <_>>]> <arr [<bind <_>> <bind <_>>]>]>}>"
|
||||||
patternOfType(typ, true)
|
when typ is ref:
|
||||||
|
grabType(pointerBase(typ))
|
||||||
proc dropType*(typ: static typedesc): Pattern =
|
elif typ.hasPreservesRecordPragma:
|
||||||
## Derive a `Pattern` from type `typ` without any bindings.
|
var rec = DCompoundRec(label: typ.recordLabel.toSymbol)
|
||||||
patternOfType(typ, false)
|
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 =
|
proc fieldCount(T: typedesc): int =
|
||||||
for _, _ in fieldPairs(default T):
|
for _, _ in fieldPairs(default T):
|
||||||
inc result
|
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 =
|
proc lookup(bindings: openArray[(int, Pattern)]; i: int): Pattern =
|
||||||
for (j, b) in bindings:
|
for (j, b) in bindings:
|
||||||
if i == j: return b
|
if i == j: return b
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "20240114"
|
version = "20240116"
|
||||||
author = "Emery Hemingway"
|
author = "Emery Hemingway"
|
||||||
description = "Syndicated actors for conversational concurrency"
|
description = "Syndicated actors for conversational concurrency"
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
|
|
Loading…
Reference in New Issue