More on defining words

The compiler word create makes a new dictionary entry using the next name in the input stream and compiles the pointer value of the first free location of the current data-space context. When executed, the new dictionary entry leaves that pointer value on the stack.


create can be used to build other defining words. For example, we can make our own variation of word variable as

: make-var create 1 cells allot ;  ok<#,ram>
Here make-var can be used to make an uninitialized variable that can hold a single number. When make-var is executed, the first word within its definition (create) sets up a dictionary entry with the name coming from the next text in the input stream (alpha in the example below), the number 1 is pushed onto the stack, the word cells converts the TOS to the appropriate number of bytes (2 in this case) and the word allot increments the pointer to the next available space in memory by this number of bytes. This allots one cell to the newly created child word.
make-var alpha  ok<#,ram>
13 alpha !  ok<#,ram>
alpha @ . 13  ok<#,ram>
At run time for the newly created child word, alpha leaves its data-space address on the stack and we may store to or fetch from this address, as shown above.


As a second example, we can also build a defining word for making initialized variables.

: make-zero-var create 0 , ;  ok<#,ram>
Instead of just allotting space for the data, the word , (comma) puts TOS into the next cell of memory and increments the memory-space pointer by appropriate number of bytes. Run time use of the newly defined variable is the same as for any other variable.
make-zero-var beta  ok<#,ram>
beta @ . 0  ok<#,ram>




Subsections
Peter Jacobs 2013-06-12