\ 2011-01-14 EW list.fs \ amforth 4.0 or later \ \ a list is a series of locations with \ 2 cells each: \ 1. a pointer to the "next" entry \ next == 0 is end of list \ 2. a value \ the actual head of the list is kept \ elsewhere, e.g. in a value. \ 0 value List \ words: \ list.add ( value head -- new.head ) \ list.next ( addr -- addr' ) \ list.value ( addr -- value ) \ list.show ( head -- ) \ list.walk ( xt head -- ) : list.add ( value head -- new.head ) dp >r \ save new.head , \ store head as "next" , \ store "value" r> \ new.head on stack ; : list.next ( addr -- addr' ) i@ \ addr of next entry ; : list.value ( addr -- value ) 1+ i@ \ value ; : list.show ( head -- ) dup 0= if drop exit then begin dup 4 u0.r space dup list.next 4 u0.r space dup list.value 4 u0.r cr list.next dup 0= until drop ; : list.walk ( xt head -- ) dup 0= if drop drop exit then begin \ -- xt item over over \ -- xt item xt item list.value \ -- xt item xt value swap \ -- xt item value xt execute \ -- xt item list.next \ -- xt next dup 0= until drop drop ;