-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.cpp
62 lines (48 loc) · 1.42 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
#include "Environment.h"
Environment::Environment() : enclosing(nullptr) {
}
void Environment::define(const String& name, const Object& value) const {
if (values.find(name) != values.end()) {
values[name] = value;
}
else {
values.insert(std::make_pair(name, value));
}
}
Object Environment::get(const Token& name) const {
if (values.find(name.Lexeme()) != values.end()) {
return values.at(name.Lexeme());
}
if (enclosing != nullptr) {
return enclosing->get(name);
}
throw RuntimeError(name, "Undefined variable '" + name.Lexeme() + "'.");
}
void Environment::assign(Token name, Object value) const {
if (values.find(name.Lexeme()) != values.end()) {
values[name.Lexeme()] = value;
return;
}
if (enclosing != nullptr) {
enclosing->assign(name, value);
return;
}
throw RuntimeError(name, "Undefined variable '" + name.Lexeme() + "'.");
}
void Environment::assign(String name, Object value) const {
for (auto &i : values) {
if (objectToString(i.second) == name) {
i.second = value;
break;
}
}
//throw RuntimeError(name, String("Undefined variable ") + name + String("."));
}
void Environment::deleteVar(String name) {
for (auto &i : values) {
if (objectToString(i.second) == name) {
values.erase(i.first);
break;
}
}
}