1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* SI 413 Fall 2012
 * Lab 7
 * C++ header file for the SymbolTable class
 */
 
#ifndef ST_HPP
#define ST_HPP
 
#include <iostream>
#include <map>
#include <string>
using namespace std;
 
#include "value.hpp"
 
// Global variable to indicate if an error has occurred.
extern bool error;
 
// Declare the output streams to use everywhere
extern colorout resout;
extern colorout errout;
 
/* This class represents a simple global symbol table.
 * Later we will extend it to support dynamic scoping.
 */
class SymbolTable {
  private:
    // The actual map. It is declared private, so it can only
    // be accessed via the public methods below.
    map<string,Value> bindings;
 
  public:
    // Creates a new, empty symbol table
    SymbolTable() { }
 
    // Returns the Value bound to the given name.
    Value lookup(string name) {
      if (bindings.count(name) > 0) return bindings[name];
      if (!error) {
        error = true;
        errout << "ERROR: No binding for variable " << name << endl;
      }
      return Value();
    }
 
    // Creates a new name-value binding
    void bind(string name, Value val = Value()) {
      bindings[name] = val;
      // YOU HAVE TO WRITE THE ERROR CHECKING!
    }
 
    // Re-defines the value bound to the given name.
    void rebind(string name, Value val) {
      bindings[name] = val;
      // YOU HAVE TO WRITE THE ERROR CHECKING!
    }
};
 
#endif // ST_HPP