next up previous
Next: Print statements Up: Interpret the Program Previous: Expression evaluation

   
Variables and memory

Variables are represented by identifiers in the simple serial programming language. They can hold integer values, so the memory must be implemented by an associative container in which identifiers can be used as keys to access integers.

In the C++ standard template library, a map is the associative container that stores a value with each key. A map declaration needs a comparison function as well as the types of the key and the associated value:

Declare the interpreter memory[15] :

map<string, int, less<string> > Table;
This macro is invoked in definition 11.

(This declaration follows Appel in naming the interpreter's memory Table.)

Given Table, interpretation of the AssignStm and IdExp nodes is straightforward:

Interpret the AssignStm and IdExp rules[16] :

void Interpreter::VisitAssignStm(AssignStm* node)
{ (node->Child2())->Accept(this);
  Table[node->Child1()] = ExpValues.top(); ExpValues.pop();
}

void Interpreter::VisitIdExp(IdExp* node)
{ ExpValues.push(Table[node->Child1()]);
}
This macro is invoked in definition 28.



William Waite
1998-08-30