home .. forth .. colorforth mail list archive ..

Re: [colorforth] does>


>   Being very new to colorForth, I was wondering what the equivelant of
>   DOES> is in colorForth (if there is one?) I'm just trying to
>   understand how I can use CREATE DOES> in colorForth. Perhaps a
>   simple example (like CONSTANT) would be helpful?

ColorForth doesn't.  However, all is not lost.  We can reproduce the desired
behavior by exploiting ColorForth's ability to seamlessly change between
compile and interpret modes, plus the fact that it has a separate data stack
and return stack.  In this case, the return stack is going to be the star
player, not the data stack.

For example, suppose we want to display a string.  We can use this technique
(assuming my own Forth, as I can't post Color markup here):

: print R> count type ;

This word is used very simply:

: hello print [ 5 C, 'W C, 'o C, 'r C, 'l C, 'd C,

Some things to note:

1.  Calling print places the address of the string data on the return stack.

2.  print pops the return address of the return stack for its own use, thus
leaving the address of the word which called 'hello' on the stack.  It is
therefore not possible to "return to the string."

3.  Note that compilation is aborted after calling print.  In my case, I use [;
in the case of ColorForth, you'd switch back to yellow text.  There is no need
to terminate the word definition.

On the surface, it seems that this faithfully implements DOES>, but there seems
to be no equivalent to create.  This isn't strictly true.  Consider:

: $ >R R@ C, HERE R@ MOVE R> ALLOT ;

: hello print [ S" Hello world!  This is a longer string!" $
        ^^^^^                                              ^
      This is the                                    This is the
      DOES> part                                     CREATE part

While not 100% convenient, it works well enough to eliminate the need for
CREATE and DOES> in most cases, thus making the compiler a bit simpler.  It's
also more orthogonal too, as the : is now used to universally bind meaning to
words, regardless of how the word is actually constructed.  This meshes well
with ColorForth's philosophy, where there is precisely *one* way to define a
new word: make it red.

Remember that CREATE parses ahead in the input stream, to create a word with a
name not known at compile-time (the name is known at interpret/load-time
though, obviously).  ColorForth uses color to flag when a word should be
defined, and hence, CREATE's behavior cannot be faithfully reproduced in a
ColorForth environment.  But, that's OK -- the solution above is not only
simpler, it's actually easier to use when in a ColorForth environment. 
Remember that I have lots of ugly punctuation in my Forth examples, because of
the limitations of e-mail.  It's much cleaner in ColorForth.

--
Samuel A. Falvo II


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com