A solution for words which must have two behaviours (state smart) ! 
Author Message
 A solution for words which must have two behaviours (state smart) !

\ The following example implements a method for handling a lot state
\ problems and the name is FLYER. I think it will work on most ANSI
\ without rewriting. Non ANSI systems should add MARKER POSTPONE  S"
\ and VALUE or change this source to use their own compatible
\ I am aware of the fact that >R and R> could not work on all systems,
\ take care and keep on thinking ;-)

\ Test flyer from Albert Nijhof, version 2


\ Documentation: Flyer version 2.00
\ The original code was designed by Albert Nijhof, you could contact
\ him thru the dutch FIG chapter if you want to use his code commercial.
\ It is written according the ANSI X3.215-1994 standard paper.
\ With this code one could further eliminate the use of state in Forth.
\ It is a small contribution to the discussion about STATE and S"
\ Most immediate words can be written with a compile time action only.
\ The use of FLYER in such a definition allows use while interpreting.
\ You can use FLYER in all definitions, which must have a compile and
\ interpret action. For example 'TO'. With FLYER you only have to write
\ it's compile time behaviour, FLYER takes care of the rest. It also
\ a handy rotating buffer for interpreting 'S"' automaticly. As you will
\ notice FLYER will also handle ." for you, and it will work with your
\ own definitions which must also have a compile and interpret
\ In the routine COLD, BARRIER and FHERE should be initialized. In QUIT
\ must be checked if HERESWAP is executed. If this is not already done
\ HERESWAP must be executed at once. The extra QUIT code follows here:
\ Have fun.
\ Willem Ouwerkerk, chairman of the Dutch FIG chapter.

\ Do a coroutine call (call the calling definition). DIVE does a swap
\ on return addresses. In the case of the example showed at the end
\ of the code '."' Because DIVE is used in FLYER, it first returns to
\ finish the definition of '."' and then finishes the definition of
\ FLYER restoring the normal dictionary and executing the compiled
\ code in the HIDDEN buffer. The sequence 'R> R> SWAP' can on some
\ systems be replaced by '2R>'. This routine could be different for
\ your system, so take care !!
\ NOTE1: The word FLYER itself does also have a dangerous line, where
\        the fly code is pushed to the resturnstack.
\        HERE R>  SWAP >R >R             \ Yes, this piece of code !!!
\ NOTE2: This code needs a change also if you are working with a Forth
\        system that uses a separate segment for high level code.

: DIVE          ( -- )
    R>                                  \ Pop return address to calling
    R>                                  \ Pop return address of previous
    SWAP                                \ Address of calling def. on top
    >R                                  \ Push this address and
    >R                                  \ address of previous def.
    ;                                   \ Return to previous definition,
                                        \ instead of calling definition.

\ The FLYER routines

0 VALUE FHERE                           \ Fly HERE
0 VALUE FLY?                            \ Am I flying
0 VALUE BARRIER                         \ Begin of circulair flyer

$100 CONSTANT FLY-BUFFER                \ Length of flyer buffer

: CIRCULAR      ( -- )
  FHERE  BARRIER  DUP FLY-BUFFER +      \ Flyer-here and address window
  WITHIN IF  EXIT  THEN                 \ Ready if FHERE is within
  BARRIER  TO FHERE                     \ No, set FHERE back to barrier.

: HERESWAP      ( -- )
  FHERE  HERE  DUP TO FHERE  - ALLOT    \ Swap between both dictionary's
  FLY? 0= TO FLY?                       \ Toggle Flyer flag

: FLYER         ( -- )

  CIRCULAR  HERESWAP                    \ Swap to flyer dictionary.
  HERE R>  SWAP >R >R                   \ Push start of 'flying'
  ]  DIVE  POSTPONE EXIT  POSTPONE [    \ Compile temporary definition,
  HERESWAP                              \ back to normal dictionary

\ Initialize flyer pointers

HERE $2000 +                            \ Define a buffer at BARRIER
  DUP TO BARRIER                        \ and initialize
      TO FHERE                          \ BARRIER and FHERE

\ Test flyer with ." definition

: ."  FLYER  POSTPONE ." ; IMMEDIATE    \ Redefined vsn could also be
                                        \ when interpreting.
: S"  FLYER  POSTPONE S" ; IMMEDIATE    \ CORE 'S"' is one with only an
                                        \ action at compiletime, this is
                                        \ the change for the version in
                                        \ file wordset.

CR ." Hey, FLYER is working !!"         \ As you will notice.

    For more information about the  Dutch Forth Users Group,
    orders, 8052-ANS-Forth, ByteForth for AT89x051, Hardware
    course, Bamboe and the 'Egel project.
    Phone: (+31)(0)26 4431305 (voice) or reply with an E-mail.

Mon, 12 Nov 2001 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Package available: combined words and repair STATE-smart systems

2. State-smart words

3. State-smart words

4. State smart words in ANS Forth

5. Compile-a-line model & state-smart words

6. the TO-behaviours vs DO-behaviour (of DOES words)

7. How to replace one or two words with one word with one line of awk code

8. DISA is having an Ada Summit in two weeks

9. Report to Word best solution?

10. The best solution for printing report into MS Word document

11. How to use the pid vis with a two-state output

12. Switching a boolean between two states


Powered by phpBB® Forum Software