import type { Assertion, Bindings, Handle, Ref, Turn } from "./actor.js"; import { Dictionary, IdentityMap, is, Record, Tuple } from "@preserves/core"; import * as S from './gen/sturdy.js'; export * from './gen/sturdy.js'; export function match(p: S.Pattern, v: Assertion): Bindings | null { let bindings: Bindings = {}; function walk(p: S.Pattern, v: Assertion): boolean { switch (p.label) { case S.$__: return true; case S.$bind: if (walk(S.PBind._.pattern(p), v)) { bindings[S.PBind._.name(p).asPreservesText()] = v; return true; } return false; case S.$and: for (const pp of S.PAnd._.patterns(p)) { if (!walk(pp, v)) return false; } return true; case S.$not: { const savedBindings = bindings; bindings = {}; const result = !walk(S.PNot._.pattern(p), v) bindings = savedBindings; return result; } case S.$lit: return is(S.Lit._.value(p), v); case S.$compound: { const ctor = S.PCompound._.ctor(p); const members = S.PCompound._.members(p); switch (ctor.label) { case S.$rec: if (!Record.isRecord, Ref>(v)) return false; if (!is(S.CRec._.label(ctor), v.label)) return false; if (S.CRec._.arity(ctor) !== v.length) return false; for (const [key, pp] of members) { if (typeof key !== 'number') return false; if (!walk(pp, v[key])) return false; } return true; case S.$arr: if (!Array.isArray(v)) return false; if ('label' in v) return false; if (S.CArr._.arity(ctor) !== v.length) return false; for (const [key, pp] of members) { if (typeof key !== 'number') return false; if (!walk(pp, v[key])) return false; } return true; case S.$dict: if (!Dictionary.isDictionary(v)) return false; for (const [key, pp] of members) { const vv = v.get(key as Assertion); if (vv === void 0) return false; if (!walk(pp, vv)) return false; } return true; } } } } return walk(p, v) ? bindings : null; } export function instantiate(t: S.Template, b: Bindings): Assertion { function walk(t: S.Template): Assertion { switch (t.label) { case S.$ref: { const n = S.TRef._.name(t).asPreservesText() const v = b[n]; if (v === void 0) throw new Error(`Unbound reference: ${n}`); return v; } case S.$lit: return S.Lit._.value(t) as Assertion; case S.$compound: { const ctor = S.TCompound._.ctor(t); const members = S.TCompound._.members(t); switch (ctor.label) { case S.$rec: { const v = Record( S.CRec._.label(ctor) as Assertion, [] as Assertion[], ); v.length = S.CRec._.arity(ctor); for (const [key, tt] of members) { v[key as number] = walk(tt); } return v; } case S.$arr: { const v = []; v.length = S.CArr._.arity(ctor); for (const [key, tt] of members) { v[key as number] = walk(tt); } return v; } case S.$dict: { const v = new Dictionary(); for (const [key, tt] of members) { v.set(key as Assertion, walk(tt)); } return v; } } } } } return walk(t); } export function rewrite(r: S.Rewrite, v: Assertion): Assertion | null { const bindings = match(S.Rewrite._.pattern(r), v); if (bindings === null) return null; return instantiate(S.Rewrite._.template(r), bindings); } export function examineAlternatives(cav: S.Caveat, v: Assertion): Assertion | null { if (cav.label === S.$or) { for (const r of S.Alts._.alternatives(cav)) { const w = rewrite(r, v); if (w !== null) return w; } return null; } else { return rewrite(cav, v); } } export function runRewrites(a: S.Attenuation | undefined, v: Assertion): Assertion | null { if (a !== void 0) { for (const stage of a) { const w = examineAlternatives(stage, v); if (w === null) return null; v = w; } } return v; } const _a = Symbol.for('a'); export function rfilter(... patterns: S.Pattern[]): S.Caveat { const ps = patterns.map(p => S.Rewrite(S.PBind(_a, p), S.TRef(_a))); return ps.length === 1 ? ps[0] : S.Alts(ps); } export function attenuate(ref: Ref, ... a: S.Attenuation): Ref { return { ... ref, attenuation: [... a, ... (ref.attenuation ?? [])] }; } export function forwarder(t: Turn, ref: Ref): { proxy: Ref, revoker: Ref } { let underlying: Ref | null = ref; let handleMap = new IdentityMap(); let proxy = t.ref({ assert(turn: Turn, assertion: Assertion, handle: Handle): void { if (underlying === null) return; handleMap.set(handle, turn.assert(underlying, assertion)); }, retract(turn: Turn, handle: Handle): void { if (underlying === null) return; turn.retract(handleMap.get(handle)); handleMap.delete(handle); }, message(turn: Turn, body: Assertion): void { if (underlying === null) return; turn.message(underlying, body); }, sync(turn: Turn, peer: Ref): void { if (underlying === null) return; turn._sync(underlying, peer); }, }); let revoker = t.ref({ message(turn: Turn, _body: Assertion): void { underlying = null; handleMap.forEach(h => turn.retract(h)); }, }); return { proxy, revoker }; }