[comp.lang.forth] The most fantastically fun word in FORTH ever!

cs472226@umbc5.umbc.edu (David Wood (CS472226)) (05/05/89)

   Okay, maybe I heaped on the adverbs a little too thick.
   But I've recently rediscovered my favorite word in FORTH! Sure, you
could talk about WISC hardware and the latest version of FORTH for x
system, but how often can one word provoke such enthusiasm?
   The word is, in GS16FORTH:
   : myself LATEST PFA CFA , ; immediate
   What this does is during compilation of a word, it takes the Name
Field Address of the most recently entered word (which would be the one
you're compiling when this appears [LATEST]), gets its Parameter Field
Address [PFA], gets its Code Field Address from that [CFA], then
compiles it into the dictionary [,]. The [immediate] after it means the
word is executed rather than compiled during the compilation phase.
   The result: The word's CFA is compiled into the word itself. Instant
recursion! The word now calls itself! Used indiscriminately, it will do
such nasty things as infinite loops, blown stacks, and other
heinousness.
   Error handling in the form of compilation-only protection is
forthcoming (no pun intended), but if you're just a little careful, I'm
sure there are people here who can find all sorts of wonderful ways to
abuse it, and I want to hear about every one! 

                                                    -David Wood
************************************************************************
*  A Mind is a Terrible Thing  ***  Sometime in June, this account     *
* to have Oozing out          ***  turn into a pumpkin. Help this poor * 
* your ears...               ***  system addict stay in touch with     *
*      -- The League of     ***  his contacts on UseNET and/or         *
*         Sadistic         ***  BitNET. All contributions will be      *
*         Telepaths       ***  generously accepted through E-MAIL.     *
************************************************************************

bouma@cs.purdue.EDU (William J. Bouma) (05/06/89)

In article <1993@umbc3.UMBC.EDU> cs472226@umbc5.umbc.edu.UUCP (David Wood (CS472226)) writes:
>
>   But I've recently rediscovered my favorite word in FORTH! Sure, you
>could talk about WISC hardware and the latest version of FORTH for x
>system, but how often can one word provoke such enthusiasm?
>   The word is, in GS16FORTH:
>   : myself LATEST PFA CFA , ; immediate
>   The result: The word's CFA is compiled into the word itself. Instant
>recursion! The word now calls itself! Used indiscriminately, it will do
>such nasty things as infinite loops, blown stacks, and other
>heinousness.
>                                                    -David Wood
>************************************************************************

Nice word!
Isn't recursion in the FORTH standard yet?
Way back when I got my first first FORTH program in the form of assembly
language for a 6502 micro, it did not allow recursion. That was FIG-FORTH79
standard. It was really stupid , since all I had to do to make recursion
work in the thing was to put the word being defined into the dictionary as
soon as it was seen, rather than after the definition was complete. The
stack in FORTH makes recursion automatic.

Could someone send|tell me how to get a description of the current FORTH
standard?

: unlink [compile] ' dup lfa @ swap nfa context @ @ dup >r 
         begin r> drop pfa lfa dup >r @ 2dup = until
         2drop r> ! ;

-- 
Bill <bouma@cs.purdue.edu>  ||  ...!purdue!bouma 

A-PIRARD@BLIULG11.BITNET (Andr'e PIRARD) (05/08/89)

David,

Look at this one, not bad either.

: MACRO \ create a word interpreting the rest of the line
   CREATE IMMEDIATE     \ an immediate word
   0 WORD C@ 1+ ALLOT   \ with end-of-line in pfa
   DOES> COUNT EVALUATE ; \ and interpreting its text

It's used as: MACRO FOR-SHORT any interpretable text

Of course, you must have EVALUATE \ addr count --
That saves the current input stream environment, sets it to the input one
and finally restores the original input stream where it was left.
EVALUATE is most useful in many situations. It's standard in Comforth but
shouldn't be too hard to implement in any other Forth system.

Another one:

: TRANSIENT \ define overlay loader
  CREATE    \ fna fnl <name> --
    HERE C! HERE COUNT DUP 1+ ALLOT CMOVE   \ store filename
  DOES>             \ load overlay and execute synonym
    L>IN @ >IN !    \ regress input stream
    COUNT ." Loading " 2DUP TYPE SPACE OVREAD ;

" filename" TRANSIENT EDIT

will define a word EDIT used to load the overlay file <filename>, then
execute the synonym word EDIT from that file. The trick here is that the
input stream is backed to re-execute the very same word again, once the
overlay is loaded. This is possible only if L>IN gives you the offset
in TIB of the last word interpreted, just another one of Comforth.

Forth is the kind of language to suggest wit instead of brute force.
These examples are much in line with the beauty of the language itself.

Any other?

Andr .

toma@tekgvs.LABS.TEK.COM (Tom Almy) (05/09/89)

In article <6716@medusa.cs.purdue.edu> bouma@cs.purdue.EDU (William J. Bouma) writes:
>In article <1993@umbc3.UMBC.EDU> cs472226@umbc5.umbc.edu.UUCP (David Wood (CS472226)) writes:
>>   But I've recently rediscovered my favorite word in FORTH! 
>>   The word is, in GS16FORTH:
>>   : myself LATEST PFA CFA , ; immediate
>>   The result: The word's CFA is compiled into the word itself. Instant
>>recursion!

>Nice word!
>Isn't recursion in the FORTH standard yet?
>Way back when I got my first first FORTH program in the form of assembly
>language for a 6502 micro, it did not allow recursion. That was FIG-FORTH79
>standard. It was really stupid , since all I had to do to make recursion
>work in the thing was to put the word being defined into the dictionary as
>soon as it was seen, rather than after the definition was complete. The
>stack in FORTH makes recursion automatic.

Well, MYSELF or RECURSE or (who knows what else it might be called) is 
found in most implementations, and can be easily added to those Forths
that don't have it.  

There is a good reason *not* to make a word be visible in the dictionary
before it is seen -- sometimes it is nice to be able to redefine a
word in terms of itself.  As an example:

: /  DUP 0= IF ." ATTEMPT TO DIVIDE " DROP . ." BY ZERO"  ABORT THEN  / ;


There is a nice feature in Laboratory Microsystem's Forths (and others)
that allows forward referencing of colon definitions and thus mutually
recursive words.  It is used in this way:

F: WORD-B

: WORD-A   ... WORD-B ... ;

R: WORD-B  ... WORD-A ... ;


Tom Almy
toma@tekgvs.labs.tek.com