The Top-Level Interpreter

The interpreter runs your program. Create files src/interpreter.c and src/interpreter.h and write a function that performs a program (given by a tree.)

The interpreter starts by getting the value of main from the symbol table. Let's call that tree M. Then it simplifies M, yielding tree R.

If R is not any kind of action (which includes A ~> B nodes) then the interpreter prints R.

Otherwise, it calls performAction(R), whose return-value is a tree. The top-level interpreter should print the value returned by performAction, unless it an empty list.

Here is a sketch of performAction(R).

performAction(R)
  1. First, simplify R. Let's call the simplified tree S.

  2. If S is not an action, it is an error. The remaining steps assume that S is an action.

  3. If S is a readChar or readInt action then read a character or an integer and return it (as a tree) as the result of performAction.

  4. If S is produce(T) then simplify T and return the simplified result from performAction.

  5. If S is print(T) then simplify T and print the result. Return [ ] (as a tree) from performAction.

  6. If S is printList(T) then simplify T and print the result in the format called for by printList. Return [ ] (as a tree) from performAction.

  7. If S has the form A ~> B, then do

    1. x = performAction(A)
    2. return performAction(B x) (an application of B to x).

When the interpreter is done, it should return 1 if there was an error and 0 if there was no error.


Submitting your work

When you have both the simplifier and top-level interpreter working, submit it using the following command.

  ~abrahamsonk/5220/bin/submit interpreter interpreter.c interpreter.h simplify.c simplify.h ast.c ast.h symboltable.c symboltable.h stringtable.c stringtable.h allocate.h
Include any other modules needed to test your interpreter.