Add syntax for sync. Closes #6

This commit is contained in:
Tony Garnock-Jones 2024-05-28 21:09:40 +02:00
parent d14ddc39f7
commit 81696a90b4
3 changed files with 30 additions and 0 deletions

View File

@ -328,6 +328,8 @@ ${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')}
x(ctx.parser.stopStatement, (s, t) => t`${terminalWrap(t, s.facetToStop, s.body)};`);
xf(ctx.parser.syncStatement, (s, t) => t`_sync(${walk(s.peerToSyncWith)}, __SYNDICATE__.Turn.ref({ message() {${walk(s.body)}} }));`);
return tree;
}

View File

@ -57,6 +57,10 @@ export interface StopStatement extends StatementTurnAction {
facetToStop: FacetToStop;
}
export interface SyncStatement extends StatementTurnAction {
peerToSyncWith: Expr;
}
export interface GenericEventEndpointStatement extends StatementTurnAction {
facetToStop: FacetToStop | 'none' | 'once-wrapper';
once: boolean;
@ -385,6 +389,15 @@ export class SyndicateParser {
alt(this.block(o.body), this.statementBoundary));
});
// Principal: Turn
readonly syncStatement: Pattern<SyncStatement> =
this.turnAction(o => {
o.body = [];
return seq(atom('sync'),
map(this.expr1(), e => o.peerToSyncWith = e),
this.block(o.body));
});
// Principal: none
readonly atStatement: Pattern<AtStatement> =
scope(o => {

View File

@ -46,6 +46,21 @@ __SYNDICATE__.Turn.active.facet(() => {
});
describe('sync', () => {
it('empty body', () => expectCodeEqual(`sync p {}`, `
__SYNDICATE__.Turn.active._sync(p, __SYNDICATE__.Turn.ref({
message() {}
}));`));
it('nonempty body', () => expectCodeEqual(`sync p {q(); r();}`, `
__SYNDICATE__.Turn.active._sync(p, __SYNDICATE__.Turn.ref({
message() {
q();
r();
}
}));`));
});
describe('spawn', () => {
it('without name', () => expectCodeEqual(`spawn { a; b; c; }`, `