ForthOS control of scope of identifiers

Introduction

Traditional Forth permits groups of identifiers to be located in distinct vocabularies. By removing and adding vocabularies from the search order, a fair amount of control can be exercised over which identifiers are currently visible.

However, it is often the case that certain functions, constants, or variables are only ever used within a very restricted range of source. The ability to bound the scope of an identifier is thus helpful, as it makes this limited scope explicit, and helps keep the definitions vocabulary from becoming littered with transient names.

Local

	: localDef ( -- )   ; Local

Local operates upon the most recent definition. It records this definition, and arranges that this identifier will be removed from the definitions vocabulary when either the "only" or "definitions" words are invoked. It is thus used when a word will be used by subsequent words, but should not be visible beyond the end of compilation of this source.

scrLocal

	: verylocalDef ( -- )   ; scrLocal

scrLocal is similar to Local, but provides an even more limited scope. The affected word is removed from visibility when the current source screen is completed.

Debugging

While the identifiers affected by Local and scrLocal are not visible for most purposes, it is often useful in debugging for the decompiler to provide useful symbolic names, and even to be able to set breakpoints on them. When an identifier is removed from visibility, it is not simply deleted. Instead, it is moved from the definitions vocabulary into the "(locals)" vocabulary (itself located in the "extensions" vocabulary).

Thus, setting a breakpoint in "verylocalDef" is accomplished with:

	debug (locals).verylocalDef