home *** CD-ROM | disk | FTP | other *** search
- 0 CONSTANT US>D ( convert unsigned single to double )
-
- : % ( n1 n2 - ) ( calculates and prints percentage to tenths )
- 35 10 GOTOXY ( position cursor )
- 10000 SWAP */ 5 + 10 / ( figure percentage and round )
- US>D <# # ASCII . HOLD #S #> ( format number as string )
- TYPE ASCII % EMIT ; ( type it with % )
-
- ( Note: Cursor-positioning is vendor dependent. )
- ( ASCII is immediate. It puts on the stack the )
- ( ASCII value of the character that follows it. )
-
- Listing 1: Calculating and printing percentage
-
-
-
- : .0% ( n1 n2 - n3 ) ( n3 = %age n1 is of n2, rounded to tenths )
- 10000 SWAP */ 5 + 10 / ;
-
- : TENTHS ( n - adr cnt ) US>D <# # ASCII . HOLD #S #> ;
-
- : %. ( n1 n2 - ) .0% TENTHS TYPE ASCII % EMIT ;
-
- : %.R ( # n1 n2 - ) ( # is width of field; display flush right )
- %.0 TENTHS ROT OVER - SPACES TYPE ASCII % EMIT ;
-
- Listing 2: A more general approach
-
-
-
- 440 CONSTANT A ( note defined by its frequency )
-
- : OCTAVE ( creates a note of double the frequency )
- 2* CREATE ,
- DOES> ( <adr> -- freq ) @ ;
-
- A OCTAVE A' ( defines the frequency of the octave )
-
- : OCTAVE 2* CONSTANT ; ( alternate definition )
-
-
- Listing 3: Using CONSTANT in a defining word
-
-
-
- CREATE OPTIONS ] >PRINTER >DISK >SCREEN >DOS [
-
- : DO-OPTION ( n - ) 2* OPTIONS + @ EXECUTE ;
-
- 0 DO-OPTION ( to printer )
- 1 DO-OPTION ( to disk )
- 3 DO-OPTION ( to DOS )
- 4 DO-OPTION ( unpredictable results )
-
- Listing 4: Execution array, first definition
-
-
-
- 0 CONSTANT F -1 CONSTANT T
-
- : VECTOR: : ( compile operators )
- DOES> SWAP 2* + @ EXECUTE ;
-
- VECTOR: OPTION >PRINTER >DISK >SCREEN >DOS ;
-
- 0 OPTION ( to printer )
- 2 OPTION ( to screen )
-
- Listing 5: A defining word for execution vectors
-
-
-
- CREATE BITS 1 C, 2 C, 4 C, 8 C, 16 C, 32 C, 64 C, 128 C,
-
- : S>B ( ? - f ) 0<> ; ( forces to a boolean: -1 or 0 )
- : MASK ( bit# - mask ) BITS + C@ ;
-
- : AIM ( # a - bit# a ) SWAP 8 /MOD ROT + ;
-
- : +BIT ( bit# a - ) AIM SWAP MASK OVER C@ OR SWAP C! ;
- : -BIT ( bit# a - ) AIM SWAP MASK NOT OVER C@ AND SWAP C! ;
- : @BIT ( bit# a - f ) AIM C@ SWAP MASK AND S>B ;
- : ~BIT ( bit# a - ) AIM 2DUP @BIT IF -BIT ELSE +BIT THEN ;
-
- Listing 6: Bit twiddlers
-
-
-
- CREATE TEST 16 ALLOT
-
- : SETflags TEST 16 ERASE
- ASCII ! TEST +BIT
- ASCII & 1+ ASCII # DO I TEST +BIT LOOP
- ASCII ( TEST +BIT ASCII ) TEST +BIT ASCII ' TEST +BIT
- ASCII ` TEST +BIT ASCII _ TEST +BIT ASCII - TEST +BIT
- ASCII { TEST +BIT ASCII } TEST +BIT
- ASCII Z 1+ ASCII @ DO I TEST +BIT LOOP
- ASCII 9 1+ ASCII 0 DO I TEST +BIT LOOP ;
-
- : READOUT 128 0 DO I TEST @BIT IF I EMIT THEN LOOP SPACE ;
-
- : READ 16 0 DO TEST I + @ . 2 +LOOP ;
-
- SETflags ok
-
- READOUT !#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`{} ok
-
- READ 0 0 9210 1023 -1 -30721 1 10240 ok
-
- Listing 7: Bits for valid filename characters
-
-
-
-
- CREATE LEGAL 0 , 0 , 9210 , 1023 , -1 , -30721 , 1 , 10240 ,
-
- ( Bit set in LEGAL only if character is legal in filename )
- ( Map is by ASCII value of the character. )
-
- : OK-CHAR? ( ASCII-char -- f ; T = valid character for filename )
- LEGAL @BIT ;
-
- Listing 8: Checking characters