]> gitweb.factorcode.org Git - factor.git/commitdiff
add a new "make-tuple" combinator for cleaving values into tuple slots by name. make...
authorJoe Groff <arcata@gmail.com>
Wed, 22 Jul 2009 17:43:44 +0000 (12:43 -0500)
committerJoe Groff <arcata@gmail.com>
Wed, 22 Jul 2009 17:43:44 +0000 (12:43 -0500)
extra/combinators/tuple/tuple-docs.factor [new file with mode: 0644]
extra/combinators/tuple/tuple.factor [new file with mode: 0644]
extra/gpu/demos/bunny/bunny.factor
extra/gpu/demos/raytrace/raytrace.factor
extra/gpu/render/render.factor

diff --git a/extra/combinators/tuple/tuple-docs.factor b/extra/combinators/tuple/tuple-docs.factor
new file mode 100644 (file)
index 0000000..aedb013
--- /dev/null
@@ -0,0 +1,43 @@
+! (c)2009 Joe Groff bsd license
+USING: assocs classes help.markup help.syntax kernel math
+quotations strings ;
+IN: combinators.tuple
+
+HELP: 2make-tuple
+{ $values
+    { "x" object } { "y" object } { "class" class } { "assoc" assoc }
+    { "tuple" tuple }
+}
+{ $description "Constructs a " { $link tuple } " of " { $snippet "class" } " by calling the quotations making up the values of " { $snippet "assoc" } " on " { $snippet "x" } " and " { $snippet "y" } ", assigning the result of each call to the slot named by the corresponding key. The quotations must have the effect " { $snippet "( x y -- slot-value )" } ". The order in which the quotations is called is undefined." } ;
+
+HELP: 3make-tuple
+{ $values
+    { "x" object } { "y" object } { "z" object } { "class" class } { "assoc" "a list of " { $link string } "/" { $link quotation } " pairs" }
+    { "tuple" tuple }
+}
+{ $description "Constructs a " { $link tuple } " of " { $snippet "class" } " by calling the quotations making up the values of " { $snippet "assoc" } " on " { $snippet "x" } ", " { $snippet "y" } ", and " { $snippet "z" } ", assigning the result of each call to the slot named by the corresponding key. The quotations must have the effect " { $snippet "( x y z -- slot-value )" } ". The order in which the quotations is called is undefined." } ;
+
+HELP: make-tuple
+{ $values
+    { "x" object } { "class" class } { "assoc" "a list of " { $link string } "/" { $link quotation } " pairs" }
+    { "tuple" tuple }
+}
+{ $description "Constructs a " { $link tuple } " of " { $snippet "class" } " by calling the quotations making up the values of " { $snippet "assoc" } " on " { $snippet "x" } ", assigning the result of each call to the slot named by the corresponding key. The quotations must have the effect " { $snippet "( x -- slot-value )" } ". The order in which the quotations is called is undefined." } ;
+
+HELP: nmake-tuple
+{ $values
+    { "class" class } { "assoc" "a list of " { $link string } "/" { $link quotation } " pairs" } { "n" integer }
+}
+{ $description "Constructs a " { $link tuple } " of " { $snippet "class" } " by calling the quotations making up the values of " { $snippet "assoc" } " on the top " { $snippet "n" } " values on the datastack below " { $snippet "class" } ", assigning the result of each call to the slot named by the corresponding key. The order in which the quotations is called is undefined." } ;
+
+{ make-tuple 2make-tuple 3make-tuple nmake-tuple } related-words
+
+ARTICLE: "combinators.tuple" "Tuple-constructing combinators"
+"The " { $vocab-link "combinators.tuple" } " vocabulary provides dataflow combinators that construct " { $link tuple } " objects."
+{ $subsection make-tuple }
+{ $subsection 2make-tuple }
+{ $subsection 3make-tuple }
+{ $subsection nmake-tuple }
+;
+
+ABOUT: "combinators.tuple"
diff --git a/extra/combinators/tuple/tuple.factor b/extra/combinators/tuple/tuple.factor
new file mode 100644 (file)
index 0000000..c4e0ef4
--- /dev/null
@@ -0,0 +1,29 @@
+! (c)2009 Joe Groff bsd license
+USING: accessors assocs classes.tuple generalizations kernel
+locals quotations sequences ;
+IN: combinators.tuple
+
+<PRIVATE
+
+:: (tuple-slot-quot) ( slot assoc n -- quot )
+    slot name>> assoc at [
+        slot initial>> :> initial
+        { n ndrop initial } >quotation
+    ] unless* ;
+
+PRIVATE>
+
+MACRO:: nmake-tuple ( class assoc n -- )
+    class all-slots [ assoc n (tuple-slot-quot) ] map :> quots
+    class <wrapper> :> \class
+    { quots n ncleave \class boa } >quotation ;
+    
+: make-tuple ( x class assoc -- tuple )
+    1 nmake-tuple ; inline
+
+: 2make-tuple ( x y class assoc -- tuple )
+    2 nmake-tuple ; inline
+
+: 3make-tuple ( x y z class assoc -- tuple )
+    3 nmake-tuple ; inline
+
index ea15dc7884846520a36de464b4d5ca003e972eaf..a1b42d9f1294953597885868cf164e7ebe12182a 100755 (executable)
@@ -1,3 +1,4 @@
+! (c)2009 Joe Groff bsd license
 USING: accessors alien.c-types arrays combinators combinators.short-circuit
 game-worlds gpu gpu.buffers gpu.util.wasd gpu.framebuffers gpu.render
 gpu.shaders gpu.state gpu.textures gpu.util grouping http.client images
