-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvironment.cpp
65 lines (52 loc) · 1.21 KB
/
environment.cpp
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
#include <cstdio>
#include <string>
using namespace std;
template <class T>
class Environment{
public:
Environment(){
parent = 0;
}
Environment( Environment<T> * p ){
parent = p;
}
Environment<T> * getScope( std::string key ){
typename std::map<std::string, Environment<T> * >::iterator it;
it = scopes.find( key );
if( it != scopes.end() ){
return (*it).second;
} else {
return 0;
}
}
void addScope( std::string key ){
scopes[key] = new Environment<T>( this );
}
void add( std::string key, T * n ){
typename std::map<std::string, T* >::iterator it;
it = elements.find( key );
if( it != elements.end() ){
elements[key] = n;
} else {
elements[key] = n;
}
}
T * get( std::string key ){
typename std::map<std::string, T* >::iterator it;
it = elements.find( key );
if( it != elements.end() ){
return (*it).second;
} else if( parent != 0 ) {
return parent -> get( key );
} else {
return 0;
}
}
bool is( std::string key ){
return (elements.find( key ) != elements.end());
}
protected:
std::map< std::string, Environment<T> * > scopes;
std::map< std::string, T * > elements;
Environment<T> * parent;
};