DEFERred Words

You can create your own deferred words by defining with DEFER. For example:

    DEFER CAKE

creates a deferred word called CAKE. Initially, this does nothing, which isn't terribly useful. However, you can set the action of the word at any time with IS, which accepts an execution token. For example:

    : CREAM-CAKE  CR ." Yummy!" ;
    ' CREAM-CAKE IS CAKE

Now, whenever you execute the word CAKE, you'll get the response "Yummy!". Now try:

    : MUD-CAKE  CR ." Yuck!" ;
    ' MUD-CAKE IS CAKE

Now, CAKE provokes the response "Yuck!". The power of these words is that, unlike simply redefining a word with the same name, the action of the word changes immediately in every definition it has been compiled into.


Built-in DEFERred words

CamelForth comes with several important words defined as deferred words. This means that by substituting your own definitions, you can easily change the behaviour of the entire system.

The following standard words are currently deferred:

EMIT ( char -- )
This word is at the heart of the Forth output system, so changing it means that every character displayed on the console can be processed by your own routine. The default word for EMIT is CEMIT.
KEY ( -- char )
Often used for getting data from the user. The default word for KEY is CKEY.
TYPE ( c-addr u -- )
The standard word for outputting strings. The default word for TYPE is CTYPE.

OZ Errors

Also set up as deferred words are the following, which are executed whenever a specific OZ error is detected:

(RC_QUIT)
The action to take when the system receives an RC_QUIT error (this is a request for the application to terminate itself). The default word is BYE. This should always be the last word in the (RC_QUIT) word definition in any case, as who knows what will happen if OZ asks an application to quit but it refuses! The main use for this vector is to allow applications to tidy up (by closing files etc) before quitting.
(RC_DRAW)
The action to take when the system receives an RC_DRAW error (ie the display has become corrupted). The default word is CINIT which defines the normal console window and clears it. Under normal circumstances, this word will not be called, since CamelForth asks OZ to try and preserve the screen during pre-emption.
(RC_ESC)
The action to take when the system receives an RC_ESC error (ie ESC has been pressed with escape-detection enabled). The default word for this is ESCAPE. See next section for more details on escape detection.

Note that any word you assign to these error handlers must leave all stacks balanced, and cannot assume anything about the contents of any of the stacks.

There are some other deferred words in the system; these are dealt with in the documentation dealing with the facilities they are related to.


More about CamelForth

Back to the Z88 home page