Define a primitive data type: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (auto syntax highlight fix)
Line 751: Line 751:
=== Method 1: Safe Integer Store operators===
=== Method 1: Safe Integer Store operators===
Forth is a fetch and store based virtual machine. If we create a safe version of store (!) the programmer simply uses this operator rather than the standard store operator.
Forth is a fetch and store based virtual machine. If we create a safe version of store (!) the programmer simply uses this operator rather than the standard store operator.
<LANG>DECIMAL
<syntaxhighlight>DECIMAL
: CLIP ( n lo hi -- n') ROT MIN MAX ;
: CLIP ( n lo hi -- n') ROT MIN MAX ;
: BETWEEN ( n lo hi -- flag) 1+ WITHIN ;
: BETWEEN ( n lo hi -- flag) 1+ WITHIN ;
Line 759: Line 759:
: SAFE! ( n addr -- )
: SAFE! ( n addr -- )
OVER 1 10 BETWEEN 0= ABORT" out of range!"
OVER 1 10 BETWEEN 0= ABORT" out of range!"
! ;</LANG>
! ;</syntaxhighlight>
Testing
Testing
<LANG>VARIABLE X
<syntaxhighlight>VARIABLE X
7 X SAFE! X ? 7 ok
7 X SAFE! X ? 7 ok
12 X CLIP! X ? 10 ok
12 X CLIP! X ? 10 ok
Line 771: Line 771:
$7FAA2B0C throw
$7FAA2B0C throw
$7FAD4698 c(abort")
$7FAD4698 c(abort")
</syntaxhighlight>
</LANG>
=== Method 2: redefine standard "store" operator as DEFER word ===
=== Method 2: redefine standard "store" operator as DEFER word ===
Using the code in Method 1, we can re-define the store (!) operator to be switchable.
Using the code in Method 1, we can re-define the store (!) operator to be switchable.
<LANG>DECIMAL
<syntaxhighlight>DECIMAL
: FAST! ( n addr -- ) ! ; ( alias the standard version)
: FAST! ( n addr -- ) ! ; ( alias the standard version)


Line 782: Line 782:
: LIMITS-ON ( -- ) ['] SAFE! IS ! ;
: LIMITS-ON ( -- ) ['] SAFE! IS ! ;
: LIMITS-OFF ( -- ) ['] FAST! IS ! ;
: LIMITS-OFF ( -- ) ['] FAST! IS ! ;
: CLIPPING-ON ( -- ) ['] CLIP! IS ! ; </LANG>
: CLIPPING-ON ( -- ) ['] CLIP! IS ! ; </syntaxhighlight>


Testing
Testing
<LANG>VARIABLE Y
<syntaxhighlight>VARIABLE Y


LIMITS-OFF ok
LIMITS-OFF ok
Line 807: Line 807:
$7FAA2B0C throw
$7FAA2B0C throw
$7FAD4460 c(abort")
$7FAD4460 c(abort")
</syntaxhighlight>
</LANG>
=== Method 3: Create a safe VALUE assignment operator===
=== Method 3: Create a safe VALUE assignment operator===
A VALUE defines a numerical data type that returns it's value rather than an address (pointer) like a variable.
A VALUE defines a numerical data type that returns it's value rather than an address (pointer) like a variable.
We can create a word that assigns a number to a VALUE but tests for out of range errors.
We can create a word that assigns a number to a VALUE but tests for out of range errors.
<LANG>: (->) ( n <text> -- )
<syntaxhighlight>: (->) ( n <text> -- )
OVER 1 10 BETWEEN 0= ABORT" out of range!"
OVER 1 10 BETWEEN 0= ABORT" out of range!"
>BODY ! ;
>BODY ! ;
Line 819: Line 819:
IF POSTPONE ['] POSTPONE (->) \ compiling action
IF POSTPONE ['] POSTPONE (->) \ compiling action
ELSE ' (->) \ interpret action
ELSE ' (->) \ interpret action
THEN ; IMMEDIATE</LANG>
THEN ; IMMEDIATE</syntaxhighlight>


Test
Test
<LANG>0 VALUE Z ok
<syntaxhighlight>0 VALUE Z ok
99 TO Z ok ( normal assignment)
99 TO Z ok ( normal assignment)
Z . 99 ok
Z . 99 ok
Line 831: Line 831:
$7FAA2B0C throw
$7FAA2B0C throw
$7FAD4570 c(abort")
$7FAD4570 c(abort")
$7FAD45DC (->)</LANG>
$7FAD45DC (->)</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==