Loading...
 
Skip to main content
Forth

buildsdoes

The <BUILDS DOES> construct in Forth is a little dated but still worth understanding.
This construct provides a simple way to build new data types.

In emforth variables and constants are coded in assembler for speed but they could also be defined as <BUILDS DOES> type words and this is the simplest example of how to use <BUILDS DOES>.
We'll assume the variable is stored in the code space for simplicity.

To define a variable in forth we write something like.

VARIABLE MYVAR

The word VARIABLE is a constructor or building word which creates new Forth words.
In the above example a new word "MYVAR" is created. When MYVAR is executed (either interactively or as part of a compiled definition) it's action is to place the address of the variable storage area onto the data stack.

We could also have a variation on VARIABLE which sets the initial value using a value of the stack.
This alternative might be used like this.
1234 VARIABLE MYVAR // define MYVAR and initialize to 1234

The word CONSTANT is very similar eg.
1234 CONSTANT MYCON
The above creates a new word "MYCON" which places the number 1234 on the data stack when called.

To define the word "VARIABLE" as <BUILDS DOES> construct we do this.

: VARIABLE 
   <BUILDS
   0 ,
   DOES> 
;

Pretty simple. This version initializes the variable to zero.
When variable is called "<BUILDS" creates the new word.
The " 0 , " writes a zero word into the location pointed to by DP (dictionary pointer)
"DOES>" sets up the runtime code which pushes the data address onto the stack.
Since this address is all we want there is no more to do. If we wanted to process the data somehow we would have more code after the "DOES>"

To define the alternative version which sets the initial value of the variable from a number on the stack we would do this.

: VARIABLE 
   <BUILDS
   ,
   DOES> 
;

Much the same as before except we don't push a zero onto the stack before the comma.

Now for CONSTANT...

: CONSTANT 
   <BUILDS
   ,
   DOES> 
   @
;

This is exactly the same as before except we "fetch" the value at the address which was placed on the stack by the DOES> runtime code.

Either the building code or the runtime code can be as complex as you like.

SAM7 FORTH

eddie


Created by eddie. Last Modification: Saturday 26 of January, 2008 21:56:14 AEDT by eddie.

Main Index

Switch Theme

Switch Theme

Shoutbox

eddie, 18:52 AEST, Wed 11 of Sep, 2024: Most pages should be working again.
System Administrator, 08:03 AEST, Sat 10 of Aug, 2024: Lots of images are still broken. I'm working on it. Maybe 1/2 way through.
admin2, 14:05 AEST, Mon 05 of Aug, 2024: running tiki 27
admin2, 16:01 AEST, Sun 09 of Jun, 2024: Wiki running tikiwiki version 27alpha on a raspberry pi-3. About 1/2 the images are missing and most thumbnails not working. Slow manual rebuild. About %20 done.
eddie, 20:23 AEST, Sun 19 of May, 2024: Images moved from wiki_up to file galleries and wiki pages fixed.

Last-Visited Pages

Online Users

62 online users