Re: Why does this work? POSTPONE [Beginner]
- From: Josh Grams <josh@xxxxxxxxxxx>
- Date: Sat, 17 Oct 2009 00:52:44 GMT
CuppoJava wrote:
PS:
Oops, I didn't see your postscript the first time.
Is there a really clear explanation of what exactly the following
words do and how it does them? The books I've read haven't spent too
much time with them:
[ ] : POSTPONE CREATE DOES> , ' [']
Hmm...that's an interesting collection of words.
What books have you read? You might try Stephen Pelc's _Programming
Forth_, if you haven't already.
If you're interested in learning to use Forth (as opposed to learning
how Forth is implemented, which isn't the same thing at all), you
probably shouldn't worry too much about exactly how the words work, as
the details change from one implementation to another, and each one
often has different limitations and features. So it's better (IMO) to
focus on *what* they do.
That said, I don't think there's a lot that you need to know to use
those words, so here's my two cent explanations:
The brackets allow you to switch out of compilation state temporarily.
`[` switches to the interpreter and `]` switches back to the compiler.
For instance, it might be used to show the calculation of a literal:
: PAGES ( u -- u' ) [ 4 1024 * ] LITERAL * ;
----
Colon `:` takes a name from the input stream and begins a new
definition. On a Standard system, the name isn't visible until `;`
(semicolon). Some other systems make it visible immediately. This has
a minor effect on the syntax for recursion and for extending the
functionality of an existing word.
----
CREATE defines words that hold data. Like colon, it takes a name from
the input stream. The name is immediately visible, and returns an
address. The word starts out empty, and you can use ALLOT and , (comma)
to allocate space for whatever data you want. `DOES>` allows you to add
behavior to the most recently CREATEd word, so that it can do other
things than simply return the address. The usual example is:
: CONSTANT ( x "name" -- ) CREATE ,
DOES> ( [addr] -- x ) @ ;
`DOES>` ends the definition of CONSTANT and starts a new piece of code
which will be added to the behavior of each name that CONSTANT creates.
So take:
144 CONSTANT gross
When CONSTANT is interpreted, CREATE will define a name (gross), and `,`
will allocate a cell and store the value (144). `gross` will initially
return the address, but then DOES> will immediately add the behavior
`@`.
----
In Forth, most memory is allocated by starting at the bottom of a big
block of memory and working upward. It is generally never freed, except
by exiting or restarting the Forth system. There is a word HERE which
returns the first free memory address. So , (comma) simply stores a
value in the cell at HERE, and allocates that cell by advancing HERE to
point past it.
In some systems, you can use comma to compile a word into the current
definition. But some systems may do other things (for instance, compile
the machine code for a CALL instruction), so to be portable you should
use `COMPILE,` (compile-comma) for that purpose.
----
`'` (tick) takes a name and returns a reference to the code (called an
"execution token" or "xt") for that name. The details of how name map
to code is one thing that varies a lot between different Forth
implementations. In general, you can use tick on any words that you
define yourself, but it may not work on all words that come with your
Forth system.
----
You might try to do this:
: foo ' bar execute ;
But since `'` is not immediate, it will get compiled into `foo`. So
when you execute `foo`, it will parse a name and try to look it up.
Which may not be what you wanted. To do this, we have the immediate
word `[']` (bracket-tick) which looks up a name at compile time and
compiles its xt.
--Josh
.
- References:
- Why does this work? POSTPONE [Beginner]
- From: CuppoJava
- Why does this work? POSTPONE [Beginner]
- Prev by Date: Re: ANS Forth's third stack
- Next by Date: Re: The return stack is making it hard to factor my words. [Beginner]
- Previous by thread: Re: Why does this work? POSTPONE [Beginner]
- Next by thread: Re: Why does this work? POSTPONE [Beginner]
- Index(es):
Relevant Pages
|