Adapt to non-throwing fromPreserve
Use the new fromPreserve proc that indicates failure with its return value to test if a handler can process a pattern match.
This commit is contained in:
parent
4f7f7e7780
commit
2a09e61f2b
|
@ -1,10 +1,12 @@
|
||||||
# SPDX-License-Identifier: ISC
|
# SPDX-License-Identifier: ISC
|
||||||
|
|
||||||
import std/[asyncdispatch, macros, options]
|
import std/[asyncdispatch, macros, options]
|
||||||
import preserves
|
import preserves, preserves/records
|
||||||
import syndicate/[assertions, dataspaces, events, skeletons]
|
import syndicate/[assertions, dataspaces, events, skeletons]
|
||||||
|
|
||||||
export preserves.`%`
|
export preserves.`%`
|
||||||
|
export preserves.fromPreserve
|
||||||
|
export records.init
|
||||||
export assertions.`?_`
|
export assertions.`?_`
|
||||||
export assertions.`?*`
|
export assertions.`?*`
|
||||||
export assertions.Observe
|
export assertions.Observe
|
||||||
|
@ -28,6 +30,7 @@ export events.EventKind
|
||||||
export skeletons.Analysis
|
export skeletons.Analysis
|
||||||
|
|
||||||
export asyncdispatch.`callback=`
|
export asyncdispatch.`callback=`
|
||||||
|
export options.get
|
||||||
|
|
||||||
proc getCurrentFacet*(): Facet = raiseAssert("must be called from within the DSL")
|
proc getCurrentFacet*(): Facet = raiseAssert("must be called from within the DSL")
|
||||||
## Return the current `Facet` for this context.
|
## Return the current `Facet` for this context.
|
||||||
|
@ -50,13 +53,13 @@ proc wrapDoHandler(pattern, handler: NimNode): NimNode =
|
||||||
## Generate a procedure that unpacks a `pattern` match to fit the
|
## Generate a procedure that unpacks a `pattern` match to fit the
|
||||||
## parameters of `handler`, and calls the body of `handler`.
|
## parameters of `handler`, and calls the body of `handler`.
|
||||||
handler.expectKind nnkDo
|
handler.expectKind nnkDo
|
||||||
let
|
var
|
||||||
formalArgs = handler[3]
|
formalArgs = handler[3]
|
||||||
cbFacetSym = genSym(nskParam, "facet")
|
cbFacetSym = genSym(nskParam, "facet")
|
||||||
scriptFacetSym = genSym(nskParam, "facet")
|
scriptFacetSym = genSym(nskParam, "facet")
|
||||||
recSym = genSym(nskParam, "bindings")
|
recSym = genSym(nskParam, "bindings")
|
||||||
var
|
varSection = newNimNode(nnkVarSection, handler)
|
||||||
letSection = newNimNode(nnkLetSection, handler)
|
conditional: NimNode
|
||||||
argCount: int
|
argCount: int
|
||||||
for i, arg in formalArgs:
|
for i, arg in formalArgs:
|
||||||
if i > 0:
|
if i > 0:
|
||||||
|
@ -67,16 +70,27 @@ proc wrapDoHandler(pattern, handler: NimNode): NimNode =
|
||||||
else:
|
else:
|
||||||
if arg[1].kind == nnkEmpty:
|
if arg[1].kind == nnkEmpty:
|
||||||
error("type required for capture", arg)
|
error("type required for capture", arg)
|
||||||
var letDef = newNimNode(nnkIdentDefs, arg)
|
var varDef = newNimNode(nnkIdentDefs, arg)
|
||||||
arg.copyChildrenTo letDef
|
arg.copyChildrenTo varDef
|
||||||
letDef[2] = newCall("preserveTo",
|
varSection.add(varDef)
|
||||||
newNimNode(nnkBracketExpr).add(recSym, newLit(pred i)),
|
var conversion = newCall("fromPreserve", varDef[0],
|
||||||
letDef[1])
|
newNimNode(nnkBracketExpr).add(recSym, newLit(pred i)))
|
||||||
letSection.add(letDef)
|
if conditional.isNil:
|
||||||
|
conditional = conversion
|
||||||
|
else:
|
||||||
|
conditional = infix(conditional, "and", conversion)
|
||||||
inc(argCount)
|
inc(argCount)
|
||||||
let
|
var scriptBody = newStmtList()
|
||||||
|
if argCount > 0:
|
||||||
|
scriptBody.add(
|
||||||
|
varSection,
|
||||||
|
newNimNode(nnkIfStmt).add(
|
||||||
|
newNimNode(nnkElifBranch).add(
|
||||||
|
conditional, handler[6])))
|
||||||
|
else:
|
||||||
|
scriptBody.add(handler[6])
|
||||||
|
var
|
||||||
scriptSym = genSym(nskProc, "script")
|
scriptSym = genSym(nskProc, "script")
|
||||||
scriptBody = newStmtList(letSection, handler[6])
|
|
||||||
handlerSym = genSym(nskProc, "handler")
|
handlerSym = genSym(nskProc, "handler")
|
||||||
litArgCount = newLit argCount
|
litArgCount = newLit argCount
|
||||||
quote do:
|
quote do:
|
||||||
|
@ -85,7 +99,7 @@ proc wrapDoHandler(pattern, handler: NimNode): NimNode =
|
||||||
# this should be a compile-time check
|
# this should be a compile-time check
|
||||||
assert(
|
assert(
|
||||||
`litArgCount` == len(`recSym`),
|
`litArgCount` == len(`recSym`),
|
||||||
"cannot unpack " & $`litArgCount` & " bindings from " & $(%`recSym`))
|
"cannot unpack " & $`litArgCount` & " bindings from " & $(toPreserve `recSym`))
|
||||||
proc `scriptSym`(`scriptFacetSym`: Facet) =
|
proc `scriptSym`(`scriptFacetSym`: Facet) =
|
||||||
proc getCurrentFacet(): Facet {.inject, used.} = `scriptFacetSym`
|
proc getCurrentFacet(): Facet {.inject, used.} = `scriptFacetSym`
|
||||||
`scriptBody`
|
`scriptBody`
|
||||||
|
@ -114,7 +128,7 @@ proc onEvent(event: EventKind, pattern, handler: NimNode): NimNode =
|
||||||
proc getCurrentFacet(): Facet {.inject, used.} = facet
|
proc getCurrentFacet(): Facet {.inject, used.} = facet
|
||||||
`handler`
|
`handler`
|
||||||
let a = `pattern`
|
let a = `pattern`
|
||||||
result.assertion = Observe % a
|
result.assertion = Observe.init(a)
|
||||||
result.analysis = some(analyzeAssertion(a))
|
result.analysis = some(analyzeAssertion(a))
|
||||||
result.callback = wrap(facet, EventKind(`event`), `handlerSym`)
|
result.callback = wrap(facet, EventKind(`event`), `handlerSym`)
|
||||||
|
|
||||||
|
|
|
@ -553,7 +553,8 @@ template declareField*(facet: Facet; F: untyped; T: typedesc; initial: T): untyp
|
||||||
facet.fields[fieldOff] = x
|
facet.fields[fieldOff] = x
|
||||||
proc get(f: DistinctField): T {.used.} =
|
proc get(f: DistinctField): T {.used.} =
|
||||||
facet.actor.dataspace.dataflow.recordObservation(f.id)
|
facet.actor.dataspace.dataflow.recordObservation(f.id)
|
||||||
fromPreserve[T](result, facet.fields[fieldOff])
|
if not fromPreserve[T](result, facet.fields[fieldOff]):
|
||||||
|
raise newException(ValueError, "cannot convert field " & $F & " to " & $T)
|
||||||
proc getPreserve(f: DistinctField): Preserve {.used.} =
|
proc getPreserve(f: DistinctField): Preserve {.used.} =
|
||||||
facet.actor.dataspace.dataflow.recordObservation(f.id)
|
facet.actor.dataspace.dataflow.recordObservation(f.id)
|
||||||
facet.fields[fieldOff]
|
facet.fields[fieldOff]
|
||||||
|
|
|
@ -7,12 +7,12 @@ import syndicate
|
||||||
const TimeLaterThan* = RecordClass(label: symbol"TimeLaterThan", arity: 1)
|
const TimeLaterThan* = RecordClass(label: symbol"TimeLaterThan", arity: 1)
|
||||||
|
|
||||||
proc toPreserveHook*(time: Monotime): Preserve =
|
proc toPreserveHook*(time: Monotime): Preserve =
|
||||||
%time.ticks
|
time.ticks.toPreserve
|
||||||
|
|
||||||
proc fromPreserveHook*(result: var Monotime; p: Preserve) =
|
proc fromPreserveHook*(mt: var Monotime; p: Preserve): bool =
|
||||||
if p.kind != pkSignedInteger:
|
if p.kind == pkSignedInteger:
|
||||||
raise newException(ValueError, "not a preserved time: " & $p)
|
mt = cast[MonoTime]((p.int.int64,))
|
||||||
result = cast[MonoTime]((p.int.int64,))
|
result = true
|
||||||
|
|
||||||
syndicate timerDriver:
|
syndicate timerDriver:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue