
SAVE-INPUT and RESTORE-INPUT
Quote:
>> I use them very often!
>> The idea is to somehow take a snapshot of the state of the input stream,
>> do your bits, and then restore the input stream to what it was.
> In a definition, saving and restoring >IN is more
> attractive to me. Here is the definition of ANEW that
> I like.
> : POSSIBLY BL WORD FIND ?DUP AND IF EXECUTE THEN ;
I love this one! The "FIND ?DUP AND IF" is particularly smart.
Quote:
I hate this one.
Probably more on `religious' grounds than anything else, but the way I
see it, >IN is a leftover of the implementation specification that Forth 83
was, that Forth 94 couldn't deprecate because too much user code would
have been broken.
Put bluntly: global variables are BAD, and public global variables are
even worse.
They won't show up in my code, unless I absolutely have to. In that
precise case, there was no reason to make my code dependent on an
implementation feature (even if described in the ANS Standard) when
a clean alternative was available.
Quote:
> POSSIBLY <name> executes <name> if <name> is defined.
> It's good for initialization of words that might or might
> not have been defined earlier.
> Outside a definition SAVE-INPUT and RESTORE-INPUT could
> be used to iterate compilation/interpretation of a script.
> Here is a stript-down outline of an exercise I once did.
> I wanted to check different approaches to doing something.
> : DROPPED ( x1 x2 ... xn n -- )
> 0 ?DO DROP LOOP
> ;
> : DUPPED ( x1 x2 ... xn n -- x1 x2 ... xn x1 x2 ... xn )
> DUP 0 ?DO DUP >R 1- PICK R> LOOP DROP
> ;
> : STOP-INPUT ( x1 x2 ... xn n flag -- )
> IF DROPPED
> ELSE DUP 1+ DUPPED RESTORE-INPUT DROP
> THEN
> ;
> VARIABLE #ROUND
> 0 #ROUND !
> SAVE-INPUT 1 #ROUND +!
> .( \ This is Together. ) CR
> \ This is One.
> \ This is Together.
> \ This is Two.
> \ This is Together.
> \ This is Three.
> \ This is Together.
What this code is trying to achieve is not, well, crystal-clear (at
least to me) and an explanation of that "exercise about `something'"
would have been welcome in the first place...
--