@@ -229,16 +230,14 @@ BEFORE: bunny-world begin-world
             { depth-attachment 1.0 }
         } clear-framebuffer
     ] [
-        render-set new
-            triangles-mode >>primitive-mode
-            { T{ color-attachment f 0 } T{ color-attachment f 1 } } >>output-attachments
-            swap {
-                [ <bunny-uniforms> >>uniforms ]
-                [ bunny>> vertex-array>> >>vertex-array ]
-                [ bunny>> index-elements>> >>indexes ]
-                [ sobel>> framebuffer>> >>framebuffer ]
-            } cleave
-        render
+        {
+            { "primitive-mode"     [ drop triangles-mode ] }
+            { "output-attachments" [ drop { T{ color-attachment f 0 } T{ color-attachment f 1 } } ] }
+            { "uniforms"           [ <bunny-uniforms> ] }
+            { "vertex-array"       [ bunny>> vertex-array>> ] }
+            { "indexes"            [ bunny>> index-elements>> ] }
+            { "framebuffer"        [ sobel>> framebuffer>> ] }
+        } <render-set> render
     ] bi ;
 
 : <sobel-uniforms> ( sobel -- uniforms )
@@ -250,13 +249,12 @@ BEFORE: bunny-world begin-world
 : draw-sobel ( world -- )
     T{ depth-state { comparison f } } set-gpu-state*
 
-    render-set new
-        triangle-strip-mode >>primitive-mode
-        T{ index-range f 0 4 } >>indexes
-        swap sobel>>
-        [ <sobel-uniforms> >>uniforms ]
-        [ vertex-array>> >>vertex-array ] bi
-    render ;
+    sobel>> {
+        { "primitive-mode" [ drop triangle-strip-mode ] }
+        { "indexes"        [ drop T{ index-range f 0 4 } ] }
+        { "uniforms"       [ <sobel-uniforms> ] }
+        { "vertex-array"   [ vertex-array>> ] }
+    } <render-set> render ;
 
 : draw-sobeled-bunny ( world -- )
     [ draw-bunny ] [ draw-sobel ] bi ;
@@ -264,13 +262,12 @@ BEFORE: bunny-world begin-world
 : draw-loading ( world -- )
     T{ depth-state { comparison f } } set-gpu-state*
 
-    render-set new
-        triangle-strip-mode >>primitive-mode
-        T{ index-range f 0 4 } >>indexes
-        swap loading>>
-        [ { 1.0 -1.0 } swap texture>> loading-uniforms boa >>uniforms ]
-        [ vertex-array>> >>vertex-array ] bi
-    render ;
+    loading>> {
+        { "primitive-mode" [ drop triangle-strip-mode ] }
+        { "indexes"        [ drop T{ index-range f 0 4 } ] }
+        { "uniforms"       [ { 1.0 -1.0 } swap texture>> loading-uniforms boa ] }
+        { "vertex-array"   [ vertex-array>> ] }
+    } <render-set> render ;
 
 M: bunny-world draw-world*
     dup bunny>>
index df323d3c829543884970464cf5598f3435476946..9ac943150d1b5c275614ee1c87958e95a45c77df 100644 (file)
@@ -1,7 +1,7 @@
 ! (c)2009 Joe Groff bsd license
-USING: accessors arrays game-loop game-worlds generalizations
-gpu gpu.render gpu.shaders gpu.util gpu.util.wasd kernel
-literals math math.matrices math.order math.vectors
+USING: accessors arrays combinators.tuple game-loop game-worlds
+generalizations gpu gpu.render gpu.shaders gpu.util gpu.util.wasd
+kernel literals math math.matrices math.order math.vectors
 method-chains sequences ui ui.gadgets ui.gadgets.worlds
 ui.pixel-formats ;
 IN: gpu.demos.raytrace
@@ -97,13 +97,12 @@ AFTER: raytrace-world tick*
     spheres>> [ tick-sphere ] each ;
 
 M: raytrace-world draw-world*
-    render-set new
-        triangle-strip-mode >>primitive-mode
-        T{ index-range f 0 4 } >>indexes
-        swap
-        [ <sphere-uniforms> >>uniforms ]
-        [ vertex-array>> >>vertex-array ] bi
-    render ;
+    {
+        { "primitive-mode" [ drop triangle-strip-mode    ] }
+        { "indexes"        [ drop T{ index-range f 0 4 } ] }
+        { "uniforms"       [ <sphere-uniforms>           ] }
+        { "vertex-array"   [ vertex-array>>              ] }
+    } <render-set> render ;
 
 M: raytrace-world pref-dim* drop { 1024 768 } ;
 M: raytrace-world tick-length drop 1000 30 /i ;
index 65a99f94d7c7f6fa2527aa62663d2a5868fb64e8..feb2f3f76801a53be4ea753667b11372d3829534 100644 (file)
@@ -1,7 +1,7 @@
 ! (c)2009 Joe Groff bsd license
 USING: accessors alien alien.c-types alien.structs arrays
 assocs classes.mixin classes.parser classes.singleton
-classes.tuple classes.tuple.private combinators destructors fry
+classes.tuple classes.tuple.private combinators combinators.tuple destructors fry
 generic generic.parser gpu gpu.buffers gpu.framebuffers
 gpu.framebuffers.private gpu.shaders gpu.state gpu.textures
 gpu.textures.private half-floats images kernel lexer locals
@@ -474,13 +474,22 @@ M: vertex-array dispose
 PRIVATE>
 
 TUPLE: render-set
-    { primitive-mode primitive-mode }
-    { vertex-array vertex-array }
-    { uniforms uniform-tuple }
-    { indexes vertex-indexes initial: T{ index-range } } 
-    { instances ?integer initial: f }
-    { framebuffer any-framebuffer initial: system-framebuffer }
-    { output-attachments sequence initial: { default-attachment } } ;
+    { primitive-mode primitive-mode read-only }
+    { vertex-array vertex-array read-only }
+    { uniforms uniform-tuple read-only }
+    { indexes vertex-indexes initial: T{ index-range } read-only } 
+    { instances ?integer initial: f read-only }
+    { framebuffer any-framebuffer initial: system-framebuffer read-only }
+    { output-attachments sequence initial: { default-attachment } read-only } ;
+
+: <render-set> ( x quot-assoc -- render-set )
+    render-set swap make-tuple ; inline
+
+: 2<render-set> ( x y quot-assoc -- render-set )
+    render-set swap 2make-tuple ; inline
+
+: 3<render-set> ( x y z quot-assoc -- render-set )
+    render-set swap 3make-tuple ; inline
 
 : render ( render-set -- )
     {