Lecture: A Metacircular Scheme Interpreter

Background Material

Reading

Theme

The scheme interpreter is an implementation of the environment model of computation discussed. Evaluation (SICP sec. 4-1) has to parts:
  1. To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions.
  2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied.
This implemented with two mutually recursive procedures: eval and apply. The procedure eval processes the expression, determining its syntax, and dispatches routines to handle the special forms. If a function application is detected, its subexpressions are evaluated and apply is called. The procedure apply creates a new environment by extending the defining environment of the function to include bindings of the function parameters to the argument values and calls eval to evaluate the function body in the extended environment.

The interpreter is called metacircular, since the interpreter is written in the language it is interpreting. Note that by using abstraction the interpreter can easily be modified to incorporate different syntax. The semantics can be changed by changing the parts of the interpreter (see the exercises). The use of such an interpreter allows us to study modifications to the language syntax and semantics.

Topics

  1. Environment model for function evaluation (SICP sec. 3.2, Louden sec. 13.2)
  2. The core evaluator (SICP sec. 4.1.1)
  3. Representing expressions (SICP sec. 4.1.2)
  4. Evaluator data structures (SICP sec. 4.1.3)
  5. Running the evaluator (SICP sec. 4.1.4)

Sample Functions

References

Exercises

Created: Apr. 10, 2007 (revised April 5, 2012) by jjohnson AT cs DOT drexel DOT DOT edu