Extending the dictionary

Forth belongs to the class of Threaded Interpretive Languages. This means that it can interpret commands typed at the console, as well as compile new subroutines and programs. The Forth compiler is part of the language and special words are used to make new dictionary entries (i.e. words). The most important are : (start a new definition) and ; (terminate the definition). Let's try this out by typing:


: *+ * + ; \fbox{$\hookleftarrow$} ok<$,ram>


What happened? The action of ``:'' is to create a new dictionary entry named *+ and switch from interpret to compile mode. In compile mode, the interpreter looks up words and, rather than executing them, installs pointers to their code. If the text is a number, instead of pushing it onto the stack, FlashForth builds the number into the dictionary space allotted for the new word, following special code that puts the stored number onto the stack whenever the word is executed. The run-time action of *+ is thus to execute sequentially the previously-defined words * and +


The word ``;'' is special. It is an immediate word and is always executed, even if the system is in compile mode. What ``;'' does is twofold. First, it installs the code that returns control to the next outer level of the interpreter and, second, it switched back from compile mode to interpret mode.


Now, try out your new word:


decimal 5 6 7 *+ . \fbox{$\hookleftarrow$} 47 ok<#,ram>


This example illustrated two principal activities of working in Forth: adding a new word to the dictionary, and trying it out as soon as it was defined.


Note that, in FlashForth, names of dictionary entries are limited to 15 characters. Also, FlashForth will not redefine a word that already exists in the dictionary. This can be convenient as you build up your library of Forth code because it allows you to have repeated definitions, say for special function registers, in several files and not have to worry about the repetition.




Subsections
Peter Jacobs 2013-06-12