extra/tuple-syntax--named tuple slot literals
parent
ee8932b888
commit
d830ed9314
|
@ -0,0 +1 @@
|
||||||
|
Tuple literals with named slots
|
|
@ -0,0 +1 @@
|
||||||
|
Daniel Ehrenberg
|
|
@ -0,0 +1 @@
|
||||||
|
syntax
|
|
@ -0,0 +1,10 @@
|
||||||
|
USING: help.markup help.syntax tuple-syntax ;
|
||||||
|
|
||||||
|
HELP: TUPLE{
|
||||||
|
{ $syntax "TUPLE{ class slot-name: value... }" }
|
||||||
|
{ $values { "class" "a tuple class word" } { "slot-name" "the name of a slot, without the tuple class name" } { "value" "the value for a slot" } }
|
||||||
|
{ $description "Marks the beginning of a literal tuple. Literal tuples are terminated by " { $link POSTPONE: } } ". The class word must be specified. Slots which aren't specified are set to f. If slot names are duplicated, the latest one is used." }
|
||||||
|
{ $see-also POSTPONE: T{ } ;
|
||||||
|
|
||||||
|
IN: tuple-syntax
|
||||||
|
ABOUT: POSTPONE: TUPLE{
|
|
@ -0,0 +1,7 @@
|
||||||
|
USING: tools.test tuple-syntax ;
|
||||||
|
|
||||||
|
TUPLE: foo bar baz ;
|
||||||
|
|
||||||
|
[ T{ foo } ] [ TUPLE{ foo } ] unit-test
|
||||||
|
[ T{ foo 1 { 2 3 } { 4 { 5 } } } ]
|
||||||
|
[ TUPLE{ foo bar: { 2 3 } delegate: 1 baz: { 4 { 5 } } } ] unit-test
|
|
@ -0,0 +1,21 @@
|
||||||
|
USING: kernel sequences slots parser words classes ;
|
||||||
|
IN: tuple-syntax
|
||||||
|
|
||||||
|
! TUPLE: foo bar baz ;
|
||||||
|
! TUPLE{ foo bar: 1 baz: 2 }
|
||||||
|
|
||||||
|
: parse-object ( -- object )
|
||||||
|
scan-word dup parsing? [ V{ } clone swap execute first ] when ;
|
||||||
|
|
||||||
|
: parse-slot-writer ( tuple -- slot-setter )
|
||||||
|
scan dup "}" = [ 2drop f ] [
|
||||||
|
1 head* swap class "slots" word-prop
|
||||||
|
[ slot-spec-name = ] curry* find nip slot-spec-writer
|
||||||
|
] if ;
|
||||||
|
|
||||||
|
: parse-slots ( accum tuple -- accum tuple )
|
||||||
|
dup parse-slot-writer
|
||||||
|
[ parse-object pick rot execute parse-slots ] when* ;
|
||||||
|
|
||||||
|
: TUPLE{
|
||||||
|
scan-word construct-empty parse-slots parsed ; parsing
|
Loading…
Reference in New Issue