-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.cpp
51 lines (45 loc) · 1.26 KB
/
scope.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
#include "scope.h"
#include "error.h"
#include "functions.h"
#include "object.h"
#include <string>
Scope::Scope(Scope* sc) : par_scope_{sc} {
}
Object* Scope::Get(const std::string& name) {
if (mp_.find(name) == mp_.end()) {
return nullptr;
}
return mp_[name];
}
void Scope::Set(const std::string& name, Object* obj) {
mp_[name] = obj;
}
bool Scope::TrySet(const std::string& name, Object* obj) {
if (mp_.find(name) == mp_.end()) {
return false;
}
mp_[name] = obj;
return true;
}
bool Scope::TrySetCar(const std::string& name, Object* obj) {
if (mp_.find(name) == mp_.end()) {
return false;
}
auto ptr = mp_[name];
if (!Is<Variable>(ptr) || !Is<Cell>(As<Variable>(ptr)->GetVal())) {
throw RuntimeError("Set-car must change a list or pair");
}
As<Cell>(As<Variable>(ptr)->GetVal())->GetFirst() = obj;
return true;
}
bool Scope::TrySetCdr(const std::string& name, Object* obj) {
if (mp_.find(name) == mp_.end()) {
return false;
}
auto ptr = mp_[name];
if (!Is<Variable>(ptr) || !Is<Cell>(As<Variable>(ptr)->GetVal())) {
throw RuntimeError("Set-cdr must change a list or pair");
}
As<Cell>(As<Variable>(ptr)->GetVal())->GetSecond() = obj;
return true;
}