From c8d581648431d6df4cc1db9e87a1ad1306aeddb8 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 8 Feb 2021 07:06:30 -0800 Subject: matcher: fix backreferencing in predicate. * share/txr/stdlib/match.tl (compile-predicate-match): Always allocate res-var as a gensym; do not use resvar. Otherwise we will freshly bind resvar as a local, failing to back-reference. * tests/011/patmatch.tl: Add test cases, the second of which fails before this change. --- share/txr/stdlib/match.tl | 2 +- tests/011/patmatch.tl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/share/txr/stdlib/match.tl b/share/txr/stdlib/match.tl index 0aea24b2..a1222c5a 100644 --- a/share/txr/stdlib/match.tl +++ b/share/txr/stdlib/match.tl @@ -288,7 +288,7 @@ (let ((sym (cadr vm))) (set args (append (ldiff args vm) sym)) sym)))) - (res-var (if rvar rvar (gensym "res-"))) + (res-var (gensym "res-")) (arg-var (if avar avar (gensym "obj-")))) (unless avar (set args (append args (list arg-var)))) diff --git a/tests/011/patmatch.tl b/tests/011/patmatch.tl index a616b9d2..f49b115d 100644 --- a/tests/011/patmatch.tl +++ b/tests/011/patmatch.tl @@ -94,6 +94,12 @@ (t (1 2 3))) (test (when-match @(@x (< . @sym)) '(3 2 1) (list x sym)) nil) +(test (let ((x t)) + (when-match @(@x (< . @sym)) '(1 2 3) (list x sym))) + (t (1 2 3))) +(test (let ((x nil)) + (when-match @(@x (< . @sym)) '(1 2 3) (list x sym))) + nil) (test (if-match (@(or @a) @a) '(1 2) a :no) :no) (test (if-match (@(and @a) @a) '(1 2) a :no) :no) -- cgit v1.2.3