Secret of Immediate Words? 
Author Message
 Secret of Immediate Words?

I had to read this a few times, but I think I now understand what Chuck
meant when he said Forth didn't have any syntax.  The concepts here are very
powerful.  A very extensible language that could do anything that you could
imagine.

This is how OO concepts must be implemented in various Forths.  Any limit to
this ability?

Quote:
> They use the stack it the stack is appropriate to what they do. Here are
> some words that add compatibility with ANS to an older Forth:


> : ASCII   CHAR [COMPILE] LITERAL ; IMMEDIATE \ Compile only
> : [CHAR]  [COMPILE] ASCII ; IMMEDIATE \ ANS compatibility

> CHAR is an ordinary word.

> WORD scans the input stream for a space ( BL ) delimited entity and
> leaves it in a temporary buffer as a counted string, putting the address

> right after the count -- the first one in the string. The effect is to
> put the ASCII code for the first character in a space-delimited sequence
> on the stack and to discard subsequent ones.

> LITERAL is an immediate word that takes an item from the stack and puts
> it into the dictionary, along with code to put it back at run time. Used
> sensibly, it adds to a growing definition.

> [COMPILE] is an override that causes a following immediate word to be
> compiled instead of executed.

> ASCII is from older Forths. It puts the ASCII code for a character into
> a definition. It lets me write ' ASCII - ' instead of ' 45 ' in a
> definition. It's not that I have no ASCII table; it clarifies the intent
> of the code. A proper definition if ASCII works in compile and in
> interactive mode; mine doesn't. The ANS name for my ASCII is [CHAR] .

> Jerry

> P.S. A proper ASCII could be built this way (assuming that interactive
> input always comes from the keyboard):

> : ASCII  ( -- symbol code if not compiling)

>              ELSE KEY
>              THEN ; IMMEDIATE

> An immediate word executes even in compile mode, but it can execute any
> time, so this ASCII is OK. For example, LITERAL does its work any time:

> SwiftForth 2.2.2 09Mar2001
> here . 4632424  ok
> 22 literal  ok
> here . 4632435  ok

> The "22", and the code to push it onto the stack are now orphans in the
> dictionary. "Orphans" because there is no way for them to run. Like
> greedy DNA, they take up room without other effect.

> P.P.S In ANS Forth, [COMPILE] is replaced by POSTPONE.
> --
> Engineering is the art of making what you want from things you can get.
>



Tue, 12 Apr 2005 11:10:20 GMT  
 Secret of Immediate Words?

Quote:
> ans-4th 'POSTPONE' can suppress the 'immediacy' of the next word in the
> input stream such that it can be compiled as other words, while in
compiling
> state. e.g. in a 'colon definition'.

So Forth gives the programmer total control.


Tue, 12 Apr 2005 11:11:49 GMT  
 Secret of Immediate Words?

Quote:

> > Similar to defining words, it has instructions it executes when it
> > is encountered, which lay down instructions that will be executed when
> > the word which was being compiled is executed.

> So it is a pre interpreter for Forth words.  Then immediate words would have
> to be marked a certain way for the compiler to distinguish them.  A
> different dictionary?

a) a special bit in the header is set for immediate words
   and clear for non-immediate words;
b) a separate word list (it is a bit less correct to
   call it "vocabulary" or "dictionary").

But if your system uses a separate word list for immediate words,
the mechanim of word lists is used by the system and
its use by the application is subject to restrictions,
to gurantee that the whole thing does not break.

I prefer the first approach.



Tue, 12 Apr 2005 14:10:19 GMT  
 Secret of Immediate Words?

Quote:

> > Similar to defining words, it has instructions it executes when it
> > is encountered, which lay down instructions that will be executed when
> > the word which was being compiled is executed.

> So it is a pre interpreter for Forth words.  Then immediate words would have
> to be marked a certain way for the compiler to distinguish them.  A
> different dictionary?

"Pre interpreter" is a poor term.  There's no "pre" anything, and indeed
the only "interpreting" is the parsing of the input stream, which takes
place identically when handling source files and a command line.  As the
input stream is processed, words are looked up in the dictionary and
executed.  Execution of : (colon) causes a special mode that we call
"compiling" (which may or may not be the same sort of process that
C, etc., call "compiling") which generates references to words that
are not immediate, while executing immediate words.

The result of "compiling" a colon definition is executable code.
The exact form of this executable code differs from one implementation
to another (it may be actual machine code, tokens, or pointer lists,
for example), but what it does is execute.

