summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-01-15 17:30:01 -0800
committerKaz Kylheku <kaz@kylheku.com>2021-01-15 17:30:01 -0800
commiteb11c7d3acb1b37decf10455115440ff31881243 (patch)
treebb66e556f67a344b1080df0bc9efac630b0dc88c
parent84e0e1784a72f3210303b9f42f31122fd648f0e9 (diff)
downloadtxr-eb11c7d3acb1b37decf10455115440ff31881243.tar.gz
txr-eb11c7d3acb1b37decf10455115440ff31881243.tar.bz2
txr-eb11c7d3acb1b37decf10455115440ff31881243.zip
matcher: support @(or pats ..) operator.
* share/txr/stdlib/match.tl (compile-or-match): New function. (compile-match): Route or operator to new function.
-rw-r--r--share/txr/stdlib/match.tl17
1 files changed, 17 insertions, 0 deletions
diff --git a/share/txr/stdlib/match.tl b/share/txr/stdlib/match.tl
index 3af557e6..f86acb09 100644
--- a/share/txr/stdlib/match.tl
+++ b/share/txr/stdlib/match.tl
@@ -204,6 +204,22 @@
var-exprs (unless (eq op 'some)
(mapcar (ret ^(nreverse ,@1)) collect-vars))))))
+(defun compile-or-match (or-pat obj-var)
+ (flet ((submatch-fun (om)
+ ^(let ,om.(get-temps)
+ ,om.(wrap-guards
+ ^(progn ,*om.(assignments)
+ (if ,om.test-expr t))))))
+ (let* ((or-matches (mapcar (op compile-match @1 obj-var) (cdr or-pat)))
+ (guard (new match-guard
+ guard-expr ^(or ,*[mapcar submatch-fun or-matches]))))
+ (new compiled-match
+ pattern or-pat
+ obj-var obj-var
+ guard-chain (list guard)
+ test-expr t
+ vars (uniq (mappend .vars or-matches))))))
+
(defun compile-match (pat : (obj-var (gensym)))
(cond
((consp pat)
@@ -218,6 +234,7 @@
(all (compile-loop-match exp obj-var))
(usr:all* (compile-loop-match exp obj-var))
(some (compile-loop-match exp obj-var))
+ (or (compile-or-match exp obj-var))
(t (compile-predicate-match exp obj-var)))
(compile-error *match-form*
"unrecognized pattern syntax ~s" pat))))