Re: Still not getting it.
- From: Charlie Springer <RAM@xxxxxxxxxxxx>
- Date: Fri, 1 Jul 2005 17:09:22 -0700
On Fri, 1 Jul 2005 13:45:42 -0700, Paul Marciano wrote
(in article <1120250742.280929.235650@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>):
> 1. Try as I might I still can't make intelligent use of create> does.
> It seems that mastering this construct is key to unlocking the power of
> the language. Does anyone know of a reference that really goes deep
> into create> does with lots of real examples?
I can remember when I first struggled with this (though it was called <BUILDS
DOES> ). I wasn't till I had a good grasp of the layout of the Forth code in
a definition that I began to grasp the idea.
In a colon definition, at compile time -- as the Forth word addresses are
being laid down in memory for a typical indirect threaded Forth -- pointers
to CREATE and any words that follow are laid down in memory. They are not
executed. The words following DOES> are what you want to execute when a
created word is run.
You have to deal with:
Compile time for the creator, the definition using CREATE DOES> .
Run time of the creator is compile time of the created word. Below, when
Vector executes is creates a definition.
Run time of the created word is the DOES code.
An old example (maybe out of date since I don't use ANSI except as a subset
in MOPS) for creating arrays or vectors.
: Vector ( Create one dim array of 16 bit data. Starts at zero. )
CREATE 2* allot ( n <name> --- Used 10 Vector myArray )
DOES> SWAP 2* + ; ( n baseaddr --- addr )
Note that at runtime, the created word leaves the address of its parameter
field on the stack.
Maybe this would use CELL or something else now.
1024 Vector stimulus OK
1024 Vector gain OK
1024 vector response OK
4000 50 stimulus ! OK
50 stimulus @ . 4000 OK
What happened here? When Vector first executes it creates a header for a word
called stimulus and allots 2K bytes for data. It is invoked again to create
gain and response arrays. This is just like the word Variable, but it makes
more space. It also makes the pointer to the DOES code the execution address
for all three.
So, when stimulus (or gain or response) executes, it takes whatever is on the
stack, multiplies it by two and adds it to its base address of the parameter,
or in this case you might say its data field . This leaves the address of the
nth element on the stack.
More sophisticated code for structures and assemblers can have words that
create words that create words. Once you get the hang of it, you have a very
powerful tool.
Hope that helps more than confuses.
Note: alternate if your 2* is slow.
DOES> SWAP 2* + ; ( n baseaddr --- addr )
DOES> OVER + + ; ( n baseaddr --- addr )
- Charlie Springer
.
- Follow-Ups:
- Re: Still not getting it.
- From: Judges1318
- Re: Still not getting it.
- References:
- Still not getting it.
- From: Paul Marciano
- Still not getting it.
- Prev by Date: Re: Standard again
- Next by Date: Re: Unnecessary abstraction
- Previous by thread: Still not getting it.
- Next by thread: Re: Still not getting it.
- Index(es):
Relevant Pages
|