In article <2026…@mips.complang.tuwien.ac.at>,
Anton Ertl <ant…@mips.complang.tuwien.ac.at> wrote:
>albe…@spenarnc.xs4all.nl writes:
>>In this day and age it is time to terminate the use of characters
>>to store a string count.
>
>Forthunately, the designers of Forth-94 already recognized this over
>three decades ago, and focussed on the c-addr u representation, with
>only a few words existing for dealing with the counted-string legacy.
>
>>With great pleasure I see that Gforth has adopted the use of a cell
>>to store the count of a string, officially.
>>$@ replaces COUNT.
>
>Not really, as explained below.
It think functionally it is.
>
>>$! has no equivalent in brain damaged strings
>
>Exactly.
>
>The $tring implementation differs from COUNTed strings in more than
>just the count size.
>
>The single-cell $tring representation does not point to the count cell
>(as it would, if $trings were just COUNTed strings with bigger
>counts).
>
>Instead, what you have on the stack is the address of a cell (e.g., it
>may come from a variable). E.g., here's how you use it:
>
>variable mystring \ 0 mystring ! \ automatic for variables in Gforth
>s" bla" mystring $!
>mystring $@ type
>
>What's remarkable here is that you do not have to allocate space for
>the string data explicitly, allocating a cell is enough. $! ALLOCATEs
>space for the string data and its length, and stores the address of
>that memory in the given cell.
>
>When the cell is overwritten by another $-word, e.g., by another
>$tring, the old string that the cell points to is freed. So it is
>important to initialize these cells to 0 at the beginning, and then
>only work on them with $-words. It's also important that any c-addr u
>pairs derived from the cell have been consumed at that point (they
>will become invalid once the string data has been freed).
>
That is an implementation issue!
>When you keep this discipline, the reward is that the storage
>allocation of strings becomes much easier: You only need to allocate a
>cell for any string you want to manage, and do not have to worry about
>the length of the string. You also do not need a string stack, the
>data stack holds the addresses of the cells just fine (but OTOH, you
>need to deal with those cells).
In advanced systems you reap this benefit.
It is strangely compatible with what I do with fig-Forth on CP/M
and till this day in ciforth.
The difference is that in my forths the address of a string is a buffer
that accommodates the largest string that you will put there.
So you can use $@ $! portable between gforth and ciforth, more or less.
This design doesn't require ALLOCATE. The disadvantage is that you have
to ALLOT space, but this isn't actually a great disadvantage as my 45
years practical experience for this setup shows.
$! and $@ can be defined such that the actually storage provided
is "system defined".
ciforth follows 1, gforth follows 2.
1 (addr len addr1 -- ) require a buffer at addr1 to accomodate a string of
len Obviously the prior content of the string as addr1 is lost.
2. (addr len addr -- ) require a variable at addr to accomodate a handle
and storage is allocated automatically for the string.
The previous content of the handle is lost, and the storage allocated is
freed.
Requiring ALLOCATE is a great burden on small systems.
I have redefined $! $@ on occasion to ALLOCATE the string.
The program will runs as before, but from then on you don't have
to worry of overwriting buffers.
An example of straight use of $! $@ .
This program converts a dump like
-D000 AF 66 23 89 89
to a bin and and a dump where also ascii is show.
Never mind the detail, the binary data is collected into `buf.
\ --------------------------------------------------------
WANT DUMP REALLOT
DATA buf
1,000,000 ALLOT \ Can be avoided
\ From string put all hex characters into `buf .
: to-buf BEGIN BL $/ EVALUATE buf $C+ DUP 0= UNTIL 2DROP ;
\ Get a line from file , drop first word, leave remainder of file.
: read-line ^J $/ BL $/ 2DROP to-buf ;
\ Convert file passed as argument1, dump it, and leave a binary in
\ a file as argument 2.
: doit 1 ARG[] GET-FILE HEX ONLY
\ 'buf OVER REALLOT \ *****************
0 buf !
BEGIN read-line DUP 0= UNTIL 2DROP
buf $@ DUMP
buf $@ 2 ARG[] PUT-FILE ;
\---------------------------------------------------
Used as: appledump apple.1 apple.bin
As shown the size of a turnkey is at least one megabyte caused by buf.
This is surely enough and serves for a throw away program.
But the buffer can be increased by REALLOT during runtime and the
allocating during compile time can be avoided.
The size needed is certainly not greater than the initial file:
`` OVER REALLOT '' works.
Now the program is 70 kbyte and no restrictions.
Note that it is a great hassle to have to accomodate ALLOCATE in
turnkey programs.
>
>- anton
Groetjes Albert
--
The Chinese government is satisfied with its military superiority over USA.
The next 5 year plan has as primary goal to advance life expectancy
over 80 years, like Western Europe.