Proposal: Obsolescence for SAVE-INPUT and RESTORE-INPUT
This page is dedicated to discussing this specific proposal
ContributeContributions
ruv [291] Obsolescence for SAVE-INPUT and RESTORE-INPUTProposal2023-03-02 19:04:49
Author
Ruv
Change Log
- 2023-03-02 Initial version
Problem
The words SAVE-INPUT
and RESTORE-INPUT
almost don't bear any usefulness to programs, and they only burden implementers.
These words have the following problems:
- Too few guarantees to programs:
RESTORE-INPUT
may work or fail depending on on the input source kind (file, pipe, string, keyboard). - The returned flag of
RESTORE-INPUT
is inconsistent with other words: true means fail, false means success. A better variant could be a throwable ior. - In some systems
RESTORE-INPUT
works incorrectly in some cases: it restores the position in the input buffer and returns success, but doesn't restore the content of the input buffer.
These words are almost not used in programs. I asked in comp.lang.forth and ForthHub. Only one program was mentioned: "Lambda Expressions in ANS Forth" by Gerry Jackson (sources), and in this case the problem can be also solved without these words.
What is sometimes required in programs is an ability to parse (extract) a text fragment from the input stream, and later translate (evaluate) this fragment in the current context, regardless whether the input stream was switched or not. Such an API should be designed separately. The words SAVE-INPUT
and RESTORE-INPUT
cannot help on that.
Solution
Declare the words SAVE-INPUT
and RESTORE-INPUT
as obsolescent, to destandardize (remove from the standard) them on the next iteration.
Consequences
This change doesn't affect the standard systems. The new (or updated) standard systems can be made slightly simpler by not providing an implementation and documentation for these words (if they are not used internally).
The standard programs that employ these words gain a new environmental dependency, and later they become non compliant to the new versions of the standard.
Proposal
In the section 1.4.2 Obsolescent features, after the phrase "This standard designates the following word as obsolescent:", add:
- 6.2.2182 SAVE-INPUT
- 6.2.2148 RESTORE-INPUT
In each of the glossary entries 6.2.2182 SAVE-INPUT
and 6.2.2148 RESTORE-INPUT
add the following note:
Note:
This word is obsolescent and is included as a concession to existing implementations.
albert [350] SAVE-INPUTComment2024-07-10 09:40:03
If you have a Forth that always slurps the file for including and lock blocks that are interpreted in memory, life becomes much easier. I remembered testing properly handling exceptions coming from a string evaluated from a block that you have loaded from a file. That was in transputer forth and Marcel Hendrix was able to pull that off. I am not sure I could do that flawlessly in ciforth and honestly I'm not sure tforth was defect-free despite the elaborate testing.
If everything is in memory you can get away with SAVE and RESTORE. The remember the start and end and the current parsepointer, and restoring it is a breeze.
E.g.
: EXECUTE-PARSING ROT ROT SAVE SET-SRC CATCH RESTORE THROW ;
A simple exeampe counting words in a string
SAVE SET-SRC 0 BEGIN NAME NIP WHILE 1+ REPEAT RESTORE
shows that you don't have to manipulate execution tokens, as long as SET-SRC can make a given string the input buffer.
I can't propose to replace SAVE-INPUT and RESTORE-INPUT by SAVE and RESTORE, because not having REFILL and line by line compilation is too revolutionary. Most modern Forth will slurp files for include nowadays, however.