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:
This macro is invoked in definition 11.
map<string, int, less<string> > Table;
(This declaration follows Appel in naming the interpreter's memory Table.)
Given Table, interpretation of the AssignStm and IdExp nodes is straightforward:
This macro is invoked in definition 28.
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()]);
}