/ arrays-lw /

Arrays by Leo Wong (ato)

description:


This approach is unique in having no proponents. It is part of
Flight, a Forth scripting language. Several Flight programs can
be found by Googling comp.lang.forth. In conformance with the
design goals of Flight, bounds checking is included.


main idea:


Treat arrays like values.


\ Example of use:
4 array bar
10 0 ato bar 20 1 ato bar 30 2 ato bar 40 3 ato bar

3 bar .
0 bar .
123 3 ato bar
3 bar .
1 3 +ato bar
3 bar .


Note that as in most Forth numerical arguments go before:


1 3 +ato bar NOT: 1 +ato 3 bar


This approach can be extended to different-sized data (including
strings) and 2 or more dimensions by having their own versions of
array array> ato +ato. A Flight scripter would not be expected to
implement these words.


\ Sample implementation:

\ If in range, return element address, else abort
: array> ( n 'array -- a )
2DUP @ 0 SWAP WITHIN 0= ABORT" array out of range"
CELL+ SWAP CELLS + ;

\ Define array
: array \ Usage: n array <name>
CREATE ( n -- ) DUP , CELLS ALLOT
DOES> ( n -- x ) array> @ ;

\ Store to array
: (ato) ( x n array -- ) >BODY array> ! ;
: ato \ Usage: x n ato <name>
STATE @
IF POSTPONE ['] POSTPONE (ato) ELSE ' (ato) THEN ; IMMEDIATE

: (+ato) ( x n array -- ) >BODY array> +! ;
: +ato \ Usage: n +ato <name>
STATE @
IF POSTPONE ['] POSTPONE (+ato) ELSE ' (+ato) THEN ; IMMEDIATE



page written by:

Leo Wong
albany.net/~hello/


generated Wed Jul 23 02:53:42 2003mlg