]> gitweb.factorcode.org Git - factor.git/commitdiff
Implement sequence matching in extra/match.
authorMatthew Willis <matthew.willis@mac.com>
Wed, 12 Mar 2008 09:11:03 +0000 (02:11 -0700)
committerMatthew Willis <matthew.willis@mac.com>
Wed, 12 Mar 2008 09:11:03 +0000 (02:11 -0700)
extra/match/match.factor

index 722c330a328fad2a3e7f280076ea11ba9e88b8de..36af5c990a0417b8adc9a1f11f68d62328acb11e 100755 (executable)
@@ -65,3 +65,26 @@ MACRO: match-cond ( assoc -- )
     -rot
     match [ "Pattern does not match" throw ] unless*
     [ replace-patterns ] bind ;
+
+: ?1-tail ( seq -- tail/f )
+    dup length zero? not [ 1 tail ] [ drop f ] if ;
+
+: (match-first) ( seq pattern-seq -- bindings leftover/f )
+    2dup [ length ] 2apply < [ 2drop f f ]
+    [
+        2dup length head over match
+        [ nip swap ?1-tail ] [ >r 1 tail r> (match-first) ] if*
+    ] if ;
+    
+: match-first ( seq pattern-seq -- bindings )
+    (match-first) drop ;
+
+: (match-all) ( seq pattern-seq -- )
+    tuck (match-first) swap 
+    [ 
+        , [ swap (match-all) ] [ drop ] if* 
+    ] [ 2drop ] if* ;
+
+: match-all ( seq pattern-seq -- bindings-seq )
+    [ (match-all) ] { } make ;
+