Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Conklin E.K.Forth programmer's handbook.2000.pdf
Скачиваний:
321
Добавлен:
23.08.2013
Размер:
2.04 Mб
Скачать
Forth Programmer’s Handbook

Glossary

MARKER <name> ( — ) Core Ext

Create a dictionary definition for name, to be used as a deletion boundary. When name is executed, remove the definition of name and all subsequent definitions from the dictionary. Restore all dictionary allocation and search order pointers to the state they had just prior to the definition of name.

4.6 WORD LISTS

Word lists are collections of definitions residing within the dictionary. ANS Forth guarantees there will be at least eight wordlists available to the user. Dictionary searches proceed from one word list to another in a specified sequence. This mechanism allows you to control which list or lists are to be searched. Within each word list, the search is from newest to oldest.

Word lists have three principal uses:

!In the resident system, to segregate special-purpose words such as those in the ASSEMBLER, to allow them to have the same names as standard Forth words.

!In a target compiler environment where two types of CPU exist, to segregate target versions of FORTH and ASSEMBLER words from the host versions.

!In applications running in the host system, to protect against accidental misuse of words only intended for programmers.

4.6.1 Basic Principles

The standard word lists provided by typical Forth systems are:

FORTH

ASSEMBLER (on most systems)

EDITOR (on systems with an internal editor)

Other lists may be created, as described below. The FORTH word list contains most of the familiar words such as DUP, SWAP, DO, etc. Another word list on most systems is ASSEMBLER, containing words used to assemble machine

The Forth Interpreter and Compiler 165

Forth Programmer’s Handbook

code. EDITOR contains the editing commands for editing source text.

The use of separate word lists makes it possible, for instance, for the word I to supply a loop index in one context (FORTH), to insert a string in another context (EDITOR), or to name a register in yet another (ASSEMBLER).

When the Forth interpreter receives a word, whether it is one you have typed at the keyboard or one it gets from a file, it looks for that word in an ordered sequence of word lists. The sequence is called the search order. A word will not be found unless it is contained in a word list in the search order. The search order may be changed at any time. A pointer to the first word list in the search order is kept in the variable CONTEXT.

To display the search order, use ORDER.

When a Forth word is compiled, it will be placed in the current compilation word list. This is not necessarily the same word list that is first in the interpretation search order. A pointer to the current compilation word list is kept in the variable CURRENT. Words are provided, as described below, for manipulating both the interpretation search order and the compilation word list.

You may change the contents of CONTEXT (i.e., select the word list to search first) simply by naming the desired word list. For example, the word:

ASSEMBLER

changes CONTEXT so future searches will begin with the ASSEMBLER word list. (CONTEXT is set to ASSEMBLER by the defining words CODE and ;CODE.)

Similarly, you may employ the word:

EDITOR

to set CONTEXT to begin by searching the EDITOR word list. In many cases, EDITOR commands are found in FORTH and automatically set CONTEXT to the EDITOR word list.

The contents of CURRENT, which selects the compilation word list, may also be changed. The word DEFINITIONS sets CURRENT to the word list indicated by CONTEXT. For example, in the phrase:

EDITOR DEFINITIONS

166 The Forth Interpreter and Compiler

Forth Programmer’s Handbook

EDITOR sets the value in CONTEXT to be the EDITOR word list. DEFINITIONS then sets CURRENT also to EDITOR. Thereafter, any future definitions will be linked according to the EDITOR word list. Subsequent changes in the search order will change CONTEXT, but CURRENT remains as set until explicitly changed. When the system starts, or following an EMPTY, the default word list for both CONTEXT and CURRENT is FORTH.

Invoking the name of a word list always replaces the word list that previously was at the head of the search order. To add a word list to the head of the search order and still retain the previous word list in the search order, use ALSO (see below) followed by the name of the word list you want to add.

4.6.2 Managing Word Lists

Here are some words for manipulating word lists:

Glossary

ALSO ( — ) Search Ext

Duplicate the first word list in the search order, increasing the number of word lists in the search order by one. Commonly used in the phrase ALSO name, which has the effect of adding name to the top of the search order.

ASSEMBLER

( — )

Tools Ext

Set future dictionary searches to begin with the ASSEMBLER word list (avail-

able on most systems).

 

CONTEXT

( — a-addr )

Core

Return a-addr, the address of a cell that contains a pointer to the first word list

in the search order.

 

 

CURRENT

( — a-addr )

common usage

Return a-addr, the address of a cell that contains a pointer to the current compi-

lation word list.

 

 

DEFINITIONS

( — )

Search

Change the compilation word list to be the same as the current first word list in the search order. Set a pointer to this word list in the variable CURRENT.

The Forth Interpreter and Compiler 167

Forth Programmer’s Handbook

Subsequent changes to the interpretation search order will not affect the compilation word list; this word list remains in effect until explicitly changed.

EDITOR

( — )

Tools Ext

 

Set future dictionary searches to begin with the EDITOR word list.

 

FORTH

( — )

Search Ext

Set future dictionary searches to begin with the FORTH word list, which contains all standard words provided by the system implementation.

ONLY

( — )

Search Ext

 

Reduce the search order to the minimum word list(s), usually just FORTH.

ORDER

( — )

Search Ext

Display names of the word lists in the search order, in their present search order sequence. Also, display the word list into which new definitions will be placed (the CURRENT word list).

PREVIOUS

( — )

Search Ext

Remove the first word list (the one in the CONTEXT position) from the search

order. This may be used to undo the effect of an ALSO.

 

VOCABULARY <name>

( — )

common usage

Create a word list name. Subsequent execution of name replaces the first word list in the search order with name. When name becomes the compilation word list, new definitions will be appended to name’s list.

WORDS

( — )

Tools

 

Displays the names of all the words in the first word list of the search order.

 

4.6.3

Sealed Word Lists

 

The word list mechanism offers an exceptionally powerful security technique. You can implement this by setting up a special application word list consisting of a limited number of commands guaranteed to be safe for users. You then ensure that no application word can change CONTEXT, and that CONTEXT is set so the text interpreter will only search the application word list.

168 The Forth Interpreter and Compiler

Соседние файлы в предмете Электротехника