Page 1 of 1

A better ;s

Posted: Sun Nov 23, 2025 8:47 pm
by JimBoyd
One of the words from the Forth-83 Standard in the UNCONTROLLED REFERENCE WORDS section is ;S . It is used to stop interpretation of a block. On some Forth's it can be defined as an alias for EXIT , the runtime compiled by ; (semicolon). This will not work with my Forth. My Forth's INTERPRET executes I/C to interpret or compile one word at a time. EXIT would just exit I/C back into interpret, which would keep interpreting. One way to define ;S is this:

Code: Select all

: ;S  ( -- )
   B/BUF >IN ! ;

where B/BUF is 1024.
I don't care for this definition. I find it too limited. A better definition can be written using my Forth's LEAVE . Even though my Forth's I/C is a deferred word which executes (I/C) , only one extra level of nesting needs removed. My Forth's deferred words do not add an extra level of nesting and my Forth's LEAVE removes two items from the return stack then removes a third item and places it in IP . In other words, it removes two items and falls through to EXIT . LEAVE itself can not be used as an alias for ;S , it removes too many items from the return stack; however, ;S can be defined with LEAVE .

Code: Select all

: ;S  ( -- )
   LEAVE -;

Note the -; in the definition. EXIT is not compiled because it will never be reached.
This version of ;S works by exiting not (I/C) but by exiting INTERPRET into the word which executed INTERPRET .
I said I found the other definition of ;S too limited. Because loading a block causes the values of BLK and >IN to be saved before interpreting and restored afterwords, both versions will stop the interpretation of a block without altering the text stream which contained LOAD . This version has other uses.
My Forth has the word PRINT which redirects output to both the screen and the printer temporarily. It's definition:

Code: Select all

: PRINT
   LOGGER INTERPRET CONSOLE ;

Because PRINT uses INTERPRET , ;S can be used to exit back into PRINT before the text stream is exhausted.
PRINT is normally used like this:

Code: Select all

PRINT WORDS

This prints a listing of the words in the CONTEXT vocabulary.
The following will send to the screen and the printer a listing of the vocabularies and the search order.

Code: Select all

PRINT VOCS ORDER

The following will print a listing of the words in the CONTEXT vocabulary to the printer (and screen) without sending the list of vocabularies to the printer.

Code: Select all

PRINT WORDS ;S VOCS

Because ;S exits the interpreter without affecting the value of >IN , the instance of INTERPRET which executed PRINT will resume interpreting the text stream if it has not been exhausted. This is why I think my definition of ;S is better than the version which sets >IN equal to the block size.