-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvariables.cc
More file actions
54 lines (43 loc) · 869 Bytes
/
variables.cc
File metadata and controls
54 lines (43 loc) · 869 Bytes
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
#include <stdexcept>
#include <cstddef>
#include <utility>
#include "variables.h"
using namespace std;
class RegularVariable
{
void Set( std::string s )
{
value = s;
}
std::string Get( )
{
return value;
}
std::string value;
};
Scope::Scope()
{
parent = NULL;
}
Scope::~Scope()
{
map<string, Variable *>::iterator i;
// Destroy all variables when exiting a scope
for(i = variables.begin(); i != variables.end(); i ++ )
{
delete i->second;
}
}
string Scope::lookup( string name )
{
map<string, Variable *>::iterator i;
i = variables.find( name );
if( i == variables.end() ) {
if( parent == NULL ) {
throw runtime_error("Variable not defined");
} else {
return parent->lookup( name );
}
}
return i->second->Get( );
}