[net.lang.forth] Advice on Forth

ma168x@sdcc3.UUCP (John Wavrik) (06/28/86)

Perhaps now is a good time for a Forth enthusiast to confess to
some "illegal" practices that can be used during development of
applications.  Before making the confession, however, I must
say that about 90% of program development is done in the usual
way. I produce one or two new screens -- most definitions are
only a few lines long -- definitions are factored so that
control structures are not more than two levels deep.  A screen
is tested. If any of the new words on it have errors, I FORGET
back to the first word, correct the errors, and reload the
screen. [I also use screens for data storage, virtual arrays,
etc., the organization of a disk file into fixed size blocks is
a big advantage!  I do not find this organization inhibiting for
programs because of the way I write Forth.]
   I sometimes use a decompiler. Threaded code makes it easy to
write a decompiler (mine is only 3-5 screens). It is a simple-
minded decompiler, it reports the compiled BRANCH and 0BRANCH
together with offsets, rather than reconstructing the IF..THEN,
etc. Commercial decompilers are advertised to produce perfect
reconstruction of source -- but I have no experience with them.
Since Forth words should not be long and should not have complex
control structures, the simple decompiler has been fine. I most-
ly use it to understand how the vendor of my system has imple-
mented words (which, sometimes, I'd like to modify).
   If I know I will be experimenting with the action of a par-
ticular word, I'll "vector" it:

    : EXPER    'EXPER @ EXECUTE  ;

('EXPER is a variable which will contain the CFA of the version
of the word I'm trying.)  [Actually, the system I use most
(MMS-Forth) provides a defining word VECT which creates a
foward reference just like that created by vectored execution.]
   It should be no secret that the action of previous words can
be changed by a patch.  Suppose a word OLDWORD is defined:

       : OLDWORD    W1 W2 .....  ;

If, after thoughtful consideration, the programmer wants to
have the word begin with some new actions then define:

      : PATCH  <newactions>  W1  ;    ( note the W1)

and do   FIND PATCH ' OLDWORD !

If the programmer wants to replace the action of OLDWORD by
the action of NEWWORD then:

      : NEWWORD  <define the new word>  ;

and do  FIND NEWWORD ' OLDWORD !   FIND EXIT ' OLDWORD 2+ !

[This works on FIG-Forth and FORTH-79 systems where EXIT is
the word compiled by the semi-colon. If it doesn't work for
your Forth, just replace  FIND EXIT  by whatever address is
found at the end of your colon definitions.]  There are also
ways of patching which involve changing the code field of words
(and which work for CODE definitions as well as COLON defin-
itions).
   Sometimes I decide to change the limits in a loop, or some
number that arises as a parameter. I recognize the address of
the literal handler in each version of Forth that I use. A
memory dump shows the location of the number to be changed. I
can also replace any address in a compiled definition by the
address of a word that executes the original word and then
prints the stack (or whatever your heart desires).
   A professional programmer or computer scientist who under-
stand how Forth operates could probably figure out how to make
the above changes and more. The Forth system is so open and
simple that all kinds of unusual manipulations are possible. The
interesting thing is that the same strange things can be done
almost portably on any "threaded code" Forth system. Even more
interesting is the fact that Forth systems are not equipped with
mechanisms for doing these things -- because they are not
necessary.
   I counsel humility:  If you find that you are using tricks
like this frequently, please be willing to accept the possibil-
ity that you don't understand the language.
   When I first started programming in Forth, I didn't realized
how important it was to make the logic of ALL words clear and to
choose names carefully. I still have programs written during my
first year to remind me -- no one can read them, including me!
I have words A1, .., A5 which correspond to the steps in an
algorithm in some book -- which book and which algorithm I've
forgotten. I was, at the time, still enthralled by the cryptic
notation of APL. I produced many one-letter words. Indenting,
factoring, etc. all seemed like a waste of space and memory.
(When you're young, you tend to do stupid things.)
   A programmer who is willing to meet Forth on its own terms
will find it a satisfying language -- not because it supports
tricks (although it does), but because it supports a solid
structured programming style. If names for words are wisely
chosen and the logic of all levels of the application are clear,
Forth can be amazingly easy to maintain. The degree of control
it provides over storage and hardware are major assets.
   I believe that "public domain" versions of Forth play an
important role in disseminating the language. I think, however,
that most users would be best served by purchasing one of the
better commercial versions (they include good editors, floating
point, strings, etc.)  The people who sell these implementations
are often people who do custom programming -- and they are
offering you the version of Forth they use to make their living!

                                            --John J Wavrik
                                              Math Dept - UCSD
                                              La Jolla, Calif
..ucbvax!sdcsvax!sdcc3!ma168x