From 88bfb1373bbe73859de94c8b3e83a73b84eaf268 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 25 Feb 2021 11:43:38 +0100 Subject: [PATCH] Tweak matcher --- actor.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/actor.ts b/actor.ts index b8dab66..eadaa9c 100644 --- a/actor.ts +++ b/actor.ts @@ -176,7 +176,7 @@ export class Turn { //--------------------------------------------------------------------------- export function match(p: Pattern, v: Assertion): Bindings | null { - const bindings: Bindings = {}; + let bindings: Bindings = {}; function walk(p: Pattern, v: Assertion): boolean { return fold(p, { @@ -218,12 +218,20 @@ export function match(p: Pattern, v: Assertion): Bindings | null { if ('type' in t) { switch (t.type) { case 'bind': - bindings[t.name] = v; - return walk(t.pattern, v); + if (walk(t.pattern, v)) { + bindings[t.name] = v; + return true; + } + return false; case 'discard': return true; - case 'not': - return !walk(t.pattern, v); + case 'not': { + const savedBindings = bindings; + bindings = {}; + const result = !walk(t.pattern, v) + bindings = savedBindings; + return result; + } case 'and': for (const p of t.patterns) { if (!walk(p, v)) return false;