Define a primitive data type: Difference between revisions

Line 567:
$7FAA2B0C throw
$7FAD4698 c(abort")
</LANG>
=== Method 2: redefine standard "store" operator as DEFER word ===
If using a special store operator is not-acceptable we can re-define the store (!) operator to be switchable with limits on or off.
<LANG>DECIMAL
: BETWEEN ( n lo hi -- ) 1+ WITHIN ;
 
: FAST! ( n addr -- ) ! ; ( alias the standard version)
: SAFE! ( n addr -- )
OVER 1 10 BETWEEN 0= ABORT" out of range!"
! ;
 
DEFER !
 
\ commands to change the action of '!'
: LIMITS-ON ( -- ) ['] SAFE! IS ! ;
: LIMITS-OFF ( -- ) ['] FAST! IS ! ;
 
VARIABLE Y
\ tests at console
LIMITS-OFF ok
1 Y ! Y ? 1 ok
10 Y ! Y ? 10 ok
11 Y ! Y ? 11 ok
0 Y ! Y ? 0 ok
LIMITS-ON ok
1 Y ! Y ? 1 ok
10 Y ! Y ? 10 ok
11 Y ! Y ?
:25: out of range!
11 Y >>>!<<< Y ?
Backtrace:
$7FAA2B0C throw
$7FAD4460 c(abort")
0 Y ! Y ?
:26: out of range!
0 Y >>>!<<< Y ?
Backtrace:
$7FAA2B0C throw
$7FAD4460 c(abort")
</LANG>
 
 
</LANG>
 
Anonymous user