-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.cpp
More file actions
153 lines (120 loc) · 3.31 KB
/
env.cpp
File metadata and controls
153 lines (120 loc) · 3.31 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<vector>
#include<unordered_map>
#include<sstream>
#include<memory>
class Environment {
private:
std::unordered_map<std::string, int> storage;
public:
std::shared_ptr<Environment> outer;
Environment() : outer(nullptr) {}
int get(const std::string& name) {
auto it = storage.find(name);
if (it != storage.end()) {
return it->second;
}
if (outer != nullptr) {
return outer->get(name);
}
return 0;
}
void set(const std::string& name, int val) {
storage[name] = val;
}
};
std::shared_ptr<Environment> NewEnv() {
return std::make_shared<Environment>();
}
std::shared_ptr<Environment> NewEnclosedEnv(std::shared_ptr<Environment> outer) {
auto env = std::make_shared<Environment>();
env->outer = outer;
return env;
}
int evalExpr(const std::string& expr, std::shared_ptr<Environment> env) {
std::istringstream iss(expr);
int val;
if (iss >> val) {
char remaining;
if (!(iss >> remaining)) {
return val;
}
}
return env->get(expr);
}
int parseLet(std::vector<std::string>& words, std::shared_ptr<Environment> env) {
if (words.size() < 4) {
std::cout << "Error: Invalid let statement\n";
}
words.erase(words.begin()); // let is removed
std::string var = words[0];
words.erase(words.begin()); // var is removed
if (!words.empty() && words[0] == "=") {
words.erase(words.begin()); // = is removed
}
if (words.empty()) {
std::cout << "Error: nothing to bind\n";
}
std::string expr = words[0];
int val = evalExpr(expr, env);
env->set(var, val);
return val;
}
int parseIdent(const std::string& ident, std::shared_ptr<Environment> env) {
return env->get(ident);
}
int scopeCount = 0;
int Eval(const std::string& line, std::shared_ptr<Environment>& currentEnv) {
std::vector<std::string> words;
std::istringstream iss(line);
std::string word;
while (iss >> word) {
words.push_back(word);
}
if (words.empty()) {
return 0;
}
if (words[0] == "let") {
return parseLet(words, currentEnv);
} else if (words[0] == "deep") {
currentEnv = NewEnclosedEnv(currentEnv);
++scopeCount;
std::cout << scopeCount << ": Scope Deep!\n";
return 0;
} else if (words[0] == "shallow") {
if (currentEnv->outer != nullptr) {
currentEnv = currentEnv->outer;
std::cout << "Out Of Scope " << scopeCount << "\n";
--scopeCount;
} else {
std::cout << "Currently In Global Scope\n";
}
return 0;
} else {
return parseIdent(words[0], currentEnv);
}
return 0;
}
const std::string Prompt = ">> ";
void Start(std::istream& in, std::ostream& out) {
std::string line;
auto currentEnv = NewEnv();
while (true) {
out << Prompt;
out.flush();
if (!std::getline(in, line)) {
return;
}
if (line == "exit") {
return;
}
int result = Eval(line, currentEnv);
if (result != 0 || (line != "deep" && line != "shallow")) {
out << result << "\n";
}
}
}
int main() {
Start(std::cin, std::cout);
std::cin.get();
}