A buffer is just a linear sequence of memory. These words provide a way to incrementally store or retrieve values from it.
To begin, create a memory region to use as a buffer.
Then you can set this as the current buffer:
When a buffer is set, the vocabulary sets an internal index to the first address in it. This will be incremented when you add data and decremented when you remove data.
Let's add some stuff using buffer:add:
And then retreive the values:
You can remove all values using buffer:empty:
And ask the buffer how many items it contains:
The other functions are buffer:start, which returns the address of the buffer, buffer:end, which returns the address of the last value, and buffer:preserve. The first is easy to demo:
The last one is useful. Only one buffer is ever active at a given time. The buffer:preserve combinator lets you execute a word, saving and restoring the current buffer indexes. So the word could assign and use a new buffer and this will reset the previous one after control returns.
There are a few notes that need to be considered. The preserve combinator saves the start and current index but not the contents. If the word you call uses the same buffer, the contents will remain altered.
Finally, the buffer words have one interesting trait: they store an ASCII NULL after adding each item to the buffer. This lets one use them to build strings easily.