RosettaCodeData/Task/Flow-control-structures/Forth/flow-control-structures.fth

14 lines
269 B
Forth
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
: checked-array
CREATE ( size -- ) DUP , CELLS ALLOT
DOES> ( i -- a+i )
2DUP @ 0 SWAP WITHIN IF
SWAP 1+ CELLS +
ELSE
1 THROW
THEN ;
8 checked-array myarray
: safe-access ( i -- a[i] )
['] myarray CATCH 1 = IF ." Out of bounds!" 0 THEN ;