Digest #169 2021-11-23
Contributions
example - New Line characters in a string passed to EVALUATE
How should evaluate handle a string containing two lines of source text separated by a new-line?
Replies
1. Problem of overflow
The "Rationale" section says:
The application must take care of any string overflow situation where the length of the resulting string would exceed the maximal length that can be stored in a length character. Also it has to assure that the memory is available to store the resulting string.
But the "Typical Use" section doesn't show how to check for overflow, especially when +PLACE
is used. Could you please fill this gap?
The most robust behavior for an application in a case of overflow is to throw an exception. It seems, these words are useless when an application needs to throw an exception if a string doesn't fit a target buffer.
I think, the following words can be far more useful and less error prone (their names are placeholders):
place-counted ( c-addr1 u1 c-addr2 u2 -- )
+place-counted ( c-addr1 u1 c-addr2 u2 -- )
Where (c-addr2 u2)
is the target buffer starting address and size. These words should check for overflow and throw an exception if any.
In their turn, the words place
and +place
just encourage a bad practice since almost nobody checks for overflow when uses them, I think. So it's better to not include these words into the standard.
2. Specify what instead of how
I support the Anton's idea that we should not specify how to copy the characters, i.e. "from lower addresses to higher addresses". In this regard, these words should be similar to MOVE
rather than to CMOVE
.
So it's better to specify the result, and don't specify how to achieve this result. The reference implementation should be corrected accordingly (i.e. store the length character after copy the characters).
3. Note concerning char size
It seems, in the subsection "Restriction on the Length of Counted Strings", “1 CHARS = 1 byte” is assumed.
But “1 CHARS = 1” means that the size of 1 character is 1 address unit (not 1 byte). For example, an address unit may be 2 bytes.
In this regard, it's unclear what does the phrase "Requesting standard systems to have byte size characters limit counted strings to the known maximal length of 255 characters" mean.
referenceImplementation - Portable implementation for POSTPONE
POSTPONE must always occur between : and ; ?
referenceImplementation - Portable implementation for POSTPONE
In general, POSTPONE
may occur (may be referenced to) outside of a colon definition (: name ... ;
).
For example, with synonym
:
synonym p postpone
But when it's encountered by the Forth text interpreter in a standard program, it may occur only inside a colon definition.
example - New Line characters in a string passed to EVALUATE
To augment Stephens question: is the 2nd test supposed to work?
: warp-c
cr ." Wrapper-name: " parse-name type
." Forth-name: " parse-name type ;
\ this one works
cr ." TEST ONE"
s\" warp-c wone fone" evaluate
\ this one does not on all systems
cr ." TEST TWO"
s\" warp-c wone fone\nwarp-c wtwo ftwo\n" evaluate