albe…@spenarnc.xs4all.nl writes:
>In article <2026…@mips.complang.tuwien.ac.at>,
>Anton Ertl <ant…@mips.complang.tuwien.ac.at> wrote:
>>albe…@spenarnc.xs4all.nl writes:
>>>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.
Relevant functionality of $trings:
* $tring data can be as long as fits contiguously in memory.
* No need to specify the size of $tring data for variables holding
$trings.
The fact that $trings can use variables and other containers of single
cells might be considered an API rather than functionality issue.
>>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!
Given that my example does not work on an implementation like you
outline, it's not just an implementation issue.
>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.
Either your buffer must store its maximum length somewhere, or your $!
will happily overflow your buffer if you give it a too-long string.
And what happens if the string is too long for the buffer, even if you
check it?
>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.
addr can address any cell, it does not need to be a variable.
>Requiring ALLOCATE is a great burden on small systems.
$trings were not designed for systems where ALLOCATE is a great
burden.
How small are the systems that you have in mind? C was born on a
small PDP-11 and includes malloc(). Unix V4, the first Unix version
written in C also includes a SNOBOL interpreter (a string-oriented
language). Microsoft BASIC ran on, e.g., the Commodore PET with 4KB
RAM; it includes a garbage collector for strings in order to support
as long strings as fit in memory.
And if you have that little memory and a predetermined maximum length
for strings, how big is the advantage of supporting strings longer
than 255 bytes?
>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.
This does not look like a good use of any kind of any string extension
to standard Forth to me, as producing a hex dump can be done one
character at a time if need be, and in any case, the capabilities of
standard Forth are sufficient to deal with that, certainly in the area
of strings.
>Note that it is a great hassle to have to accomodate ALLOCATE in
>turnkey programs.
Only if you want to preserve the contents of the ALLOCATEd stuff (but
Gforth has $variable to do that in the case of $trings.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html