summaryrefslogtreecommitdiffstats
path: root/txr.1
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-02-05 21:51:04 -0800
committerKaz Kylheku <kaz@kylheku.com>2021-02-05 21:51:04 -0800
commita9870a810fda3bd415f4250188f0af17e53fe759 (patch)
tree9e56bd688f2f77c6e7f758a97f891ec47dd69d3a /txr.1
parent05f4a2d0d33c5e2cdc0569775cf8218824c7078e (diff)
downloadtxr-a9870a810fda3bd415f4250188f0af17e53fe759.tar.gz
txr-a9870a810fda3bd415f4250188f0af17e53fe759.tar.bz2
txr-a9870a810fda3bd415f4250188f0af17e53fe759.zip
matcher: back-reference Lisp variables.
* share/txr/stdlib/match.tl (struct var-list): New slot, menv. (var-list exists): Method now falls back on lexical scope and dynamic variables. (get-var-list): New function. (when-match, if-match, match-case, when-exprs-match): Capture macro environment and use get-vars-list to convert to a vars object which carries it as the menv slot. With this, the compiler framework has access to the lexical environment. * tests/011/patmatch.tl: Test cases of back-referencing with Lisp lexicals. * txr.1: Documented.
Diffstat (limited to 'txr.1')
-rw-r--r--txr.125
1 files changed, 25 insertions, 0 deletions
diff --git a/txr.1 b/txr.1
index 74359b7c..21c2fd2e 100644
--- a/txr.1
+++ b/txr.1
@@ -39818,6 +39818,31 @@ and binds a to the leftmost
and fails to match a list like
.codn "(1 2)" .
+Pattern variables exist in the same namespace as Lisp variables,
+and are fully integrated in it. Patterns not only bind variables,
+but have visibility to existing variables in scope, including
+lexical variables and special/global variables. When a variable
+is mentioned in a pattern, if it already has a binding as a Lisp variable, then
+it denotes a reference to that variable in exactly the same way that a pattern
+variable back-references itself in a pattern: the Lisp variable
+is require to compare
+.code equal
+to the corresponding object being examined by the pattern. For instance,
+the following function returns the third element of a list, if the
+first two elements are repetitions of the
+.code x
+argument, otherwise
+.codn nil :
+
+.verb
+ (defun x-x-y (list x)
+ (when-match (@x @x @y) list y))
+
+ (x-x-y '(1 1 2) 1) -> 2
+ (x-x-y '(1 2 3) 1) -> nil ;; no @x @x match
+ (x-x-y '(1 1 2 r2) 1) -> nil ;; list too long
+.brev
+
The pattern-matching notation is documented in the following
sections; sections describing the pattern matching macros follow.