Listing 2.   UTILITY WORDS

 

 

( BENCH MARKS, 23MAR85CHT )

HEX

: LOOPS     7FFF FOR NEXT ;

 

: LOOPTEST      FOR LOOPS NEXT ;

 

: +TEST     7FFF FOR   I DUP + DROP   NEXT ;

 

: +TESTS        FOR +TEST NEXT ;

 

: -TEST     7FFF FOR   7FFF I - DROP   NEXT ;

 

: -TESTS        FOR -TEST NEXT ;

 

: *TEST     7FFF FOR   I DUP * DROP   NEXT ;

 

: *TESTS        FOR *TEST NEXT ;

 

: /TEST     7FFF FOR   7FFF I / DROP   NEXT ;

 

: /TESTS        FOR /TEST NEXT ;

DECIMAL

 

( WORDS, 26apr89cht )

 

VARIABLE #OUT                    Keep the character count

 

: COUNT ( a -- a+1 c )           Standard COUNT

   1 C@+ SWAP ;

 

: TYPE ( a n -- )                Standard TYPE

   ?DUP

   IF 1 -

      FOR COUNT

         32 MAX 126 MIN          Avoid control/graphic characters

         EMIT

      NEXT DROP

   ELSE DROP

   THEN ;

 

: .ID   ( lfa - )                Display name of a word

   2 +                           Get name field address

   COUNT DUP 2 +                 Characters to be sent out

   #OUT +!                       Update character count

   TYPE                          Display name

   SPACE SPACE  ;                Separators

 

: WORDS ( -- )                   Display names in a vocabulary

   0 #OUT !                      Initiate character count

   CONTEXT DUP @ -               Pointer to vocabulary thread

   @                             lfa of the last word in the thread

   BEGIN

      #OUT @ 70 >                Near the end of a line?

      IF 0 #OUT ! CR THEN        If so, do a carriage return.

      DUP .ID                    Display a name.

      @                          lfa of the next word

      DUP 0=                     End of a thread?

   UNTIL DROP   ;

 

( MEMORY DUMP, 23MAR86CHT )

 

: (DUMP) ( a -- a+16 )           Dump 16 bytes.  Return next address.

   DUP 5 U.R                     Show the starting address first.

   3 SPACES

   15 FOR

      1 C@+                      Get next byte.

      SWAP 3 U.R                 Print it.

   NEXT  ;

 

: DUMP ( a n -- )                Dump with ASCII representation also.

   16 / 1 -                      Dump 16 bytes on a line.

   FOR

      CR DUP 16 TYPE             Display ASCII's first

      3 SPACES

      (DUMP)                     Followed by byte dump.

   NEXT   DROP ;

 

: ERASE ( a n -- )               Handy utility words.

   0 FILL ;

 

: BLANK ( a n -- )

   32 FILL ;

 

: CMOVE ( s d n -- )             Copy forward.

   ?DUP

   IF 1 -

      FOR

         >R 1 C@+                Read a byte from source.

         SWAP R> 1 C!+           Write a byte to destination.

      NEXT

   THEN 2DROP ;

 

: CMOVE> ( s d n -- )            Copy backward.

   ?DUP

   IF 1 -

      DUP >R +                   End of destination array.

      SWAP I + SWAP R>           End of source array.

      FOR                        Scan backward.

         >R 1 C@-

         SWAP R> 1 C!-

      NEXT

   THEN 2DROP ;

 

( LIST, 23MAR86CHT )           Screen display utility words.

 

: -TRAILING ( a n -- a n' )      To minimize RS232 traffic.

   BEGIN

      DUP 0= IF EXIT THEN        Terminate if we have a blank string.

      2DUP + 1 - C@ 32 =         Blank?

   WHILE 1 -                     Decrement count.

   REPEAT ;

 

VARIABLE SCR                     Current screen number under editing.

VARIABLE L#                      Current line number.

64 CONSTANT C/L                  Characters per line

 

: .LINE ( SCR # -- )             Display one line.

   C/L *                         Character offset.

   SWAP BLOCK +                  Address in the disk buffer.

   C/L -TRAILING                 Significant length.

   TYPE ;                        Print one line of text.

 

: LIST ( SCR -- )                Display one screen.

   DUP SCR !                     Remember this screen number.

   DUP CR ."  SCR#  " .          Title line.

   0                             Initial line number.

   15 FOR

      2DUP CR

      DUP 3 U.R                  Show line number first.

      2 SPACES

      .LINE                      Then show text.

      1 +

   NEXT 2DROP ;

 

: T ( N -- )                     Assign current line and display it.

   DUP L# !                      Current line.

   CR

   SCR @ SWAP .LINE   ;          Display it.

 

: L ( -- )                       Display current screen.

   SCR @                         Get screen number from SCR.

   LIST ;                        Display it.

 

: N                              Display next screen.

   1 SCR +! L ;

 

: B                              Display previous screen.

   -1 SCR +! L ;

 

( LINE EDITING, 23MAR86CHT )       Simple 'Starting Forth' line editor.

 

HEX

: WHERE ( N -- addr )            Return address of line n.

   C/L *

   SCR @ BLOCK +   ;

 

: P                              Put following text on current line.

   L# @ WHERE                    Address of current line.

   DUP C/L 20 FILL               Clear it.

   94 WORD                       Get text following P.

   1 C@+ SWAP                    Get the length of text.

   BEGIN   DUP                   If text is not yet exhausted,

   WHILE 1 - >R                  copy character to screen buffer.

      1 C@+ SWAP                 Get next character.

      ROT 1 C!+                  Store it in buffer.

      SWAP R>

   REPEAT

   DROP 2DROP

   UPDATE ;                      Set update bit.

 

: M ( N M -- )                   Copy line n to line m.

   WHERE SWAP                    Address of line m.

   WHERE SWAP                    Address of line n.

   C/L 2/ MOVE                   Copy line.

   UPDATE ;                      Mark update bit.

 

: U                              Insert a line under the current line.

   1 L# +!                       Make next line the current line.

   L# @ 1 +                      One line further down.

   0F OVER -                     To the end of this screen.

   FOR   DUP I +                 Move all line down by one.

      DUP 1 - SWAP M

   NEXT DROP

   P ;                           Copy following text to current line.

 

: X                              Delete current line.

   L# @ 1 +                      Next line.

   0F OVER -                     To end of screen.

   FOR DUP                       Copy all line up by one line.

      DUP 1 - M

      1 +

   NEXT   DROP

   0F WHERE

   C/L 20 FILL   ;               Blank fill the last line.

 

DECIMAL

 

( STACK PICTURE, 19may89cht )

 

: ?   @ . ;                      How can I survive without this?

 

: .S                             Display 5 items on the data stack.

   >R >R >R >R                   Dig into the data stack.

   DUP U.                        5th item.

   R> DUP U.                     4th item.

   R> DUP U.                     3rd.

   R> DUP U.                     2nd.

   R> DUP U.  ;                  1st.

 

: %                              Handy tool.  Insert it any where

                                 you need help, most likely in a loop.

   CR .S                         Snapshot the data stack.

   KEY 32 =                      Pause for you to think.

   IF ABORT" done" THEN ;        Hit space bar if you have enough.

 

 

¡@