patterns: replace dropType with matchType
This commit is contained in:
parent
8dca5d4663
commit
b6f7e90b95
|
@ -16,7 +16,7 @@ export actors, dataspace, dataspaces, patterns
|
|||
type Assertion* {.deprecated: "Assertion and Preserve[void] replaced by Value".} = Value
|
||||
|
||||
proc `!`*(typ: static typedesc): Pattern {.inline.} =
|
||||
patterns.dropType(typ)
|
||||
patterns.matchType(typ)
|
||||
|
||||
proc `?`*[T](val: T): Pattern {.inline.} =
|
||||
patterns.drop[T](val)
|
||||
|
|
|
@ -161,25 +161,25 @@ proc fieldCount(T: typedesc): int =
|
|||
for _, _ in fieldPairs(default T):
|
||||
inc result
|
||||
|
||||
proc dropType*(typ: static typedesc): Pattern =
|
||||
proc matchType*(typ: static typedesc): Pattern =
|
||||
## Derive a `Pattern` from type `typ` without any bindings.
|
||||
when typ is ref:
|
||||
dropType(pointerBase(typ))
|
||||
matchType(pointerBase(typ))
|
||||
elif typ.hasPreservesRecordPragma:
|
||||
var group = PatternGroup(`type`: GroupType(orKind: GroupTypeKind.`rec`))
|
||||
group.`type`.rec.label = typ.recordLabel.toSymbol
|
||||
for _, f in fieldPairs(default typ):
|
||||
group.entries[group.entries.len.toPreserves] = dropType(typeof f)
|
||||
group.entries[group.entries.len.toPreserves] = matchType(typeof f)
|
||||
group.toPattern
|
||||
elif typ.hasPreservesDictionaryPragma:
|
||||
var group = PatternGroup(`type`: GroupType(orKind: GroupTypeKind.`dict`))
|
||||
for key, val in fieldPairs(default typ):
|
||||
group.entries[key.toSymbol] = dropType(typeof val)
|
||||
group.entries[key.toSymbol] = matchType(typeof val)
|
||||
group.toPattern
|
||||
elif typ is tuple:
|
||||
var group = PatternGroup(`type`: GroupType(orKind: GroupTypeKind.`arr`))
|
||||
for _, f in fieldPairs(default typ):
|
||||
group.entries[group.entries.len.toPreserves] = dropType(typeof f)
|
||||
group.entries[group.entries.len.toPreserves] = matchType(typeof f)
|
||||
group.toPattern
|
||||
elif typ is array:
|
||||
var group = PatternGroup(`type`: GroupType(orKind: GroupTypeKind.`arr`))
|
||||
|
@ -191,11 +191,11 @@ proc dropType*(typ: static typedesc): Pattern =
|
|||
drop()
|
||||
|
||||
proc grabType*(typ: static typedesc): Pattern =
|
||||
PatternBind(pattern: typ.dropType).toPattern
|
||||
PatternBind(pattern: typ.matchType).toPattern
|
||||
|
||||
proc grabWithin*(T: static typedesc): Pattern =
|
||||
## Construct a `Pattern` that binds the fields within type `T`.
|
||||
result = dropType(T)
|
||||
result = matchType(T)
|
||||
for entry in result.group.entries.mvalues:
|
||||
case entry.orKind
|
||||
of `discard`: entry = grab()
|
||||
|
@ -404,7 +404,7 @@ proc observePattern*(pat: Pattern): Pattern =
|
|||
observation = sample.drop.observePattern
|
||||
assert $observation ==
|
||||
"<group <rec Observe> {0: <group <rec group> {0: <group <rec rec> {0: <lit sample>}> 1: <group <dict> {}>}> 1: <_>}>"
|
||||
result = dropType Observe
|
||||
result = matchType Observe
|
||||
result.group.entries[0.toPreserves] = pat.toPreserves.drop
|
||||
|
||||
proc observePattern*(pat: Pattern; injects: openarray[(seq[Value], Pattern)]): Pattern =
|
||||
|
@ -417,7 +417,7 @@ proc observePattern*(pat: Pattern; injects: openarray[(seq[Value], Pattern)]): P
|
|||
observation = v.drop.observePattern { @[1.toPreserves, "b".toSymbol]: grab() }
|
||||
assert $observation ==
|
||||
"<group <rec Observe> {0: <group <rec group> {0: <group <rec arr> {}> 1: <group <dict> {0: <group <rec group> {0: <group <rec dict> {}> 1: <group <dict> {}>}> 1: <group <rec group> {0: <group <rec dict> {}> 1: <group <dict> {a: <group <rec lit> {0: <lit #f>}> b: <bind <_>>}>}>}>}> 1: <_>}>"
|
||||
result = dropType Observe
|
||||
result = matchType Observe
|
||||
var meta = pat.toPreserves.drop
|
||||
for (path, pat) in injects:
|
||||
metaApply(meta, pat, path, 0)
|
||||
|
|
Loading…
Reference in New Issue