Sunday, March 21, 2010

Some Forth string words

I have been experimenting with pForth, and composed some words to manipulate strings:

: str: create 0 , 0 , ;      \ create a string variable
: >str ( a n str -- ) 2! ;     \ store a string in a string variable
: str> ( str -- a n ) 2@ ;     \ retrieve a string onto the stack
: .str ( str -- ) str> type ;  \ print a string

\ example usage
str: greeting                  \ define a string 
s" hello world" greeting >str  \ set it to a simple string
greeting .str                  \ print the string

It's very Noddy, I know.

No comments: