]> gitweb.factorcode.org Git - factor.git/commitdiff
specialized-arrays, specialized-vectors: fix some code duplication and prettyprinting
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Thu, 10 Sep 2009 19:46:26 +0000 (14:46 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Thu, 10 Sep 2009 19:46:26 +0000 (14:46 -0500)
basis/specialized-arrays/specialized-arrays-tests.factor
basis/specialized-arrays/specialized-arrays.factor
basis/specialized-vectors/specialized-vectors.factor

index 3290eccd2f8e3fb06d95236d105a071ef5f4e09f..ebc21eec5675e8e7b2c45838565a09645b292aee 100755 (executable)
@@ -1,9 +1,10 @@
 IN: specialized-arrays.tests
 USING: tools.test alien.syntax specialized-arrays
-specialized-arrays sequences alien.c-types accessors
-kernel arrays combinators compiler classes.struct
+specialized-arrays.private sequences alien.c-types accessors
+kernel arrays combinators compiler compiler.units classes.struct
 combinators.smart compiler.tree.debugger math libc destructors
-sequences.private ;
+sequences.private multiline eval words vocabs namespaces
+assocs prettyprint ;
 
 SPECIALIZED-ARRAY: int
 SPECIALIZED-ARRAY: bool
@@ -106,3 +107,43 @@ SPECIALIZED-ARRAY: fixed-string
 [ { ALIEN: 123 ALIEN: 223 ALIEN: 323 ALIEN: 423 } ] [
     ALIEN: 123 4 <direct-fixed-string-array> [ (underlying)>> ] { } map-as
 ] unit-test
+
+! Ensure that byte-length works with direct arrays
+[ 400 ] [
+    ALIEN: 123 100 <direct-int-array> byte-length
+] unit-test
+
+! Test prettyprinting
+[ "int-array{ 1 2 3 }" ] [ int-array{ 1 2 3 } unparse ] unit-test
+[ "int-array@ f 100" ] [ f 100 <direct-int-array> unparse ] unit-test
+
+! If the C type doesn't exist, don't generate a vocab
+[ ] [
+    [ "__does_not_exist__" specialized-array-vocab forget-vocab ] with-compilation-unit
+    "__does_not_exist__" c-types get delete-at
+] unit-test
+
+[
+    <"
+IN: specialized-arrays.tests
+USING: specialized-arrays ;
+
+SPECIALIZED-ARRAY: __does_not_exist__ "> eval( -- )
+] must-fail
+
+[ ] [
+    <"
+IN: specialized-arrays.tests
+USING: classes.struct specialized-arrays ;
+
+STRUCT: __does_not_exist__ { x int } ;
+
+SPECIALIZED-ARRAY: __does_not_exist__
+"> eval( -- )
+] unit-test
+
+[ f ] [
+    "__does_not_exist__-array{"
+    "__does_not_exist__" specialized-array-vocab lookup
+    deferred?
+] unit-test
index 3a1ce48e683fc6494e2bc93b7a76b8cce1d0518c..15245cc71016c7fe1d38abd771bc18e869648117 100755 (executable)
@@ -1,10 +1,10 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types assocs byte-arrays classes
-compiler.units functors io kernel lexer libc math
-math.vectors.specialization namespaces parser
-prettyprint.custom sequences sequences.private strings summary
-vocabs vocabs.loader vocabs.parser words ;
+compiler.units functors kernel lexer libc math
+math.vectors.specialization namespaces parser prettyprint.custom
+sequences sequences.private strings summary vocabs vocabs.loader
+vocabs.parser words fry combinators ;
 IN: specialized-arrays
 
 MIXIN: specialized-array
@@ -86,8 +86,12 @@ M: A resize
     ] [ drop ] 2bi
     <direct-A> ; inline
 
-M: A byte-length underlying>> length ; inline
+M: A byte-length length T heap-size * ; inline
+
+M: A direct-array-syntax drop \ A@ ;
+
 M: A pprint-delims drop \ A{ \ } ;
+
 M: A >pprint-sequence ;
 
 SYNTAX: A{ \ } [ >A ] parse-literal ;
@@ -100,34 +104,30 @@ A T c-type-boxed-class f specialize-vector-words
 ;FUNCTOR
 
 : underlying-type ( c-type -- c-type' )
-    dup c-types get at string? [
-        c-types get at underlying-type
-    ] when ;
+    dup c-types get at {
+        { [ dup not ] [ drop no-c-type ] }
+        { [ dup string? ] [ nip underlying-type ] }
+        [ drop ]
+    } cond ;
 
 : specialized-array-vocab ( c-type -- vocab )
     "specialized-arrays.instances." prepend ;
 
-: defining-array-message ( type -- )
-    "quiet" get [ drop ] [
-        "Generating specialized " " arrays..." surround print
-    ] if ;
-
 PRIVATE>
 
-: define-array-vocab ( type  -- vocab )
-    underlying-type
-    dup specialized-array-vocab vocab
-    [ ] [
-        [ defining-array-message ]
+: generate-vocab ( vocab-name quot -- vocab )
+    [ dup vocab [ ] ] dip '[
         [
             [
-                dup specialized-array-vocab
-                [ define-array ] with-current-vocab
+                 _ with-current-vocab
             ] with-compilation-unit
-        ]
-        [ specialized-array-vocab ]
-        tri
-    ] ?if ;
+        ] keep
+    ] ?if ; inline
+
+: define-array-vocab ( type -- vocab )
+    underlying-type
+    [ specialized-array-vocab ] [ '[ _ define-array ] ] bi
+    generate-vocab ;
 
 M: string require-c-array define-array-vocab drop ;
 
index 19f32a7fdbf0df27fd303dc610f3ee0be76e6804..dbadd7a74a5f945e439d295ae01f349360970d57 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2008, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien.c-types assocs compiler.units functors
-growable io kernel lexer namespaces parser prettyprint.custom
+growable kernel lexer namespaces parser prettyprint.custom
 sequences specialized-arrays specialized-arrays.private strings
 vocabs vocabs.parser ;
 QUALIFIED: vectors.functor
@@ -44,27 +44,12 @@ INSTANCE: V S
 : specialized-vector-vocab ( type -- vocab )
     "specialized-vectors.instances." prepend ;
 
-: defining-vector-message ( type -- )
-    "quiet" get [ drop ] [
-        "Generating specialized " " vectors..." surround print
-    ] if ;
-
 PRIVATE>
 
-: define-vector-vocab ( type  -- vocab )
+: define-vector-vocab ( type -- vocab )
     underlying-type
-    dup specialized-vector-vocab vocab
-    [ ] [
-        [ defining-vector-message ]
-        [
-            [
-                dup specialized-vector-vocab
-                [ define-vector ] with-current-vocab
-            ] with-compilation-unit
-        ]
-        [ specialized-vector-vocab ]
-        tri
-    ] ?if ;
+    [ specialized-vector-vocab ] [ '[ _ define-vector ] ] bi
+    generate-vocab ;
 
 SYNTAX: SPECIALIZED-VECTOR:
     scan