-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbtable.h
90 lines (72 loc) · 2.14 KB
/
symbtable.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef SYMBTABLE_H_INCLUDED
#define SYMBTABLE_H_INCLUDED
#include <string>
#include <list>
#include <utility>
/* If USE_HASH_MAP is defined, we will use the SGI hash_map extension.
* Otherwise, we will use the STL map container, which is generally not
* implemented as a hash table (in SGI's implementation, it uses a red-
* black tree).
*
* g++, at least recent versions, uses SGI's STL implementation. If you
* are using GNU libstdc++ version 3, you will need to say USE_EXT_HASH_MAP,
* which does the same thing as USE_HASH_MAP, but #includes <ext/hash_map>
* instead.
*/
#include <map>
enum type_t {
TY_BAD,
TY_INT,
TY_DOUBLE,
TY_FUNC
};
// The values in a SymbolTable; accessed by string keys.
class Symbol {
public:
Symbol();
Symbol(const std::string &name, type_t type = TY_BAD, int addr = -1);
// Accessors
virtual const std::string &name() const;
// Accessor and reference accessor for the type
virtual type_t &type();
virtual type_t type() const;
// Accessor and reference accessor for the address
virtual int &address();
virtual int address() const;
// Comparison operators
virtual bool operator<(const Symbol &s) const;
virtual bool operator==(const Symbol &s) const;
virtual bool operator!=(const Symbol &s) const;
// virtual int numargs();
// virtual void setNumArgs( int num);
private:
std::string nam;
type_t typ;
int addr;
int numarg;
};
class SymbolTable {
protected:
/* Associative array from std::string to such lists; see above for the
* meaning of USE_HASH_MAP */
typedef std::map<std::string,Symbol> symtabsingle;
typedef std::list<symtabsingle> symtabint;
public:
SymbolTable();
SymbolTable(const SymbolTable &st);
SymbolTable &operator=(const SymbolTable &st);
Symbol *operator[](const std::string &s);
const Symbol *operator[](const std::string &s) const;
bool insert(Symbol s);
bool exists(const std::string &s) const;
int levelof(const std::string &s) const;
// Number of variables in the top symbol table.
int numvars() const;
// Enter/leave a new scope.
void enter();
void leave();
private:
// The actual list of tables.
symtabint hashes;
};
#endif // SYMBTABLE_H_INCLUDED