Generate literal patterns by preserving values
This commit is contained in:
parent
07cd833e3d
commit
57e4bb6bad
|
@ -30,9 +30,9 @@ import ./syndicate/[actors, dataspaces, durings, patterns]
|
||||||
|
|
||||||
from ./syndicate/relays import connectStdio, connectUnix
|
from ./syndicate/relays import connectStdio, connectUnix
|
||||||
|
|
||||||
export Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction, `$`, `?`,
|
export Actor, Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction,
|
||||||
bootDataspace, connectStdio, connectUnix, drop, facet, grab, message,
|
`$`, `?`, `??`, bootDataspace, connectStdio, connectUnix, drop, facet,
|
||||||
newDataspace, publish, retract, replace, run, stop, unembed
|
future, grab, message, newDataspace, publish, retract, replace, run, stop, unembed
|
||||||
|
|
||||||
proc `?`*[T](val: T): Pattern =
|
proc `?`*[T](val: T): Pattern =
|
||||||
## Construct a `Pattern` from value of type `T`.
|
## Construct a `Pattern` from value of type `T`.
|
||||||
|
@ -42,9 +42,6 @@ proc `?`*[T](val: T): Pattern =
|
||||||
value: AnyAtom(
|
value: AnyAtom(
|
||||||
orKind: AnyAtomKind.embedded,
|
orKind: AnyAtomKind.embedded,
|
||||||
embedded: embed(val))))
|
embedded: embed(val))))
|
||||||
# elif T is Preserve[Ref]:
|
|
||||||
# result = grab()
|
|
||||||
# TODO
|
|
||||||
elif T is ptr | ref:
|
elif T is ptr | ref:
|
||||||
if system.`==`(val, nil): result = ?(Symbol "null")
|
if system.`==`(val, nil): result = ?(Symbol "null")
|
||||||
else: result = ?(val[])
|
else: result = ?(val[])
|
||||||
|
@ -94,8 +91,7 @@ proc `?`*[T](val: T): Pattern =
|
||||||
rec: DCompoundRec(
|
rec: DCompoundRec(
|
||||||
label: label, fields: fields))
|
label: label, fields: fields))
|
||||||
else:
|
else:
|
||||||
{.error: "cannot derive literal pattern from " & $T.}
|
?(toPreserve(val, Ref))
|
||||||
|
|
||||||
|
|
||||||
proc `?`*(T: static typedesc): Pattern =
|
proc `?`*(T: static typedesc): Pattern =
|
||||||
## Construct a `Pattern` from type `T`.
|
## Construct a `Pattern` from type `T`.
|
||||||
|
@ -116,7 +112,6 @@ proc `?`*(T: static typedesc): Pattern =
|
||||||
## general case will return a wildcard binding.
|
## general case will return a wildcard binding.
|
||||||
when T is ref:
|
when T is ref:
|
||||||
?pointerBase(T)
|
?pointerBase(T)
|
||||||
elif T is Preserve: grab()
|
|
||||||
elif T.hasPreservesRecordPragma:
|
elif T.hasPreservesRecordPragma:
|
||||||
var
|
var
|
||||||
label = T.recordLabel.tosymbol(Ref)
|
label = T.recordLabel.tosymbol(Ref)
|
||||||
|
|
|
@ -475,3 +475,5 @@ proc sync*(turn, refer: Ref, cb: proc(t: Turn) {.gcsafe.}) =
|
||||||
|
|
||||||
proc runActor*(name: string; bootProc: TurnAction): Future[void] =
|
proc runActor*(name: string; bootProc: TurnAction): Future[void] =
|
||||||
bootActor(name, bootProc).future
|
bootActor(name, bootProc).future
|
||||||
|
|
||||||
|
proc future*(actor): Future[void] = actor.future
|
||||||
|
|
|
@ -34,8 +34,7 @@ proc grab*(mem: Membrane; key: Oid|Ref): WireSymbol =
|
||||||
|
|
||||||
proc drop*(mem: var Membrane; sym: WireSymbol) =
|
proc drop*(mem: var Membrane; sym: WireSymbol) =
|
||||||
## Drop a `WireSymbol` from a `Membrane`.
|
## Drop a `WireSymbol` from a `Membrane`.
|
||||||
when not defined(release): assert sym.mem == mem,
|
assert sym.mem == mem, "cannot drop WireSymbol at the wrong Membrane"
|
||||||
"cannot drop WireSymbol at the wrong Membrane"
|
|
||||||
assert sym.count > 0
|
assert sym.count > 0
|
||||||
dec sym.count
|
dec sym.count
|
||||||
if sym.count < 1:
|
if sym.count < 1:
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
||||||
# SPDX-License-Identifier: Unlicense
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
import std/[tables, typetraits]
|
import std/[sequtils, tables, typetraits]
|
||||||
from std/sequtils import toSeq
|
|
||||||
|
|
||||||
import preserves
|
import preserves
|
||||||
import ./protocols/dataspacePatterns
|
import ./protocols/dataspacePatterns
|
||||||
|
@ -11,6 +10,7 @@ from ./actors import Ref
|
||||||
export dataspacePatterns.`$`, PatternKind, DCompoundKind
|
export dataspacePatterns.`$`, PatternKind, DCompoundKind
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Assertion = Preserve[Ref]
|
||||||
AnyAtom* = dataspacePatterns.AnyAtom[Ref]
|
AnyAtom* = dataspacePatterns.AnyAtom[Ref]
|
||||||
DBind* = dataspacePatterns.DBind[Ref]
|
DBind* = dataspacePatterns.DBind[Ref]
|
||||||
DCompound* = dataspacePatterns.DCompound[Ref]
|
DCompound* = dataspacePatterns.DCompound[Ref]
|
||||||
|
@ -20,38 +20,95 @@ type
|
||||||
DLit* = dataspacePatterns.DLit[Ref]
|
DLit* = dataspacePatterns.DLit[Ref]
|
||||||
Pattern* = dataspacePatterns.Pattern[Ref]
|
Pattern* = dataspacePatterns.Pattern[Ref]
|
||||||
|
|
||||||
proc `?`*(d: DBind): Pattern =
|
proc `?`*(d: sink DBind): Pattern =
|
||||||
Pattern(orKind: PatternKind.DBind, dbind: d)
|
Pattern(orKind: PatternKind.DBind, dbind: d)
|
||||||
|
|
||||||
proc `?`*(d: DLit): Pattern =
|
proc `?`*(d: sink DLit): Pattern =
|
||||||
Pattern(orKind: PatternKind.DLit, dlit: d)
|
Pattern(orKind: PatternKind.DLit, dlit: d)
|
||||||
|
|
||||||
proc `?`*(d: DCompound): Pattern =
|
proc `?`*(aa: sink AnyAtom): Pattern =
|
||||||
|
?DLit(value: aa)
|
||||||
|
|
||||||
|
proc `?`*(d: sink DCompound): Pattern =
|
||||||
Pattern(orKind: PatternKind.DCompound, dcompound: d)
|
Pattern(orKind: PatternKind.DCompound, dcompound: d)
|
||||||
|
|
||||||
proc `?`*(d: DCompoundRec): Pattern =
|
proc `?`*(d: sink DCompoundRec): Pattern =
|
||||||
?DCompound(orKind: DCompoundKind.rec, rec: d)
|
?DCompound(orKind: DCompoundKind.rec, rec: d)
|
||||||
|
|
||||||
|
proc `?`*(d: sink DCompoundArr): Pattern =
|
||||||
|
?DCompound(orKind: DCompoundKind.arr, arr: d)
|
||||||
|
|
||||||
|
proc `?`*(d: sink DCompoundDict): Pattern =
|
||||||
|
?DCompound(orKind: DCompoundKind.dict, dict: d)
|
||||||
|
|
||||||
proc `?`*(x: bool): Pattern =
|
proc `?`*(x: bool): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`bool`, bool: x))
|
?AnyAtom(orKind: AnyAtomKind.`bool`, bool: x)
|
||||||
|
|
||||||
proc `?`*(x: float32): Pattern =
|
proc `?`*(x: float32): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`float`, float: x))
|
?AnyAtom(orKind: AnyAtomKind.`float`, float: x)
|
||||||
|
|
||||||
proc `?`*(x: float64): Pattern =
|
proc `?`*(x: float64): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`double`, double: x))
|
?AnyAtom(orKind: AnyAtomKind.`double`, double: x)
|
||||||
|
|
||||||
proc `?`*(x: int): Pattern =
|
proc `?`*(x: int): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`int`, int: x))
|
?AnyAtom(orKind: AnyAtomKind.`int`, int: x)
|
||||||
|
|
||||||
proc `?`*(s: string): Pattern =
|
proc `?`*(s: sink string): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`string`, string: s))
|
?AnyAtom(orKind: AnyAtomKind.`string`, string: s)
|
||||||
|
|
||||||
proc `?`*(x: seq[byte]): Pattern =
|
proc `?`*(x: sink seq[byte]): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`bytes`, bytes: x))
|
?AnyAtom(orKind: AnyAtomKind.`bytes`, bytes: x)
|
||||||
|
|
||||||
proc `?`*(x: Symbol): Pattern =
|
proc `?`*(x: sink Symbol): Pattern =
|
||||||
?DLit(value: AnyAtom(orKind: AnyAtomKind.`symbol`, symbol: x))
|
?AnyAtom(orKind: AnyAtomKind.`symbol`, symbol: x)
|
||||||
|
|
||||||
|
proc `?`*[T](pr: Preserve[T]): Pattern =
|
||||||
|
assert not pr.embedded
|
||||||
|
case pr.kind
|
||||||
|
of pkBoolean: ?pr.bool
|
||||||
|
of pkFloat: ?pr.float
|
||||||
|
of pkDouble: ?pr.double
|
||||||
|
of pkSignedInteger: ?(int pr.int) # TODO: overflow!
|
||||||
|
of pkString: ?pr.string
|
||||||
|
of pkByteString: ?pr.bytes
|
||||||
|
of pkSymbol: ?pr.symbol
|
||||||
|
of pkRecord:
|
||||||
|
?DCompoundRec(
|
||||||
|
label: pr.label,
|
||||||
|
fields: map[Preserve[T], Pattern](pr.fields, `?`[T]))
|
||||||
|
of pkSequence:
|
||||||
|
?DCompoundArr(items: map(pr.sequence, `?`[T]))
|
||||||
|
of pkSet: raise newException(
|
||||||
|
ValueError, "cannot construct a pattern over a set literal")
|
||||||
|
of pkDictionary:
|
||||||
|
var dict = DCompoundDict()
|
||||||
|
for key, val in pr.pairs: dict.entries[key] = ?val
|
||||||
|
?dict
|
||||||
|
of pkEmbedded:
|
||||||
|
raiseAssert "cannot construct a pattern over a embedded literal"
|
||||||
|
|
||||||
|
proc `??`*(pat: Pattern): Pattern =
|
||||||
|
## Construct a `Pattern` that matches a `Pattern`.
|
||||||
|
case pat.orKind
|
||||||
|
of PatternKind.DDiscard, PatternKind.DBind:
|
||||||
|
result = pat
|
||||||
|
of PatternKind.DLit:
|
||||||
|
result = ?(pat.toPreserve(Ref))
|
||||||
|
of PatternKind.DCompound:
|
||||||
|
case pat.dcompound.orKind
|
||||||
|
of DCompoundKind.rec:
|
||||||
|
var fields = move pat.dcompound.rec.fields
|
||||||
|
result = ?(pat.toPreserve(Ref))
|
||||||
|
result.dcompound.rec.fields[1].dcompound.arr.items = fields
|
||||||
|
of DCompoundKind.arr:
|
||||||
|
var items = move pat.dcompound.arr.items
|
||||||
|
result = ?(pat.toPreserve(Ref))
|
||||||
|
result.dcompound.rec.fields[0].dcompound.arr.items = items
|
||||||
|
of DCompoundKind.dict:
|
||||||
|
# var entries = move pat.dcompound.dict.entries
|
||||||
|
# result = ?(pat.toPreserve(Ref))
|
||||||
|
stderr.writeLine "pattern construction from DCompoundKind not implemented"
|
||||||
|
raiseAssert "not implemented"
|
||||||
|
|
||||||
proc drop*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
proc drop*(): Pattern = Pattern(orKind: PatternKind.DDiscard)
|
||||||
proc grab*(): Pattern = ?DBind(pattern: drop())
|
proc grab*(): Pattern = ?DBind(pattern: drop())
|
||||||
|
|
Loading…
Reference in New Issue