Early descriptions of Forth assumed an implementation involving
pointer lists, and referred to an "inner interpreter" or (better)
"address interpreter" executing such lists, but that terminology
has always been misleading, because most folks assume "interpreting"
to mean a much more complex process, such as handling interpreted
Basic and similar languages.  In ANS Forth the term "interpret" is
strictly limited to processing the input stream.

And, yes, immediate words are marked, as Michael describes.  The
word FIND, for example, returns a flag which is positive if the
found word is immediate, negative otherwise.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================



Wed, 13 Apr 2005 02:11:14 GMT  
 Secret of Immediate Words?

Quote:

> I had to read this a few times, but I think I now understand what Chuck
> meant when he said Forth didn't have any syntax.  The concepts here are very
> powerful.  A very extensible language that could do anything that you could
> imagine.

Exactly!

Quote:
> This is how OO concepts must be implemented in various Forths.  Any limit to
> this ability?

No limits that I know of, but you're only scratching the surface so far.
Now understand DOES> and what it makes possible...

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather   (US & Canada)   800-55-FORTH
FORTH Inc.                         +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================



Wed, 13 Apr 2005 03:45:21 GMT  
 Secret of Immediate Words?
On Fri, 25 Oct 2002 03:11:49 GMT

Quote:

> > ans-4th 'POSTPONE' can suppress the 'immediacy' of the next word in the
> > input stream such that it can be compiled as other words, while in
> compiling
> > state. e.g. in a 'colon definition'.

> So Forth gives the programmer total control.

that's the consequence (and the reason) :)

best,
        hp

--
   >>> pse, reply to : clf -at- lxhp -dot- in-berlin -dot- de <<<
Linux,Assembly,Forth: http://www.lxhp.in-berlin.de/index-lx.shtml en/de



Wed, 13 Apr 2005 07:39:12 GMT  
 Secret of Immediate Words?

Quote:
> Here is an example of an IMMEDIATE word that is so familiar we rarely
think
> of it in that context. But it shows clearly why immediacy is vital to the
> Forth compilation mechanism. We begin a new definition with a colon, ":" .
> Colon _is_ the compiler (or anyway, one of them!) in Forth. Colon is
EXECUTEd,
> and it performs the following actions:

> 1. Create a new dictionary header whose name is the first piece
>    of text following ":" , as in the definition

> : *+    *  +   ;

>    In the above example the new name is "*+" .

> 2. Switch the system from interpret- to compile mode. Now each
>    subsequent piece of text (e.g. "*" and "+" above) is read,
>    looked up in the dictionary, and (if found) compiled rather
>    than EXECUTEd (because the system is in compile mode). If
>    the text is not found in the dictionary, the system tries to
>    convert it to a number in the current base. If this is pos-
>    sible, the number is entered into the code as a LITERAL.
>    (And, BTW, LITERAL is an IMMEDIATE word!) If the text is
>    neither an existing word nor a number, the system ABORTs
>    with an error message.

> 3. So now what? Barring an error, does the system keep on
>    compiling everything you enter after the colon and the
>    name? Help! How can you halt the Sorcerer's Apprentice?

>    The answer, of course, is the semicolon ";" that termin-
>    ates a definition. It is IMMEDIATE, so instead of being
>    compiled, it is EXECUTEd. The task of ";" is to install
>    appropriate code for popping the return stack and trans-
>    ferring control to the next word in sequence (or whatever
>    the particular Forth's word-terminating procedure is).
>    Then it switches the system back to interpret mode.

> So you see, being able to mark a word IMMEDIATE (usually IMMEDIATE
> sets a bit or flag in the definition that has just finished) is vital to
> being able to exit the compiler.

> Since an IMMEDIATE word simply has a bit/flag set, you can see how it
works:
> when the system is interpreting, it ignores this flag. But if it is
compiling,
> the system checks this flag--if it is set, the word's "execution token" is
> passed to the execution mechanism which EXECUTEs it just as though the
system
> were in interpretation mode; whereas if the flag is not set, the execution
> token is simply compiled into the new definition under construction.

> Finally, this behavior can be modified (an IMMEDIATE word can be compiled,
> e.g.) by words like POSTPONE and [COMPILE]. See the widely available
dpANS6
> for details.

Great example, thank you.

Quote:
> You will also find a description of compilation, with diagrams, in the
latest
> version of "A Beginner's Guide to Forth (HTML)" at

> http://www.*-*-*.com/ .{*filter*}ia.edu/classes/551.jvn.fall01/

I look forward to reading this.

Thanks,

John



Wed, 13 Apr 2005 23:38:24 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software