Shorthand syntax for arrays-of-arrays, arrays-of-quotations, and hashtables
parent
11b721c90c
commit
340abc119a
|
@ -0,0 +1 @@
|
|||
Joe Groff
|
|
@ -0,0 +1,29 @@
|
|||
USING: help.markup help.syntax ;
|
||||
IN: arrays.nested-syntax
|
||||
|
||||
HELP: {{
|
||||
{ $syntax "{{ zim zang ;; zoop ;; zidilly zam ;; ... }}" }
|
||||
{ $description "Shorthand for a literal array of arrays. Subarrays are separated by the " { $link POSTPONE: ;; } " token." }
|
||||
{ $examples "The following blocks of code push an equivalent array onto the stack:" { $example "{{ 1 ;; 2 3 ;; 4 5 6 }}" } { $example "{ { 1 } { 2 3 } { 4 5 6 } }" } } ;
|
||||
|
||||
HELP: H{{
|
||||
{ $syntax "H{{ zim zang ;; zoop zidilly ;; zam zung ;; ... }}" }
|
||||
{ $description "Shorthand for a literal hashtable. Key-value pairs are separated by the " { $link POSTPONE: ;; } " token." }
|
||||
{ $examples "The following blocks of code push an equivalent hash table onto the stack:" { $example "H{{ \"Monday\" 1 ;; \"Tuesday\" 2 ;; \"Wednesday\" 3 ;; \"Thursday\" 4 }}" } { $example "H{ { \"Monday\" 1 } { \"Tuesday\" 2 } { \"Wednesday\" 3 } { \"Thursday\" 4 } }" } } ;
|
||||
|
||||
HELP: [[
|
||||
{ $syntax "[[ foo ;; bar bas ;; qux quux quuuux ;; ... ]]" }
|
||||
{ $description "Shorthand for a literal array of quotations. Each quotation is separated by the " { $link POSTPONE: ;; } " token." }
|
||||
{ $examples "The following blocks of code are equivalent:" { $example "[[ 1+ ;; 2 + ]] cleave" } { $example "{ [ 1+ ] [ 2 + ] } cleave" } } ;
|
||||
|
||||
{ POSTPONE: {{ POSTPONE: H{{ POSTPONE: [[ } related-words
|
||||
|
||||
HELP: ;;
|
||||
{ $description "Separator token used in the " { $link POSTPONE: {{ } ", " { $link POSTPONE: H{{ } ", and " { $link POSTPONE: [[ } " literal syntaxes." } ;
|
||||
|
||||
HELP: }}
|
||||
{ $description "Delimiter token used to close the " { $link POSTPONE: {{ } " and " { $link POSTPONE: H{{ } " literal syntaxes." } ;
|
||||
|
||||
HELP: ]]
|
||||
{ $description "Delimiter token used to close the " { $link POSTPONE: [[ } " literal syntax." } ;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
USING: arrays.nested-syntax kernel tools.test ;
|
||||
IN: arrays.nested-syntax.tests
|
||||
|
||||
[ { { 1 } { 2 3 } { 4 5 6 } } ]
|
||||
[ {{ 1 ;; 2 3 ;; 4 5 6 }} ] unit-test
|
||||
|
||||
[ H{ { "foo" 1 } { "bar" 2 } { "bas" 3 } } ]
|
||||
[ H{{ "foo" 1 ;; "bar" 2 ;; "bas" 3 }} ] unit-test
|
||||
|
||||
[ { [ drop ] [ nip ] } ]
|
||||
[ [[ drop ;; nip ]] ] unit-test
|
|
@ -0,0 +1,10 @@
|
|||
USING: arrays hashtables kernel parser quotations sequences splitting ;
|
||||
IN: arrays.nested-syntax
|
||||
|
||||
: ;; ( -- * ) ";; can only be used in [[ ]] , {{ }} , or H{{ }} blocks" throw ;
|
||||
DEFER: ]] delimiter
|
||||
DEFER: }} delimiter
|
||||
|
||||
: [[ \ ]] [ { POSTPONE: ;; } split [ >quotation ] map ] parse-literal ; parsing
|
||||
: {{ \ }} [ { POSTPONE: ;; } split [ >array ] map ] parse-literal ; parsing
|
||||
: H{{ \ }} [ { POSTPONE: ;; } split >hashtable ] parse-literal ; parsing
|
|
@ -0,0 +1 @@
|
|||
Shorthand syntax for defining arrays of quotations or arrays of arrays
|
|
@ -0,0 +1 @@
|
|||
extensions
|
Loading…
Reference in New Issue