From 81696a90b46781158925e048329669ca98a4c436 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 28 May 2024 21:09:40 +0200 Subject: [PATCH] Add syntax for sync. Closes #6 --- packages/compiler/src/compiler/codegen.ts | 2 ++ packages/compiler/src/compiler/grammar.ts | 13 +++++++++++++ packages/compiler/test/compiler.test.ts | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/compiler/src/compiler/codegen.ts b/packages/compiler/src/compiler/codegen.ts index de78c9e..5f71eaf 100644 --- a/packages/compiler/src/compiler/codegen.ts +++ b/packages/compiler/src/compiler/codegen.ts @@ -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; } diff --git a/packages/compiler/src/compiler/grammar.ts b/packages/compiler/src/compiler/grammar.ts index ae623a6..fa045bc 100644 --- a/packages/compiler/src/compiler/grammar.ts +++ b/packages/compiler/src/compiler/grammar.ts @@ -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 = + 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 = scope(o => { diff --git a/packages/compiler/test/compiler.test.ts b/packages/compiler/test/compiler.test.ts index b77e73c..8db0cef 100644 --- a/packages/compiler/test/compiler.test.ts +++ b/packages/compiler/test/compiler.test.ts @@ -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; }`, `