Export depattern proc
This commit is contained in:
parent
8bc0ee2ae5
commit
23c69f63a5
|
@ -289,31 +289,40 @@ proc grabDictionary*(bindings: sink openArray[(string, Pattern)]): Pattern =
|
||||||
for (key, val) in bindings.items:
|
for (key, val) in bindings.items:
|
||||||
result.dcompound.dict.entries[key.toSymbol(Cap)] = val
|
result.dcompound.dict.entries[key.toSymbol(Cap)] = val
|
||||||
|
|
||||||
func depattern(comp: DCompound): Preserve[Cap]
|
proc depattern(comp: DCompound; values: var seq[Value]; index: var int): Value {.gcsafe.}
|
||||||
|
|
||||||
func depattern(pat: Pattern): Preserve[Cap] =
|
proc depattern(pat: Pattern; values: var seq[Value]; index: var int): Value =
|
||||||
case pat.orKind
|
case pat.orKind
|
||||||
of PatternKind.DDiscard, PatternKind.DBind:
|
of PatternKind.DDiscard:
|
||||||
discard
|
discard
|
||||||
|
of PatternKind.DBind:
|
||||||
|
if index < values.len:
|
||||||
|
result = move values[index]
|
||||||
|
inc index
|
||||||
of PatternKind.DLit:
|
of PatternKind.DLit:
|
||||||
result = pat.dlit.value.toPreserve(Cap)
|
result = pat.dlit.value.toPreserve(Cap)
|
||||||
of PatternKind.DCompound:
|
of PatternKind.DCompound:
|
||||||
result = depattern(pat.dcompound)
|
result = depattern(pat.dcompound, values, index)
|
||||||
|
|
||||||
func depattern(comp: DCompound): Preserve[Cap] =
|
proc depattern(comp: DCompound; values: var seq[Value]; index: var int): Value {.gcsafe.} =
|
||||||
case comp.orKind
|
case comp.orKind
|
||||||
of DCompoundKind.rec:
|
of DCompoundKind.rec:
|
||||||
result = initRecord(comp.rec.label, comp.rec.fields.len)
|
result = initRecord(comp.rec.label, comp.rec.fields.len)
|
||||||
for i, f in comp.rec.fields:
|
for i, f in comp.rec.fields:
|
||||||
result[i] = depattern(f)
|
result[i] = depattern(f, values, index)
|
||||||
of DCompoundKind.arr:
|
of DCompoundKind.arr:
|
||||||
result = initSequence(comp.arr.items.len, Cap)
|
result = initSequence(comp.arr.items.len, Cap)
|
||||||
for i, e in comp.arr.items:
|
for i, e in comp.arr.items:
|
||||||
result[i] = depattern(e)
|
result[i] = depattern(e, values, index)
|
||||||
of DCompoundKind.dict:
|
of DCompoundKind.dict:
|
||||||
result = initDictionary(Cap)
|
result = initDictionary(Cap)
|
||||||
for key, val in comp.dict.entries:
|
for key, val in comp.dict.entries:
|
||||||
result[key] = depattern(val)
|
result[key] = depattern(val, values, index)
|
||||||
|
|
||||||
|
proc depattern*(pat: Pattern; values: sink seq[Value]): Value =
|
||||||
|
## Convert a `Pattern` to a `Value` while replacing binds with `values`.
|
||||||
|
var index: int
|
||||||
|
depattern(pat, values, index)
|
||||||
|
|
||||||
type Literal*[T] = object
|
type Literal*[T] = object
|
||||||
## A wrapper type to deserialize patterns to native values.
|
## A wrapper type to deserialize patterns to native values.
|
||||||
|
@ -321,7 +330,7 @@ type Literal*[T] = object
|
||||||
|
|
||||||
proc fromPreserveHook*[T, E](lit: var Literal[T]; pr: Preserve[E]): bool =
|
proc fromPreserveHook*[T, E](lit: var Literal[T]; pr: Preserve[E]): bool =
|
||||||
var pat: Pattern
|
var pat: Pattern
|
||||||
pat.fromPreserve(pr) and lit.value.fromPreserve(depattern pat)
|
pat.fromPreserve(pr) and lit.value.fromPreserve(depattern(pat, @[]))
|
||||||
|
|
||||||
proc toPreserveHook*[T](lit: Literal[T]; E: typedesc): Preserve[E] =
|
proc toPreserveHook*[T](lit: Literal[T]; E: typedesc): Preserve[E] =
|
||||||
lit.value.grab.toPreserve(E)
|
lit.value.grab.toPreserve(E)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "20231102"
|
version = "20231107"
|
||||||
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