core: gen_program, using ProgramSpec dict with (cat, stage) as key
This commit is contained in:
parent
49cac25cb7
commit
effd9cf412
1 changed files with 11 additions and 1 deletions
|
|
@ -18,6 +18,7 @@ type Categ = tuple[Symbol, Symbol]
|
|||
type Proc = Callable[[Message, Scope], Awaitable[Scope | None]]
|
||||
type Program = Callable[[Scope], Proc]
|
||||
type Syn = Callable[[Message], Message]
|
||||
type ProgramSpec = dict[tuple[Symbol, Symbol], Proc]
|
||||
|
||||
type Handler = Callable[[Message, Scope],
|
||||
Awaitable[Message | None | tuple[Message | None, Scope | None]]]
|
||||
|
|
@ -29,6 +30,15 @@ async def proc_dummy(msg: Message, scope: Scope) -> None:
|
|||
|
||||
global_mailbox: Mailbox = asyncio.Queue()
|
||||
|
||||
def gen_program(spec: ProgramSpec, default: Proc = proc_dummy) -> Program:
|
||||
def program(scope: Scope) -> Proc:
|
||||
key = (scope.categ[1], scope.stage)
|
||||
proc = spec.get(key, spec.get((key[0], k.default)))
|
||||
if proc is None:
|
||||
proc = spec.get((k.default, key[1]), default)
|
||||
return proc
|
||||
return program
|
||||
|
||||
@dataclass
|
||||
class Scope:
|
||||
|
||||
|
|
@ -36,7 +46,7 @@ class Scope:
|
|||
categ: Categ = (k.csys, k.c00)
|
||||
stage: Symbol = k.initial
|
||||
proc: Proc = field(init=False)
|
||||
program: Program = lambda x: proc_dummy
|
||||
program: Program = gen_program({})
|
||||
syns: list[Syn] = field(default_factory=list)
|
||||
environ: Mailbox = global_mailbox
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue