[net.lang.forth] Answers to your questions about FORTH

philm@astroatc.UUCP (Phil Mason) (06/17/86)

In article <650@ucbcad.BERKELEY.EDU> faustus@ucbcad.BERKELEY.EDU (Wayne A. Christopher) writes:
>Please explain to us how some of the features you mention work.  What is
>"threaded"?  How can forth be both a "high level language" and "low level
>language"?  How can it be customized?  And most importantly, what does it
>look like?  Can you include a short program in both C and forth so we can
>compare them?  Thanks.

By the way, follow-ups are to "net.lang.forth". Subscribe now! I think we've 
exhausted the architectural arguments pertainent to "net.arch". 

"Threaded code" refers to the technique of describing algorithms by the use of 
a list of the addresses of all of the component routines in the proper order.
A very simple interpreter recursively follows each address entry in the
algorithm, following each of the address entries for each component routine 
and each component of each component, etcetra, until machine code is found at 
the lowest level. The machine code is executed and then you are popped up a 
level and continue interpreting/executing until you are finished with the
routine. 

Threaded code is incredibly compact and simple to analyze. The major
drawback is the expense of subroutine calls (i.e., thread following) on most
computers. Threaded code is as least efficient as the product of a typical
code generator of a medium quality compiler. Good hand coding and a good 
compiler can almost always perform better than raw threaded code. FORTH, which 
uses threaded code, allows embedded assembly language among the threaded code 
for speed, if desired.

Threaded code works best if all arguments and results of the routines are
handled via a stack. In FORTH, the argument/result stack is called the
"parameter stack". Routines take from the stack the arguments that they need
and put onto the stack their results. The parameter stack is directly analogous
to the stack of an HP calculator. For example, the "+" key takes two parameters
off the stack and puts their sum onto the stack. FORTH uses PostFix or Reverse 
Polish Notation as do HP calculators. 

I don't happen to have meaty examples lying around right now, but here is some
simple FORTH code to do factorials (iteratively) :

: FACT    ( n -- )    ":" means begin a FORTH routine definition called "FACT".
                      "( n -- )" is a comment that means FACT takes one
                          number off of the stack and returns nothing.

     DUP              "DUP" duplicates the item on the top of the stack, i.e.
                          there are two "n"s on the stack now.

     1                "1" is a number to be pushed onto the stack - the stack
                          now looks like this : "( n n 1 -- )", "1" is on the
                          top of the stack.
     
     DO               "DO" takes the top two items off the stack - the first one
                          is the beginning value for the loop and the second 
                          one is the ending value of the loop. The loop will
                          go from 1 to n-1.

          I           "I" puts the current value of the loop index onto the 
                          stack.

          *           "*" takes the top two items off the stack and replaces 
                          them with their product.

     LOOP             "LOOP" increases the index by one  - if it is equvalent
                          to the ending value, exits the loop; otherwise,
                          execution branches to the word after the "DO" word.

     .                "." prints the top item of the stack on the terminal.

     CR               "CR" issues a carriage return on the terminal.

;                     ";" ends the definition of FACT.

Upon typing this definition into the FORTH interpreter, try this :

6 FACT

You will get :

720

You can easily write a new definition to make a list of factorials :

: FACTLIST ( n -- )

  1 + 
  1
     DO
         I DUP
         .
         FACT
     LOOP
;

You should be able to tell me what this one does.          

As you can see, FACT is able to be used exactly as if it were a primitive
FORTH word. FORTH is extensible in just this way. You can create vocabularies 
of words for use with your application. The implicit parameter passing can
make your life alot easier. Good documenting skills are really essential
if you want to be able to maintain any code in any language.

FORTH is a high-level language because you can abstract detail and hide 
information in modules, just like you can in many other languages.

FORTH is a low-level language since it is really implemented as a 
portable pusedo-machine that actually runs FORTH as its native code. All FORTH
words are accessible to the programmer, even the ones used to complete and
enhance the FORTH interpreter. Only a very small part of FORTH is actually in 
assembly language. The rest is in FORTH itself! 

FORTH is like a software toolbox AND like a sophisticated modular language 
rolled into one. I believe that it has the potential to be of both worlds.


-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Kirk  : Bones ?                |  Phil Mason, Astronautics Technical Center
Bones : He's dead Jim.         |  Madison, Wisconsin - "Eat Cheese or Die!"
- - - - - - - - - - - - - - - -|  
...seismo-uwvax-astroatc!philm |  I would really like to believe that my
...ihnp4-nicmad/               |  employer shares all my opinions, but . . . 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=