From Newsgroup: comp.lang.forth
( Display window (stdout) )
Demo Keep script running in spite of faults
Overview
===
FigForth WARNING set to -1 enables (ABORT) as alternative error
handling. The default for (ABORT) is to perform ABORT but it's
acceptable to replace (ABORT) action with custom user action. In the
following demo a custom error handling is assigned to (ABORT) by
action of switch settings. The alternaltive error handling will use
BacForth 'structured cut' to recover from an error (invalid octal
number) and keep the script running.
The _objective_ is to handle errors without halting the script.
Error handling is arranged to log error, make repairs, cease action
of offending script line, and continue running rest of script.
This allows test/demo scripts to include fault handling cases and
continue on with diagnotics and other cases.
Notes:
i. Fig's BACK is redefined for BacForth BACK TRACKING
i. TCF := Threaded Code Fragment, a BacForth concept
i. BacForth employs only L-stack; I added a C-stack for cut
addressing to avoid interactions with L-stack data.
Being virtual stacks it only cost one cell for the stack
and a few words for push and pop operations.
===
-- OX ( "octal_number<bl>" -- current_base_number )
-- Octal number parser. Used to illustrate PRO and CUT actions
: Ox
PRO \ push TCF to L-stack
BASE @ DUP
R> 2>R \ rack BASE value
TMP! \ save BASE value
BACK \ return here on error
2R> >R BASE ! \ restore BASE
STATE @ IF \ snub the compile
'CUT: TRUE ABORT" Snubbed" ;' EVAL
ENDIF
TREK \ resolve BACK
CUT: \ mark BACK for error return
BL WORD \ parse octal number
OCTAL HERE NUMBER DROP [COMPILE] LITERAL
TMP@ BASE ! \ restore BASE
CONT \ control flows to TCF
; IMMEDIATE
Setup
sp! decimal
ERESET \ default error handling
-uabort \ enable user error handling
-ecut \ enable cut action
show abort
ABORT status:
WARNING -1
(ABORT) UABORT
UABORT (ERR_DUMP)
ERRTRACE Disabled
ERRCUT Enabled
--
: boo ." BOO " 42 . ; \ some TCF downstream word
: fault 0 ? ; \ some fault
Interpret
cr i. Ox 644 . boo
420 BOO 42
cr i. Ox 755 . boo
493 BOO 42
cr i. Ox 644 . Ox 755 . boo
420 493 BOO 42
cr i. Ox 644 . hex Ox 755 . boo
420 1ED BOO 2A
i. base? --> 10
( For unbelievers ) i. BASE @ 1 - . --> 9
--
-- Three fault cases
--
i. Ox 972 . boo -->
( continued after fault )
i. Ox 644 . Ox 69 . boo --> 420
( continued after fault )
i. Ox 755 . fault . boo --> 493
( continued after fault )
Compiled
: FOO Ox 644 . boo ;
i. foo --> 420 BOO 42
: BAR hex Ox 49 . boo ; \ compile fails
' BAR see \ BAR has been snubbed
BAR
134597616 HEX
134597620 CUT:
134597624 TRUE
134597628 0BRANCH 32 ==> 134597664
134597636 (") Snubbed
134597648 ABORT_MSG
134597652 ECR
134597656 ERR_ABORTQ
134597660 ERROR
134597664 ;S
i. bar \ snubbed BAR does not halt script -->
i. base? --> 10
( For unbelievers ) i. BASE @ 1 - . --> 9
Create file
sys [ -f /tmp/foo ] && rm /tmp/foo
"/tmp/foo" Ox 755 FCREATE HCLOSE
i. "ls -og /tmp/foo|cut -c1-10" /sys 1 cuu --> -rwxr-xr-x
sys rm /tmp/foo
"/tmp/foo" Ox 644 FCREATE HCLOSE
i. "ls -og /tmp/foo|cut -c1-10" /sys 1 cuu --> -rw-r--r--
-fin-
( Command window (errout) )
"ox" /demo
===
972?
CURRENT and CONTEXT are WRK PREVOC is WRK BASE: 8
Latest: fault HERE : 134597572 UNUSED : 11541
S?: 0 0 134597573
--- CUTTING
===
69?
CURRENT and CONTEXT are WRK PREVOC is WRK BASE: 8
Latest: fault HERE : 134597572 UNUSED : 11541
S?: 6 0 134597574
--- CUTTING
===
fault? MEMORY ACCESS FAULT
CURRENT and CONTEXT are WRK PREVOC is WRK BASE: 10
Latest: fault HERE : 134597572 UNUSED : 11541
S?: empty
--- CUTTING
===
49?
CURRENT and CONTEXT are WRK PREVOC is WRK BASE: 8
Latest: BAR HERE : 134597620 UNUSED : 11529
S?: 4 0 134597622
--- CUTTING
===
bar? ABORTED
CURRENT and CONTEXT are WRK PREVOC is WRK BASE: 16
Latest: BAR HERE : 134597668 UNUSED : 11517
S?: 134597640
--- CUTTING OK
--
me
--- Synchronet 3.22a-Linux NewsLink 1.2