Skip to content

Instantly share code, notes, and snippets.

@lsparrish
Created January 8, 2010 06:39
Show Gist options
  • Save lsparrish/271895 to your computer and use it in GitHub Desktop.
Save lsparrish/271895 to your computer and use it in GitHub Desktop.
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( >last and expose -- move a dictionary header to the top of )
( the dictionary. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
: tod ( -a ) last @ ;
: nod ( -a ) tod @ ;
: d' ( "-a ) ' drop which @ ;
: after ( a-a ) last repeat @ 2dup @ =if nip ;then again ;
: remove ( a- ) dup @ swap after ! ;
: replace ( a- ) tod over ! last ! ;
---reveal---
: >last ( a- ) dup remove replace ;
: expose ( "- ) d' >last ;
}}
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Elements are equivalent to variables, but are contiguous. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
: list ( n-a ) here swap allot ;
: setxt ( a- ) last @ d->xt ! ;
: element ( a-a ) create dup setxt 1+ ;
---reveal---
: elements ( n"- ) dup list swap for element next drop ;
}}
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( A vocab word is a word that can be used to open and shut a )
( vocabulary section from the dictionary. It consists of )
( three fields which point to three dictionary headers: the )
( word before the vocabulary starts, the first word to be )
( hidden, and the first word not to be hidden. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
3 elements shown hidden before
: tod ( -a ) last @ ;
: nod ( -a ) tod @ ;
: current ( -a ) shown @ ;
: .shown ( -a ) current d->xt @ ;
: .hidden ( -a ) .shown 1+ ;
: .before ( -a ) .shown 1+ 1+ ;
: after ( a-a ) last repeat @ 2dup @ =if nip ;then again ;
: remove ( a- ) current dup @ swap after ! ;
: replace ( a- ) current tod over ! last ! ;
: fields ( - ) shown .shown 3 copy ;
: open ( a- ) @ shown ! .hidden @ .shown @ ! ;
: shut ( a- ) @ shown ! .before @ .shown @ ! ;
: :find ( a-af ) last repeat @ 2dup =if drop @ TRUE ;then dup 0; drop again ;
: open? ( a-af ) dup 1+ @ :find nip ;
: toggle ( a- ) open? if shut ;then open ;
---reveal---
expose open expose shut
: .vocab ( a- ) compiler @ if literal, then ` toggle ;
: vocab ( "- ) create 3 allot ['] .vocab reclass ;
: (( tod shown ! nod before ! 0 hidden ! fields ;
: )) tod hidden ! fields remove replace ;
}}
vocab foo ((
stub bar
stub baz
))
foo words
: test ['] foo shut words cr ['] foo open words cr ; test
: test foo words ; test test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment