forth/array.fs

54 lines
1.5 KiB
Forth
Executable File

\ array.fs - Array of cells in Forth
\ 2013 David Meyer <papa@sdf.org> +JMJ
\ Source: Leonard Morgenstern. Arrays in Forth. Len's Forth Tutorial.
\ c. 1996.
\ <http://www.forth.org/svfig/Len/arrays.htm>
\ accessed 2013-10-17.
: array ( n -- ) ( i -- addr )
create cells allot
does> swap cells +
;
\ table - Array of n-cell records
: table ( n len -- ) ( i -- addr )
create dup , * cells allot
does> dup @ swap * cells + \ rot instead of swap?
;
\ Original source from web page ...
: unindexed-array ( n -- ) ( -- a)
create allot ;
80 unindexed-array u-foo \ Make an 80-byte unindexed array
u-foo \ Return the origin addr of u-foo
: array ( n -- ) ( i -- addr)
create cells allot
does> cells + ;
100 array foo \ Make an array with 100 cells
3 foo \ Return address of fourth element
: long-element-array ( n len -- ) ( i -- addr)
create dup , * cells allot
does> dup @ swap * cells + ;
10 5 long-element-array th-room \ Create array for 10 rooms
4 th-room \ Find address of room 4
variable current-offset
: offset ( n -- ) ( addr -- addr')
create current-offset @ ,
does> @ cells + ;
current-offset off \ Set variable to 0
1 offset }descriptor
1 offset }north
1 offset }east
1 offset }south
1 offset }west
\ Examples:
3 th-room }north @ \ Rm# The room north of room 3
4 th-room }descriptor @ execute
\ Print the description of room 4