]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/variants/variants-tests.factor
Allow variants to be spread across multiple parsing words with a VARIANT-MEMBER:...
[factor.git] / extra / variants / variants-tests.factor
index ef48b36b9c7afa51f4fac84bd670e4d8092b3e04..f49cda6a993c3af5243fb220558980b18f12603b 100644 (file)
@@ -19,3 +19,21 @@ VARIANT: list
 
 [ 4 ]
 [ 5 6 7 8 nil <cons> <cons> <cons> <cons> list-length ] unit-test
+
+
+VARIANT: list2 ;
+VARIANT-MEMBER: list2 nil2
+VARIANT-MEMBER: list2 cons2: { { first object } { rest list2 } }
+
+[ t ] [ nil2 list2? ] unit-test
+[ t ] [ 1 nil2 <cons2> list2? ] unit-test
+[ f ] [ 1 list2? ] unit-test
+
+: list2-length ( list2 -- length )
+    {
+        { nil2  [ 0 ] }
+        { cons2 [ nip list2-length 1 + ] }
+    } match ;
+
+[ 4 ]
+[ 5 6 7 8 nil2 <cons2> <cons2> <cons2> <cons2> list2-length ] unit-test