Our evaluator for JavaScript will be implemented as a JavaScript program. It may seem circular to think about evaluating JavaScript programs using an evaluator that is itself implemented in JavaScript. However, evaluation is a process, so it is appropriate to describe the evaluation process using JavaScript, which, after all, is our tool for describing processes.[1] An evaluator that is written in the same language that it evaluates is said to be metacircular.
The metacircular evaluator is essentially a JavaScript formulation of the environment model of evaluation described in section 3.2. Recall that the model specifies the evaluation of function application in two basic steps:
These two rules describe the essence of the evaluation process, a basic cycle in which statements and expressions to be evaluated in environments are reduced to functions to be applied to arguments, which in turn are reduced to new statements and expressions to be evaluated in new environments, and so on, until we get down to names, whose values are looked up in the environment, and to operators and primitive functions, which are applied directly (see figure 4.2).[2] This evaluation cycle will be embodied by the interplay between the two critical functions in the evaluator, evaluate and apply, which are described in section 4.1.1 (see figure 4.2).
The implementation of the evaluator will depend upon functions that define the syntax of the statements and expressions to be evaluated. We will use data abstraction to make the evaluator independent of the representation of the language. For example, rather than committing to a choice that an assignment is to be represented by a string beginning with a name followed by =, we use an abstract predicate is_assignment to test for an assignment, and we use abstract selectors assignment_symbol and assignment_value_expression to access the parts of an assignment. The data abstraction layers presented in section 4.1.2 will allow the evaluator to remain independent of concrete syntactic issues, such as the keywords of the interpreted language, and of the choice of data structures that represent the program components. There are also operations, described in section 4.1.3, that specify the representation of functions and environments. For example, make_function constructs compound functions, lookup_symbol_value accesses the values of names, and apply_primitive_function applies a primitive function to a given list of arguments.