home .. forth .. colorforth mail list archive ..

Re: [colorforth] How is colorForth different from other Forths?


On Thursday 18 December 2003 06:51 am, John Drake wrote:
> Looking at the wordlist link that Mark provided I
> see two words that might handle this.  The words
> are:
>
>  "mark" - Record current dictionary pointers
> and
>  "empty" - Record current dictionary pointers

These words won't even help.  To implement temporary buffers, it's best 
to use definitions like this:

: buf1 here ;
: buf2 here 4096 + ;
: buf3 here 6144 + ;

and so on.  The only problem with this is that you cannot pre-allocate 
the temporary buffers, so that you can dynamically build another buffer 
thereafter.  The first time you use , or a related word, the value of 
'here' changes, and consequently, buf1..buf3 will return different 
values.

ALLOT is necessary if you're looking to establish multiple buffers.  I 
use ALLOT extensively in the implementation of FS/Forth's target 
compiler, and I don't think it's easily implementable any other way.  
For example, because I build my program's ELF headers using , and C, 
words, and because I build the string table for the ELF executable 
dynamically, I must pre-allocate and ensure a unique address for the 
string table, so that subsequent execution of the ELF header words won't 
change the address of the string table (effectively corrupting it in the 
process, as the contents of the buffer don't move with it!).

Assuming H is the variable used to hold the current HERE pointer, then:

VARIABLE H
: here H @ ;
: allot h +! ;
: , here ! 4 allot ;
: 2, here ! 2 allot ;  ( little-endian CPUs only )
: 1, here c! 1 allot ;

As can be seen, implementing ALLOT isn't hard.  Frankly, ALLOT itself 
really isn't necessary if H is exposed to the programmer directly (which 
I don't recall if it is or isn't in ColorForth).

--
Samuel A. Falvo II


---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com