A context is used to map symbols to values. It is a stack of symbol tables. The Context object represents a node of this stack. It contains a pointer next to another Context object, and a C++ library template map from string to RefPtr. The next pointer is used to organize context objects into stacks. When looking up a symbol in a context object, it first checks its own map, and returns the result if found. Otherwise, if the next pointer is non-null, it (recursively) checks that node. This is implemented by the find method.
The set method stores a name in the its own map. This is used by the set and setq operations, and is also used by the main function to store the built-in symbols before any user code is interpreted.
The stack organization supports the notion of nested scopes. New scopes are created:
The scope method allocates and returns a new Context object whose next pointer points to the context object which created it. This places it on top of the creating context object. Since the new top context object is empty, the new scope initially has exactly the same contents as the old one. New definitions are placed in this new top context, and appear to replace existing definitions since find looks there first. The previous definitions are not destroyed, but merely covered; when that top context is deleted, the definitions it contains are deleted, and the old (lower) ones reappear. This is what happens when the code using the new scope returns.
To see the scope in action, you might run this lisp code:
context.h
[Download]context.cpp
[Download]