Skip to content

Instantly share code, notes, and snippets.

@lsparrish
Created January 3, 2010 02:07
Show Gist options
  • Save lsparrish/267777 to your computer and use it in GitHub Desktop.
Save lsparrish/267777 to your computer and use it in GitHub Desktop.
( tokens in a linked list )
variable last
: new ( "- ) 32 accept ( "- Get a little text )
tib keepString ( -$ Make a permanent string of it )
here last @ , last ! ( - Link in last, set the pointer as the new last )
, ; ( $- compile in the text address after the link )
: access ( a-$ ) 1+ @ ; ( a-$ From the link, go to string pointer and access it )
: next ( a-a )
dup access type @ ; ( a-a Type the text by the current link, follow the link. )
new first
new second
new third
new fourth
last @ next
( Based on code from http://gist.github.com/267828 )
( character addressing versions )
: c!+ ( na-a ) dup 1+ >r c! r> ;
: c@+ ( a-an ) dup 1+ swap c@ ;
( cell addressing versions )
: !+ ( na-a ) dup cell+ >r ! r> ;
: @+ ( a-an ) dup cell+ swap @ ;
( Read text input )
create tib 512 allot
variable break
: accept ( c- ) break ! tib repeat key dup break @ =if drop 0 swap ! ;then swap c!+ again ;
( Get the length of a string )
: getLength ( $-n ) 0 swap repeat c@+ 0 =if drop ;then swap 1+ swap again ;
( Store a string somewhere permanently and return a reference to it )
create strings 8192 allot
variable str strings str !
: keepString ( $-$ )
str @ swap dup getLength 0 do c@+ str @ c! str ++ loop drop 0 str @ c! str ++ ;
( Display a string )
: type repeat c@+ dup 0 =if 2drop ;then emit again ;
( Clean up )
hide break
hide strings
hide str
( tokens in a linked list )
variable last
: new ( "- ) 32 accept ( "- Get a little text )
tib keepString ( -$ Make a permanent string of it )
here last @ , last ! ( - Link in last, set the pointer as the new last )
, ; ( $- compile in the text address after the link )
: access ( a-$ ) cell+ @ ; ( a-$ From the link, go to string pointer and access it )
: next ( a-a )
dup access type @ ; ( a-a Type the text by the current link, follow the link. )
new
first
new
second
new
third
new
fourth
last @ next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment