]> gitweb.factorcode.org Git - factor.git/commitdiff
Add set-slots{ and set-slots[ words in slots.syntax and document them.
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 28 Sep 2010 19:08:23 +0000 (14:08 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 28 Sep 2010 20:15:56 +0000 (15:15 -0500)
extra/slots/syntax/syntax-docs.factor
extra/slots/syntax/syntax.factor

index 84e6e89dacc670069fe1efa970c0c15f5aaa76d0..6e201eac770e9fc2cfa1f80d30195530cbea50f5 100755 (executable)
@@ -22,11 +22,33 @@ HELP: slots{
            "{ 3 5 }"
 } ;
 
+HELP: set-slots{
+{ $description "Sets slot values in a tuple from an array." }
+{ $example "USING: prettyprint slots.syntax kernel ;"
+           "IN: slots.syntax.example"
+           "TUPLE: rectangle width height ;"
+           "rectangle new { 3 5 } set-slots{ width height } ."
+           "T{ rectangle { width 3 } { height 5 } }"
+} ;
+
+HELP: set-slots[
+{ $description "Sets slot values in a tuple from the stack." }
+{ $example "USING: prettyprint slots.syntax kernel ;"
+           "IN: slots.syntax.example"
+           "TUPLE: rectangle width height ;"
+           "rectangle new 3 5 set-slots[ width height ] ."
+           "T{ rectangle { width 3 } { height 5 } }"
+} ;
+
 ARTICLE: "slots.syntax" "Slots syntax sugar"
-"The " { $vocab-link "slots.syntax" } " vocabulary provides an alternative syntax for taking a sequence of slots from a tuple." $nl
+"The " { $vocab-link "slots.syntax" } " vocabulary provides an alternative syntax for getting and setting multiple values of a tuple." $nl
 "Syntax sugar for cleaving slots to the stack:"
 { $subsections POSTPONE: slots[ }
-"Syntax sugar for cleaving slots to an array:"
-{ $subsections POSTPONE: slots{ } ;
+"Cleaving slots to an array:"
+{ $subsections POSTPONE: slots{ }
+"Setting slots from the stack:"
+{ $subsections POSTPONE: set-slots[ }
+"Setting slots from an array:"
+{ $subsections POSTPONE: set-slots{ } ;
 
 ABOUT: "slots.syntax"
index 7bfe238fa83515dbbc0a1ef263dfa48088abad82..9b647bc5c648af9719fcc2fae694d085a7e4ebf6 100755 (executable)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2010 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: combinators combinators.smart fry lexer quotations
-sequences slots  ;
+sequences slots words ;
 IN: slots.syntax
 
 SYNTAX: slots[
@@ -11,3 +11,15 @@ SYNTAX: slots[
 SYNTAX: slots{
     "}" [ reader-word 1quotation ] map-tokens
     '[ [ _ cleave ] output>array ] append! ;
+
+: writer-word* ( name -- word )
+    ">>" prepend "accessors" lookup ;
+
+SYNTAX: set-slots[
+    "]" [ writer-word* 1quotation ] map-tokens
+    '[ _ spread ] append! ;
+
+SYNTAX: set-slots{
+    "}" [ writer-word* 1quotation ] map-tokens
+    [ length ] [ ] bi
+    '[ _ firstn _ spread ] append! ;