Subject:
|
CONSTANTS and VALUES in pbForth
|
Newsgroups:
|
lugnet.robotics.rcx.pbforth
|
Date:
|
Wed, 24 May 2000 13:03:44 GMT
|
Viewed:
|
1557 times
|
| |
| |
> I still want to create a constant from within a definition. I
> have an initializing word that will calculate the value of
> the constant. For various reasons I can't do this calculation
> except within the runtime behavior of the word.
There are a couple of things in this note. One is an introduction
to CREATE DOES> and the other is a solution to your actual problem.
Basically, what CREATE DOES> does is separate the run-time and
compile time semantics of a word. This is especially useful for
data structures like arrays. The following is an example from
Brodie's "Starting Forth", and illustrates how CONSTANT works
in the first place...
: NEW_CONST CREATE , DOES> @ ;
What this NEW_CONST does is CREATE a dictionary entry and compile
the constant value (using ",") in the following cell. That's the
compile time behaviour.
When you interpret a CREATEed word, it returns the address of the cell
following the definition, which is where it compiled the constant's
value. The @ just grabs it and puts it on the stack for you.
76 NEW_CONST TROMBONES makes a new constant with a value
of 76....
Soooo, how does this work INSIDE a definition?
This is a common enough problem that ANSI FORTH has a construct called
VALUE which Ernst alluded to. Here's how it works...
32 VALUE SILLY
Which creates a word called SILLY. The run-time semantics are like a
CONSTANT, the number 32 is placed on the stack. If you want to make
this "value" 45, just do the following...
45 TO SILLY
This works at the console AND inside a definition, cool huh?
I hope this little tutoruial has helped anyone that has run into this
issue.
Cheers,
Ralph Hempel - P.Eng
--------------------------------------------------------
Check out pbFORTH for LEGO Mindstorms at:
<http://www.hempeldesigngroup.com/lego/pbFORTH>
--------------------------------------------------------
Reply to: rhempel at bmts dot com
--------------------------------------------------------
|
|
Message has 1 Reply: | | grasshopper learns new tricks
|
| CREATE DOES>, VALUE and EVALUATE. Thank's guys. These will all enhance my code. I knew about CREATE DOES> but never really understood it. Your description pushed me finally over the edge of basic comprehension. EVALUATE is really cool and VALUE is (...) (25 years ago, 24-May-00, to lugnet.robotics.rcx.pbforth)
|
Message is in Reply To:
| | Re: Forth question
|
| (...) I still want to create a constant from within a definition. I have an initializing word that will calculate the value of the constant. For various reasons I can't do this calculation except within the runtime behavior of the word. This is not (...) (25 years ago, 24-May-00, to lugnet.robotics.rcx.pbforth)
|
9 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|