Building Applications with CamelForth

3. The top-level definition

When building an application, it's important to ensure that any unexpected errors are properly trapped, so the user is not thrown back into CamelForth. This can be easily done by using the exception handling routines to catch any errors thrown by the main program word (which will typically be an infinite loop).

Other considerations are initialising the screen (or windows, if you decide to use these), and exiting gracefully when the program terminates.

All these things can be done with a single word which should be assigned to the deferred word (COLD) - this means that on startup, your word will be executed, and not the normal CamelForth interpreter.


Puzzle Of The Pyramid

For our example application, we just need to initialise the normal console window, turn ESCape detection off, and execute the main adventure with GAME, trapping any errors. Finally, we'll terminate the application. The code to do this is as follows:

   : PYRAMID
       CINIT             \ initialise the console window
       6 OS_ESC DROP     \ turn off escape detection
       ['] GAME CATCH    \ execute game, catching errors
       ?DUP
       IF  CR ." Exception " . ." has occurred."
           CR ." Please report to programmer. Press a key to exit." 
           KEY DROP
       THEN
       BYE ;             \ exit application

   ' PYRAMID IS (COLD)   \ this is our entry point from COLD


Next section: Dealing with OZ errors

Previous section: Dataspaces


More on CamelForth

Other ROMs

Back to the Z88 home page