Anonymous user
Define a primitive data type: Difference between revisions
→{{header|Forth}}
Line 547:
=={{header|Forth}}==
{{ works with|ANS/ISO Forth|}}
Standard Forth does not type data but it does "type" words that act on data.
=== Method 1: Safe Integer Store
Forth is fetch and store based virtual machine. If we create a safe
<LANG>DECIMAL
:
: BETWEEN ( n lo hi -- flag) 1+ WITHIN ;▼
\ programmer chooses CLIPPED or SAFE integer assignment
: CLIP! ( n addr -- ) SWAP 1 10 CLIP SWAP ! ;
: SAFE! ( n addr -- )
OVER 1 10 BETWEEN 0= ABORT" out of range!"
! ;
\ testing
VARIABLE X▼
7 X SAFE! X ? 7 ok▼
12 X CLIP! X ? 10 ok
▲VARIABLE X
▲7 X SAFE! ok
99 X SAFE!
:64: out of range!
Line 569 ⟶ 573:
</LANG>
=== Method 2: redefine standard "store" operator as DEFER word ===
<LANG>DECIMAL
▲: BETWEEN ( n lo hi -- ) 1+ WITHIN ;
: FAST! ( n addr -- ) ! ; ( alias the standard version)
DEFER !
\ commands to change the action of '!'
: LIMITS-ON ( -- ) ['] SAFE! IS ! ;
: LIMITS-OFF ( -- ) ['] FAST! IS ! ;
: CLIPPING-ON ( -- ) ['] CLIP! IS ! ;
VARIABLE Y
|