Re: Still not getting it.



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



.



Relevant Pages

  • Re: Basic info needed regarding filters (FIR)
    ... advanced digital filter topologies... ... run thousands of interrupts per second, and have guaranteed response ... flag to let the main loop know that a command string is ready, ... main loop in turn calls the command parser, which executes the string. ...
    (sci.electronics.design)
  • Re: Problem with detecting the reception of a datagram
    ... the source system must know that the system is not UP and automatically ... the recieved bytes is zero in the listen module. ... UP and it only executes if the destination system is UP and sending ... thnking u for the response.. ...
    (comp.unix.programmer)
  • Re: How to conditionally use a specified module
    ... that will still fail if the boolean value is set at runtime since use ... executes at compile time. ...
    (perl.beginners)
  • Re: How to conditionally use a specified module
    ... that will still fail if the boolean value is set at runtime since use ... executes at compile time. ... The if pragma executes at compile time. ...
    (perl.beginners)
  • Summary: $HOME environment
    ... Chris ... How can I get the $HOME directory environment from the user I would ... executes, so effectively you're asking su to execute "echo /cw". ...
    (SunManagers)