factored range checking
Author Message
factored range checking

0 [IF] DEMO - FACTORED INDEX RANGE CHECKING
Michael Gassenenko in his paper _C-Style Indexing in ANS Forth_
(www.forth.org.ru/mlg/cstyle.htm) presents a nice set of simple
array words. Without range checking the words are very simple and
even if range checking is added not much simplicity is lost. However,
a high degree of simplicity and flexibility can be maintained if
the range checking is kept factored out and provided in a word that can
be inserted when desired.
In this demo _? is the factored index range checking. When range
checking is desired it can be inserted before the array word [] .
For demo purpose _? on range error will print a message and refill.
For real operations it can be made to throw or abort.
To support _? the array is structured to contain the column size of
each column and the number of rows. If the array needs to be constructed
without storing the column sizes (as done in MLG's example) then other
ways of defining _? using []LEN can be done but won't apply in the
same manner as in this example.
One last note, in the demo when a row index fails the range check,
the column index is left on the stack. In real operations using a
throw or abort this would be a don't care.
[THEN]

\ C-STYLE ARRAY INDEXING

\ FACTORED ARRAY INDEX RANGE CHECK
: _? ( I A -- I A)

0= IF ." (!) RANGE ERROR " DROP . REFILL DROP THEN ;

\ DISPLAY TEST RESULT
: =? ( N1 N2 --) = IF ."  YES" ELSE ."  NO" THEN ;

\ CREATE FIXED SIZED SLANTED ARRAY
ALIGN
3 , HERE 00 , 01 , 02 ,            \ COLUMN SIZE AND 3 ELEMENTS
4 , HERE 10 , 11 , 12 , 13 ,       \ COLUMN SIZE AND 4 ELEMENTS
5 , HERE 20 , 21 , 22 , 23 , 24 ,  \ COLUMN SIZE AND 5 ELEMENTS
3 , HERE 3 ROLL , ROT , SWAP ,     \ ROW SIZE AND ROW VECTORS

\ EXAMPLE CHECKING
2  0 CC    []    [] 02 =?    \ PASSED,
2  0 CC _? [] _? [] 02 =?    \ PASSED,
3  1 CC _? [] _? [] 13 =?    \ PASSED,
4  2 CC _? [] _? [] 24 =?    \ PASSED,
2 -1 CC _? [] _? []
. \ ROW CHECK FAILED SO COLUMN INDEX WAS LEFT OVER
3  0 CC _? [] _? []
4  1 CC _? [] _? []
5  2 CC _? [] _? []

--
Tom Zegub
WasteLand   http://www.*-*-*.com/ ~tzegub

Tue, 27 Nov 2001 03:00:00 GMT
factored range checking
The actual URL for Michael Gassenko's informative paper appears to be:

http://www.forth.org.ru/~mlg/CStyleIn/CStyleIndexing.html

- Bob Cunningham

Quote:

> 0 [IF] DEMO - FACTORED INDEX RANGE CHECKING
>     Michael Gassenenko in his paper _C-Style Indexing in ANS Forth_
>   (www.forth.org.ru/mlg/cstyle.htm) presents a nice set of simple
>   array words. ...

Wed, 28 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages