[comp.sys.handhelds] HP48 New IFERR II

billw@hpcvra.cv.hp.com. (William C Wickes) (03/06/91)

                      The Return of New IFERR

* The library listed at the end of this note is a new version of the
* IFERR library previously posted, which corrects the problem associated
* with command line execution.  You should delete version A of this library
* if you still have it.  Please notify me if any problems arise with
* version B.

Several HP 48 programmers have expressed a concern that the current error
trapping mechanism via IFERR is vulnerable to repeated ATTN key
presses, which can prevent a program from cleaning up when it is
interrupted by the user.  This problem arises because IFERR and all
other branching constructs built into the HP 48 check to see if ATTN
key has been pressed so that users cannot (easily) lock themselves
into an uninterruptible loop.

The object listed below in ASC-> form is an HP 48 library titled "IF
ERROR ver. B", that provides an alternate version of IFERR/THEN which can be
uniformly substituted for the built-in versions. The difference
between this version and that built into the HP 48 is in the handling
of ATTN.  The ATTN key check is disabled for the THEN
clause of the version provided in this library, allowing for an
"unbreakable" recursive error recovery.

Version B includes an additional modification.  The built-in
IFERR...THEN automatically clears the key buffer, and cancels pending
execution of the object associated with the key that started command
line execution.  In version B, the replacement IFERR does not clear
keys.  A new command FLUSHKEYS is provided, so that the programmer can
control pending key execution.  FLUSHKEYS takes no arguments, and
clears the key buffer and the pending key object.

An example of the use of the new IFERR is given in the program below.
In the "main" program, a loop display a counter running from 1 to
1000.  If you press ATTN while it is running, the error trap starts
another counting loop, this time from 1 to 50, to show you that an
error termination sequence of some length is running.  If you
interrupt it, it just restarts itself.

In the first line, a temporary variable, et, is allocated. This
variable will hold the error termination procedure after it is created
in the next part of the program. The error termination procedure is
simply a cleanup procedure protected by an IFERR which branches back
to itself recursively if an error occurs in its execution. It is
created in a list to avoid the inclusion of the opening and closing
program quotes, which themselves have attention key checks. The
remainder of the program is protected by IFERR...THEN et EVAL END.

Notes:
* You must access the error termination procedure via a temporary variable
because global variable execution itself contains an attention key check.

* If your cleanup procedure has an unavoidable error, the only way to stop
infinite execution is by [ON]-[C].

* The library (which has ID 1793) is auto-attaching: to use it, all you need
to do is store it in a port (port 0, say) by putting it on the stack and
executing :0:1793 STO, and then turning the HP 48 off and on.

* To convert old programs containing IFERR to the new version, you
need only to EDIT (VISIT), then ENTER when the library is attached.
Downloading a program in ASCII form via kermit will also automatically
use the new IFERR.

Bill Wickes
HP Corvallis

Sample error trap program:
----------------- cut here ----------------------------------------------
%%HP: T(3)A(R)F(.);
\<< 0 \-> et
  \<< { IFERR 1 50
             FOR j
              "YOUR CLEANUP PROC." 1 DISP 
              "#" j \->STR + " HERE" + 2 DISP
             NEXT 
        THEN et EVAL
        ELSE DONE
        END } OBJ\-> DROP
    'et' STO 
    IFERR 1 1000
      FOR j
        "YOUR MAIN PROC." 1 DISP
        "#" j \->STR " NOW" + + 2 DISP
      NEXT
    THEN et EVAL
    END
  \>>
\>>
----------------- cut here ----------------------------------------------


IF ERROR Library:
----------------- cut here ----------------------------------------------
%%HP: T(3)A(R)F(.);
"04B20F530001946402542525F4250202675627E20224011074100000000BA000
3D000E4A20C900000000000000000064000E4000000000000000000940000000
00000000000000000000000000000008300040458454E4100509464542525000
9064C4553584B4549535200620008300012000E4A208200034000FF000011003
9100DA1007D10051200D9D20119201070090770B21309107000D9D20E1A81BEE
60E5E40E8F608BE40D9D20EB78309146A4A35CB916D9D20E68353864467C04B2
130772441DC83CB916D9D206DB8337314DEC83B2130C8F83D48815EF3133D407
BF60B213008836088362B336B213029E20107400E38412107100D9D20E1A81B2
1300107200D9D20E1A81FACF3ACFF38AA04A2170D9D2079E60E8E60F6A04B213
079E60777A5B7970CB916D9D204423079E60E8E6079E60777A572D70B213017D
00B213029E20107300D9D20A025229E20107200DEBB0B2130D9D20A025229E20
107000E03521DF6029E20107500B2130D9D20A435210CB04345206DB029E2010
7600E2A32FCCB01DF6029E20107500B2130D9D20A5252223524423063352A025
229E20107100E03521DF607D042B2130001F1AC9"