Benutzer-Werkzeuge

Webseiten-Werkzeuge


words:checked-array
require ",

VARIABLE range-checking    TRUE range-checking !

: Unchecked-Array ( n -- )  
    CREATE CELLS ALLOT 
  DOES> ( n -- addr ) 
    SWAP CELLS + ;

: Checked-Array ( n -- )
   HERE  >IN @  BL WORD COUNT ",  >IN !  ( n c-addr )
   CREATE OVER , COUNT , , CELLS ALLOT
  DOES> ( n -- addr )
    range-checking @ IF
      >R  DUP R@ @ U< 0= 
      IF ." Index out of range in Array '" R@ CELL+ 2@ TYPE 
         ." '! Actual Index: " U. 
         ." Maximal Index: " R@ @ 1- U. ABORT 
      THEN
      R> 
    THEN
     3 CELLS + SWAP CELLS + ;

: Array ( n -- )  range-checking @ IF Checked-Array ELSE Unchecked-Array THEN ;

Tags: ANS-Forth data-structures safety bounds checking

words/checked-array.txt · Zuletzt geändert: 2010-12-29 18:12 (Externe Bearbeitung)