From 947b0979d05b190fee65698864cd1e14b5797f66 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 20 Jan 2021 22:59:02 -0800 Subject: matcher: allow variables to back-reference. Multiple occurrences of variables unify using equal. * share/txr/stdlib/match.tl (var-list): New struct type. Used for tracking what variables have been defined. (compile-struct-match, compile-vec-match, compile-atom-match, compile-op-match, compile-cons-structure, compile-require-match, compile-let-match, compile-loop-match, compile-parallel-match, compile-not-match): Take var-match argument and pass it down. (compile-parallel-match): Take var-match argument and pass copies of it down to the compile jobs of the branches, since they do not unify. (compile-var-match, comiple-let-match, compile-op-match): Handle variables carefully: check for variable already being defined and generate a backreference instead of a new binding match. (compile-match): Take optional var-list argument, instantiating the object if it is missing, pass down to all recursive compile unctions. * txr.1: Documented. --- txr.1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'txr.1') diff --git a/txr.1 b/txr.1 index 860492e2..e4e224b9 100644 --- a/txr.1 +++ b/txr.1 @@ -39646,6 +39646,29 @@ notation. The reason is that a struct literal produces an object which loses information about how it was specified in the literal syntax, but those details are critically important in pattern matching. +A pattern can contain multiple occurrences of the same variable. +Except in the case when these variables occur in different branches +of an +.code @(or) +or +.code @(and) +pattern operator, those repeated variables denote one variable. +The left-most, or in the case of +.code @(let) +nesting, outermost, occurrence of the variable binds to the corresponding +element of the object. The remaining occurrences of the variable must +correspond to objects which are +.code equal +to that object, or else there is no match. +For instance, the pattern +.code "(@a @a)" +matches a list like +.code "(1 1)" +and binds a to the leftmost +.codn 1 , +and fails to match a list like +.codn "(1 2)" . + The pattern-matching notation is documented in the following sections; sections describing the pattern matching macros follow. -- cgit v1.2.3