2021-09-01 11:44:28 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
|
|
# SPDX-License-Identifier: Unlicense
|
2021-06-29 15:18:09 +00:00
|
|
|
|
2022-04-23 21:05:48 +00:00
|
|
|
## This module implements the `Syndicate DSL <https://syndicate-lang.org/doc/syndicate/>`_.
|
|
|
|
|
|
|
|
runnableExamples:
|
|
|
|
from syndicate/protocols/simpleChatProtocol import Present, Says
|
|
|
|
import std/asyncdispatch
|
|
|
|
|
|
|
|
bootDataspace("example") do (ds: Ref; turn: var Turn):
|
|
|
|
let
|
|
|
|
me = "user"
|
|
|
|
presenceHandle = publish(turn, ds, Present(username: me))
|
|
|
|
|
|
|
|
onMessage(turn, ds, ?Says) do (who: string; what: string):
|
|
|
|
echo who, ": ", what
|
|
|
|
retract(turn, presenceHandle)
|
|
|
|
|
|
|
|
during(turn, ds, ?Present) do (username: string):
|
|
|
|
echo "[", username, " arrived]"
|
|
|
|
message(turn, ds, Says(who: me, what: "users are losers"))
|
|
|
|
do:
|
|
|
|
echo "[", username, "departed]"
|
|
|
|
|
|
|
|
poll()
|
|
|
|
|
2022-04-24 01:01:30 +00:00
|
|
|
import std/[macros, tables, typetraits]
|
|
|
|
import preserves
|
2022-03-12 16:35:03 +00:00
|
|
|
import ./syndicate/[actors, dataspaces, durings, patterns]
|
2022-08-29 19:47:12 +00:00
|
|
|
import ./syndicate/protocols/dataspace
|
2022-03-12 16:35:03 +00:00
|
|
|
|
|
|
|
from ./syndicate/relays import connectStdio, connectUnix
|
|
|
|
|
2022-06-16 03:27:25 +00:00
|
|
|
export Actor, Assertion, Facet, Handle, Ref, Symbol, Turn, TurnAction,
|
2022-08-29 19:47:12 +00:00
|
|
|
`$`, `?`, analyse, bootDataspace, connectStdio, connectUnix, drop, facet,
|
2022-06-16 03:27:25 +00:00
|
|
|
future, grab, message, newDataspace, publish, retract, replace, run, stop, unembed
|
2022-04-24 01:01:30 +00:00
|
|
|
|
2022-03-12 16:08:22 +00:00
|
|
|
type
|
2022-08-29 19:47:12 +00:00
|
|
|
Observe* = dataspace.Observe[Ref]
|
2022-03-12 16:08:22 +00:00
|
|
|
PublishProc = proc (turn: var Turn; v: Assertion; h: Handle) {.closure.}
|
|
|
|
RetractProc = proc (turn: var Turn; h: Handle) {.closure.}
|
|
|
|
MessageProc = proc (turn: var Turn; v: Assertion) {.closure.}
|
|
|
|
ClosureEntity = ref object of Entity
|
|
|
|
publishImpl: PublishProc
|
|
|
|
retractImpl: RetractProc
|
|
|
|
messageImpl: MessageProc
|
|
|
|
|
|
|
|
method publish(e: ClosureEntity; turn: var Turn; v: Assertion; h: Handle) =
|
|
|
|
if not e.publishImpl.isNil: e.publishImpl(turn, v, h)
|
|
|
|
|
|
|
|
method retract(e: ClosureEntity; turn: var Turn; h: Handle) =
|
|
|
|
if not e.retractImpl.isNil: e.retractImpl(turn, h)
|
|
|
|
|
|
|
|
method message(e: ClosureEntity; turn: var Turn; v: Assertion) =
|
|
|
|
if not e.messageImpl.isNil: e.messageImpl(turn, v)
|
2021-11-03 18:21:52 +00:00
|
|
|
|
2022-08-29 14:09:07 +00:00
|
|
|
proc argumentCount(handler: NimNode): int =
|
|
|
|
handler.expectKind {nnkDo, nnkStmtList}
|
|
|
|
if handler.kind == nnkDo: result = pred handler[3].len
|
|
|
|
|
2021-10-29 14:55:49 +00:00
|
|
|
proc wrapPublishHandler(handler: NimNode): NimNode =
|
2022-06-12 19:09:34 +00:00
|
|
|
handler.expectKind {nnkDo, nnkStmtList}
|
2021-10-29 14:55:49 +00:00
|
|
|
var innerProc = newNimNode(nnkProcDef)
|
|
|
|
handler.copyChildrenTo innerProc
|
|
|
|
innerProc[0] = genSym(nskProc, "message")
|
2021-08-27 20:03:02 +00:00
|
|
|
var
|
2021-10-29 14:55:49 +00:00
|
|
|
valuesSym = genSym(nskVar, "values")
|
|
|
|
valuesTuple = newNimNode(nnkTupleTy, handler)
|
|
|
|
innerTuple = newNimNode(nnkVarTuple, handler)
|
|
|
|
varSectionInner = newNimNode(nnkVarSection, handler).add(innerTuple)
|
2022-06-12 19:09:34 +00:00
|
|
|
if handler.kind == nnkDo:
|
|
|
|
for i, arg in handler[3]:
|
|
|
|
if i > 0:
|
|
|
|
arg.expectKind nnkIdentDefs
|
|
|
|
if arg[1].kind == nnkEmpty:
|
|
|
|
error("type required for capture", arg)
|
|
|
|
var def = newNimNode(nnkIdentDefs, arg)
|
|
|
|
arg.copyChildrenTo def
|
|
|
|
valuesTuple.add(def)
|
|
|
|
innerTuple.add(arg[0])
|
2021-10-29 14:55:49 +00:00
|
|
|
innerTuple.add(newEmptyNode(), valuesSym)
|
2021-08-27 20:03:02 +00:00
|
|
|
var
|
2021-10-29 14:55:49 +00:00
|
|
|
varSectionOuter = newNimNode(nnkVarSection, handler).add(
|
|
|
|
newIdentDefs(valuesSym, valuesTuple))
|
2022-06-12 19:09:34 +00:00
|
|
|
publishBody =
|
|
|
|
if handler.kind == nnkStmtList: handler
|
|
|
|
else: newStmtList(varSectionInner, handler[6])
|
2021-10-29 14:55:49 +00:00
|
|
|
turnSym = ident"turn"
|
|
|
|
handleSym = ident"handle"
|
|
|
|
handlerSym = genSym(nskProc, "publish")
|
2021-07-08 09:50:13 +00:00
|
|
|
quote do:
|
2022-03-12 16:08:22 +00:00
|
|
|
proc `handlerSym`(`turnSym`: var Turn; bindings: Assertion; `handleSym`: Handle) =
|
2021-10-29 14:55:49 +00:00
|
|
|
`varSectionOuter`
|
|
|
|
if fromPreserve(`valuesSym`, bindings):
|
2021-11-02 12:02:20 +00:00
|
|
|
`publishBody`
|
2021-07-08 09:50:13 +00:00
|
|
|
|
2021-10-29 14:55:49 +00:00
|
|
|
proc wrapMessageHandler(handler: NimNode): NimNode =
|
2022-06-12 19:09:34 +00:00
|
|
|
handler.expectKind {nnkDo, nnkStmtList}
|
2021-10-29 14:55:49 +00:00
|
|
|
var innerProc = newNimNode(nnkProcDef)
|
|
|
|
handler.copyChildrenTo innerProc
|
|
|
|
innerProc[0] = genSym(nskProc, "message")
|
|
|
|
var
|
|
|
|
valuesSym = genSym(nskVar, "values")
|
|
|
|
valuesTuple = newNimNode(nnkTupleTy, handler)
|
|
|
|
innerTuple = newNimNode(nnkVarTuple, handler)
|
|
|
|
varSectionInner = newNimNode(nnkVarSection, handler).add(innerTuple)
|
2022-06-12 19:09:34 +00:00
|
|
|
if handler.kind == nnkDo:
|
|
|
|
for i, arg in handler[3]:
|
|
|
|
if i > 0:
|
|
|
|
arg.expectKind nnkIdentDefs
|
|
|
|
if arg[1].kind == nnkEmpty:
|
|
|
|
error("type required for capture", arg)
|
|
|
|
var def = newNimNode(nnkIdentDefs, arg)
|
|
|
|
arg.copyChildrenTo def
|
|
|
|
valuesTuple.add(def)
|
|
|
|
innerTuple.add(arg[0])
|
2021-10-29 14:55:49 +00:00
|
|
|
innerTuple.add(newEmptyNode(), valuesSym)
|
|
|
|
var
|
|
|
|
varSectionOuter = newNimNode(nnkVarSection, handler).add(
|
|
|
|
newIdentDefs(valuesSym, valuesTuple))
|
|
|
|
body = newStmtList(varSectionInner, handler[6])
|
|
|
|
turnSym = ident"turn"
|
|
|
|
handlerSym = genSym(nskProc, "message")
|
|
|
|
quote do:
|
2022-03-12 16:08:22 +00:00
|
|
|
proc `handlerSym`(`turnSym`: var Turn; bindings: Assertion) =
|
2021-10-29 14:55:49 +00:00
|
|
|
`varSectionOuter`
|
|
|
|
if fromPreserve(`valuesSym`, bindings):
|
|
|
|
`body`
|
2021-07-08 09:50:13 +00:00
|
|
|
|
2022-04-23 23:31:34 +00:00
|
|
|
macro onPublish*(turn: Turn; ds: Ref; pattern: Pattern; handler: untyped) =
|
|
|
|
## Call `handler` when an assertion matching `pattern` is published at `ds`.
|
2021-07-08 09:50:13 +00:00
|
|
|
let
|
2022-08-29 14:09:07 +00:00
|
|
|
argCount = argumentCount(handler)
|
2022-04-23 23:31:34 +00:00
|
|
|
handlerProc = wrapPublishHandler(handler)
|
2021-10-29 14:55:49 +00:00
|
|
|
handlerSym = handlerProc[0]
|
2021-06-29 15:18:09 +00:00
|
|
|
result = quote do:
|
2022-08-29 14:09:07 +00:00
|
|
|
doAssert `pattern`.analyse.capturePaths.len == `argCount`, "mismatch between pattern capture and handler arguments"
|
2021-10-29 14:55:49 +00:00
|
|
|
`handlerProc`
|
2022-03-12 16:08:22 +00:00
|
|
|
discard observe(`turn`, `ds`, `pattern`, ClosureEntity(publishImpl: `handlerSym`))
|
2021-06-29 15:18:09 +00:00
|
|
|
|
2022-04-23 23:31:34 +00:00
|
|
|
macro onMessage*(turn: Turn; ds: Ref; pattern: Pattern; handler: untyped) =
|
|
|
|
## Call `handler` when an message matching `pattern` is broadcasted at `ds`.
|
2021-10-29 14:55:49 +00:00
|
|
|
let
|
2022-08-29 14:09:07 +00:00
|
|
|
argCount = argumentCount(handler)
|
2022-04-23 23:31:34 +00:00
|
|
|
handlerProc = wrapMessageHandler(handler)
|
2021-10-29 14:55:49 +00:00
|
|
|
handlerSym = handlerProc[0]
|
|
|
|
result = quote do:
|
2022-08-29 14:09:07 +00:00
|
|
|
doAssert `pattern`.analyse.capturePaths.len == `argCount`, "mismatch between pattern capture and handler arguments"
|
2021-10-29 14:55:49 +00:00
|
|
|
`handlerProc`
|
2022-03-12 16:08:22 +00:00
|
|
|
discard observe(`turn`, `ds`, `pattern`, ClosureEntity(messageImpl: `handlerSym`))
|
2022-04-23 21:05:48 +00:00
|
|
|
|
|
|
|
proc wrapDuringHandler(entryBody, exitBody: NimNode): NimNode =
|
2022-06-12 19:09:34 +00:00
|
|
|
entryBody.expectKind {nnkDo, nnkStmtList}
|
2022-04-23 21:05:48 +00:00
|
|
|
var innerProc = newNimNode(nnkProcDef)
|
|
|
|
entryBody.copyChildrenTo innerProc
|
|
|
|
innerProc[0] = genSym(nskProc, "during")
|
|
|
|
var
|
|
|
|
valuesSym = ident("rawValues")
|
|
|
|
valuesTuple = newNimNode(nnkTupleTy, entryBody)
|
|
|
|
innerTuple = newNimNode(nnkVarTuple, entryBody)
|
|
|
|
varSectionInner = newNimNode(nnkVarSection, entryBody).add(innerTuple)
|
2022-06-12 19:09:34 +00:00
|
|
|
if entryBody.kind == nnkDo:
|
|
|
|
for i, arg in entryBody[3]:
|
|
|
|
if i > 0:
|
|
|
|
arg.expectKind nnkIdentDefs
|
|
|
|
if arg[1].kind == nnkEmpty:
|
|
|
|
error("type required for capture", arg)
|
|
|
|
var def = newNimNode(nnkIdentDefs, arg)
|
|
|
|
arg.copyChildrenTo def
|
|
|
|
valuesTuple.add(def)
|
|
|
|
innerTuple.add(arg[0])
|
2022-04-23 21:05:48 +00:00
|
|
|
innerTuple.add(newEmptyNode(), valuesSym)
|
|
|
|
var
|
|
|
|
varSectionOuter = newNimNode(nnkVarSection, entryBody).add(
|
|
|
|
newIdentDefs(valuesSym, valuesTuple))
|
2022-06-12 19:09:34 +00:00
|
|
|
publishBody =
|
|
|
|
if entryBody.kind == nnkStmtList: entryBody
|
|
|
|
else: newStmtList(varSectionInner, entryBody[6])
|
2022-04-23 21:05:48 +00:00
|
|
|
turnSym = ident"turn"
|
|
|
|
bindingsSym = ident"bindings"
|
|
|
|
handleSym = ident"duringHandle"
|
|
|
|
entrySym = genSym(nskProc, "during")
|
|
|
|
duringSym = genSym(nskProc, "during")
|
|
|
|
if exitBody.isNil:
|
|
|
|
quote do:
|
|
|
|
proc `duringSym`(`turnSym`: var Turn; `bindingsSym`: Assertion; `handleSym`: Handle): TurnAction =
|
|
|
|
`varSectionOuter`
|
|
|
|
if fromPreserve(`valuesSym`, `bindingsSym`):
|
|
|
|
`publishBody`
|
|
|
|
else:
|
|
|
|
quote do:
|
|
|
|
proc `duringSym`(`turnSym`: var Turn; `bindingsSym`: Assertion; `handleSym`: Handle): TurnAction =
|
|
|
|
`varSectionOuter`
|
|
|
|
if fromPreserve(`valuesSym`, `bindingsSym`):
|
|
|
|
`publishBody`
|
2022-06-16 17:13:28 +00:00
|
|
|
proc action(`turnSym`: var Turn) =
|
2022-04-23 21:05:48 +00:00
|
|
|
`exitBody`
|
|
|
|
result = action
|
|
|
|
|
2022-06-16 17:13:28 +00:00
|
|
|
macro during*(turn: var Turn; ds: Ref; pattern: Pattern; publishBody, retractBody: untyped) =
|
2022-04-23 21:05:48 +00:00
|
|
|
## Call `publishBody` when an assertion matching `pattern` is published to `ds` and
|
|
|
|
## call `retractBody` on retraction. Assertions that match `pattern` but are not
|
|
|
|
## convertable to the arguments of `publishBody` are silently discarded.
|
|
|
|
##
|
|
|
|
## The following symbols are injected into the scope of both bodies:
|
2022-06-16 17:13:28 +00:00
|
|
|
## - `turn` - active turn at entry of `publishBody` and `retractBody`
|
2022-04-23 21:05:48 +00:00
|
|
|
## - `bindings` - raw Preserves sequence that matched `pattern`
|
|
|
|
## - `duringHandle` - dataspace handle of the assertion that triggered `publishBody`
|
|
|
|
let
|
2022-08-29 14:09:07 +00:00
|
|
|
argCount = argumentCount(publishBody)
|
2022-04-23 21:05:48 +00:00
|
|
|
callbackProc = wrapDuringHandler(publishBody, retractBody)
|
|
|
|
callbackSym = callbackProc[0]
|
|
|
|
result = quote do:
|
2022-08-29 14:09:07 +00:00
|
|
|
doAssert `pattern`.analyse.capturePaths.len == `argCount`, "mismatch between pattern capture and handler arguments"
|
2022-04-23 21:05:48 +00:00
|
|
|
`callbackProc`
|
|
|
|
discard observe(`turn`, `ds`, `pattern`, during(`callbackSym`))
|
|
|
|
|
2022-06-16 17:13:28 +00:00
|
|
|
macro during*(turn: var Turn; ds: Ref; pattern: Pattern; publishBody: untyped) =
|
2022-04-23 21:05:48 +00:00
|
|
|
## Variant of `during` without a retract body.
|
|
|
|
let
|
2022-08-29 14:09:07 +00:00
|
|
|
argCount = argumentCount(publishBody)
|
2022-04-23 21:05:48 +00:00
|
|
|
callbackProc = wrapDuringHandler(publishBody, nil)
|
|
|
|
callbackSym = callbackProc[0]
|
|
|
|
result = quote do:
|
2022-08-29 14:09:07 +00:00
|
|
|
doAssert `pattern`.analyse.capturePaths.len == `argCount`, "mismatch between pattern capture and handler arguments"
|
2022-04-23 21:05:48 +00:00
|
|
|
`callbackProc`
|
|
|
|
discard observe(`turn`, `ds`, `pattern`, during(`callbackSym`))
|